weave-typescript 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -98,4 +98,72 @@ export interface LockRunRow {
98
98
  pgAdvisoryXactLock: string;
99
99
  }
100
100
  export declare function lockRun(client: Client, args: LockRunArgs): Promise<void>;
101
+ export declare const queryEventsByTypeQuery = "-- name: QueryEventsByType :many\nSELECT\n id,\n tenant_id,\n run_id,\n sequence,\n type,\n data,\n causation_id,\n correlation_id,\n occurred_at\nFROM atc.events\nWHERE tenant_id = $1\n AND type = $2\n AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())\n AND (\n $3::timestamptz IS NULL\n OR (occurred_at, sequence) < ($3::timestamptz, $4::bigint)\n )\nORDER BY occurred_at DESC, sequence DESC\nLIMIT $5";
102
+ export interface QueryEventsByTypeArgs {
103
+ tenantId: string;
104
+ eventType: string;
105
+ afterOccurredAt: Date;
106
+ afterSequence: string;
107
+ pageSize: string;
108
+ }
109
+ export interface QueryEventsByTypeRow {
110
+ id: string;
111
+ tenantId: string;
112
+ runId: string;
113
+ sequence: string;
114
+ type: string;
115
+ data: any | null;
116
+ causationId: string | null;
117
+ correlationId: string | null;
118
+ occurredAt: Date;
119
+ }
120
+ export declare function queryEventsByType(client: Client, args: QueryEventsByTypeArgs): Promise<QueryEventsByTypeRow[]>;
121
+ export declare const queryEventsByTimeRangeQuery = "-- name: QueryEventsByTimeRange :many\nSELECT\n id,\n tenant_id,\n run_id,\n sequence,\n type,\n data,\n causation_id,\n correlation_id,\n occurred_at\nFROM atc.events\nWHERE tenant_id = $1\n AND occurred_at >= $2\n AND occurred_at < $3\n AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())\nORDER BY occurred_at ASC, sequence ASC\nLIMIT $4";
122
+ export interface QueryEventsByTimeRangeArgs {
123
+ tenantId: string;
124
+ startTime: Date;
125
+ endTime: Date;
126
+ pageSize: string;
127
+ }
128
+ export interface QueryEventsByTimeRangeRow {
129
+ id: string;
130
+ tenantId: string;
131
+ runId: string;
132
+ sequence: string;
133
+ type: string;
134
+ data: any | null;
135
+ causationId: string | null;
136
+ correlationId: string | null;
137
+ occurredAt: Date;
138
+ }
139
+ export declare function queryEventsByTimeRange(client: Client, args: QueryEventsByTimeRangeArgs): Promise<QueryEventsByTimeRangeRow[]>;
140
+ export declare const queryEventsByRunAndTypesQuery = "-- name: QueryEventsByRunAndTypes :many\nSELECT\n id,\n tenant_id,\n run_id,\n sequence,\n type,\n data,\n causation_id,\n correlation_id,\n occurred_at\nFROM atc.events\nWHERE tenant_id = $1\n AND run_id = $2\n AND type = ANY($3::text[])\n AND sequence > $4\n AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())\nORDER BY sequence ASC\nLIMIT $5";
141
+ export interface QueryEventsByRunAndTypesArgs {
142
+ tenantId: string;
143
+ runId: string;
144
+ eventTypes: string[];
145
+ afterSequence: string;
146
+ pageSize: string;
147
+ }
148
+ export interface QueryEventsByRunAndTypesRow {
149
+ id: string;
150
+ tenantId: string;
151
+ runId: string;
152
+ sequence: string;
153
+ type: string;
154
+ data: any | null;
155
+ causationId: string | null;
156
+ correlationId: string | null;
157
+ occurredAt: Date;
158
+ }
159
+ export declare function queryEventsByRunAndTypes(client: Client, args: QueryEventsByRunAndTypesArgs): Promise<QueryEventsByRunAndTypesRow[]>;
160
+ export declare const countEventsByTypeQuery = "-- name: CountEventsByType :one\nSELECT COUNT(*)::BIGINT AS total\nFROM atc.events\nWHERE tenant_id = $1\n AND type = $2\n AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())";
161
+ export interface CountEventsByTypeArgs {
162
+ tenantId: string;
163
+ eventType: string;
164
+ }
165
+ export interface CountEventsByTypeRow {
166
+ total: string;
167
+ }
168
+ export declare function countEventsByType(client: Client, args: CountEventsByTypeArgs): Promise<CountEventsByTypeRow | null>;
101
169
  export {};
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.lockRunQuery = exports.listEventsByTypeQuery = exports.loadAllEventsQuery = exports.loadEventsQuery = exports.getCurrentVersionQuery = exports.insertEventQuery = void 0;
3
+ exports.countEventsByTypeQuery = exports.queryEventsByRunAndTypesQuery = exports.queryEventsByTimeRangeQuery = exports.queryEventsByTypeQuery = exports.lockRunQuery = exports.listEventsByTypeQuery = exports.loadAllEventsQuery = exports.loadEventsQuery = exports.getCurrentVersionQuery = exports.insertEventQuery = void 0;
4
4
  exports.insertEvent = insertEvent;
5
5
  exports.getCurrentVersion = getCurrentVersion;
6
6
  exports.loadEvents = loadEvents;
7
7
  exports.loadAllEvents = loadAllEvents;
8
8
  exports.listEventsByType = listEventsByType;
9
9
  exports.lockRun = lockRun;
10
+ exports.queryEventsByType = queryEventsByType;
11
+ exports.queryEventsByTimeRange = queryEventsByTimeRange;
12
+ exports.queryEventsByRunAndTypes = queryEventsByRunAndTypes;
13
+ exports.countEventsByType = countEventsByType;
10
14
  exports.insertEventQuery = `-- name: InsertEvent :one
11
15
  INSERT INTO atc.events (
12
16
  id,
@@ -189,3 +193,141 @@ async function lockRun(client, args) {
189
193
  rowMode: "array"
190
194
  });
191
195
  }
196
+ exports.queryEventsByTypeQuery = `-- name: QueryEventsByType :many
197
+ SELECT
198
+ id,
199
+ tenant_id,
200
+ run_id,
201
+ sequence,
202
+ type,
203
+ data,
204
+ causation_id,
205
+ correlation_id,
206
+ occurred_at
207
+ FROM atc.events
208
+ WHERE tenant_id = $1
209
+ AND type = $2
210
+ AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())
211
+ AND (
212
+ $3::timestamptz IS NULL
213
+ OR (occurred_at, sequence) < ($3::timestamptz, $4::bigint)
214
+ )
215
+ ORDER BY occurred_at DESC, sequence DESC
216
+ LIMIT $5`;
217
+ async function queryEventsByType(client, args) {
218
+ const result = await client.query({
219
+ text: exports.queryEventsByTypeQuery,
220
+ values: [args.tenantId, args.eventType, args.afterOccurredAt, args.afterSequence, args.pageSize],
221
+ rowMode: "array"
222
+ });
223
+ return result.rows.map(row => {
224
+ return {
225
+ id: row[0],
226
+ tenantId: row[1],
227
+ runId: row[2],
228
+ sequence: row[3],
229
+ type: row[4],
230
+ data: row[5],
231
+ causationId: row[6],
232
+ correlationId: row[7],
233
+ occurredAt: row[8]
234
+ };
235
+ });
236
+ }
237
+ exports.queryEventsByTimeRangeQuery = `-- name: QueryEventsByTimeRange :many
238
+ SELECT
239
+ id,
240
+ tenant_id,
241
+ run_id,
242
+ sequence,
243
+ type,
244
+ data,
245
+ causation_id,
246
+ correlation_id,
247
+ occurred_at
248
+ FROM atc.events
249
+ WHERE tenant_id = $1
250
+ AND occurred_at >= $2
251
+ AND occurred_at < $3
252
+ AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())
253
+ ORDER BY occurred_at ASC, sequence ASC
254
+ LIMIT $4`;
255
+ async function queryEventsByTimeRange(client, args) {
256
+ const result = await client.query({
257
+ text: exports.queryEventsByTimeRangeQuery,
258
+ values: [args.tenantId, args.startTime, args.endTime, args.pageSize],
259
+ rowMode: "array"
260
+ });
261
+ return result.rows.map(row => {
262
+ return {
263
+ id: row[0],
264
+ tenantId: row[1],
265
+ runId: row[2],
266
+ sequence: row[3],
267
+ type: row[4],
268
+ data: row[5],
269
+ causationId: row[6],
270
+ correlationId: row[7],
271
+ occurredAt: row[8]
272
+ };
273
+ });
274
+ }
275
+ exports.queryEventsByRunAndTypesQuery = `-- name: QueryEventsByRunAndTypes :many
276
+ SELECT
277
+ id,
278
+ tenant_id,
279
+ run_id,
280
+ sequence,
281
+ type,
282
+ data,
283
+ causation_id,
284
+ correlation_id,
285
+ occurred_at
286
+ FROM atc.events
287
+ WHERE tenant_id = $1
288
+ AND run_id = $2
289
+ AND type = ANY($3::text[])
290
+ AND sequence > $4
291
+ AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())
292
+ ORDER BY sequence ASC
293
+ LIMIT $5`;
294
+ async function queryEventsByRunAndTypes(client, args) {
295
+ const result = await client.query({
296
+ text: exports.queryEventsByRunAndTypesQuery,
297
+ values: [args.tenantId, args.runId, args.eventTypes, args.afterSequence, args.pageSize],
298
+ rowMode: "array"
299
+ });
300
+ return result.rows.map(row => {
301
+ return {
302
+ id: row[0],
303
+ tenantId: row[1],
304
+ runId: row[2],
305
+ sequence: row[3],
306
+ type: row[4],
307
+ data: row[5],
308
+ causationId: row[6],
309
+ correlationId: row[7],
310
+ occurredAt: row[8]
311
+ };
312
+ });
313
+ }
314
+ exports.countEventsByTypeQuery = `-- name: CountEventsByType :one
315
+ SELECT COUNT(*)::BIGINT AS total
316
+ FROM atc.events
317
+ WHERE tenant_id = $1
318
+ AND type = $2
319
+ AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())`;
320
+ async function countEventsByType(client, args) {
321
+ const result = await client.query({
322
+ text: exports.countEventsByTypeQuery,
323
+ values: [args.tenantId, args.eventType],
324
+ rowMode: "array"
325
+ });
326
+ if (result.rows.length !== 1) {
327
+ return null;
328
+ }
329
+ const row = result.rows[0];
330
+ return {
331
+ total: row[0]
332
+ };
333
+ }
@@ -0,0 +1,139 @@
1
+ import { QueryArrayConfig, QueryArrayResult } from "pg";
2
+ interface Client {
3
+ query: (config: QueryArrayConfig) => Promise<QueryArrayResult>;
4
+ }
5
+ export declare const insertInspectionEventQuery = "-- name: InsertInspectionEvent :one\nINSERT INTO atc.inspection_events (\n id,\n tenant_id,\n occurred_at,\n type,\n category,\n subject_kind,\n subject_id,\n actor_kind,\n actor_id,\n run_id,\n process_id,\n action,\n outcome,\n reason,\n correlation_id,\n causation_id,\n schema_version,\n data,\n expires_at\n) VALUES (\n $1,\n $2,\n $3,\n $4,\n $5,\n $6,\n $7,\n $8,\n $9,\n $10,\n $11,\n $12,\n $13,\n $14,\n $15,\n $16,\n $17,\n $18,\n $19\n)\nRETURNING\n id,\n tenant_id,\n occurred_at,\n type,\n category,\n subject_kind,\n subject_id,\n actor_kind,\n actor_id,\n run_id,\n process_id,\n action,\n outcome,\n reason,\n correlation_id,\n causation_id,\n schema_version,\n data,\n expires_at,\n sequence";
6
+ export interface InsertInspectionEventArgs {
7
+ id: string;
8
+ tenantId: string;
9
+ occurredAt: Date;
10
+ type: string;
11
+ category: string;
12
+ subjectKind: string;
13
+ subjectId: string;
14
+ actorKind: string;
15
+ actorId: string;
16
+ runId: string | null;
17
+ processId: string | null;
18
+ action: string;
19
+ outcome: string;
20
+ reason: string | null;
21
+ correlationId: string | null;
22
+ causationId: string | null;
23
+ schemaVersion: number;
24
+ data: any;
25
+ expiresAt: Date;
26
+ }
27
+ export interface InsertInspectionEventRow {
28
+ id: string;
29
+ tenantId: string;
30
+ occurredAt: Date;
31
+ type: string;
32
+ category: string;
33
+ subjectKind: string;
34
+ subjectId: string;
35
+ actorKind: string;
36
+ actorId: string;
37
+ runId: string | null;
38
+ processId: string | null;
39
+ action: string;
40
+ outcome: string;
41
+ reason: string | null;
42
+ correlationId: string | null;
43
+ causationId: string | null;
44
+ schemaVersion: number;
45
+ data: any;
46
+ expiresAt: Date;
47
+ sequence: string;
48
+ }
49
+ export declare function insertInspectionEvent(client: Client, args: InsertInspectionEventArgs): Promise<InsertInspectionEventRow | null>;
50
+ export declare const queryInspectionEventsQuery = "-- name: QueryInspectionEvents :many\nSELECT\n id,\n tenant_id,\n occurred_at,\n type,\n category,\n subject_kind,\n subject_id,\n actor_kind,\n actor_id,\n run_id,\n process_id,\n action,\n outcome,\n reason,\n correlation_id,\n causation_id,\n schema_version,\n data,\n expires_at,\n sequence\nFROM atc.inspection_events\nWHERE tenant_id = $1\n AND (\n $2::text IS NULL\n OR (\n subject_kind = $2::text\n AND subject_id = $3::text\n )\n )\n AND (\n $4::text IS NULL\n OR (\n actor_kind = $4::text\n AND actor_id = $5::text\n )\n )\n AND ($6::text IS NULL OR run_id = $6::text)\n AND ($7::text IS NULL OR process_id = $7::text)\n AND ($8::text IS NULL OR action = $8::text)\n AND ($9::text IS NULL OR outcome = $9::text)\n AND (\n $10::timestamptz IS NULL\n OR occurred_at >= $10::timestamptz\n )\n AND (\n $11::timestamptz IS NULL\n OR occurred_at < $11::timestamptz\n )\n AND (\n $12::text IS NULL\n OR type LIKE replace(replace($12::text, '*', '%'), '?', '_')\n )\n AND (\n $13::timestamptz IS NULL\n OR (\n occurred_at,\n sequence\n ) < (\n $13::timestamptz,\n $14::bigint\n )\n )\nORDER BY occurred_at DESC, sequence DESC\nLIMIT $15";
51
+ export interface QueryInspectionEventsArgs {
52
+ tenantId: string;
53
+ subjectKind: string | null;
54
+ subjectId: string | null;
55
+ actorKind: string | null;
56
+ actorId: string | null;
57
+ runId: string | null;
58
+ processId: string | null;
59
+ action: string | null;
60
+ outcome: string | null;
61
+ startTime: Date | null;
62
+ endTime: Date | null;
63
+ typeFilter: string | null;
64
+ afterOccurredAt: Date | null;
65
+ afterSequence: string;
66
+ pageSize: string;
67
+ }
68
+ export interface QueryInspectionEventsRow {
69
+ id: string;
70
+ tenantId: string;
71
+ occurredAt: Date;
72
+ type: string;
73
+ category: string;
74
+ subjectKind: string;
75
+ subjectId: string;
76
+ actorKind: string;
77
+ actorId: string;
78
+ runId: string | null;
79
+ processId: string | null;
80
+ action: string;
81
+ outcome: string;
82
+ reason: string | null;
83
+ correlationId: string | null;
84
+ causationId: string | null;
85
+ schemaVersion: number;
86
+ data: any;
87
+ expiresAt: Date;
88
+ sequence: string;
89
+ }
90
+ export declare function queryInspectionEvents(client: Client, args: QueryInspectionEventsArgs): Promise<QueryInspectionEventsRow[]>;
91
+ export declare const loadInspectionEventsFromSequenceQuery = "-- name: LoadInspectionEventsFromSequence :many\nSELECT\n id,\n tenant_id,\n occurred_at,\n type,\n category,\n subject_kind,\n subject_id,\n actor_kind,\n actor_id,\n run_id,\n process_id,\n action,\n outcome,\n reason,\n correlation_id,\n causation_id,\n schema_version,\n data,\n expires_at,\n sequence\nFROM atc.inspection_events\nWHERE tenant_id = $1\n AND sequence > $2\n AND (\n $3::text IS NULL\n OR (\n subject_kind = $3::text\n AND subject_id = $4::text\n )\n )\n AND (\n $5::text IS NULL\n OR (\n actor_kind = $5::text\n AND actor_id = $6::text\n )\n )\n AND ($7::text IS NULL OR run_id = $7::text)\n AND ($8::text IS NULL OR process_id = $8::text)\n AND ($9::text IS NULL OR action = $9::text)\n AND ($10::text IS NULL OR outcome = $10::text)\n AND (\n $11::timestamptz IS NULL\n OR occurred_at >= $11::timestamptz\n )\n AND (\n $12::timestamptz IS NULL\n OR occurred_at < $12::timestamptz\n )\n AND (\n $13::text IS NULL\n OR type LIKE replace(replace($13::text, '*', '%'), '?', '_')\n )\nORDER BY sequence ASC";
92
+ export interface LoadInspectionEventsFromSequenceArgs {
93
+ tenantId: string;
94
+ afterSequence: string;
95
+ subjectKind: string | null;
96
+ subjectId: string | null;
97
+ actorKind: string | null;
98
+ actorId: string | null;
99
+ runId: string | null;
100
+ processId: string | null;
101
+ action: string | null;
102
+ outcome: string | null;
103
+ startTime: Date | null;
104
+ endTime: Date | null;
105
+ typeFilter: string | null;
106
+ }
107
+ export interface LoadInspectionEventsFromSequenceRow {
108
+ id: string;
109
+ tenantId: string;
110
+ occurredAt: Date;
111
+ type: string;
112
+ category: string;
113
+ subjectKind: string;
114
+ subjectId: string;
115
+ actorKind: string;
116
+ actorId: string;
117
+ runId: string | null;
118
+ processId: string | null;
119
+ action: string;
120
+ outcome: string;
121
+ reason: string | null;
122
+ correlationId: string | null;
123
+ causationId: string | null;
124
+ schemaVersion: number;
125
+ data: any;
126
+ expiresAt: Date;
127
+ sequence: string;
128
+ }
129
+ export declare function loadInspectionEventsFromSequence(client: Client, args: LoadInspectionEventsFromSequenceArgs): Promise<LoadInspectionEventsFromSequenceRow[]>;
130
+ export declare const deleteExpiredInspectionEventsQuery = "-- name: DeleteExpiredInspectionEvents :one\nWITH expired AS (\n SELECT id\n FROM atc.inspection_events\n WHERE expires_at < $1::timestamptz\n ORDER BY expires_at ASC\n LIMIT $2\n),\ndeleted AS (\n DELETE FROM atc.inspection_events\n WHERE id IN (SELECT id FROM expired)\n RETURNING id\n)\nSELECT COUNT(*)::BIGINT AS total\nFROM deleted";
131
+ export interface DeleteExpiredInspectionEventsArgs {
132
+ expiresBefore: Date;
133
+ deleteLimit: string;
134
+ }
135
+ export interface DeleteExpiredInspectionEventsRow {
136
+ total: string;
137
+ }
138
+ export declare function deleteExpiredInspectionEvents(client: Client, args: DeleteExpiredInspectionEventsArgs): Promise<DeleteExpiredInspectionEventsRow | null>;
139
+ export {};
@@ -0,0 +1,316 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deleteExpiredInspectionEventsQuery = exports.loadInspectionEventsFromSequenceQuery = exports.queryInspectionEventsQuery = exports.insertInspectionEventQuery = void 0;
4
+ exports.insertInspectionEvent = insertInspectionEvent;
5
+ exports.queryInspectionEvents = queryInspectionEvents;
6
+ exports.loadInspectionEventsFromSequence = loadInspectionEventsFromSequence;
7
+ exports.deleteExpiredInspectionEvents = deleteExpiredInspectionEvents;
8
+ exports.insertInspectionEventQuery = `-- name: InsertInspectionEvent :one
9
+ INSERT INTO atc.inspection_events (
10
+ id,
11
+ tenant_id,
12
+ occurred_at,
13
+ type,
14
+ category,
15
+ subject_kind,
16
+ subject_id,
17
+ actor_kind,
18
+ actor_id,
19
+ run_id,
20
+ process_id,
21
+ action,
22
+ outcome,
23
+ reason,
24
+ correlation_id,
25
+ causation_id,
26
+ schema_version,
27
+ data,
28
+ expires_at
29
+ ) VALUES (
30
+ $1,
31
+ $2,
32
+ $3,
33
+ $4,
34
+ $5,
35
+ $6,
36
+ $7,
37
+ $8,
38
+ $9,
39
+ $10,
40
+ $11,
41
+ $12,
42
+ $13,
43
+ $14,
44
+ $15,
45
+ $16,
46
+ $17,
47
+ $18,
48
+ $19
49
+ )
50
+ RETURNING
51
+ id,
52
+ tenant_id,
53
+ occurred_at,
54
+ type,
55
+ category,
56
+ subject_kind,
57
+ subject_id,
58
+ actor_kind,
59
+ actor_id,
60
+ run_id,
61
+ process_id,
62
+ action,
63
+ outcome,
64
+ reason,
65
+ correlation_id,
66
+ causation_id,
67
+ schema_version,
68
+ data,
69
+ expires_at,
70
+ sequence`;
71
+ async function insertInspectionEvent(client, args) {
72
+ const result = await client.query({
73
+ text: exports.insertInspectionEventQuery,
74
+ values: [args.id, args.tenantId, args.occurredAt, args.type, args.category, args.subjectKind, args.subjectId, args.actorKind, args.actorId, args.runId, args.processId, args.action, args.outcome, args.reason, args.correlationId, args.causationId, args.schemaVersion, args.data, args.expiresAt],
75
+ rowMode: "array"
76
+ });
77
+ if (result.rows.length !== 1) {
78
+ return null;
79
+ }
80
+ const row = result.rows[0];
81
+ return {
82
+ id: row[0],
83
+ tenantId: row[1],
84
+ occurredAt: row[2],
85
+ type: row[3],
86
+ category: row[4],
87
+ subjectKind: row[5],
88
+ subjectId: row[6],
89
+ actorKind: row[7],
90
+ actorId: row[8],
91
+ runId: row[9],
92
+ processId: row[10],
93
+ action: row[11],
94
+ outcome: row[12],
95
+ reason: row[13],
96
+ correlationId: row[14],
97
+ causationId: row[15],
98
+ schemaVersion: row[16],
99
+ data: row[17],
100
+ expiresAt: row[18],
101
+ sequence: row[19]
102
+ };
103
+ }
104
+ exports.queryInspectionEventsQuery = `-- name: QueryInspectionEvents :many
105
+ SELECT
106
+ id,
107
+ tenant_id,
108
+ occurred_at,
109
+ type,
110
+ category,
111
+ subject_kind,
112
+ subject_id,
113
+ actor_kind,
114
+ actor_id,
115
+ run_id,
116
+ process_id,
117
+ action,
118
+ outcome,
119
+ reason,
120
+ correlation_id,
121
+ causation_id,
122
+ schema_version,
123
+ data,
124
+ expires_at,
125
+ sequence
126
+ FROM atc.inspection_events
127
+ WHERE tenant_id = $1
128
+ AND (
129
+ $2::text IS NULL
130
+ OR (
131
+ subject_kind = $2::text
132
+ AND subject_id = $3::text
133
+ )
134
+ )
135
+ AND (
136
+ $4::text IS NULL
137
+ OR (
138
+ actor_kind = $4::text
139
+ AND actor_id = $5::text
140
+ )
141
+ )
142
+ AND ($6::text IS NULL OR run_id = $6::text)
143
+ AND ($7::text IS NULL OR process_id = $7::text)
144
+ AND ($8::text IS NULL OR action = $8::text)
145
+ AND ($9::text IS NULL OR outcome = $9::text)
146
+ AND (
147
+ $10::timestamptz IS NULL
148
+ OR occurred_at >= $10::timestamptz
149
+ )
150
+ AND (
151
+ $11::timestamptz IS NULL
152
+ OR occurred_at < $11::timestamptz
153
+ )
154
+ AND (
155
+ $12::text IS NULL
156
+ OR type LIKE replace(replace($12::text, '*', '%'), '?', '_')
157
+ )
158
+ AND (
159
+ $13::timestamptz IS NULL
160
+ OR (
161
+ occurred_at,
162
+ sequence
163
+ ) < (
164
+ $13::timestamptz,
165
+ $14::bigint
166
+ )
167
+ )
168
+ ORDER BY occurred_at DESC, sequence DESC
169
+ LIMIT $15`;
170
+ async function queryInspectionEvents(client, args) {
171
+ const result = await client.query({
172
+ text: exports.queryInspectionEventsQuery,
173
+ values: [args.tenantId, args.subjectKind, args.subjectId, args.actorKind, args.actorId, args.runId, args.processId, args.action, args.outcome, args.startTime, args.endTime, args.typeFilter, args.afterOccurredAt, args.afterSequence, args.pageSize],
174
+ rowMode: "array"
175
+ });
176
+ return result.rows.map(row => {
177
+ return {
178
+ id: row[0],
179
+ tenantId: row[1],
180
+ occurredAt: row[2],
181
+ type: row[3],
182
+ category: row[4],
183
+ subjectKind: row[5],
184
+ subjectId: row[6],
185
+ actorKind: row[7],
186
+ actorId: row[8],
187
+ runId: row[9],
188
+ processId: row[10],
189
+ action: row[11],
190
+ outcome: row[12],
191
+ reason: row[13],
192
+ correlationId: row[14],
193
+ causationId: row[15],
194
+ schemaVersion: row[16],
195
+ data: row[17],
196
+ expiresAt: row[18],
197
+ sequence: row[19]
198
+ };
199
+ });
200
+ }
201
+ exports.loadInspectionEventsFromSequenceQuery = `-- name: LoadInspectionEventsFromSequence :many
202
+ SELECT
203
+ id,
204
+ tenant_id,
205
+ occurred_at,
206
+ type,
207
+ category,
208
+ subject_kind,
209
+ subject_id,
210
+ actor_kind,
211
+ actor_id,
212
+ run_id,
213
+ process_id,
214
+ action,
215
+ outcome,
216
+ reason,
217
+ correlation_id,
218
+ causation_id,
219
+ schema_version,
220
+ data,
221
+ expires_at,
222
+ sequence
223
+ FROM atc.inspection_events
224
+ WHERE tenant_id = $1
225
+ AND sequence > $2
226
+ AND (
227
+ $3::text IS NULL
228
+ OR (
229
+ subject_kind = $3::text
230
+ AND subject_id = $4::text
231
+ )
232
+ )
233
+ AND (
234
+ $5::text IS NULL
235
+ OR (
236
+ actor_kind = $5::text
237
+ AND actor_id = $6::text
238
+ )
239
+ )
240
+ AND ($7::text IS NULL OR run_id = $7::text)
241
+ AND ($8::text IS NULL OR process_id = $8::text)
242
+ AND ($9::text IS NULL OR action = $9::text)
243
+ AND ($10::text IS NULL OR outcome = $10::text)
244
+ AND (
245
+ $11::timestamptz IS NULL
246
+ OR occurred_at >= $11::timestamptz
247
+ )
248
+ AND (
249
+ $12::timestamptz IS NULL
250
+ OR occurred_at < $12::timestamptz
251
+ )
252
+ AND (
253
+ $13::text IS NULL
254
+ OR type LIKE replace(replace($13::text, '*', '%'), '?', '_')
255
+ )
256
+ ORDER BY sequence ASC`;
257
+ async function loadInspectionEventsFromSequence(client, args) {
258
+ const result = await client.query({
259
+ text: exports.loadInspectionEventsFromSequenceQuery,
260
+ values: [args.tenantId, args.afterSequence, args.subjectKind, args.subjectId, args.actorKind, args.actorId, args.runId, args.processId, args.action, args.outcome, args.startTime, args.endTime, args.typeFilter],
261
+ rowMode: "array"
262
+ });
263
+ return result.rows.map(row => {
264
+ return {
265
+ id: row[0],
266
+ tenantId: row[1],
267
+ occurredAt: row[2],
268
+ type: row[3],
269
+ category: row[4],
270
+ subjectKind: row[5],
271
+ subjectId: row[6],
272
+ actorKind: row[7],
273
+ actorId: row[8],
274
+ runId: row[9],
275
+ processId: row[10],
276
+ action: row[11],
277
+ outcome: row[12],
278
+ reason: row[13],
279
+ correlationId: row[14],
280
+ causationId: row[15],
281
+ schemaVersion: row[16],
282
+ data: row[17],
283
+ expiresAt: row[18],
284
+ sequence: row[19]
285
+ };
286
+ });
287
+ }
288
+ exports.deleteExpiredInspectionEventsQuery = `-- name: DeleteExpiredInspectionEvents :one
289
+ WITH expired AS (
290
+ SELECT id
291
+ FROM atc.inspection_events
292
+ WHERE expires_at < $1::timestamptz
293
+ ORDER BY expires_at ASC
294
+ LIMIT $2
295
+ ),
296
+ deleted AS (
297
+ DELETE FROM atc.inspection_events
298
+ WHERE id IN (SELECT id FROM expired)
299
+ RETURNING id
300
+ )
301
+ SELECT COUNT(*)::BIGINT AS total
302
+ FROM deleted`;
303
+ async function deleteExpiredInspectionEvents(client, args) {
304
+ const result = await client.query({
305
+ text: exports.deleteExpiredInspectionEventsQuery,
306
+ values: [args.expiresBefore, args.deleteLimit],
307
+ rowMode: "array"
308
+ });
309
+ if (result.rows.length !== 1) {
310
+ return null;
311
+ }
312
+ const row = result.rows[0];
313
+ return {
314
+ total: row[0]
315
+ };
316
+ }