From 78586120133bc7dd9f568940e89e432ebaff592d Mon Sep 17 00:00:00 2001 From: Ariel Weinberger Date: Sun, 31 Dec 2023 15:02:10 -0800 Subject: [PATCH] fix clickhouse knex connection --- apps/server/.eslintrc.json | 4 +++- .../src/app/clickhouse/clickhouse.service.ts | 23 +++++++++++-------- .../app/metrics/project-metrics.service.ts | 7 ++++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/apps/server/.eslintrc.json b/apps/server/.eslintrc.json index 9d9c0db5..e9a9025b 100644 --- a/apps/server/.eslintrc.json +++ b/apps/server/.eslintrc.json @@ -4,7 +4,9 @@ "overrides": [ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], - "rules": {} + "rules": { + "no-case-declarations": "off" + } }, { "files": ["*.ts", "*.tsx"], diff --git a/apps/server/src/app/clickhouse/clickhouse.service.ts b/apps/server/src/app/clickhouse/clickhouse.service.ts index 693bfc67..cfe015db 100644 --- a/apps/server/src/app/clickhouse/clickhouse.service.ts +++ b/apps/server/src/app/clickhouse/clickhouse.service.ts @@ -4,6 +4,7 @@ import { createLogger } from "../logger/create-logger"; import { pino } from "pino"; import { createClient, ClickHouseClient } from "@clickhouse/client"; // or '@clickhouse/client-web' import { knex, Knex } from "knex"; +import clickhouesDialect from "@pezzo/knex-clickhouse-dialect"; @Injectable() export class ClickHouseService implements OnModuleInit { @@ -36,17 +37,12 @@ export class ClickHouseService implements OnModuleInit { }); this.logger.info("Creating Knex instance"); + const connectionStr = + `${protocol}://${username}:${password}@${host}:${port}/${database}` as any; this.knex = knex({ - client: "mysql2", - connection: { - host, - port: 9004, - user: username, - password, - database, - timezone: "Z", - }, + client: clickhouesDialect as any, + connection: () => connectionStr, }); await this.healthCheck(); @@ -59,7 +55,14 @@ export class ClickHouseService implements OnModuleInit { format: "JSONEachRow", }); } catch (error) { - this.logger.error({ error }, "ClickHouse healthcheck failed"); + this.logger.error({ error }, "ClickHouse client healthcheck failed"); + throw error; + } + + try { + await this.knex.raw("SELECT 1"); + } catch (error) { + this.logger.error({ error }, "Knex healthcheck failed"); throw error; } } diff --git a/apps/server/src/app/metrics/project-metrics.service.ts b/apps/server/src/app/metrics/project-metrics.service.ts index 253f4b36..d34d04e9 100644 --- a/apps/server/src/app/metrics/project-metrics.service.ts +++ b/apps/server/src/app/metrics/project-metrics.service.ts @@ -67,9 +67,12 @@ export class ProjectMetricsService { switch (metric) { case DeltaMetricType.AverageRequestDuration: + const currentValue = /*sql*/ `avgIf(r.duration, r.isError = false AND r.requestTimestamp >= currentStartDate AND r.responseTimestamp <= currentEndDate)`; + const previousValue = /*sql*/ `avgIf(r.duration, r.isError = false AND r.requestTimestamp >= previousStartDate AND r.responseTimestamp <= previousEndDate)`; + selectStatement = /*sql*/ ` - avgIf(r.duration, r.isError = false AND r.requestTimestamp >= currentStartDate AND r.responseTimestamp <= currentEndDate) AS currentValue, - avgIf(r.duration, r.isError = false AND r.requestTimestamp >= previousStartDate AND r.responseTimestamp <= previousEndDate) AS previousValue + if(isNaN(${currentValue}), 0, ${currentValue}) AS currentValue, + if(isNaN(${previousValue}), 0, ${previousValue}) AS previousValue `; break; case DeltaMetricType.TotalRequests: