@meyicloud/meyi-insight-server 1.1.9 → 1.1.10

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.
@@ -22,8 +22,8 @@ export class AlertModel {
22
22
  if (cached && Date.now() - cached.at < ACCOUNT_NAME_TTL_MS)
23
23
  return cached.name;
24
24
  try {
25
- const res = await this.db.execute(sql `
26
- SELECT account_name FROM "meyiconnect"."connectors"
25
+ const res = await this.db.execute(sql `
26
+ SELECT account_name FROM "meyiconnect"."connectors"
27
27
  WHERE account_id = ${accountId} AND tenant_id::text = ${tenantId} LIMIT 1`);
28
28
  const rows = res?.rows ?? (Array.isArray(res) ? res : []);
29
29
  const name = rows[0]?.account_name ?? null;
@@ -113,24 +113,24 @@ export class RetentionService {
113
113
  if (!tenantId)
114
114
  throw new Error("tenantId is required");
115
115
  const { cutoffIso, ctx, dryRun } = this.validate(opts);
116
- const incidentPurgeable = sql `tenant_id = ${tenantId}::text
117
- AND status IN ('resolved','closed')
116
+ const incidentPurgeable = sql `tenant_id = ${tenantId}::text
117
+ AND status IN ('resolved','closed')
118
118
  AND COALESCE(resolved_at, updated_at, created_at) < ${cutoffIso}::timestamptz`;
119
119
  if (dryRun) {
120
- const incidents = await this.count(sql `
120
+ const incidents = await this.count(sql `
121
121
  SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_incidents" WHERE ${incidentPurgeable}`);
122
- const alerts = await this.count(sql `
123
- SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_alerts" a
124
- WHERE a.tenant_id = ${tenantId}::text
125
- AND a.created_at < ${cutoffIso}::timestamptz
126
- AND (a.incident_id IS NULL OR a.incident_id IN (
122
+ const alerts = await this.count(sql `
123
+ SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_alerts" a
124
+ WHERE a.tenant_id = ${tenantId}::text
125
+ AND a.created_at < ${cutoffIso}::timestamptz
126
+ AND (a.incident_id IS NULL OR a.incident_id IN (
127
127
  SELECT id FROM "meyiconnect"."insight_incidents" WHERE ${incidentPurgeable}))`);
128
- const alarmGroups = await this.count(sql `
129
- SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_alarm_groups"
130
- WHERE tenant_id = ${tenantId}::text AND last_alarm_at < ${cutoffIso}::timestamptz
128
+ const alarmGroups = await this.count(sql `
129
+ SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_alarm_groups"
130
+ WHERE tenant_id = ${tenantId}::text AND last_alarm_at < ${cutoffIso}::timestamptz
131
131
  AND status IN ('flushed','closed')`);
132
- const executions = await this.count(sql `
133
- SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_workflow_executions"
132
+ const executions = await this.count(sql `
133
+ SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_workflow_executions"
134
134
  WHERE tenant_id = ${tenantId}::text AND triggered_at < ${cutoffIso}::timestamptz`);
135
135
  return {
136
136
  tables: [
@@ -143,79 +143,79 @@ export class RetentionService {
143
143
  };
144
144
  }
145
145
  // 1. Incidents. Status is re-checked in the outer DELETE so an incident reopened mid-run is kept.
146
- const incidentRun = await this.deleteInBatches(ctx, (limit) => sql `
147
- DELETE FROM "meyiconnect"."insight_incidents"
148
- WHERE id IN (
149
- SELECT id FROM "meyiconnect"."insight_incidents"
150
- WHERE ${incidentPurgeable}
151
- LIMIT ${limit}
152
- )
153
- AND tenant_id = ${tenantId}::text
154
- AND status IN ('resolved','closed')
146
+ const incidentRun = await this.deleteInBatches(ctx, (limit) => sql `
147
+ DELETE FROM "meyiconnect"."insight_incidents"
148
+ WHERE id IN (
149
+ SELECT id FROM "meyiconnect"."insight_incidents"
150
+ WHERE ${incidentPurgeable}
151
+ LIMIT ${limit}
152
+ )
153
+ AND tenant_id = ${tenantId}::text
154
+ AND status IN ('resolved','closed')
155
155
  RETURNING id`);
156
156
  const incidentIds = incidentRun.rows.map((r) => String(r.id));
157
157
  // 2. Alerts linked to incidents purged in this run: delete the old ones, unlink the rest
158
158
  // (as the manual incident delete does), and unlink alarm groups.
159
159
  let alertsDeleted = 0;
160
160
  for (const chunk of chunks(incidentIds, ID_CHUNK)) {
161
- const linked = await this.deleteInBatches(ctx, (limit) => sql `
162
- DELETE FROM "meyiconnect"."insight_alerts"
163
- WHERE id IN (
164
- SELECT id FROM "meyiconnect"."insight_alerts"
165
- WHERE tenant_id = ${tenantId}::text
166
- AND created_at < ${cutoffIso}::timestamptz
167
- AND incident_id IN (${uuidList(chunk)})
168
- ORDER BY created_at
169
- LIMIT ${limit}
170
- )
161
+ const linked = await this.deleteInBatches(ctx, (limit) => sql `
162
+ DELETE FROM "meyiconnect"."insight_alerts"
163
+ WHERE id IN (
164
+ SELECT id FROM "meyiconnect"."insight_alerts"
165
+ WHERE tenant_id = ${tenantId}::text
166
+ AND created_at < ${cutoffIso}::timestamptz
167
+ AND incident_id IN (${uuidList(chunk)})
168
+ ORDER BY created_at
169
+ LIMIT ${limit}
170
+ )
171
171
  AND tenant_id = ${tenantId}::text`);
172
172
  alertsDeleted += linked.deleted;
173
173
  this.checkAbort(ctx);
174
- await this.inTx(ctx, sql `
175
- UPDATE "meyiconnect"."insight_alerts" SET incident_id = NULL
174
+ await this.inTx(ctx, sql `
175
+ UPDATE "meyiconnect"."insight_alerts" SET incident_id = NULL
176
176
  WHERE tenant_id = ${tenantId}::text AND incident_id IN (${uuidList(chunk)})`);
177
- await this.inTx(ctx, sql `
178
- UPDATE "meyiconnect"."insight_alerts" SET converted_incident_id = NULL
177
+ await this.inTx(ctx, sql `
178
+ UPDATE "meyiconnect"."insight_alerts" SET converted_incident_id = NULL
179
179
  WHERE tenant_id = ${tenantId}::text AND converted_incident_id IN (${uuidList(chunk)})`);
180
- await this.inTx(ctx, sql `
181
- UPDATE "meyiconnect"."insight_alarm_groups" SET incident_id = NULL
180
+ await this.inTx(ctx, sql `
181
+ UPDATE "meyiconnect"."insight_alarm_groups" SET incident_id = NULL
182
182
  WHERE tenant_id = ${tenantId}::text AND incident_id IN (${uuidList(chunk)})`);
183
183
  }
184
184
  // 3. Unlinked alerts.
185
- const unlinked = await this.deleteInBatches(ctx, (limit) => sql `
186
- DELETE FROM "meyiconnect"."insight_alerts"
187
- WHERE id IN (
188
- SELECT id FROM "meyiconnect"."insight_alerts"
189
- WHERE tenant_id = ${tenantId}::text
190
- AND created_at < ${cutoffIso}::timestamptz
191
- AND incident_id IS NULL
192
- ORDER BY created_at
193
- LIMIT ${limit}
194
- )
195
- AND tenant_id = ${tenantId}::text
185
+ const unlinked = await this.deleteInBatches(ctx, (limit) => sql `
186
+ DELETE FROM "meyiconnect"."insight_alerts"
187
+ WHERE id IN (
188
+ SELECT id FROM "meyiconnect"."insight_alerts"
189
+ WHERE tenant_id = ${tenantId}::text
190
+ AND created_at < ${cutoffIso}::timestamptz
191
+ AND incident_id IS NULL
192
+ ORDER BY created_at
193
+ LIMIT ${limit}
194
+ )
195
+ AND tenant_id = ${tenantId}::text
196
196
  AND incident_id IS NULL`);
197
197
  alertsDeleted += unlinked.deleted;
198
198
  // 4. Alarm groups that have been flushed (buffering groups are live and kept).
199
- const groups = await this.deleteInBatches(ctx, (limit) => sql `
200
- DELETE FROM "meyiconnect"."insight_alarm_groups"
201
- WHERE id IN (
202
- SELECT id FROM "meyiconnect"."insight_alarm_groups"
203
- WHERE tenant_id = ${tenantId}::text
204
- AND last_alarm_at < ${cutoffIso}::timestamptz
205
- AND status IN ('flushed','closed')
206
- LIMIT ${limit}
207
- )
199
+ const groups = await this.deleteInBatches(ctx, (limit) => sql `
200
+ DELETE FROM "meyiconnect"."insight_alarm_groups"
201
+ WHERE id IN (
202
+ SELECT id FROM "meyiconnect"."insight_alarm_groups"
203
+ WHERE tenant_id = ${tenantId}::text
204
+ AND last_alarm_at < ${cutoffIso}::timestamptz
205
+ AND status IN ('flushed','closed')
206
+ LIMIT ${limit}
207
+ )
208
208
  AND tenant_id = ${tenantId}::text`);
209
209
  // 5. Workflow executions.
210
- const executions = await this.deleteInBatches(ctx, (limit) => sql `
211
- DELETE FROM "meyiconnect"."insight_workflow_executions"
212
- WHERE id IN (
213
- SELECT id FROM "meyiconnect"."insight_workflow_executions"
214
- WHERE tenant_id = ${tenantId}::text
215
- AND triggered_at < ${cutoffIso}::timestamptz
216
- ORDER BY triggered_at
217
- LIMIT ${limit}
218
- )
210
+ const executions = await this.deleteInBatches(ctx, (limit) => sql `
211
+ DELETE FROM "meyiconnect"."insight_workflow_executions"
212
+ WHERE id IN (
213
+ SELECT id FROM "meyiconnect"."insight_workflow_executions"
214
+ WHERE tenant_id = ${tenantId}::text
215
+ AND triggered_at < ${cutoffIso}::timestamptz
216
+ ORDER BY triggered_at
217
+ LIMIT ${limit}
218
+ )
219
219
  AND tenant_id = ${tenantId}::text`);
220
220
  return {
221
221
  tables: [
@@ -266,13 +266,13 @@ export class RetentionService {
266
266
  const steps = [
267
267
  {
268
268
  table: "insight_job_queue",
269
- where: sql `(
270
- payload->>'tenant_id' = ${t}
271
- OR payload->>'tenantId' = ${t}
272
- OR payload->>'incident_id' IN (
273
- SELECT id::text FROM "meyiconnect"."insight_incidents" WHERE tenant_id = ${t})
274
- OR (payload->>'sop_id' IS NOT NULL AND payload->>'id' IN (
275
- SELECT id::text FROM "meyiconnect"."insight_sops" WHERE tenant_id = ${t}))
269
+ where: sql `(
270
+ payload->>'tenant_id' = ${t}
271
+ OR payload->>'tenantId' = ${t}
272
+ OR payload->>'incident_id' IN (
273
+ SELECT id::text FROM "meyiconnect"."insight_incidents" WHERE tenant_id = ${t})
274
+ OR (payload->>'sop_id' IS NOT NULL AND payload->>'id' IN (
275
+ SELECT id::text FROM "meyiconnect"."insight_sops" WHERE tenant_id = ${t}))
276
276
  )`,
277
277
  },
278
278
  { table: "insight_workflow_executions", where: tenantIs },
@@ -313,9 +313,9 @@ export class RetentionService {
313
313
  tables.push({ table: step.table, deleted: await this.count(sql `SELECT COUNT(*)::bigint AS n FROM ${ref} WHERE ${step.where}`) });
314
314
  continue;
315
315
  }
316
- const run = await this.deleteInBatches(ctx, (limit) => sql `
317
- DELETE FROM ${ref}
318
- WHERE id IN (SELECT id FROM ${ref} WHERE ${step.where} LIMIT ${limit})
316
+ const run = await this.deleteInBatches(ctx, (limit) => sql `
317
+ DELETE FROM ${ref}
318
+ WHERE id IN (SELECT id FROM ${ref} WHERE ${step.where} LIMIT ${limit})
319
319
  AND ${step.where}`);
320
320
  tables.push({ table: step.table, deleted: run.deleted });
321
321
  }
@@ -369,11 +369,11 @@ export class RetentionService {
369
369
  async purgeSystemBefore(opts) {
370
370
  const { cutoffIso, ctx, dryRun } = this.validate(opts);
371
371
  if (dryRun) {
372
- const logs = await this.count(sql `
373
- SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_webhook_failure_logs"
372
+ const logs = await this.count(sql `
373
+ SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_webhook_failure_logs"
374
374
  WHERE created_at < ${cutoffIso}::timestamptz`);
375
- const jobs = await this.count(sql `
376
- SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_job_queue"
375
+ const jobs = await this.count(sql `
376
+ SELECT COUNT(*)::bigint AS n FROM "meyiconnect"."insight_job_queue"
377
377
  WHERE status IN ('done','failed') AND COALESCE(updated_at, created_at) < ${cutoffIso}::timestamptz`);
378
378
  return {
379
379
  tables: [
@@ -382,21 +382,21 @@ export class RetentionService {
382
382
  ],
383
383
  };
384
384
  }
385
- const logs = await this.deleteInBatches(ctx, (limit) => sql `
386
- DELETE FROM "meyiconnect"."insight_webhook_failure_logs"
387
- WHERE id IN (
388
- SELECT id FROM "meyiconnect"."insight_webhook_failure_logs"
389
- WHERE created_at < ${cutoffIso}::timestamptz
390
- ORDER BY created_at
391
- LIMIT ${limit}
385
+ const logs = await this.deleteInBatches(ctx, (limit) => sql `
386
+ DELETE FROM "meyiconnect"."insight_webhook_failure_logs"
387
+ WHERE id IN (
388
+ SELECT id FROM "meyiconnect"."insight_webhook_failure_logs"
389
+ WHERE created_at < ${cutoffIso}::timestamptz
390
+ ORDER BY created_at
391
+ LIMIT ${limit}
392
392
  )`);
393
- const jobs = await this.deleteInBatches(ctx, (limit) => sql `
394
- DELETE FROM "meyiconnect"."insight_job_queue"
395
- WHERE id IN (
396
- SELECT id FROM "meyiconnect"."insight_job_queue"
397
- WHERE status IN ('done','failed') AND COALESCE(updated_at, created_at) < ${cutoffIso}::timestamptz
398
- LIMIT ${limit}
399
- )
393
+ const jobs = await this.deleteInBatches(ctx, (limit) => sql `
394
+ DELETE FROM "meyiconnect"."insight_job_queue"
395
+ WHERE id IN (
396
+ SELECT id FROM "meyiconnect"."insight_job_queue"
397
+ WHERE status IN ('done','failed') AND COALESCE(updated_at, created_at) < ${cutoffIso}::timestamptz
398
+ LIMIT ${limit}
399
+ )
400
400
  AND status IN ('done','failed')`);
401
401
  return {
402
402
  tables: [
package/package.json CHANGED
@@ -1,75 +1,75 @@
1
- {
2
- "name": "@meyicloud/meyi-insight-server",
3
- "version": "1.1.9",
4
- "description": "AI-powered Root Cause Analysis server — drop-in Express backend for MeyiConnect Insight plugin",
5
- "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "import": "./dist/index.js",
11
- "types": "./dist/index.d.ts"
12
- },
13
- "./database": {
14
- "import": "./dist/database/index.js",
15
- "types": "./dist/database/index.d.ts"
16
- },
17
- "./routes": {
18
- "import": "./dist/routes/index.js",
19
- "types": "./dist/routes/index.d.ts"
20
- }
21
- },
22
- "files": [
23
- "dist",
24
- "README.md"
25
- ],
26
- "scripts": {
27
- "build": "npm run clean && npm run compile",
28
- "clean": "rimraf dist",
29
- "compile": "tsc",
30
- "start:standalone": "node dist/standalone.js",
31
- "prepublishOnly": "npm run build",
32
- "test": "tsx --test test/incident-categories.test.ts test/sla.test.ts test/tenant-purge.test.ts test/health-normalization.test.ts test/ai-usage.test.ts test/ai-pricing.test.ts test/redis-reconnect.test.ts test/alert-update.test.ts test/incident-lifecycle.test.ts test/incident-notify.test.ts",
33
- "test:smoke": "node scripts/smoke-test.mjs",
34
- "test:load": "node scripts/load-test.mjs"
35
- },
36
- "peerDependencies": {
37
- "drizzle-orm": "^0.44.0",
38
- "express": "^4.18.0",
39
- "pg": "^8.16.0"
40
- },
41
- "devDependencies": {
42
- "@types/cors": "^2.8.19",
43
- "@types/express": "^4.17.21",
44
- "@types/js-yaml": "^4.0.9",
45
- "@types/node": "^20.0.0",
46
- "@types/nodemailer": "^8.0.1",
47
- "@types/object-hash": "^3.0.6",
48
- "@types/pg": "^8.11.0",
49
- "drizzle-kit": "^0.31.10",
50
- "drizzle-orm": "^0.44.0",
51
- "express": "^4.18.0",
52
- "pg": "^8.22.0",
53
- "rimraf": "^6.1.3",
54
- "tsx": "^4.22.4",
55
- "typescript": "^5.9.3"
56
- },
57
- "dependencies": {
58
- "@aws-sdk/client-cloudformation": "^3.922.0",
59
- "@aws-sdk/client-cloudwatch": "^3.922.0",
60
- "@aws-sdk/client-sts": "^3.1143.0",
61
- "bullmq": "^5.79.1",
62
- "handlebars": "^4.7.9",
63
- "ioredis": "^5.11.1",
64
- "js-yaml": "^4.2.0",
65
- "nodemailer": "^9.0.1",
66
- "object-hash": "^3.0.0",
67
- "zod": "^4.4.3"
68
- },
69
- "engines": {
70
- "node": ">=20.0.0"
71
- },
72
- "publishConfig": {
73
- "access": "public"
74
- }
75
- }
1
+ {
2
+ "name": "@meyicloud/meyi-insight-server",
3
+ "version": "1.1.10",
4
+ "description": "AI-powered Root Cause Analysis server — drop-in Express backend for MeyiConnect Insight plugin",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./database": {
14
+ "import": "./dist/database/index.js",
15
+ "types": "./dist/database/index.d.ts"
16
+ },
17
+ "./routes": {
18
+ "import": "./dist/routes/index.js",
19
+ "types": "./dist/routes/index.d.ts"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "scripts": {
27
+ "build": "npm run clean && npm run compile",
28
+ "clean": "rimraf dist",
29
+ "compile": "tsc",
30
+ "start:standalone": "node dist/standalone.js",
31
+ "prepublishOnly": "npm run build",
32
+ "test": "tsx --test test/incident-categories.test.ts test/sla.test.ts test/tenant-purge.test.ts test/health-normalization.test.ts test/ai-usage.test.ts test/ai-pricing.test.ts test/redis-reconnect.test.ts test/alert-update.test.ts test/incident-lifecycle.test.ts test/incident-notify.test.ts",
33
+ "test:smoke": "node scripts/smoke-test.mjs",
34
+ "test:load": "node scripts/load-test.mjs"
35
+ },
36
+ "peerDependencies": {
37
+ "drizzle-orm": "^0.44.0",
38
+ "express": "^4.18.0",
39
+ "pg": "^8.16.0"
40
+ },
41
+ "devDependencies": {
42
+ "@types/cors": "^2.8.19",
43
+ "@types/express": "^4.17.21",
44
+ "@types/js-yaml": "^4.0.9",
45
+ "@types/node": "^20.0.0",
46
+ "@types/nodemailer": "^8.0.1",
47
+ "@types/object-hash": "^3.0.6",
48
+ "@types/pg": "^8.11.0",
49
+ "drizzle-kit": "^0.31.10",
50
+ "drizzle-orm": "^0.44.0",
51
+ "express": "^4.18.0",
52
+ "pg": "^8.22.0",
53
+ "rimraf": "^6.1.3",
54
+ "tsx": "^4.22.4",
55
+ "typescript": "^5.9.3"
56
+ },
57
+ "dependencies": {
58
+ "@aws-sdk/client-cloudformation": "^3.922.0",
59
+ "@aws-sdk/client-cloudwatch": "^3.922.0",
60
+ "@aws-sdk/client-sts": "^3.1143.0",
61
+ "bullmq": "^5.79.1",
62
+ "handlebars": "^4.7.9",
63
+ "ioredis": "^5.11.1",
64
+ "js-yaml": "^4.2.0",
65
+ "nodemailer": "^9.0.1",
66
+ "object-hash": "^3.0.0",
67
+ "zod": "^4.4.3"
68
+ },
69
+ "engines": {
70
+ "node": ">=20.0.0"
71
+ },
72
+ "publishConfig": {
73
+ "access": "public"
74
+ }
75
+ }