-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.ts
More file actions
232 lines (210 loc) · 6.98 KB
/
index.ts
File metadata and controls
232 lines (210 loc) · 6.98 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import type { ClickHouseSettings } from "@clickhouse/client";
export type { ClickHouseSettings };
import { ClickhouseClient } from "./client/client.js";
import { ClickhouseReader, ClickhouseWriter } from "./client/types.js";
import { NoopClient } from "./client/noop.js";
import {
insertTaskRunsCompactArrays,
insertRawTaskRunPayloadsCompactArrays,
getTaskRunsQueryBuilder,
getTaskActivityQueryBuilder,
getCurrentRunningStats,
getAverageDurations,
getTaskUsageByOrganization,
getTaskRunsCountQueryBuilder,
getTaskRunTagsQueryBuilder,
} from "./taskRuns.js";
import {
getSpanDetailsQueryBuilder,
getSpanDetailsQueryBuilderV2,
getTraceDetailedSummaryQueryBuilder,
getTraceDetailedSummaryQueryBuilderV2,
getTraceSummaryQueryBuilder,
getTraceSummaryQueryBuilderV2,
insertTaskEvents,
insertTaskEventsV2,
getLogDetailQueryBuilderV2,
getLogsSearchListQueryBuilder,
} from "./taskEvents.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 * from "./taskEvents.js";
export type * from "./client/queryBuilder.js";
// Re-export column constants, indices, and type-safe accessors
export {
TASK_RUN_COLUMNS,
TASK_RUN_INDEX,
PAYLOAD_COLUMNS,
PAYLOAD_INDEX,
getTaskRunField,
getPayloadField,
} from "./taskRuns.js";
// TSQL query execution
export {
executeTSQL,
createTSQLExecutor,
type ExecuteTSQLOptions,
type TableSchema,
type TSQLQueryResult,
type TSQLQuerySuccess,
type QueryStats,
type FieldMappings,
type WhereClauseCondition,
} from "./client/tsql.js";
export type { OutputColumnMetadata } from "@internal/tsql";
// Errors
export { QueryError } from "./client/errors.js";
export type LogsQuerySettings = {
list?: ClickHouseSettings;
detail?: ClickHouseSettings;
};
export type ClickhouseCommonConfig = {
keepAlive?: {
enabled?: boolean;
idleSocketTtl?: number;
};
httpAgent?: HttpAgent | HttpsAgent;
clickhouseSettings?: ClickHouseSettings;
logger?: Logger;
logLevel?: LogLevel;
compression?: {
request?: boolean;
response?: boolean;
};
maxOpenConnections?: number;
logsQuerySettings?: LogsQuerySettings;
};
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;
private readonly logsQuerySettings?: LogsQuerySettings;
constructor(config: ClickHouseConfig) {
this.logger = config.logger ?? new Logger("ClickHouse", config.logLevel ?? "debug");
this.logsQuerySettings = config.logsQuerySettings;
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 {
insertCompactArrays: insertTaskRunsCompactArrays(this.writer),
insertPayloadsCompactArrays: insertRawTaskRunPayloadsCompactArrays(this.writer),
queryBuilder: getTaskRunsQueryBuilder(this.reader),
countQueryBuilder: getTaskRunsCountQueryBuilder(this.reader),
tagQueryBuilder: getTaskRunTagsQueryBuilder(this.reader),
getTaskActivity: getTaskActivityQueryBuilder(this.reader),
getCurrentRunningStats: getCurrentRunningStats(this.reader),
getAverageDurations: getAverageDurations(this.reader),
getTaskUsageByOrganization: getTaskUsageByOrganization(this.reader),
};
}
get taskEvents() {
return {
insert: insertTaskEvents(this.writer),
traceSummaryQueryBuilder: getTraceSummaryQueryBuilder(this.reader),
traceDetailedSummaryQueryBuilder: getTraceDetailedSummaryQueryBuilder(this.reader),
spanDetailsQueryBuilder: getSpanDetailsQueryBuilder(this.reader),
};
}
get taskEventsV2() {
return {
insert: insertTaskEventsV2(this.writer),
traceSummaryQueryBuilder: getTraceSummaryQueryBuilderV2(this.reader),
traceDetailedSummaryQueryBuilder: getTraceDetailedSummaryQueryBuilderV2(this.reader),
spanDetailsQueryBuilder: getSpanDetailsQueryBuilderV2(this.reader),
logDetailQueryBuilder: getLogDetailQueryBuilderV2(this.reader, this.logsQuerySettings?.detail),
};
}
get taskEventsSearch() {
return {
logsListQueryBuilder: getLogsSearchListQueryBuilder(this.reader, this.logsQuerySettings?.list),
};
}
}