MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / chQueryWithMeta

Function chQueryWithMeta

packages/db/src/clickhouse/client.ts:322–366  ·  view source on GitHub ↗
(
  query: string,
  clickhouseSettings?: ClickHouseSettings
)

Source from the content-addressed store, hash-verified

320}) as ClickHouseClient;
321
322export async function chQueryWithMeta<T extends Record<string, any>>(
323 query: string,
324 clickhouseSettings?: ClickHouseSettings
325): Promise<ResponseJSON<T>> {
326 const start = Date.now();
327 let host: string | undefined;
328 const res = await withRetry((client, ctx) => {
329 host = urlHostname(ctx.url);
330 return client.query({
331 query,
332 clickhouse_settings: clickhouseSettings,
333 });
334 });
335 const json = await res.json<T>();
336 const keys = Object.keys(json.data[0] || {});
337 const response = {
338 ...json,
339 data: json.data.map((item) => {
340 return keys.reduce((acc, key) => {
341 const meta = json.meta?.find((m) => m.name === key);
342 return {
343 ...acc,
344 [key]:
345 item[key] && meta?.type.includes('Int')
346 ? Number.parseFloat(item[key] as string)
347 : item[key],
348 };
349 }, {} as T);
350 }),
351 };
352
353 logger.info(
354 {
355 host,
356 query: cleanQuery(query),
357 rows: json.rows,
358 stats: response.statistics,
359 elapsed: Date.now() - start,
360 clickhouseSettings,
361 },
362 'query info'
363 );
364
365 return response;
366}
367
368export async function chQuery<T extends Record<string, any>>(
369 query: string,

Callers

nothing calls this directly

Calls 6

withRetryFunction · 0.85
urlHostnameFunction · 0.85
cleanQueryFunction · 0.85
queryMethod · 0.80
infoMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected