import {z} from 'zod' export type StatsQueryType = 'graph' | 'base_query' | 'chat_gpt_query'; export type Chain = 'mainnet' | 'testnet'; export type GetApiApiV1ServicesStatsQueriesHistoryArgs = {project_id: number, offset?: number | undefined, limit?: number | undefined, type?: Array | undefined, is_repetitive?: boolean | undefined, chain?: Chain | undefined}; export type GetApiApiV1ServicesStatsQueriesHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const statsQueryStatus = z.enum(["executing", "success", "error"]); export const statsQuery = z.object({name: z.string().optional(), addresses: z.array(z.string()).optional(), only_between: z.boolean().optional(), sql: z.string().optional(), gpt_message: z.string().optional(), repeat_interval: z.number().int().optional()}); export const statsQueryType = z.enum(["graph", "base_query", "chat_gpt_query"]); export const statsEstimateQuery = z.object({approximate_time: z.number().int(), approximate_usd_cost: z.number(), explain: z.string().optional()}); export const statsQueryResult = z.object({id: z.string(), name: z.string().optional(), status: statsQueryStatus, query: statsQuery.optional(), type: statsQueryType.optional(), estimate: statsEstimateQuery.optional(), url: z.string().optional(), meta_url: z.string().optional(), spent_time: z.number().int().optional(), last_repeat_date: z.number().int().optional(), total_repetitions: z.number().int().optional(), total_usd_cost: z.number().optional(), usd_cost: z.number().optional(), error: z.string().optional(), all_data_in_preview: z.boolean().optional(), preview: z.array(z.array(z.string())).optional(), testnet: z.boolean(), is_gpt: z.boolean().optional(), date_create: z.number().int()}); export const getApiApiV1ServicesStatsQueriesHistoryResponse = z.object({count: z.number().int(), items: z.array(statsQueryResult)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get sql history queries * * GET /api/v1/services/stats/queries/history */ export const getApiApiV1ServicesStatsQueriesHistory = async (args: GetApiApiV1ServicesStatsQueriesHistoryArgs, options?: GetApiApiV1ServicesStatsQueriesHistoryOptions) => { const {baseUrl = "http://localhost:8888", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.type ?? []) query.append("type", String(item)) if (args.is_repetitive != null) query.append("is_repetitive", String(args.is_repetitive)) if (args.chain != null) query.append("chain", String(args.chain)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/services/stats/queries/history${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1ServicesStatsQueriesHistoryResponse, await res.json()) };