-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.ts
More file actions
153 lines (138 loc) · 4.53 KB
/
index.ts
File metadata and controls
153 lines (138 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { ClickHouseSettings } from "@clickhouse/client";
import { ClickhouseClient } from "./client/client.js";
import { ClickhouseReader, ClickhouseWriter } from "./client/types.js";
import { NoopClient } from "./client/noop.js";
import {
insertTaskRuns,
insertRawTaskRunPayloads,
getTaskRunsQueryBuilder,
getTaskActivityQueryBuilder,
getCurrentRunningStats,
getAverageDurations,
getTaskUsageByOrganization,
} from "./taskRuns.js";
import { Logger, type LogLevel } from "@trigger.dev/core/logger";
import type { Agent as HttpAgent } from "http";
import type { Agent as HttpsAgent } from "https";
export type * from "./taskRuns.js";
export type ClickhouseCommonConfig = {
keepAlive?: {
enabled?: boolean;
idleSocketTtl?: number;
};
httpAgent?: HttpAgent | HttpsAgent;
clickhouseSettings?: ClickHouseSettings;
logger?: Logger;
logLevel?: LogLevel;
compression?: {
request?: boolean;
response?: boolean;
};
maxOpenConnections?: number;
};
export type ClickHouseConfig =
| ({
name?: string;
url?: string;
writerUrl?: never;
readerUrl?: never;
} & ClickhouseCommonConfig)
| ({
name?: never;
url?: never;
writerName?: string;
writerUrl: string;
readerName?: string;
readerUrl: string;
} & ClickhouseCommonConfig);
export class ClickHouse {
public readonly reader: ClickhouseReader;
public readonly writer: ClickhouseWriter;
private readonly logger: Logger;
private _splitClients: boolean;
constructor(config: ClickHouseConfig) {
this.logger = config.logger ?? new Logger("ClickHouse", config.logLevel ?? "debug");
if (config.url) {
const url = new URL(config.url);
url.password = "redacted";
this.logger.info("🏠 Initializing ClickHouse client with url", { url: url.toString() });
const client = new ClickhouseClient({
name: config.name ?? "clickhouse",
url: config.url,
clickhouseSettings: config.clickhouseSettings,
logger: this.logger,
logLevel: config.logLevel,
keepAlive: config.keepAlive,
httpAgent: config.httpAgent,
maxOpenConnections: config.maxOpenConnections,
compression: config.compression,
});
this.reader = client;
this.writer = client;
this._splitClients = false;
} else if (config.writerUrl && config.readerUrl) {
this.reader = new ClickhouseClient({
name: config.readerName ?? "clickhouse-reader",
url: config.readerUrl,
clickhouseSettings: config.clickhouseSettings,
logger: this.logger,
logLevel: config.logLevel,
keepAlive: config.keepAlive,
httpAgent: config.httpAgent,
maxOpenConnections: config.maxOpenConnections,
compression: config.compression,
});
this.writer = new ClickhouseClient({
name: config.writerName ?? "clickhouse-writer",
url: config.writerUrl,
clickhouseSettings: config.clickhouseSettings,
logger: this.logger,
logLevel: config.logLevel,
keepAlive: config.keepAlive,
httpAgent: config.httpAgent,
maxOpenConnections: config.maxOpenConnections,
compression: config.compression,
});
this._splitClients = true;
} else {
this.reader = new NoopClient();
this.writer = new NoopClient();
this._splitClients = true;
}
}
static fromEnv(): ClickHouse {
if (
typeof process.env.CLICKHOUSE_WRITER_URL === "string" &&
typeof process.env.CLICKHOUSE_READER_URL === "string"
) {
return new ClickHouse({
writerUrl: process.env.CLICKHOUSE_WRITER_URL,
readerUrl: process.env.CLICKHOUSE_READER_URL,
writerName: process.env.CLICKHOUSE_WRITER_NAME,
readerName: process.env.CLICKHOUSE_READER_NAME,
});
}
return new ClickHouse({
url: process.env.CLICKHOUSE_URL,
name: process.env.CLICKHOUSE_NAME,
});
}
async close() {
if (this._splitClients) {
await Promise.all([this.reader.close(), this.writer.close()]);
} else {
await this.reader.close();
}
}
get taskRuns() {
return {
insert: insertTaskRuns(this.writer),
insertPayloads: insertRawTaskRunPayloads(this.writer),
queryBuilder: getTaskRunsQueryBuilder(this.reader),
getTaskActivity: getTaskActivityQueryBuilder(this.reader),
getCurrentRunningStats: getCurrentRunningStats(this.reader),
getAverageDurations: getAverageDurations(this.reader),
getTaskUsageByOrganization: getTaskUsageByOrganization(this.reader),
};
}
}