11import { createClient , type ClickHouseClient , type ClickHouseSettings } from "@clickhouse/client" ;
22import { Logger } from "./logger" ;
33import type { AssetType } from "./types" ;
4+ import { clickhouseResponseDuration } from "./metrics" ;
45
56export interface ClickHouseConfig {
67 host : string ;
@@ -163,6 +164,7 @@ export class ClickHouseWrapper {
163164 } ;
164165
165166 try {
167+ const start = process . hrtime . bigint ( ) ;
166168 await this . client . insert ( {
167169 table : "prices_raw" ,
168170 values : prices . map ( ( p ) => ( {
@@ -173,6 +175,8 @@ export class ClickHouseWrapper {
173175 } ) ) ,
174176 format : "JSONEachRow" ,
175177 } ) ;
178+ const end = process . hrtime . bigint ( ) ;
179+ clickhouseResponseDuration . labels ( { method : "insertPrices" , type : "batch" } ) . observe ( Number ( end - start ) / 1_000_000_000 ) ;
176180
177181 Logger . debug ( `[ClickHouse] Inserted ${ prices . length } price records` ) ;
178182 } catch ( error : any ) {
@@ -206,10 +210,13 @@ export class ClickHouseWrapper {
206210 ORDER BY timestamp ASC
207211 ` ;
208212
213+ const start = process . hrtime . bigint ( ) ;
209214 const result = await this . client . query ( {
210215 query,
211216 format : "JSONEachRow" ,
212217 } ) ;
218+ const end = process . hrtime . bigint ( ) ;
219+ clickhouseResponseDuration . labels ( { method : "getAllRawPrices" , type : assetType } ) . observe ( Number ( end - start ) / 1_000_000_000 ) ;
213220
214221 return result . json ( ) ;
215222 }
@@ -283,10 +290,13 @@ export class ClickHouseWrapper {
283290 ORDER BY hour ASC
284291 ` ;
285292
293+ const start = process . hrtime . bigint ( ) ;
286294 const result = await this . client . query ( {
287295 query,
288296 format : "JSONEachRow" ,
289297 } ) ;
298+ const end = process . hrtime . bigint ( ) ;
299+ clickhouseResponseDuration . labels ( { method : "getAllHourlyPrices" , type : assetType } ) . observe ( Number ( end - start ) / 1_000_000_000 ) ;
290300
291301 return result . json ( ) ;
292302 }
@@ -391,10 +401,13 @@ export class ClickHouseWrapper {
391401 ORDER BY date ASC
392402 ` ;
393403
404+ const start = process . hrtime . bigint ( ) ;
394405 const result = await this . client . query ( {
395406 query,
396407 format : "JSONEachRow" ,
397408 } ) ;
409+ const end = process . hrtime . bigint ( ) ;
410+ clickhouseResponseDuration . labels ( { method : "getAllDailyPrices" , type : assetType } ) . observe ( Number ( end - start ) / 1_000_000_000 ) ;
398411
399412 return result . json ( ) ;
400413 }
0 commit comments