Skip to content

Commit

Permalink
fix clickhouse knex connection
Browse files Browse the repository at this point in the history
  • Loading branch information
arielweinberger committed Dec 31, 2023
1 parent 94430df commit 7858612
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
4 changes: 3 additions & 1 deletion apps/server/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
"rules": {
"no-case-declarations": "off"
}
},
{
"files": ["*.ts", "*.tsx"],
Expand Down
23 changes: 13 additions & 10 deletions apps/server/src/app/clickhouse/clickhouse.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down
7 changes: 5 additions & 2 deletions apps/server/src/app/metrics/project-metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7858612

Please sign in to comment.