( query: string, clickhouseSettings?: ClickHouseSettings )
| 320 | }) as ClickHouseClient; |
| 321 | |
| 322 | export 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 | |
| 368 | export async function chQuery<T extends Record<string, any>>( |
| 369 | query: string, |
nothing calls this directly
no test coverage detected