Skip to content

Commit f112663

Browse files
fix clickhouse knex connection (#280)
1 parent 94430df commit f112663

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

apps/server/.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"overrides": [
55
{
66
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7-
"rules": {}
7+
"rules": {
8+
"no-case-declarations": "off"
9+
}
810
},
911
{
1012
"files": ["*.ts", "*.tsx"],

apps/server/src/app/clickhouse/clickhouse.service.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createLogger } from "../logger/create-logger";
44
import { pino } from "pino";
55
import { createClient, ClickHouseClient } from "@clickhouse/client"; // or '@clickhouse/client-web'
66
import { knex, Knex } from "knex";
7+
import clickhouesDialect from "@pezzo/knex-clickhouse-dialect";
78

89
@Injectable()
910
export class ClickHouseService implements OnModuleInit {
@@ -36,17 +37,12 @@ export class ClickHouseService implements OnModuleInit {
3637
});
3738

3839
this.logger.info("Creating Knex instance");
40+
const connectionStr =
41+
`${protocol}://${username}:${password}@${host}:${port}/${database}` as any;
3942

4043
this.knex = knex({
41-
client: "mysql2",
42-
connection: {
43-
host,
44-
port: 9004,
45-
user: username,
46-
password,
47-
database,
48-
timezone: "Z",
49-
},
44+
client: clickhouesDialect as any,
45+
connection: () => connectionStr,
5046
});
5147

5248
await this.healthCheck();
@@ -59,7 +55,14 @@ export class ClickHouseService implements OnModuleInit {
5955
format: "JSONEachRow",
6056
});
6157
} catch (error) {
62-
this.logger.error({ error }, "ClickHouse healthcheck failed");
58+
this.logger.error({ error }, "ClickHouse client healthcheck failed");
59+
throw error;
60+
}
61+
62+
try {
63+
await this.knex.raw("SELECT 1");
64+
} catch (error) {
65+
this.logger.error({ error }, "Knex healthcheck failed");
6366
throw error;
6467
}
6568
}

apps/server/src/app/metrics/project-metrics.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ export class ProjectMetricsService {
6767

6868
switch (metric) {
6969
case DeltaMetricType.AverageRequestDuration:
70+
const currentValue = /*sql*/ `avgIf(r.duration, r.isError = false AND r.requestTimestamp >= currentStartDate AND r.responseTimestamp <= currentEndDate)`;
71+
const previousValue = /*sql*/ `avgIf(r.duration, r.isError = false AND r.requestTimestamp >= previousStartDate AND r.responseTimestamp <= previousEndDate)`;
72+
7073
selectStatement = /*sql*/ `
71-
avgIf(r.duration, r.isError = false AND r.requestTimestamp >= currentStartDate AND r.responseTimestamp <= currentEndDate) AS currentValue,
72-
avgIf(r.duration, r.isError = false AND r.requestTimestamp >= previousStartDate AND r.responseTimestamp <= previousEndDate) AS previousValue
74+
if(isNaN(${currentValue}), 0, ${currentValue}) AS currentValue,
75+
if(isNaN(${previousValue}), 0, ${previousValue}) AS previousValue
7376
`;
7477
break;
7578
case DeltaMetricType.TotalRequests:

0 commit comments

Comments
 (0)