apiforgejs 1.0.2 → 1.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apiforgejs",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "API observability & intelligence SDK for Express.js — local-first, privacy-first",
5
5
  "main": "src/index.js",
6
6
  "keywords": [
@@ -18,16 +18,12 @@
18
18
  "author": "APIForge",
19
19
  "license": "MIT",
20
20
  "scripts": {
21
- "test": "node --test test/**/*.test.js",
22
- "test:smoke": "node --test test/smoke.test.js"
21
+ "tests": "node --test tests/**/*.test.js",
22
+ "test:smoke": "node --test tests/smoke.test.js"
23
23
  },
24
24
  "engines": {
25
25
  "node": ">=22.5.0"
26
26
  },
27
- "dependencies": {
28
- "react": "^18.0.0",
29
- "react-dom": "^18.0.0"
30
- },
31
27
  "peerDependencies": {
32
28
  "express": ">=4.0.0"
33
29
  },
package/src/database.js CHANGED
@@ -286,6 +286,21 @@ class ApiForgeDatabase {
286
286
  `).all();
287
287
  }
288
288
 
289
+ // Returns one row per (route, method, day) for the last 30 days, used by drift detection
290
+ getDriftData() {
291
+ const since30d = nowSec() - 30 * 86_400;
292
+ return this.db.prepare(`
293
+ SELECT
294
+ route, method,
295
+ CAST(bucket_ts / 86400 AS INTEGER) as day_bucket,
296
+ AVG(lat_p90) as p90
297
+ FROM api_metrics
298
+ WHERE bucket_ts >= ? AND lat_p90 IS NOT NULL
299
+ GROUP BY route, method, day_bucket
300
+ ORDER BY route, method, day_bucket
301
+ `).all(since30d);
302
+ }
303
+
289
304
  getGlobalTimeSeries(hours = 24) {
290
305
  const since = nowSec() - hours * 3600;
291
306
  return this.db.prepare(`
package/src/insights.js CHANGED
@@ -1,27 +1,19 @@
1
1
  'use strict';
2
2
 
3
- const DEAD_ENDPOINT_DAYS = 21;
4
- const REGRESSION_THRESHOLD = 0.20; // 20% worse P90 triggers regression insight
5
- const ANOMALY_Z_THRESHOLD = 2.5; // Z-score threshold for latency anomaly
3
+ const DEAD_ENDPOINT_DAYS = 21;
4
+ const REGRESSION_THRESHOLD = 0.20; // 20% worse P90 triggers regression insight
5
+ const ANOMALY_Z_THRESHOLD = 2.5; // Z-score threshold for latency anomaly
6
+ const DRIFT_SLOPE_THRESHOLD = 5; // ms/day above which progressive drift is reported
7
+ const DRIFT_MIN_DAYS = 7; // minimum number of daily data points required
6
8
 
7
9
  function getInsights(db) {
8
10
  const insights = [];
9
11
 
10
- try {
11
- insights.push(...detectLatencyAnomalies(db));
12
- } catch (_) {}
13
-
14
- try {
15
- insights.push(...detectDeadEndpoints(db));
16
- } catch (_) {}
17
-
18
- try {
19
- insights.push(...detectReleaseRegressions(db));
20
- } catch (_) {}
21
-
22
- try {
23
- insights.push(...detectUntrackedRoutes(db));
24
- } catch (_) {}
12
+ try { insights.push(...detectLatencyAnomalies(db)); } catch (_) {}
13
+ try { insights.push(...detectDeadEndpoints(db)); } catch (_) {}
14
+ try { insights.push(...detectReleaseRegressions(db)); } catch (_) {}
15
+ try { insights.push(...detectUntrackedRoutes(db)); } catch (_) {}
16
+ try { insights.push(...detectDrift(db)); } catch (_) {}
25
17
 
26
18
  return insights;
27
19
  }
@@ -33,7 +25,7 @@ function detectUntrackedRoutes(db) {
33
25
  severity: 'info',
34
26
  route: r.route,
35
27
  method: r.method,
36
- message: `\`${r.method} ${r.route}\` est déclaré dans l'application mais n'a reçu aucune requête depuis le début du monitoring.`,
28
+ message: `\`${r.method} ${r.route}\` is declared but has received no requests since monitoring started.`,
37
29
  data: { first_seen_ts: r.first_seen },
38
30
  }));
39
31
  }
@@ -69,7 +61,7 @@ function detectLatencyAnomalies(db) {
69
61
  severity: 'warning',
70
62
  route: r.route,
71
63
  method: r.method,
72
- message: `La latence P99 de \`${r.method} ${r.route}\` est anormalement élevée cette heure (${fmt(r.avg_p99)} vs moyenne ${fmt(mean)} — Z-score ${z.toFixed(1)}).`,
64
+ message: `\`${r.method} ${r.route}\` P99 latency is abnormally high this hour (${fmt(r.avg_p99)} vs baseline ${fmt(mean)} — Z-score ${z.toFixed(1)}).`,
73
65
  data: { current_p99: r.avg_p99, baseline_p99: mean, z_score: z },
74
66
  });
75
67
  }
@@ -87,7 +79,7 @@ function detectDeadEndpoints(db) {
87
79
  severity: 'info',
88
80
  route: row.route,
89
81
  method: row.method,
90
- message: `\`${row.method} ${row.route}\` n'a reçu aucune requête depuis ${daysSince} jours. Candidat à la déprécation.`,
82
+ message: `\`${row.method} ${row.route}\` has received no requests in ${daysSince} days. Consider deprecating this endpoint.`,
91
83
  data: { last_seen_ts: row.last_seen, inactive_days: daysSince },
92
84
  };
93
85
  });
@@ -115,7 +107,7 @@ function detectReleaseRegressions(db) {
115
107
  severity: 'error',
116
108
  route: a.route,
117
109
  method: a.method,
118
- message: `La latence P90 de \`${a.method} ${a.route}\` a augmenté de ${pct(delta)} depuis le déploiement ${release_tag}. Avant : ${fmt(b.avg_p90)} — Après : ${fmt(a.avg_p90)}.`,
110
+ message: `\`${a.method} ${a.route}\` P90 increased by ${pct(delta)} after ${release_tag}. Before: ${fmt(b.avg_p90)} — After: ${fmt(a.avg_p90)}.`,
119
111
  data: {
120
112
  release: release_tag,
121
113
  before_p90: b.avg_p90,
@@ -129,7 +121,7 @@ function detectReleaseRegressions(db) {
129
121
  severity: 'success',
130
122
  route: a.route,
131
123
  method: a.method,
132
- message: `Le déploiement ${release_tag} a amélioré \`${a.method} ${a.route}\` de ${pct(-delta)}. Avant : ${fmt(b.avg_p90)} — Après : ${fmt(a.avg_p90)}.`,
124
+ message: `${release_tag} improved \`${a.method} ${a.route}\` by ${pct(-delta)}. Before: ${fmt(b.avg_p90)} — After: ${fmt(a.avg_p90)}.`,
133
125
  data: {
134
126
  release: release_tag,
135
127
  before_p90: b.avg_p90,
@@ -143,6 +135,53 @@ function detectReleaseRegressions(db) {
143
135
  return insights;
144
136
  }
145
137
 
138
+ function detectDrift(db) {
139
+ const rows = db.getDriftData();
140
+ if (rows.length === 0) return [];
141
+
142
+ // Group daily P90 samples by endpoint
143
+ const byEndpoint = new Map();
144
+ for (const row of rows) {
145
+ const key = `${row.method}|${row.route}`;
146
+ if (!byEndpoint.has(key)) byEndpoint.set(key, { method: row.method, route: row.route, points: [] });
147
+ byEndpoint.get(key).points.push({ x: row.day_bucket, y: row.p90 });
148
+ }
149
+
150
+ const insights = [];
151
+ for (const { method, route, points } of byEndpoint.values()) {
152
+ if (points.length < DRIFT_MIN_DAYS) continue;
153
+
154
+ // Ordinary least squares on (day_index, p90) pairs
155
+ const x0 = points[0].x;
156
+ const xs = points.map(p => p.x - x0);
157
+ const ys = points.map(p => p.y);
158
+ const n = xs.length;
159
+ const sumX = xs.reduce((a, b) => a + b, 0);
160
+ const sumY = ys.reduce((a, b) => a + b, 0);
161
+ const sumXY = xs.reduce((s, x, i) => s + x * ys[i], 0);
162
+ const sumX2 = xs.reduce((s, x) => s + x * x, 0);
163
+ const denom = n * sumX2 - sumX * sumX;
164
+ if (denom === 0) continue;
165
+
166
+ const slope = (n * sumXY - sumX * sumY) / denom;
167
+ if (slope < DRIFT_SLOPE_THRESHOLD) continue;
168
+
169
+ const observedDays = xs[xs.length - 1];
170
+ const projection30 = Math.round(slope * 30);
171
+
172
+ insights.push({
173
+ type: 'DRIFT',
174
+ severity: 'warning',
175
+ route,
176
+ method,
177
+ message: `\`${method} ${route}\` has been progressively degrading for ${observedDays} day${observedDays !== 1 ? 's' : ''}: +${slope.toFixed(1)}ms/day. 30-day projection: +${projection30}ms.`,
178
+ data: { slope_ms_per_day: slope, observed_days: observedDays, projection_30d_ms: projection30 },
179
+ });
180
+ }
181
+
182
+ return insights;
183
+ }
184
+
146
185
  function computeHealthScore(db) {
147
186
  try {
148
187
  const { recent, baseline, activeRoutes, totalRoutes } = db.getSummary();
package/src/ui.html CHANGED
@@ -1241,8 +1241,9 @@ function Insights({ setRoute, setParams }) {
1241
1241
 
1242
1242
  const types = [
1243
1243
  {id:'ALL',label:'All'},{id:'PERF',label:'Performance'},
1244
- {id:'ANOMALY',label:'Anomaly'},{id:'DEAD',label:'Dead'},
1245
- {id:'UNTRACKED',label:'Untracked'},{id:'OK',label:'OK'},
1244
+ {id:'DRIFT',label:'Drift'},{id:'ANOMALY',label:'Anomaly'},
1245
+ {id:'DEAD',label:'Dead'},{id:'UNTRACKED',label:'Untracked'},
1246
+ {id:'OK',label:'OK'},
1246
1247
  ];
1247
1248
  const filtered = INSIGHTS.filter(i =>
1248
1249
  (typeFilter === 'ALL' || i.type === typeFilter) &&