import { appApiPath } from "@agent-native/core/client/api-path"; import { useQuery } from "@tanstack/react-query"; import { getIdToken } from "./auth"; import { addBytesProcessed } from "./cost-tracker"; export interface QueryMetricsResult { rows: Record[]; error?: string; schema?: { name: string; type: string }[]; } export async function queryMetrics(sql: string): Promise { const token = await getIdToken(); const res = await fetch(appApiPath("/api/query"), { method: "POST", headers: { "Content-Type": "application/json", ...(token && { Authorization: `Bearer ${token}` }), }, body: JSON.stringify({ query: sql }), }); if (!res.ok) { const body = await res.json().catch(() => ({})); return { rows: [], error: body.error || `Query failed (${res.status})`, }; } const data = await res.json(); if (data.bytesProcessed) { addBytesProcessed(data.bytesProcessed); } return { rows: data.rows ?? [], schema: data.schema, }; } /** * React Query hook for metrics queries. */ export function useMetricsQuery( queryKey: string[], sql: string, options?: { enabled?: boolean; refetchInterval?: number; }, ) { return useQuery({ queryKey, queryFn: () => queryMetrics(sql), enabled: options?.enabled ?? true, refetchInterval: options?.refetchInterval, staleTime: 5 * 60 * 1000, }); }