@sixb/pg 0.2.4 → 0.2.6
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.
- package/dist/agents/agent-storage.d.ts +4 -0
- package/dist/agents/agent-storage.d.ts.map +1 -1
- package/dist/agents/approvals.d.ts +2 -1
- package/dist/agents/approvals.d.ts.map +1 -1
- package/dist/agents/checkpoints.d.ts +2 -1
- package/dist/agents/checkpoints.d.ts.map +1 -1
- package/dist/agents/inbox.d.ts +23 -0
- package/dist/agents/inbox.d.ts.map +1 -0
- package/dist/agents/messages.d.ts +2 -1
- package/dist/agents/messages.d.ts.map +1 -1
- package/dist/agents/reads.d.ts +9 -0
- package/dist/agents/reads.d.ts.map +1 -0
- package/dist/agents/runs.d.ts +2 -1
- package/dist/agents/runs.d.ts.map +1 -1
- package/dist/agents/threads.d.ts.map +1 -1
- package/dist/index.js +1212 -87
- package/dist/index.js.map +17 -15
- package/dist/latest-run-query.d.ts +3 -1
- package/dist/latest-run-query.d.ts.map +1 -1
- package/dist/migrations.d.ts.map +1 -1
- package/dist/ontology-storage/replacement-plans.d.ts.map +1 -1
- package/dist/pg-ai-cost-storage.d.ts.map +1 -1
- package/dist/pg-artifact-storage.d.ts +4 -1
- package/dist/pg-artifact-storage.d.ts.map +1 -1
- package/dist/pg-sync-run-storage.d.ts +2 -1
- package/dist/pg-sync-run-storage.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/agents/agent-storage.ts +6 -0
- package/src/agents/approvals.ts +44 -0
- package/src/agents/checkpoints.ts +26 -0
- package/src/agents/inbox.ts +421 -0
- package/src/agents/messages.ts +81 -2
- package/src/agents/reads.ts +160 -0
- package/src/agents/runs.ts +25 -0
- package/src/agents/threads.ts +95 -11
- package/src/latest-run-query.ts +8 -1
- package/src/migrations/103-script-executions.sql +145 -0
- package/src/migrations/104-artifact-pdfs.sql +18 -0
- package/src/migrations/105-guest-comment-links.sql +17 -0
- package/src/migrations/106-agent-thread-inbox.sql +36 -0
- package/src/migrations/107-agent-inbox-edits.sql +3 -0
- package/src/migrations/108-agent-thread-reads.sql +73 -0
- package/src/migrations/109-sync-run-connections.sql +6 -0
- package/src/migrations.ts +14 -0
- package/src/ontology-storage/replacement-plans.ts +51 -1
- package/src/pg-ai-cost-storage.ts +35 -20
- package/src/pg-artifact-storage.ts +67 -9
- package/src/pg-sync-run-storage.ts +69 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { wallClockInstant } from "@sixb/core/calendar"
|
|
1
2
|
import {
|
|
2
3
|
type AiAccountingAggregateFragment,
|
|
3
4
|
type AiAccountingRecordSetItem,
|
|
@@ -101,6 +102,7 @@ export class PgAiCostStorage implements AiCostStorage {
|
|
|
101
102
|
const query = normalizeAiAccountingQuery(input)
|
|
102
103
|
const providerId = query.providerId ?? null
|
|
103
104
|
const modelId = query.modelId ?? null
|
|
105
|
+
const timeZone = query.timeZone
|
|
104
106
|
const filter = pgAccountingFilterValues(query)
|
|
105
107
|
const rows = await this.sql<AggregateRow[]>`
|
|
106
108
|
WITH filtered AS (
|
|
@@ -187,29 +189,31 @@ export class PgAiCostStorage implements AiCostStorage {
|
|
|
187
189
|
NULL::text AS key_1,
|
|
188
190
|
NULL::text AS key_2,
|
|
189
191
|
NULL::text AS key_3,
|
|
190
|
-
NULL::
|
|
192
|
+
NULL::text AS bucket_start,
|
|
191
193
|
classified.*
|
|
192
194
|
FROM classified
|
|
193
195
|
UNION ALL
|
|
194
196
|
SELECT 'series', NULL, NULL, NULL,
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
date_trunc('hour', occurred_at AT TIME ZONE
|
|
198
|
-
|
|
199
|
-
date_trunc('
|
|
200
|
-
|
|
201
|
-
|
|
197
|
+
to_char(
|
|
198
|
+
CASE ${input.bucket}::text
|
|
199
|
+
WHEN 'hour' THEN date_trunc('hour', occurred_at AT TIME ZONE ${timeZone})
|
|
200
|
+
WHEN 'day' THEN date_trunc('day', occurred_at AT TIME ZONE ${timeZone})
|
|
201
|
+
ELSE date_trunc('week', occurred_at AT TIME ZONE ${timeZone})
|
|
202
|
+
END,
|
|
203
|
+
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
|
|
204
|
+
),
|
|
202
205
|
classified.*
|
|
203
206
|
FROM classified
|
|
204
207
|
UNION ALL
|
|
205
208
|
SELECT 'modelSeries', provider_id, requested_model_id, NULL,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
date_trunc('hour', occurred_at AT TIME ZONE
|
|
209
|
-
|
|
210
|
-
date_trunc('
|
|
211
|
-
|
|
212
|
-
|
|
209
|
+
to_char(
|
|
210
|
+
CASE ${input.bucket}::text
|
|
211
|
+
WHEN 'hour' THEN date_trunc('hour', occurred_at AT TIME ZONE ${timeZone})
|
|
212
|
+
WHEN 'day' THEN date_trunc('day', occurred_at AT TIME ZONE ${timeZone})
|
|
213
|
+
ELSE date_trunc('week', occurred_at AT TIME ZONE ${timeZone})
|
|
214
|
+
END,
|
|
215
|
+
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
|
|
216
|
+
),
|
|
213
217
|
classified.*
|
|
214
218
|
FROM classified
|
|
215
219
|
UNION ALL
|
|
@@ -275,7 +279,10 @@ export class PgAiCostStorage implements AiCostStorage {
|
|
|
275
279
|
FROM expanded
|
|
276
280
|
GROUP BY dimension, key_1, key_2, key_3, bucket_start, amount_currency
|
|
277
281
|
`
|
|
278
|
-
return buildAiAccountingOverviewFromFragments(
|
|
282
|
+
return buildAiAccountingOverviewFromFragments(
|
|
283
|
+
input,
|
|
284
|
+
rows.map((row) => aggregateFragmentFromRow(row, timeZone))
|
|
285
|
+
)
|
|
279
286
|
}
|
|
280
287
|
|
|
281
288
|
async listModelCalls(
|
|
@@ -496,7 +503,7 @@ interface AggregateRow {
|
|
|
496
503
|
readonly key_1: string | null
|
|
497
504
|
readonly key_2: string | null
|
|
498
505
|
readonly key_3: string | null
|
|
499
|
-
readonly bucket_start:
|
|
506
|
+
readonly bucket_start: string | null
|
|
500
507
|
readonly amount_currency: string | null
|
|
501
508
|
readonly model_call_count: string
|
|
502
509
|
readonly reported_usage_call_count: string
|
|
@@ -565,7 +572,15 @@ async function findUsage(
|
|
|
565
572
|
return row ? usageFromRow(row, []) : null
|
|
566
573
|
}
|
|
567
574
|
|
|
568
|
-
|
|
575
|
+
/**
|
|
576
|
+
* One aggregated row as a fragment. A series row names its period as the zone's wall clock, which
|
|
577
|
+
* is resolved here: PostgreSQL's own reading of a repeated or skipped wall clock differs from the
|
|
578
|
+
* calendar the page draws its bars on.
|
|
579
|
+
*/
|
|
580
|
+
function aggregateFragmentFromRow(
|
|
581
|
+
row: AggregateRow,
|
|
582
|
+
timeZone: string
|
|
583
|
+
): AiAccountingAggregateFragment {
|
|
569
584
|
const dimensionKeys =
|
|
570
585
|
row.dimension === "model"
|
|
571
586
|
? { providerId: required(row.key_1), modelId: required(row.key_2) }
|
|
@@ -573,7 +588,7 @@ function aggregateFragmentFromRow(row: AggregateRow): AiAccountingAggregateFragm
|
|
|
573
588
|
? {
|
|
574
589
|
providerId: required(row.key_1),
|
|
575
590
|
modelId: required(row.key_2),
|
|
576
|
-
start:
|
|
591
|
+
start: wallClockInstant(required(row.bucket_start), timeZone),
|
|
577
592
|
}
|
|
578
593
|
: row.dimension === "person"
|
|
579
594
|
? row.key_1 && row.key_2
|
|
@@ -591,7 +606,7 @@ function aggregateFragmentFromRow(row: AggregateRow): AiAccountingAggregateFragm
|
|
|
591
606
|
: row.dimension === "workflow"
|
|
592
607
|
? { workflowId: required(row.key_1) }
|
|
593
608
|
: row.dimension === "series"
|
|
594
|
-
? { start:
|
|
609
|
+
? { start: wallClockInstant(required(row.bucket_start), timeZone) }
|
|
595
610
|
: {}
|
|
596
611
|
return {
|
|
597
612
|
dimension: row.dimension,
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
parseArtifactBoardPreviewRow,
|
|
10
10
|
parseArtifactCommentRow,
|
|
11
11
|
parseArtifactFrameGrantRow,
|
|
12
|
+
parseArtifactPdfRow,
|
|
12
13
|
parseArtifactRestorePointRow,
|
|
13
14
|
parseArtifactRow,
|
|
14
15
|
parseArtifactStateRow,
|
|
@@ -22,6 +23,8 @@ import type {
|
|
|
22
23
|
ArtifactCommentKey,
|
|
23
24
|
ArtifactCommentRecord,
|
|
24
25
|
ArtifactFrameGrantRecord,
|
|
26
|
+
ArtifactPdfKeyInput,
|
|
27
|
+
ArtifactPdfRecord,
|
|
25
28
|
ArtifactRecord,
|
|
26
29
|
ArtifactRestorePointReason,
|
|
27
30
|
ArtifactRestorePointRecord,
|
|
@@ -103,7 +106,8 @@ const FRAME_GRANT_COLUMNS = `
|
|
|
103
106
|
const COMMENT_COLUMNS = `
|
|
104
107
|
project_id AS "projectId", artifact_id AS "artifactId", id, parent_id AS "parentId", anchor,
|
|
105
108
|
body, author_principal_type AS "authorPrincipalType", author_principal_id AS "authorPrincipalId",
|
|
106
|
-
author_name AS "authorName",
|
|
109
|
+
author_name AS "authorName", author_grant_id AS "authorGrantId",
|
|
110
|
+
created_at AS "createdAt", resolved_at AS "resolvedAt",
|
|
107
111
|
resolved_by_principal_type AS "resolvedByPrincipalType",
|
|
108
112
|
resolved_by_principal_id AS "resolvedByPrincipalId",
|
|
109
113
|
origin_thread_id AS "originThreadId", origin_run_id AS "originRunId"
|
|
@@ -116,6 +120,11 @@ const BOARD_PREVIEW_COLUMNS = `
|
|
|
116
120
|
created_at AS "createdAt"
|
|
117
121
|
`
|
|
118
122
|
|
|
123
|
+
const PDF_COLUMNS = `
|
|
124
|
+
project_id AS "projectId", render_key AS "renderKey", file, pages, failure,
|
|
125
|
+
created_at AS "createdAt"
|
|
126
|
+
`
|
|
127
|
+
|
|
119
128
|
export class PgArtifactStorage implements ArtifactStorage {
|
|
120
129
|
constructor(private readonly sql: PgStoreClient) {}
|
|
121
130
|
|
|
@@ -303,7 +312,8 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
303
312
|
|
|
304
313
|
async undoTurnWrites(input: UndoArtifactTurnInput): Promise<UndoneArtifactTurn> {
|
|
305
314
|
return runPgTransaction(this.sql, async (tx) => {
|
|
306
|
-
const { projectId, turnRunId } = input
|
|
315
|
+
const { projectId, turnRunId, laterTurnRunIds = [] } = input
|
|
316
|
+
const later = tx.array([...laterTurnRunIds])
|
|
307
317
|
|
|
308
318
|
// Lock the turn's artifacts first, in id order so two of these never deadlock. A write
|
|
309
319
|
// holds the same lock, and under READ COMMITTED each statement below then reads a snapshot
|
|
@@ -317,9 +327,14 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
317
327
|
ORDER BY id
|
|
318
328
|
FOR UPDATE
|
|
319
329
|
`
|
|
330
|
+
// Each write beside the artifact's revision now, and the writes of the later turns undone
|
|
331
|
+
// with it.
|
|
320
332
|
const rows = await tx<PgRow[]>`
|
|
321
333
|
SELECT w.artifact_id AS "artifactId", w.created, w.revision, w.writes,
|
|
322
|
-
a.revision AS "currentRevision"
|
|
334
|
+
a.revision AS "currentRevision",
|
|
335
|
+
(SELECT COALESCE(SUM(l.writes), 0) FROM artifact_turn_writes l
|
|
336
|
+
WHERE l.project_id = w.project_id AND l.artifact_id = w.artifact_id
|
|
337
|
+
AND l.turn_run_id = ANY(${later}::text[])) AS "laterWrites"
|
|
323
338
|
FROM artifact_turn_writes w
|
|
324
339
|
JOIN artifacts a ON a.project_id = w.project_id AND a.id = w.artifact_id
|
|
325
340
|
WHERE w.project_id = ${projectId} AND w.turn_run_id = ${turnRunId}
|
|
@@ -329,7 +344,8 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
329
344
|
const restored: PublishedArtifactVersion[] = []
|
|
330
345
|
const deleted: string[] = []
|
|
331
346
|
for (const row of rows) {
|
|
332
|
-
const
|
|
347
|
+
const own = parseArtifactTurnWriteRow(row)
|
|
348
|
+
const write = { ...own, writes: own.writes + Number(row.laterWrites) }
|
|
333
349
|
const revision = Number(row.currentRevision)
|
|
334
350
|
|
|
335
351
|
if (write.created) {
|
|
@@ -357,17 +373,25 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
357
373
|
}
|
|
358
374
|
|
|
359
375
|
// An artifact undone holds its restore point again, and leaves the threads only the turn
|
|
360
|
-
// shared it in; one kept stays where it was made. A deleted one took its own with it.
|
|
376
|
+
// shared it in; one kept stays where it was made. A deleted one took its own with it. The
|
|
377
|
+
// later turns undone with it leave it too, and one kept keeps their records for their own
|
|
378
|
+
// undo.
|
|
361
379
|
if (restored.length > 0) {
|
|
362
380
|
const undone = restored.map(({ artifact }) => artifact.id)
|
|
381
|
+
const turns = tx.array([turnRunId, ...laterTurnRunIds])
|
|
363
382
|
await tx`
|
|
364
383
|
DELETE FROM artifact_restore_points
|
|
365
|
-
WHERE project_id = ${projectId} AND turn_run_id = ${
|
|
384
|
+
WHERE project_id = ${projectId} AND turn_run_id = ANY(${turns}::text[])
|
|
366
385
|
AND artifact_id IN ${tx(undone)}
|
|
367
386
|
`
|
|
368
387
|
await tx`
|
|
369
388
|
DELETE FROM artifact_threads
|
|
370
|
-
WHERE project_id = ${projectId} AND linked_by_run_id = ${
|
|
389
|
+
WHERE project_id = ${projectId} AND linked_by_run_id = ANY(${turns}::text[])
|
|
390
|
+
AND NOT copied AND artifact_id IN ${tx(undone)}
|
|
391
|
+
`
|
|
392
|
+
await tx`
|
|
393
|
+
DELETE FROM artifact_turn_writes
|
|
394
|
+
WHERE project_id = ${projectId} AND turn_run_id = ANY(${later}::text[])
|
|
371
395
|
AND artifact_id IN ${tx(undone)}
|
|
372
396
|
`
|
|
373
397
|
}
|
|
@@ -709,6 +733,38 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
709
733
|
})
|
|
710
734
|
}
|
|
711
735
|
|
|
736
|
+
async getPdf(input: ArtifactPdfKeyInput): Promise<ArtifactPdfRecord | null> {
|
|
737
|
+
const [row] = await this.sql<PgRow[]>`
|
|
738
|
+
SELECT ${this.sql.unsafe(PDF_COLUMNS)} FROM artifact_pdfs
|
|
739
|
+
WHERE project_id = ${input.projectId} AND render_key = ${input.renderKey}
|
|
740
|
+
`
|
|
741
|
+
return row ? parseArtifactPdfRow(row) : null
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
async putPdf(input: ArtifactPdfRecord): Promise<void> {
|
|
745
|
+
const printed = "failure" in input ? null : input
|
|
746
|
+
await this.sql`
|
|
747
|
+
INSERT INTO artifact_pdfs (project_id, render_key, file, pages, failure, created_at)
|
|
748
|
+
VALUES (
|
|
749
|
+
${input.projectId}, ${input.renderKey},
|
|
750
|
+
${printed ? JSON.stringify(printed.file) : null}::text::jsonb, ${printed?.pages ?? null},
|
|
751
|
+
${"failure" in input ? input.failure : null}, ${input.createdAt}
|
|
752
|
+
)
|
|
753
|
+
ON CONFLICT (project_id, render_key) DO UPDATE SET
|
|
754
|
+
file = excluded.file, pages = excluded.pages, failure = NULL,
|
|
755
|
+
created_at = excluded.created_at
|
|
756
|
+
WHERE artifact_pdfs.failure IS NOT NULL AND excluded.failure IS NULL
|
|
757
|
+
`
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
async clearPdfFailure(input: ArtifactPdfKeyInput): Promise<void> {
|
|
761
|
+
await this.sql`
|
|
762
|
+
DELETE FROM artifact_pdfs
|
|
763
|
+
WHERE project_id = ${input.projectId} AND render_key = ${input.renderKey}
|
|
764
|
+
AND failure IS NOT NULL
|
|
765
|
+
`
|
|
766
|
+
}
|
|
767
|
+
|
|
712
768
|
async createComment(input: CreateArtifactCommentInput): Promise<ArtifactCommentRecord> {
|
|
713
769
|
return runPgTransaction(this.sql, async (tx) => {
|
|
714
770
|
// Under the artifact's lock: two comments at once cannot both pass the limit.
|
|
@@ -739,15 +795,17 @@ export class PgArtifactStorage implements ArtifactStorage {
|
|
|
739
795
|
throw artifactStorageErrors.guestCommentsFull(input.artifactId)
|
|
740
796
|
}
|
|
741
797
|
|
|
798
|
+
const guest = input.author.type === "guest" ? input.author : undefined
|
|
742
799
|
const [row] = await tx<PgRow[]>`
|
|
743
800
|
INSERT INTO artifact_comments (
|
|
744
801
|
project_id, artifact_id, id, parent_id, anchor, body, author_principal_type,
|
|
745
|
-
author_principal_id, author_name, created_at, origin_thread_id,
|
|
802
|
+
author_principal_id, author_name, author_grant_id, created_at, origin_thread_id,
|
|
803
|
+
origin_run_id
|
|
746
804
|
) VALUES (
|
|
747
805
|
${input.projectId}, ${input.artifactId}, ${input.id}, ${input.parentId ?? null},
|
|
748
806
|
${JSON.stringify(commentAnchor(input.anchor))}::text::jsonb, ${input.body},
|
|
749
807
|
${input.author.type}, ${input.author.id},
|
|
750
|
-
${
|
|
808
|
+
${guest?.name ?? null}, ${guest?.grantId ?? null}, ${input.createdAt},
|
|
751
809
|
${input.origin?.threadId ?? null}, ${input.origin?.runId ?? null}
|
|
752
810
|
)
|
|
753
811
|
RETURNING ${tx.unsafe(COMMENT_COLUMNS)}
|
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
ConfirmRunExecutionInput,
|
|
12
12
|
ExecutionStorage,
|
|
13
13
|
FinishSyncRunInput,
|
|
14
|
+
ListLatestSyncConnectionRunsInput,
|
|
14
15
|
ListLatestSyncRunsInput,
|
|
15
16
|
ListLatestSyncRunsResult,
|
|
16
17
|
ListLostRunsInput,
|
|
@@ -71,6 +72,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
71
72
|
id,
|
|
72
73
|
execution_id,
|
|
73
74
|
sync_id,
|
|
75
|
+
connection_id,
|
|
74
76
|
dataset_id,
|
|
75
77
|
mode,
|
|
76
78
|
status,
|
|
@@ -83,6 +85,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
83
85
|
${input.id},
|
|
84
86
|
${input.executionId},
|
|
85
87
|
${input.syncId},
|
|
88
|
+
${input.connectionId ?? null},
|
|
86
89
|
${input.datasetId},
|
|
87
90
|
${input.mode},
|
|
88
91
|
${"queued"},
|
|
@@ -269,6 +272,22 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
269
272
|
params.push(...input.syncIds)
|
|
270
273
|
}
|
|
271
274
|
|
|
275
|
+
if (input.connectionId === null) {
|
|
276
|
+
whereClauses.push("connection_id IS NULL")
|
|
277
|
+
} else if (input.connectionId !== undefined) {
|
|
278
|
+
whereClauses.push(`connection_id = $${index++}`)
|
|
279
|
+
params.push(input.connectionId)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (input.visibleConnectionIds) {
|
|
283
|
+
whereClauses.push(
|
|
284
|
+
visibleConnectionsClause(input.visibleConnectionIds, (value) => {
|
|
285
|
+
params.push(value)
|
|
286
|
+
return `$${index++}`
|
|
287
|
+
})
|
|
288
|
+
)
|
|
289
|
+
}
|
|
290
|
+
|
|
272
291
|
if (input.datasetId) {
|
|
273
292
|
whereClauses.push(`dataset_id = $${index++}`)
|
|
274
293
|
params.push(input.datasetId)
|
|
@@ -302,6 +321,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
302
321
|
}
|
|
303
322
|
|
|
304
323
|
async listLatestBySyncIds(input: ListLatestSyncRunsInput): Promise<ListLatestSyncRunsResult> {
|
|
324
|
+
const { visibleConnectionIds } = input
|
|
305
325
|
const rows = await queryLatestRunsByOwnerId<DatabaseRow>({
|
|
306
326
|
sql: this.sql,
|
|
307
327
|
tableName: "sync_runs",
|
|
@@ -310,12 +330,59 @@ export class PgSyncRunStorage implements SyncRunStorage {
|
|
|
310
330
|
projectId: input.projectId,
|
|
311
331
|
ownerIdFor: (row) => row.sync_id,
|
|
312
332
|
selectList: "*, checkpoint IS NOT NULL AS checkpoint_present",
|
|
333
|
+
...(visibleConnectionIds === undefined
|
|
334
|
+
? {}
|
|
335
|
+
: { filter: (bind) => visibleConnectionsClause(visibleConnectionIds, bind) }),
|
|
313
336
|
})
|
|
314
337
|
|
|
315
338
|
return {
|
|
316
339
|
runs: rows.map(rowToSyncRunRecord),
|
|
317
340
|
}
|
|
318
341
|
}
|
|
342
|
+
|
|
343
|
+
async listLatestByConnection(
|
|
344
|
+
input: ListLatestSyncConnectionRunsInput
|
|
345
|
+
): Promise<ListLatestSyncRunsResult> {
|
|
346
|
+
if (input.syncIds.length === 0 || input.connectionIds?.length === 0) return { runs: [] }
|
|
347
|
+
|
|
348
|
+
const params: SqlParameter[] = [input.projectId]
|
|
349
|
+
const placeholders = (values: readonly string[]) =>
|
|
350
|
+
values
|
|
351
|
+
.map((value) => {
|
|
352
|
+
params.push(value)
|
|
353
|
+
return `$${params.length}`
|
|
354
|
+
})
|
|
355
|
+
.join(", ")
|
|
356
|
+
const syncFilter = `sync_id IN (${placeholders(input.syncIds)})`
|
|
357
|
+
const connectionFilter =
|
|
358
|
+
input.connectionIds === undefined
|
|
359
|
+
? "connection_id IS NOT NULL"
|
|
360
|
+
: `connection_id IN (${placeholders(input.connectionIds)})`
|
|
361
|
+
|
|
362
|
+
const rows = await this.sql.unsafe<DatabaseRow[]>(
|
|
363
|
+
`
|
|
364
|
+
SELECT DISTINCT ON (sync_id, connection_id)
|
|
365
|
+
*, checkpoint IS NOT NULL AS checkpoint_present
|
|
366
|
+
FROM sync_runs
|
|
367
|
+
WHERE project_id = $1 AND ${syncFilter} AND ${connectionFilter}
|
|
368
|
+
ORDER BY sync_id, connection_id, COALESCE(started_at, queued_at) DESC, id DESC
|
|
369
|
+
`,
|
|
370
|
+
params
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
return { runs: rows.map(rowToSyncRunRecord) }
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** `visibleConnectionIds` as a condition: runs of those connections, and runs that read none. */
|
|
378
|
+
function visibleConnectionsClause(
|
|
379
|
+
connectionIds: readonly string[],
|
|
380
|
+
bind: (value: string) => string
|
|
381
|
+
): string {
|
|
382
|
+
if (connectionIds.length === 0) return "connection_id IS NULL"
|
|
383
|
+
|
|
384
|
+
const placeholders = connectionIds.map((id) => bind(id)).join(", ")
|
|
385
|
+
return `(connection_id IS NULL OR connection_id IN (${placeholders}))`
|
|
319
386
|
}
|
|
320
387
|
|
|
321
388
|
function staleExecution(id: string): SyncRunError {
|
|
@@ -336,6 +403,7 @@ function rowToSyncRunRecord(row: DatabaseRow): SyncRunRecord {
|
|
|
336
403
|
projectId: row.project_id,
|
|
337
404
|
executionId: row.execution_id,
|
|
338
405
|
syncId: row.sync_id,
|
|
406
|
+
...(row.connection_id === null ? {} : { connectionId: row.connection_id }),
|
|
339
407
|
datasetId: row.dataset_id,
|
|
340
408
|
mode: row.mode,
|
|
341
409
|
status: row.status,
|
|
@@ -373,6 +441,7 @@ interface DatabaseRow extends PgRunExecutionColumns {
|
|
|
373
441
|
id: string
|
|
374
442
|
execution_id: string
|
|
375
443
|
sync_id: string
|
|
444
|
+
connection_id: string | null
|
|
376
445
|
dataset_id: string
|
|
377
446
|
mode: SyncRunRecord["mode"]
|
|
378
447
|
status: SyncRunRecord["status"]
|