import { query } from "./db"; import { queryCache } from "./query-cache"; export const cached = queryCache.cached; export function cacheKey(...parts: unknown[]): string { return parts.map((p) => String(p ?? "")).join(":"); } export const REAL_MODEL = `COALESCE( CASE WHEN other IS NOT NULL AND other != '' AND other::jsonb ? 'upstream_model_name' THEN other::jsonb->>'upstream_model_name' END, model_name)`; export const CACHE_CREATION = `COALESCE( CASE WHEN other IS NOT NULL AND other != '' AND other::jsonb ? 'cache_creation_tokens' THEN (other::jsonb->>'cache_creation_tokens')::bigint END, 0)`; export const CACHE_READ = `COALESCE( CASE WHEN other IS NOT NULL AND other != '' AND other::jsonb ? 'cache_tokens' THEN (other::jsonb->>'cache_tokens')::bigint END, 0)`; export const TOKEN_NAME = `COALESCE(NULLIF(BTRIM(token_name), ''), '')`; export function timeWhere( params: (string | number | boolean | null)[], startTs?: number, endTs?: number ): string { let where = "type = 2"; if (startTs) { params.push(startTs); where += ` AND created_at >= $${params.length}`; } if (endTs) { params.push(endTs); where += ` AND created_at <= $${params.length}`; } return where; } export async function getDisplayNames(): Promise> { const rows = await query( "SELECT id, display_name FROM users WHERE display_name IS NOT NULL AND display_name != ''" ); return Object.fromEntries(rows.map((r) => [r.id, r.display_name])); } export async function getChannelNames(): Promise> { const rows = await query("SELECT id, name FROM channels"); return Object.fromEntries(rows.map((r) => [r.id, r.name])); }