Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix clickhouse knex connection #280

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading