@pattern-stack/codegen 0.4.4 → 0.4.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/runtime/subsystems/index.d.ts +7 -0
- package/dist/runtime/subsystems/index.js +923 -208
- package/dist/runtime/subsystems/index.js.map +1 -1
- package/dist/runtime/subsystems/observability/index.d.ts +10 -0
- package/dist/runtime/subsystems/observability/index.js +913 -0
- package/dist/runtime/subsystems/observability/index.js.map +1 -0
- package/dist/runtime/subsystems/observability/observability.drizzle-backend.d.ts +15 -0
- package/dist/runtime/subsystems/observability/observability.drizzle-backend.js +465 -0
- package/dist/runtime/subsystems/observability/observability.drizzle-backend.js.map +1 -0
- package/dist/runtime/subsystems/observability/observability.memory-backend.d.ts +28 -0
- package/dist/runtime/subsystems/observability/observability.memory-backend.js +75 -0
- package/dist/runtime/subsystems/observability/observability.memory-backend.js.map +1 -0
- package/dist/runtime/subsystems/observability/observability.module.d.ts +56 -0
- package/dist/runtime/subsystems/observability/observability.module.js +905 -0
- package/dist/runtime/subsystems/observability/observability.module.js.map +1 -0
- package/dist/runtime/subsystems/observability/observability.protocol.d.ts +155 -0
- package/dist/runtime/subsystems/observability/observability.protocol.js +1 -0
- package/dist/runtime/subsystems/observability/observability.protocol.js.map +1 -0
- package/dist/runtime/subsystems/observability/observability.tokens.d.ts +19 -0
- package/dist/runtime/subsystems/observability/observability.tokens.js +8 -0
- package/dist/runtime/subsystems/observability/observability.tokens.js.map +1 -0
- package/dist/runtime/subsystems/observability/reporters/bridge-metrics.reporter.d.ts +87 -0
- package/dist/runtime/subsystems/observability/reporters/bridge-metrics.reporter.js +443 -0
- package/dist/runtime/subsystems/observability/reporters/bridge-metrics.reporter.js.map +1 -0
- package/dist/runtime/subsystems/sync/sync-audit.schema.d.ts +4 -4
- package/package.json +6 -1
- package/runtime/subsystems/index.ts +23 -0
- package/runtime/subsystems/observability/index.ts +35 -0
- package/runtime/subsystems/observability/observability.drizzle-backend.ts +223 -0
- package/runtime/subsystems/observability/observability.memory-backend.ts +111 -0
- package/runtime/subsystems/observability/observability.module.ts +115 -0
- package/runtime/subsystems/observability/observability.protocol.ts +167 -0
- package/runtime/subsystems/observability/observability.tokens.ts +18 -0
- package/runtime/subsystems/observability/reporters/bridge-metrics.reporter.ts +242 -0
|
@@ -0,0 +1,913 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
6
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
7
|
+
}) : x)(function(x) {
|
|
8
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
9
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
10
|
+
});
|
|
11
|
+
var __esm = (fn, res) => function __init() {
|
|
12
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
13
|
+
};
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc2) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc2 = __getOwnPropDesc(from, key)) || desc2.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
28
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
29
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
30
|
+
if (decorator = decorators[i])
|
|
31
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
32
|
+
if (kind && result) __defProp(target, key, result);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
var __decorateParam = (index5, decorator) => (target, key) => decorator(target, key, index5);
|
|
36
|
+
|
|
37
|
+
// runtime/constants/tokens.ts
|
|
38
|
+
var DRIZZLE;
|
|
39
|
+
var init_tokens = __esm({
|
|
40
|
+
"runtime/constants/tokens.ts"() {
|
|
41
|
+
"use strict";
|
|
42
|
+
DRIZZLE = "DRIZZLE";
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// runtime/subsystems/jobs/job-orchestration.schema.ts
|
|
47
|
+
import {
|
|
48
|
+
pgEnum,
|
|
49
|
+
pgTable,
|
|
50
|
+
uuid,
|
|
51
|
+
text,
|
|
52
|
+
jsonb,
|
|
53
|
+
integer,
|
|
54
|
+
timestamp,
|
|
55
|
+
index,
|
|
56
|
+
uniqueIndex
|
|
57
|
+
} from "drizzle-orm/pg-core";
|
|
58
|
+
import { sql } from "drizzle-orm";
|
|
59
|
+
var jobRunStatusEnum, jobStepKindEnum, jobStepStatusEnum, collisionModeEnum, replayFromEnum, parentClosePolicyEnum, waitKindEnum, triggerSourceEnum, jobs, jobRuns, jobSteps;
|
|
60
|
+
var init_job_orchestration_schema = __esm({
|
|
61
|
+
"runtime/subsystems/jobs/job-orchestration.schema.ts"() {
|
|
62
|
+
"use strict";
|
|
63
|
+
jobRunStatusEnum = pgEnum("job_run_status", [
|
|
64
|
+
"pending",
|
|
65
|
+
"running",
|
|
66
|
+
"waiting",
|
|
67
|
+
"completed",
|
|
68
|
+
"failed",
|
|
69
|
+
"timed_out",
|
|
70
|
+
"canceled"
|
|
71
|
+
]);
|
|
72
|
+
jobStepKindEnum = pgEnum("job_step_kind", ["task"]);
|
|
73
|
+
jobStepStatusEnum = pgEnum("job_step_status", [
|
|
74
|
+
"pending",
|
|
75
|
+
"running",
|
|
76
|
+
"completed",
|
|
77
|
+
"failed",
|
|
78
|
+
"skipped"
|
|
79
|
+
]);
|
|
80
|
+
collisionModeEnum = pgEnum("job_collision_mode", [
|
|
81
|
+
"queue",
|
|
82
|
+
"reject",
|
|
83
|
+
"replace"
|
|
84
|
+
]);
|
|
85
|
+
replayFromEnum = pgEnum("job_replay_from", [
|
|
86
|
+
"scratch",
|
|
87
|
+
"last_step",
|
|
88
|
+
"last_checkpoint"
|
|
89
|
+
]);
|
|
90
|
+
parentClosePolicyEnum = pgEnum("job_parent_close_policy", [
|
|
91
|
+
"terminate",
|
|
92
|
+
"cancel",
|
|
93
|
+
"abandon"
|
|
94
|
+
]);
|
|
95
|
+
waitKindEnum = pgEnum("job_wait_kind", ["signal"]);
|
|
96
|
+
triggerSourceEnum = pgEnum("job_trigger_source", [
|
|
97
|
+
"manual",
|
|
98
|
+
"schedule",
|
|
99
|
+
"event",
|
|
100
|
+
"parent"
|
|
101
|
+
]);
|
|
102
|
+
jobs = pgTable("job", {
|
|
103
|
+
type: text("type").primaryKey(),
|
|
104
|
+
version: integer("version").notNull().default(1),
|
|
105
|
+
pool: text("pool").notNull(),
|
|
106
|
+
scopeEntityType: text("scope_entity_type"),
|
|
107
|
+
retryPolicy: jsonb("retry_policy").notNull().$type(),
|
|
108
|
+
timeoutMs: integer("timeout_ms"),
|
|
109
|
+
concurrencyKeyTemplate: text("concurrency_key_template"),
|
|
110
|
+
collisionMode: collisionModeEnum("collision_mode").notNull().default("queue"),
|
|
111
|
+
dedupeKeyTemplate: text("dedupe_key_template"),
|
|
112
|
+
dedupeWindowMs: integer("dedupe_window_ms"),
|
|
113
|
+
priorityDefault: integer("priority_default").notNull().default(0),
|
|
114
|
+
replayFrom: replayFromEnum("replay_from").notNull().default("last_checkpoint"),
|
|
115
|
+
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
116
|
+
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow()
|
|
117
|
+
});
|
|
118
|
+
jobRuns = pgTable(
|
|
119
|
+
"job_run",
|
|
120
|
+
{
|
|
121
|
+
id: uuid("id").primaryKey().defaultRandom(),
|
|
122
|
+
jobType: text("job_type").notNull().references(() => jobs.type),
|
|
123
|
+
jobVersion: integer("job_version").notNull(),
|
|
124
|
+
parentRunId: uuid("parent_run_id").references(() => jobRuns.id),
|
|
125
|
+
/**
|
|
126
|
+
* Service generates `id` client-side via randomUUID() and sets
|
|
127
|
+
* root_run_id = id for root runs (single INSERT, no self-FK race).
|
|
128
|
+
*/
|
|
129
|
+
rootRunId: uuid("root_run_id").notNull(),
|
|
130
|
+
parentClosePolicy: parentClosePolicyEnum("parent_close_policy").notNull().default("terminate"),
|
|
131
|
+
scopeEntityType: text("scope_entity_type"),
|
|
132
|
+
scopeEntityId: text("scope_entity_id"),
|
|
133
|
+
tenantId: text("tenant_id"),
|
|
134
|
+
tags: jsonb("tags").notNull().default({}).$type(),
|
|
135
|
+
pool: text("pool").notNull(),
|
|
136
|
+
priority: integer("priority").notNull().default(0),
|
|
137
|
+
concurrencyKey: text("concurrency_key"),
|
|
138
|
+
dedupeKey: text("dedupe_key"),
|
|
139
|
+
status: jobRunStatusEnum("status").notNull().default("pending"),
|
|
140
|
+
input: jsonb("input").notNull().$type(),
|
|
141
|
+
output: jsonb("output").$type(),
|
|
142
|
+
error: jsonb("error").$type(),
|
|
143
|
+
triggerSource: triggerSourceEnum("trigger_source").notNull(),
|
|
144
|
+
triggerRef: text("trigger_ref"),
|
|
145
|
+
runAt: timestamp("run_at", { withTimezone: true }).notNull().defaultNow(),
|
|
146
|
+
startedAt: timestamp("started_at", { withTimezone: true }),
|
|
147
|
+
finishedAt: timestamp("finished_at", { withTimezone: true }),
|
|
148
|
+
claimedAt: timestamp("claimed_at", { withTimezone: true }),
|
|
149
|
+
attempts: integer("attempts").notNull().default(0),
|
|
150
|
+
// Phase 3 placeholder — see ADR-025
|
|
151
|
+
waitKind: waitKindEnum("wait_kind"),
|
|
152
|
+
// Phase 3 placeholder — see ADR-025
|
|
153
|
+
resumeToken: text("resume_token"),
|
|
154
|
+
// Phase 3 placeholder — see ADR-025
|
|
155
|
+
waitDeadline: timestamp("wait_deadline", { withTimezone: true }),
|
|
156
|
+
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
157
|
+
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow()
|
|
158
|
+
},
|
|
159
|
+
(t) => ({
|
|
160
|
+
/** Claim query: ORDER BY priority DESC, run_at ASC. */
|
|
161
|
+
idxJobRunClaim: index("idx_job_run_claim").on(t.status, t.pool, t.runAt),
|
|
162
|
+
/** Tree traversal / cascade cancel. */
|
|
163
|
+
idxJobRunRoot: index("idx_job_run_root").on(t.rootRunId),
|
|
164
|
+
/** listForScope query. */
|
|
165
|
+
idxJobRunScope: index("idx_job_run_scope").on(t.scopeEntityType, t.scopeEntityId),
|
|
166
|
+
/** Idempotency collapse — partial index. */
|
|
167
|
+
idxJobRunDedupe: index("idx_job_run_dedupe").on(t.jobType, t.dedupeKey).where(sql`${t.dedupeKey} IS NOT NULL`),
|
|
168
|
+
/** Collision check — partial index. */
|
|
169
|
+
idxJobRunConcurrency: index("idx_job_run_concurrency").on(t.concurrencyKey).where(
|
|
170
|
+
sql`${t.concurrencyKey} IS NOT NULL AND ${t.status} IN ('pending','running')`
|
|
171
|
+
)
|
|
172
|
+
})
|
|
173
|
+
);
|
|
174
|
+
jobSteps = pgTable(
|
|
175
|
+
"job_step",
|
|
176
|
+
{
|
|
177
|
+
id: uuid("id").primaryKey().defaultRandom(),
|
|
178
|
+
jobRunId: uuid("job_run_id").notNull().references(() => jobRuns.id),
|
|
179
|
+
stepId: text("step_id").notNull(),
|
|
180
|
+
kind: jobStepKindEnum("kind").notNull().default("task"),
|
|
181
|
+
/**
|
|
182
|
+
* Monotonic within run. integer (max ~2B per run) is sufficient —
|
|
183
|
+
* downgraded from ADR-022's bigint; revisit only if a single run
|
|
184
|
+
* ever exceeds 2 billion steps.
|
|
185
|
+
*/
|
|
186
|
+
seq: integer("seq").notNull(),
|
|
187
|
+
status: jobStepStatusEnum("status").notNull().default("pending"),
|
|
188
|
+
input: jsonb("input").$type(),
|
|
189
|
+
/** Memoised on success for replay. */
|
|
190
|
+
output: jsonb("output").$type(),
|
|
191
|
+
error: jsonb("error").$type(),
|
|
192
|
+
attempts: integer("attempts").notNull().default(0),
|
|
193
|
+
startedAt: timestamp("started_at", { withTimezone: true }),
|
|
194
|
+
finishedAt: timestamp("finished_at", { withTimezone: true })
|
|
195
|
+
},
|
|
196
|
+
(t) => ({
|
|
197
|
+
/** No duplicate step IDs per run. */
|
|
198
|
+
idxJobStepRunStep: uniqueIndex("idx_job_step_run_step").on(t.jobRunId, t.stepId),
|
|
199
|
+
/** Ordered timeline reads. */
|
|
200
|
+
idxJobStepTimeline: index("idx_job_step_timeline").on(t.jobRunId, t.seq)
|
|
201
|
+
})
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// runtime/subsystems/events/domain-events.schema.ts
|
|
207
|
+
import {
|
|
208
|
+
index as index3,
|
|
209
|
+
jsonb as jsonb3,
|
|
210
|
+
pgTable as pgTable3,
|
|
211
|
+
text as text3,
|
|
212
|
+
timestamp as timestamp3,
|
|
213
|
+
uuid as uuid3
|
|
214
|
+
} from "drizzle-orm/pg-core";
|
|
215
|
+
var domainEvents;
|
|
216
|
+
var init_domain_events_schema = __esm({
|
|
217
|
+
"runtime/subsystems/events/domain-events.schema.ts"() {
|
|
218
|
+
"use strict";
|
|
219
|
+
domainEvents = pgTable3(
|
|
220
|
+
"domain_events",
|
|
221
|
+
{
|
|
222
|
+
id: uuid3("id").primaryKey(),
|
|
223
|
+
type: text3("type").notNull(),
|
|
224
|
+
aggregateId: text3("aggregate_id").notNull(),
|
|
225
|
+
aggregateType: text3("aggregate_type").notNull(),
|
|
226
|
+
payload: jsonb3("payload").notNull().$type(),
|
|
227
|
+
occurredAt: timestamp3("occurred_at", { withTimezone: true }).notNull(),
|
|
228
|
+
processedAt: timestamp3("processed_at", { withTimezone: true }),
|
|
229
|
+
/** Lifecycle status: pending | processed | failed */
|
|
230
|
+
status: text3("status").notNull().default("pending"),
|
|
231
|
+
/** Error message from the last failed dispatch attempt. */
|
|
232
|
+
error: text3("error"),
|
|
233
|
+
metadata: jsonb3("metadata").$type(),
|
|
234
|
+
/** Routing pool (e.g. `events_inbound`, `events_change`, `events_outbound`). Populated by DrizzleEventBus.publish() in EVT-4. */
|
|
235
|
+
pool: text3("pool"),
|
|
236
|
+
/** Routing direction: `inbound` | `change` | `outbound`. Populated by DrizzleEventBus.publish() in EVT-4. */
|
|
237
|
+
direction: text3("direction"),
|
|
238
|
+
// conditional: emitted only when events.multi_tenant: true
|
|
239
|
+
tenantId: text3("tenant_id")
|
|
240
|
+
},
|
|
241
|
+
(t) => ({
|
|
242
|
+
/** Polling drain filter (existing — promoted from comment to declaration in EVT-1). */
|
|
243
|
+
idxDomainEventsStatusOccurredAt: index3("idx_domain_events_status_occurred_at").on(
|
|
244
|
+
t.status,
|
|
245
|
+
t.occurredAt
|
|
246
|
+
),
|
|
247
|
+
/** Event replay per aggregate (existing — promoted from comment to declaration in EVT-1). */
|
|
248
|
+
idxDomainEventsAggregate: index3("idx_domain_events_aggregate").on(
|
|
249
|
+
t.aggregateId,
|
|
250
|
+
t.aggregateType
|
|
251
|
+
),
|
|
252
|
+
/** Per-pool drain filter (EVT-1). Enables DrizzleEventBus to drain a single pool without scanning all events. */
|
|
253
|
+
idxDomainEventsPoolStatusOccurredAt: index3(
|
|
254
|
+
"idx_domain_events_pool_status_occurred_at"
|
|
255
|
+
).on(t.pool, t.status, t.occurredAt)
|
|
256
|
+
})
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// runtime/subsystems/bridge/bridge-delivery.schema.ts
|
|
262
|
+
import {
|
|
263
|
+
index as index4,
|
|
264
|
+
jsonb as jsonb4,
|
|
265
|
+
pgEnum as pgEnum3,
|
|
266
|
+
pgTable as pgTable4,
|
|
267
|
+
text as text4,
|
|
268
|
+
timestamp as timestamp4,
|
|
269
|
+
unique,
|
|
270
|
+
uuid as uuid4
|
|
271
|
+
} from "drizzle-orm/pg-core";
|
|
272
|
+
import { sql as sql3 } from "drizzle-orm";
|
|
273
|
+
var bridgeDeliveryStatusEnum, bridgeDelivery;
|
|
274
|
+
var init_bridge_delivery_schema = __esm({
|
|
275
|
+
"runtime/subsystems/bridge/bridge-delivery.schema.ts"() {
|
|
276
|
+
"use strict";
|
|
277
|
+
init_domain_events_schema();
|
|
278
|
+
init_job_orchestration_schema();
|
|
279
|
+
bridgeDeliveryStatusEnum = pgEnum3("bridge_delivery_status", [
|
|
280
|
+
"pending",
|
|
281
|
+
"delivered",
|
|
282
|
+
"skipped",
|
|
283
|
+
"failed"
|
|
284
|
+
]);
|
|
285
|
+
bridgeDelivery = pgTable4(
|
|
286
|
+
"bridge_delivery",
|
|
287
|
+
{
|
|
288
|
+
id: uuid4("id").primaryKey().defaultRandom(),
|
|
289
|
+
/** FK to the source event in the outbox. */
|
|
290
|
+
eventId: uuid4("event_id").notNull().references(() => domainEvents.id),
|
|
291
|
+
/**
|
|
292
|
+
* Stable codegen-emitted identifier for the (job, trigger) pair, of the
|
|
293
|
+
* form `<job_type>#<triggerIndex>` (BRIDGE-6). Forms the second half of
|
|
294
|
+
* the UNIQUE idempotency key.
|
|
295
|
+
*/
|
|
296
|
+
triggerId: text4("trigger_id").notNull(),
|
|
297
|
+
/**
|
|
298
|
+
* Wrapper `job_run.id` (the framework `@framework/bridge_delivery` run
|
|
299
|
+
* that drove this delivery). Nullable: the facade-eager path
|
|
300
|
+
* (`publishAndStart` Case B) pre-writes a delivered row with no wrapper.
|
|
301
|
+
*/
|
|
302
|
+
wrapperRunId: uuid4("wrapper_run_id").references(() => jobRuns.id),
|
|
303
|
+
/**
|
|
304
|
+
* Spawned user `job_run.id`. Null until status is `delivered`; remains
|
|
305
|
+
* null for `skipped` and `failed` deliveries.
|
|
306
|
+
*/
|
|
307
|
+
userRunId: uuid4("user_run_id").references(() => jobRuns.id),
|
|
308
|
+
status: bridgeDeliveryStatusEnum("status").notNull().default("pending"),
|
|
309
|
+
/** Populated when status=`skipped` (e.g. `'when_returned_false'`, `'trigger_unregistered'`). */
|
|
310
|
+
skipReason: text4("skip_reason"),
|
|
311
|
+
/** Populated when status=`failed`. Mirrors `job_run.error` shape. */
|
|
312
|
+
error: jsonb4("error").$type(),
|
|
313
|
+
/**
|
|
314
|
+
* Emitted unconditionally and nullable (JOB-8 / SYNC-6 precedent).
|
|
315
|
+
* Enforcement gated on `BRIDGE_MULTI_TENANT` at the service layer
|
|
316
|
+
* (BRIDGE-8); no DB constraint.
|
|
317
|
+
*/
|
|
318
|
+
tenantId: text4("tenant_id"),
|
|
319
|
+
attemptedAt: timestamp4("attempted_at", { withTimezone: true }).notNull().defaultNow(),
|
|
320
|
+
deliveredAt: timestamp4("delivered_at", { withTimezone: true })
|
|
321
|
+
},
|
|
322
|
+
(t) => ({
|
|
323
|
+
/**
|
|
324
|
+
* Idempotency ledger. Outbox replays and facade-vs-drain collisions both
|
|
325
|
+
* dedup through this constraint.
|
|
326
|
+
*/
|
|
327
|
+
uqBridgeDeliveryEventTrigger: unique("uq_bridge_delivery_event_trigger").on(
|
|
328
|
+
t.eventId,
|
|
329
|
+
t.triggerId
|
|
330
|
+
),
|
|
331
|
+
/** Lookup all deliveries for an event (fanout report, debugging). */
|
|
332
|
+
idxBridgeDeliveryEvent: index4("idx_bridge_delivery_event").on(t.eventId),
|
|
333
|
+
/**
|
|
334
|
+
* Ops dashboard filter — only the actionable states. Partial index keeps
|
|
335
|
+
* it small at scale (the bulk of rows will be `delivered`).
|
|
336
|
+
*/
|
|
337
|
+
idxBridgeDeliveryStatus: index4("idx_bridge_delivery_status").on(t.status).where(sql3`${t.status} IN ('pending','failed')`),
|
|
338
|
+
/**
|
|
339
|
+
* Reverse lookup from a spawned user run back to its delivery row.
|
|
340
|
+
* Partial — most rows in the bridge ledger but only successful
|
|
341
|
+
* deliveries have a `user_run_id`.
|
|
342
|
+
*/
|
|
343
|
+
idxBridgeDeliveryUserRun: index4("idx_bridge_delivery_user_run").on(t.userRunId).where(sql3`${t.userRunId} IS NOT NULL`)
|
|
344
|
+
})
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
// runtime/subsystems/observability/reporters/bridge-metrics.reporter.ts
|
|
350
|
+
var bridge_metrics_reporter_exports = {};
|
|
351
|
+
__export(bridge_metrics_reporter_exports, {
|
|
352
|
+
BridgeMetricsReporter: () => BridgeMetricsReporter
|
|
353
|
+
});
|
|
354
|
+
import {
|
|
355
|
+
Inject as Inject2,
|
|
356
|
+
Injectable as Injectable3,
|
|
357
|
+
Logger,
|
|
358
|
+
Optional
|
|
359
|
+
} from "@nestjs/common";
|
|
360
|
+
import { and, eq as eq2, gt, sql as sql4 } from "drizzle-orm";
|
|
361
|
+
var INTERVAL_NAME, DEFAULT_INTERVAL_MS, MIN_INTERVAL_MS, BridgeMetricsReporter;
|
|
362
|
+
var init_bridge_metrics_reporter = __esm({
|
|
363
|
+
"runtime/subsystems/observability/reporters/bridge-metrics.reporter.ts"() {
|
|
364
|
+
"use strict";
|
|
365
|
+
init_tokens();
|
|
366
|
+
init_bridge_delivery_schema();
|
|
367
|
+
init_domain_events_schema();
|
|
368
|
+
INTERVAL_NAME = "bridge-metrics-tick";
|
|
369
|
+
DEFAULT_INTERVAL_MS = 6e4;
|
|
370
|
+
MIN_INTERVAL_MS = 1e3;
|
|
371
|
+
BridgeMetricsReporter = class {
|
|
372
|
+
constructor(db, scheduler = null) {
|
|
373
|
+
this.db = db;
|
|
374
|
+
this.scheduler = scheduler;
|
|
375
|
+
this.intervalMs = this.resolveIntervalMs();
|
|
376
|
+
this.lastTickAt = /* @__PURE__ */ new Date();
|
|
377
|
+
}
|
|
378
|
+
db;
|
|
379
|
+
scheduler;
|
|
380
|
+
logger = new Logger(BridgeMetricsReporter.name);
|
|
381
|
+
intervalMs;
|
|
382
|
+
lastTickAt;
|
|
383
|
+
/**
|
|
384
|
+
* Timer handle retained as a field when `SchedulerRegistry` isn't
|
|
385
|
+
* available (optional dep). `SchedulerRegistry` is the Nest-idiomatic
|
|
386
|
+
* home for interval cleanup, but global-module wiring across consumer
|
|
387
|
+
* topologies doesn't always make it injectable here — the fallback
|
|
388
|
+
* keeps the reporter self-sufficient.
|
|
389
|
+
*/
|
|
390
|
+
ownedTimer = null;
|
|
391
|
+
onModuleInit() {
|
|
392
|
+
this.logger.log(
|
|
393
|
+
`BridgeMetricsReporter starting (intervalMs=${this.intervalMs}).`
|
|
394
|
+
);
|
|
395
|
+
const timer = setInterval(() => {
|
|
396
|
+
void this.tick();
|
|
397
|
+
}, this.intervalMs);
|
|
398
|
+
timer.unref?.();
|
|
399
|
+
if (this.scheduler) {
|
|
400
|
+
this.scheduler.addInterval(INTERVAL_NAME, timer);
|
|
401
|
+
} else {
|
|
402
|
+
this.ownedTimer = timer;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
onModuleDestroy() {
|
|
406
|
+
if (this.scheduler && this.scheduler.getIntervals().includes(INTERVAL_NAME)) {
|
|
407
|
+
this.scheduler.deleteInterval(INTERVAL_NAME);
|
|
408
|
+
}
|
|
409
|
+
if (this.ownedTimer !== null) {
|
|
410
|
+
clearInterval(this.ownedTimer);
|
|
411
|
+
this.ownedTimer = null;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Run one sampling tick. Public so tests can drive it deterministically
|
|
416
|
+
* without waiting on the timer.
|
|
417
|
+
*/
|
|
418
|
+
async tick() {
|
|
419
|
+
const windowStart = this.lastTickAt;
|
|
420
|
+
const windowEnd = /* @__PURE__ */ new Date();
|
|
421
|
+
this.lastTickAt = windowEnd;
|
|
422
|
+
let rows = [];
|
|
423
|
+
try {
|
|
424
|
+
rows = await this.sample(windowStart, windowEnd);
|
|
425
|
+
} catch (err) {
|
|
426
|
+
this.logger.error(
|
|
427
|
+
`bridge metrics sample failed: ${err.message}`
|
|
428
|
+
);
|
|
429
|
+
return { windowStart, windowEnd, rows: [] };
|
|
430
|
+
}
|
|
431
|
+
this.emit({ windowStart, windowEnd, rows });
|
|
432
|
+
return { windowStart, windowEnd, rows };
|
|
433
|
+
}
|
|
434
|
+
async sample(windowStart, windowEnd) {
|
|
435
|
+
const lastTransition = sql4`COALESCE(${bridgeDelivery.deliveredAt}, ${bridgeDelivery.attemptedAt})`;
|
|
436
|
+
const result = await this.db.select({
|
|
437
|
+
status: bridgeDelivery.status,
|
|
438
|
+
eventType: domainEvents.type,
|
|
439
|
+
skipReason: bridgeDelivery.skipReason,
|
|
440
|
+
count: sql4`COUNT(*)::int`
|
|
441
|
+
}).from(bridgeDelivery).innerJoin(domainEvents, eq2(bridgeDelivery.eventId, domainEvents.id)).where(
|
|
442
|
+
and(
|
|
443
|
+
gt(lastTransition, windowStart),
|
|
444
|
+
sql4`${lastTransition} <= ${windowEnd}`
|
|
445
|
+
)
|
|
446
|
+
).groupBy(
|
|
447
|
+
bridgeDelivery.status,
|
|
448
|
+
domainEvents.type,
|
|
449
|
+
bridgeDelivery.skipReason
|
|
450
|
+
);
|
|
451
|
+
return result.map((r) => ({
|
|
452
|
+
status: r.status,
|
|
453
|
+
eventType: r.eventType,
|
|
454
|
+
skipReason: r.skipReason,
|
|
455
|
+
count: r.count
|
|
456
|
+
}));
|
|
457
|
+
}
|
|
458
|
+
emit(tick) {
|
|
459
|
+
if (tick.rows.length === 0) {
|
|
460
|
+
this.logger.log(
|
|
461
|
+
`bridge_metrics tick=empty window=[${tick.windowStart.toISOString()}..${tick.windowEnd.toISOString()}]`
|
|
462
|
+
);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const totals = tick.rows.reduce(
|
|
466
|
+
(acc, r) => {
|
|
467
|
+
acc[r.status] = (acc[r.status] ?? 0) + r.count;
|
|
468
|
+
return acc;
|
|
469
|
+
},
|
|
470
|
+
{}
|
|
471
|
+
);
|
|
472
|
+
const detail = tick.rows.map(
|
|
473
|
+
(r) => `${r.eventType}|${r.status}${r.skipReason ? `:${r.skipReason}` : ""}=${r.count}`
|
|
474
|
+
).join(" ");
|
|
475
|
+
this.logger.log(
|
|
476
|
+
`bridge_metrics tick window=[${tick.windowStart.toISOString()}..${tick.windowEnd.toISOString()}] totals=${JSON.stringify(totals)} detail=[${detail}]`
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
resolveIntervalMs() {
|
|
480
|
+
const raw = process.env["BRIDGE_METRICS_INTERVAL_MS"];
|
|
481
|
+
if (!raw) return DEFAULT_INTERVAL_MS;
|
|
482
|
+
const parsed = Number.parseInt(raw, 10);
|
|
483
|
+
if (!Number.isFinite(parsed) || parsed < MIN_INTERVAL_MS) {
|
|
484
|
+
new Logger(BridgeMetricsReporter.name).warn(
|
|
485
|
+
`Ignoring BRIDGE_METRICS_INTERVAL_MS='${raw}' (invalid or < ${MIN_INTERVAL_MS}ms); using default ${DEFAULT_INTERVAL_MS}ms.`
|
|
486
|
+
);
|
|
487
|
+
return DEFAULT_INTERVAL_MS;
|
|
488
|
+
}
|
|
489
|
+
return parsed;
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
BridgeMetricsReporter = __decorateClass([
|
|
493
|
+
Injectable3(),
|
|
494
|
+
__decorateParam(0, Inject2(DRIZZLE)),
|
|
495
|
+
__decorateParam(1, Optional())
|
|
496
|
+
], BridgeMetricsReporter);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
// runtime/subsystems/observability/observability.tokens.ts
|
|
501
|
+
var OBSERVABILITY = /* @__PURE__ */ Symbol("OBSERVABILITY");
|
|
502
|
+
var OBSERVABILITY_REPORTERS = /* @__PURE__ */ Symbol("OBSERVABILITY_REPORTERS");
|
|
503
|
+
|
|
504
|
+
// runtime/subsystems/observability/observability.module.ts
|
|
505
|
+
import { Module } from "@nestjs/common";
|
|
506
|
+
|
|
507
|
+
// runtime/subsystems/observability/observability.drizzle-backend.ts
|
|
508
|
+
init_tokens();
|
|
509
|
+
init_job_orchestration_schema();
|
|
510
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
511
|
+
import { desc, eq, sql as sql2 } from "drizzle-orm";
|
|
512
|
+
|
|
513
|
+
// runtime/subsystems/sync/sync-audit.schema.ts
|
|
514
|
+
import {
|
|
515
|
+
pgEnum as pgEnum2,
|
|
516
|
+
pgTable as pgTable2,
|
|
517
|
+
uuid as uuid2,
|
|
518
|
+
text as text2,
|
|
519
|
+
jsonb as jsonb2,
|
|
520
|
+
integer as integer2,
|
|
521
|
+
boolean,
|
|
522
|
+
timestamp as timestamp2,
|
|
523
|
+
index as index2,
|
|
524
|
+
uniqueIndex as uniqueIndex2
|
|
525
|
+
} from "drizzle-orm/pg-core";
|
|
526
|
+
var syncRunDirectionEnum = pgEnum2("sync_run_direction", [
|
|
527
|
+
"inbound",
|
|
528
|
+
"outbound"
|
|
529
|
+
]);
|
|
530
|
+
var syncRunActionEnum = pgEnum2("sync_run_action", [
|
|
531
|
+
"poll",
|
|
532
|
+
"cdc",
|
|
533
|
+
"webhook",
|
|
534
|
+
"manual",
|
|
535
|
+
"writeback"
|
|
536
|
+
]);
|
|
537
|
+
var syncRunStatusEnum = pgEnum2("sync_run_status", [
|
|
538
|
+
"running",
|
|
539
|
+
"success",
|
|
540
|
+
"no_changes",
|
|
541
|
+
"failed"
|
|
542
|
+
]);
|
|
543
|
+
var syncRunItemOperationEnum = pgEnum2("sync_run_item_operation", [
|
|
544
|
+
"created",
|
|
545
|
+
"updated",
|
|
546
|
+
"deleted",
|
|
547
|
+
"noop"
|
|
548
|
+
]);
|
|
549
|
+
var syncRunItemStatusEnum = pgEnum2("sync_run_item_status", [
|
|
550
|
+
"success",
|
|
551
|
+
"failed",
|
|
552
|
+
"skipped"
|
|
553
|
+
]);
|
|
554
|
+
var syncSubscriptions = pgTable2(
|
|
555
|
+
"sync_subscriptions",
|
|
556
|
+
{
|
|
557
|
+
id: uuid2("id").primaryKey().defaultRandom(),
|
|
558
|
+
integrationId: text2("integration_id").notNull(),
|
|
559
|
+
adapter: text2("adapter").notNull(),
|
|
560
|
+
domain: text2("domain").notNull(),
|
|
561
|
+
externalRef: text2("external_ref"),
|
|
562
|
+
enabled: boolean("enabled").notNull().default(true),
|
|
563
|
+
/**
|
|
564
|
+
* Per-subscription configuration bag. Strategies type it internally;
|
|
565
|
+
* e.g. polling strategies stash `{ batchSize, highWatermark }` here.
|
|
566
|
+
*/
|
|
567
|
+
config: jsonb2("config").notNull().default({}).$type(),
|
|
568
|
+
/**
|
|
569
|
+
* Opaque cursor persisted by `ICursorStore.put()`. NULL until the first
|
|
570
|
+
* successful run advances it.
|
|
571
|
+
*/
|
|
572
|
+
cursor: jsonb2("cursor").$type(),
|
|
573
|
+
lastSyncAt: timestamp2("last_sync_at", { withTimezone: true }),
|
|
574
|
+
/** Runtime-enforced when `SYNC_MULTI_TENANT` is true; see SYNC-6. */
|
|
575
|
+
tenantId: text2("tenant_id"),
|
|
576
|
+
createdAt: timestamp2("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
577
|
+
updatedAt: timestamp2("updated_at", { withTimezone: true }).notNull().defaultNow()
|
|
578
|
+
},
|
|
579
|
+
(t) => ({
|
|
580
|
+
/**
|
|
581
|
+
* Composite uniqueness per the epic shape. `external_ref` is nullable;
|
|
582
|
+
* Postgres treats NULLs as distinct in a UNIQUE constraint, which means
|
|
583
|
+
* two rows with the same `(integration_id, adapter, domain)` and NULL
|
|
584
|
+
* external_ref are allowed. That's intentional — a subscription with
|
|
585
|
+
* NULL external_ref covers the full domain, and duplicates there would
|
|
586
|
+
* be a consumer-layer modeling issue, not a schema concern.
|
|
587
|
+
*/
|
|
588
|
+
uqSyncSubscriptionTuple: uniqueIndex2("uq_sync_subscriptions_tuple").on(
|
|
589
|
+
t.integrationId,
|
|
590
|
+
t.adapter,
|
|
591
|
+
t.domain,
|
|
592
|
+
t.externalRef
|
|
593
|
+
),
|
|
594
|
+
/** Scheduling query: list enabled subscriptions ordered by staleness. */
|
|
595
|
+
idxSyncSubscriptionsEnabledLastSync: index2(
|
|
596
|
+
"idx_sync_subscriptions_enabled_last_sync"
|
|
597
|
+
).on(t.enabled, t.lastSyncAt)
|
|
598
|
+
})
|
|
599
|
+
);
|
|
600
|
+
var syncRuns = pgTable2(
|
|
601
|
+
"sync_runs",
|
|
602
|
+
{
|
|
603
|
+
id: uuid2("id").primaryKey().defaultRandom(),
|
|
604
|
+
subscriptionId: uuid2("subscription_id").notNull().references(() => syncSubscriptions.id, { onDelete: "cascade" }),
|
|
605
|
+
direction: syncRunDirectionEnum("direction").notNull(),
|
|
606
|
+
action: syncRunActionEnum("action").notNull(),
|
|
607
|
+
status: syncRunStatusEnum("status").notNull().default("running"),
|
|
608
|
+
recordsFound: integer2("records_found").notNull().default(0),
|
|
609
|
+
recordsProcessed: integer2("records_processed").notNull().default(0),
|
|
610
|
+
cursorBefore: jsonb2("cursor_before").$type(),
|
|
611
|
+
cursorAfter: jsonb2("cursor_after").$type(),
|
|
612
|
+
durationMs: integer2("duration_ms"),
|
|
613
|
+
error: text2("error"),
|
|
614
|
+
startedAt: timestamp2("started_at", { withTimezone: true }).notNull().defaultNow(),
|
|
615
|
+
completedAt: timestamp2("completed_at", { withTimezone: true }),
|
|
616
|
+
/** Runtime-enforced when `SYNC_MULTI_TENANT` is true; see SYNC-6. */
|
|
617
|
+
tenantId: text2("tenant_id")
|
|
618
|
+
},
|
|
619
|
+
(t) => ({
|
|
620
|
+
/** Timeline read: "most recent runs for this subscription". */
|
|
621
|
+
idxSyncRunsSubscriptionStartedAt: index2(
|
|
622
|
+
"idx_sync_runs_subscription_started_at"
|
|
623
|
+
).on(t.subscriptionId, t.startedAt),
|
|
624
|
+
/** Stale-run sweeper: "runs that started > N minutes ago and are still running". */
|
|
625
|
+
idxSyncRunsStatusStartedAt: index2("idx_sync_runs_status_started_at").on(
|
|
626
|
+
t.status,
|
|
627
|
+
t.startedAt
|
|
628
|
+
)
|
|
629
|
+
})
|
|
630
|
+
);
|
|
631
|
+
var syncRunItems = pgTable2(
|
|
632
|
+
"sync_run_items",
|
|
633
|
+
{
|
|
634
|
+
id: uuid2("id").primaryKey().defaultRandom(),
|
|
635
|
+
syncRunId: uuid2("sync_run_id").notNull().references(() => syncRuns.id, { onDelete: "cascade" }),
|
|
636
|
+
entityType: text2("entity_type").notNull(),
|
|
637
|
+
externalId: text2("external_id").notNull(),
|
|
638
|
+
localId: text2("local_id"),
|
|
639
|
+
operation: syncRunItemOperationEnum("operation").notNull(),
|
|
640
|
+
status: syncRunItemStatusEnum("status").notNull(),
|
|
641
|
+
/**
|
|
642
|
+
* Structured per-field diff — ADR-0003 shape enforced by
|
|
643
|
+
* `FieldDiffSchema.parse` at the recorder service layer.
|
|
644
|
+
*
|
|
645
|
+
* Shape: `{ [fieldName]: { from: unknown, to: unknown } }`.
|
|
646
|
+
* Empty `{}` for `noop` items; `{ [field]: { from: null, to: <value> } }`
|
|
647
|
+
* for created items; `{ [field]: { from: <value>, to: null } }` for
|
|
648
|
+
* deleted items.
|
|
649
|
+
*/
|
|
650
|
+
changedFields: jsonb2("changed_fields").notNull().default({}).$type(),
|
|
651
|
+
title: text2("title"),
|
|
652
|
+
error: text2("error"),
|
|
653
|
+
createdAt: timestamp2("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
654
|
+
/** Runtime-enforced when `SYNC_MULTI_TENANT` is true; see SYNC-6. */
|
|
655
|
+
tenantId: text2("tenant_id")
|
|
656
|
+
},
|
|
657
|
+
(t) => ({
|
|
658
|
+
/** Ordered timeline within a run. */
|
|
659
|
+
idxSyncRunItemsRunCreatedAt: index2("idx_sync_run_items_run_created_at").on(
|
|
660
|
+
t.syncRunId,
|
|
661
|
+
t.createdAt
|
|
662
|
+
),
|
|
663
|
+
/** Per-record history: "every sync that touched opportunity/$extId". */
|
|
664
|
+
idxSyncRunItemsEntityExternal: index2(
|
|
665
|
+
"idx_sync_run_items_entity_external"
|
|
666
|
+
).on(t.entityType, t.externalId)
|
|
667
|
+
})
|
|
668
|
+
);
|
|
669
|
+
|
|
670
|
+
// runtime/subsystems/observability/observability.drizzle-backend.ts
|
|
671
|
+
var DrizzleObservabilityService = class {
|
|
672
|
+
constructor(db) {
|
|
673
|
+
this.db = db;
|
|
674
|
+
}
|
|
675
|
+
db;
|
|
676
|
+
async getPoolDepths() {
|
|
677
|
+
const result = await this.db.execute(sql2`
|
|
678
|
+
SELECT
|
|
679
|
+
pool AS name,
|
|
680
|
+
COUNT(*) FILTER (WHERE status = 'pending')::int AS pending,
|
|
681
|
+
COUNT(*) FILTER (WHERE status = 'running')::int AS running,
|
|
682
|
+
(percentile_cont(0.95) WITHIN GROUP (
|
|
683
|
+
ORDER BY EXTRACT(EPOCH FROM (now() - claimed_at)) * 1000
|
|
684
|
+
) FILTER (WHERE status = 'running' AND claimed_at IS NOT NULL))::int
|
|
685
|
+
AS claimed_age_p95_ms
|
|
686
|
+
FROM job_run
|
|
687
|
+
WHERE status IN ('pending','running')
|
|
688
|
+
GROUP BY pool
|
|
689
|
+
ORDER BY pool
|
|
690
|
+
`);
|
|
691
|
+
const rows = extractRows(result);
|
|
692
|
+
return rows.map((r) => ({
|
|
693
|
+
name: r.name,
|
|
694
|
+
pending: r.pending,
|
|
695
|
+
running: r.running,
|
|
696
|
+
claimedAgeP95Ms: r.claimed_age_p95_ms
|
|
697
|
+
}));
|
|
698
|
+
}
|
|
699
|
+
async getRecentSyncRuns(limit, integrationId) {
|
|
700
|
+
const base = this.db.select({
|
|
701
|
+
id: syncRuns.id,
|
|
702
|
+
subscriptionId: syncRuns.subscriptionId,
|
|
703
|
+
integrationId: syncSubscriptions.integrationId,
|
|
704
|
+
adapter: syncSubscriptions.adapter,
|
|
705
|
+
domain: syncSubscriptions.domain,
|
|
706
|
+
direction: syncRuns.direction,
|
|
707
|
+
action: syncRuns.action,
|
|
708
|
+
status: syncRuns.status,
|
|
709
|
+
recordsFound: syncRuns.recordsFound,
|
|
710
|
+
recordsProcessed: syncRuns.recordsProcessed,
|
|
711
|
+
durationMs: syncRuns.durationMs,
|
|
712
|
+
error: syncRuns.error,
|
|
713
|
+
startedAt: syncRuns.startedAt,
|
|
714
|
+
completedAt: syncRuns.completedAt
|
|
715
|
+
}).from(syncRuns).innerJoin(
|
|
716
|
+
syncSubscriptions,
|
|
717
|
+
eq(syncRuns.subscriptionId, syncSubscriptions.id)
|
|
718
|
+
);
|
|
719
|
+
const filtered = integrationId !== void 0 ? base.where(eq(syncSubscriptions.integrationId, integrationId)) : base;
|
|
720
|
+
const rows = await filtered.orderBy(desc(syncRuns.startedAt)).limit(limit);
|
|
721
|
+
return rows.map((r) => ({
|
|
722
|
+
id: r.id,
|
|
723
|
+
subscriptionId: r.subscriptionId,
|
|
724
|
+
integrationId: r.integrationId,
|
|
725
|
+
adapter: r.adapter,
|
|
726
|
+
domain: r.domain,
|
|
727
|
+
direction: r.direction,
|
|
728
|
+
action: r.action,
|
|
729
|
+
status: r.status,
|
|
730
|
+
recordsFound: r.recordsFound,
|
|
731
|
+
recordsProcessed: r.recordsProcessed,
|
|
732
|
+
durationMs: r.durationMs,
|
|
733
|
+
error: r.error,
|
|
734
|
+
startedAt: r.startedAt,
|
|
735
|
+
completedAt: r.completedAt
|
|
736
|
+
}));
|
|
737
|
+
}
|
|
738
|
+
async getBridgeDeliveryHistogram(windowHours) {
|
|
739
|
+
const result = await this.db.execute(sql2`
|
|
740
|
+
SELECT status, COUNT(*)::int AS count
|
|
741
|
+
FROM bridge_delivery
|
|
742
|
+
WHERE COALESCE(delivered_at, attempted_at) > now() - make_interval(hours => ${windowHours})
|
|
743
|
+
GROUP BY status
|
|
744
|
+
`);
|
|
745
|
+
const rows = extractRows(result);
|
|
746
|
+
const hist = {};
|
|
747
|
+
for (const r of rows) hist[r.status] = r.count;
|
|
748
|
+
return hist;
|
|
749
|
+
}
|
|
750
|
+
async getRecentFailedJobs(limit) {
|
|
751
|
+
const rows = await this.db.select({
|
|
752
|
+
id: jobRuns.id,
|
|
753
|
+
jobType: jobRuns.jobType,
|
|
754
|
+
pool: jobRuns.pool,
|
|
755
|
+
status: jobRuns.status,
|
|
756
|
+
error: jobRuns.error,
|
|
757
|
+
startedAt: jobRuns.startedAt,
|
|
758
|
+
finishedAt: jobRuns.finishedAt,
|
|
759
|
+
attempts: jobRuns.attempts
|
|
760
|
+
}).from(jobRuns).where(eq(jobRuns.status, "failed")).orderBy(desc(jobRuns.finishedAt)).limit(limit);
|
|
761
|
+
return rows.map((r) => ({
|
|
762
|
+
id: r.id,
|
|
763
|
+
jobType: r.jobType,
|
|
764
|
+
pool: r.pool,
|
|
765
|
+
status: r.status,
|
|
766
|
+
error: r.error,
|
|
767
|
+
startedAt: r.startedAt,
|
|
768
|
+
finishedAt: r.finishedAt,
|
|
769
|
+
attempts: r.attempts
|
|
770
|
+
}));
|
|
771
|
+
}
|
|
772
|
+
async getCursors() {
|
|
773
|
+
const rows = await this.db.select({
|
|
774
|
+
id: syncSubscriptions.id,
|
|
775
|
+
integrationId: syncSubscriptions.integrationId,
|
|
776
|
+
adapter: syncSubscriptions.adapter,
|
|
777
|
+
domain: syncSubscriptions.domain,
|
|
778
|
+
cursor: syncSubscriptions.cursor,
|
|
779
|
+
lastSyncAt: syncSubscriptions.lastSyncAt
|
|
780
|
+
}).from(syncSubscriptions).where(eq(syncSubscriptions.enabled, true)).orderBy(syncSubscriptions.integrationId, syncSubscriptions.domain);
|
|
781
|
+
return rows.map((r) => ({
|
|
782
|
+
subscriptionId: r.id,
|
|
783
|
+
integrationId: r.integrationId,
|
|
784
|
+
adapter: r.adapter,
|
|
785
|
+
domain: r.domain,
|
|
786
|
+
lastCursor: r.cursor,
|
|
787
|
+
lastSyncAt: r.lastSyncAt
|
|
788
|
+
}));
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
DrizzleObservabilityService = __decorateClass([
|
|
792
|
+
Injectable(),
|
|
793
|
+
__decorateParam(0, Inject(DRIZZLE))
|
|
794
|
+
], DrizzleObservabilityService);
|
|
795
|
+
function extractRows(result) {
|
|
796
|
+
const maybe = result;
|
|
797
|
+
if (Array.isArray(maybe.rows)) return maybe.rows;
|
|
798
|
+
if (Array.isArray(result)) return result;
|
|
799
|
+
return [];
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// runtime/subsystems/observability/observability.memory-backend.ts
|
|
803
|
+
import { Injectable as Injectable2 } from "@nestjs/common";
|
|
804
|
+
var MemoryObservabilityService = class {
|
|
805
|
+
pools = [];
|
|
806
|
+
syncRuns = [];
|
|
807
|
+
bridgeHistogram = {};
|
|
808
|
+
failedJobs = [];
|
|
809
|
+
cursors = [];
|
|
810
|
+
// ─── Core contract ─────────────────────────────────────────────────────
|
|
811
|
+
async getPoolDepths() {
|
|
812
|
+
return [...this.pools];
|
|
813
|
+
}
|
|
814
|
+
async getRecentSyncRuns(limit, integrationId) {
|
|
815
|
+
const filtered = integrationId !== void 0 ? this.syncRuns.filter((r) => r.integrationId === integrationId) : this.syncRuns;
|
|
816
|
+
return filtered.slice().sort((a, b) => b.startedAt.getTime() - a.startedAt.getTime()).slice(0, limit);
|
|
817
|
+
}
|
|
818
|
+
async getBridgeDeliveryHistogram(_windowHours) {
|
|
819
|
+
return { ...this.bridgeHistogram };
|
|
820
|
+
}
|
|
821
|
+
async getRecentFailedJobs(limit) {
|
|
822
|
+
return this.failedJobs.slice().sort(
|
|
823
|
+
(a, b) => (b.finishedAt?.getTime() ?? 0) - (a.finishedAt?.getTime() ?? 0)
|
|
824
|
+
).slice(0, limit);
|
|
825
|
+
}
|
|
826
|
+
async getCursors() {
|
|
827
|
+
return [...this.cursors];
|
|
828
|
+
}
|
|
829
|
+
// ─── Test seams ────────────────────────────────────────────────────────
|
|
830
|
+
/** Replace the pool-depth slice. */
|
|
831
|
+
seedPools(pools) {
|
|
832
|
+
this.pools = [...pools];
|
|
833
|
+
}
|
|
834
|
+
/** Replace the sync-run slice. */
|
|
835
|
+
seedSyncRuns(runs) {
|
|
836
|
+
this.syncRuns = [...runs];
|
|
837
|
+
}
|
|
838
|
+
/** Replace the bridge-delivery histogram. */
|
|
839
|
+
seedBridgeHistogram(hist) {
|
|
840
|
+
this.bridgeHistogram = { ...hist };
|
|
841
|
+
}
|
|
842
|
+
/** Replace the failed-jobs slice. */
|
|
843
|
+
seedFailedJobs(jobs2) {
|
|
844
|
+
this.failedJobs = [...jobs2];
|
|
845
|
+
}
|
|
846
|
+
/** Replace the cursor slice. */
|
|
847
|
+
seedCursors(cursors) {
|
|
848
|
+
this.cursors = [...cursors];
|
|
849
|
+
}
|
|
850
|
+
/** Reset every slice — for afterEach hooks. */
|
|
851
|
+
reset() {
|
|
852
|
+
this.pools = [];
|
|
853
|
+
this.syncRuns = [];
|
|
854
|
+
this.bridgeHistogram = {};
|
|
855
|
+
this.failedJobs = [];
|
|
856
|
+
this.cursors = [];
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
MemoryObservabilityService = __decorateClass([
|
|
860
|
+
Injectable2()
|
|
861
|
+
], MemoryObservabilityService);
|
|
862
|
+
|
|
863
|
+
// runtime/subsystems/observability/observability.module.ts
|
|
864
|
+
var ObservabilityModule = class {
|
|
865
|
+
static forRoot(options = { backend: "drizzle" }) {
|
|
866
|
+
const ConcreteClass = options.backend === "drizzle" ? DrizzleObservabilityService : MemoryObservabilityService;
|
|
867
|
+
const wantsBridgeMetrics = options.reporters?.bridgeMetrics === true;
|
|
868
|
+
const providers = [
|
|
869
|
+
// Register the concrete class as the canonical instance.
|
|
870
|
+
ConcreteClass,
|
|
871
|
+
// OBSERVABILITY token points at the same instance — no duplicate.
|
|
872
|
+
{ provide: OBSERVABILITY, useExisting: ConcreteClass },
|
|
873
|
+
// Expose the resolved reporter config for introspection / tests.
|
|
874
|
+
{
|
|
875
|
+
provide: OBSERVABILITY_REPORTERS,
|
|
876
|
+
useValue: options.reporters ?? {}
|
|
877
|
+
}
|
|
878
|
+
];
|
|
879
|
+
const exports = [OBSERVABILITY];
|
|
880
|
+
if (wantsBridgeMetrics) {
|
|
881
|
+
const { BridgeMetricsReporter: BridgeMetricsReporter2 } = (init_bridge_metrics_reporter(), __toCommonJS(bridge_metrics_reporter_exports));
|
|
882
|
+
providers.push(BridgeMetricsReporter2);
|
|
883
|
+
exports.push(BridgeMetricsReporter2);
|
|
884
|
+
}
|
|
885
|
+
const imports = [];
|
|
886
|
+
if (wantsBridgeMetrics) {
|
|
887
|
+
const { ScheduleModule } = __require("@nestjs/schedule");
|
|
888
|
+
imports.push(ScheduleModule.forRoot());
|
|
889
|
+
}
|
|
890
|
+
return {
|
|
891
|
+
module: ObservabilityModule,
|
|
892
|
+
global: true,
|
|
893
|
+
imports,
|
|
894
|
+
providers,
|
|
895
|
+
exports
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
};
|
|
899
|
+
ObservabilityModule = __decorateClass([
|
|
900
|
+
Module({})
|
|
901
|
+
], ObservabilityModule);
|
|
902
|
+
|
|
903
|
+
// runtime/subsystems/observability/index.ts
|
|
904
|
+
init_bridge_metrics_reporter();
|
|
905
|
+
export {
|
|
906
|
+
BridgeMetricsReporter,
|
|
907
|
+
DrizzleObservabilityService,
|
|
908
|
+
MemoryObservabilityService,
|
|
909
|
+
OBSERVABILITY,
|
|
910
|
+
OBSERVABILITY_REPORTERS,
|
|
911
|
+
ObservabilityModule
|
|
912
|
+
};
|
|
913
|
+
//# sourceMappingURL=index.js.map
|