-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
579 lines (502 loc) · 19.5 KB
/
server.ts
File metadata and controls
579 lines (502 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import { fileURLToPath } from "url";
import { rateLimit } from "express-rate-limit";
import { z } from "zod";
import fs from "fs";
import { parse } from "csv-parse/sync";
// @ts-ignore
import LogisticRegression from "ml-logistic-regression";
// @ts-ignore
import { RandomForestClassifier } from "ml-random-forest";
import { Matrix } from "ml-matrix";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// --- Global State for Models ---
let trainedModels: {
logistic?: LogisticRegression;
randomForest?: RandomForestClassifier;
scaler?: { mean: number[]; std: number[] };
featureNames?: string[];
} = {};
let monitoringState = {
psi: 0.042,
klDivergence: 0.015,
status: 'Stable' as string
};
let threatState = {
level: 'Low',
integrity: 'Verified'
};
// --- Helper Functions ---
function preprocessData(data: any[], existingScaler?: { mean: number[]; std: number[] }) {
const target = "target";
// Map 'decision' to 'target' if it exists
data.forEach(row => {
if (row.decision && row.target === undefined) {
row.target = row.decision === 'Reject' ? 1 : 0;
}
});
// Automatically select numeric features as requested (Issue 1)
const firstRow = data[0];
const numericFeatures = Object.keys(firstRow).filter(key => {
const val = firstRow[key];
// Drop target and riskProbability (Issue 2)
if (key === 'target' || key === 'decision' || key === 'riskProbability') return false;
return !isNaN(parseFloat(val)) && isFinite(val) && typeof val !== 'boolean';
});
const X = data.map(row => numericFeatures.map(feat => parseFloat(row[feat])));
const y = data.map(row => parseInt(row[target]) || 0);
const numFeatures = numericFeatures.length;
let means = existingScaler?.mean || new Array(numFeatures).fill(0);
let stds = existingScaler?.std || new Array(numFeatures).fill(1);
if (!existingScaler) {
for (let j = 0; j < numFeatures; j++) {
const col = X.map(row => row[j]);
means[j] = col.reduce((a, b) => a + b, 0) / col.length;
stds[j] = Math.sqrt(col.reduce((a, b) => a + Math.pow(b - means[j], 2), 0) / col.length) || 1;
}
}
const scaledX = X.map(row => row.map((val, j) => (val - means[j]) / stds[j]));
return { X: scaledX, y, scaler: { mean: means, std: stds }, featureNames: numericFeatures };
}
// --- Security Configuration ---
// ... (rest of the security config)
// 1. Rate Limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
standardHeaders: true,
legacyHeaders: false,
message: { status: "error", message: "Too many requests, please try again later." }
});
// 2. API Key Middleware
const apiKeyMiddleware = (req: express.Request, res: express.Response, next: express.NextFunction) => {
const apiKey = req.headers['x-api-key'];
const validApiKey = process.env.API_KEY;
if (!apiKey || apiKey !== validApiKey) {
return res.status(401).json({ status: "error", message: "Unauthorized: Invalid or missing API key." });
}
next();
};
// 3. Input Validation Schemas
const PredictRiskSchema = z.object({
applicant: z.object({
id: z.string().optional(),
name: z.string().min(2),
income: z.number().positive(),
debtRatio: z.number().min(0).max(1),
creditScore: z.number().min(300).max(850),
loanAmount: z.number().positive()
}),
modelId: z.string()
});
const SecurityAttackSchema = z.object({
type: z.enum(['INCOME_INFLATION', 'DATA_POISONING', 'FEATURE_MASKING', 'CUSTOM'])
});
const TrainModelSchema = z.object({
architecture: z.string(),
epochs: z.number().int().positive().max(100),
learningRate: z.number().positive().max(1)
});
async function startServer() {
const app = express();
const PORT = 3001;
// Global Middleware
app.use(express.json());
// Ensure models and logs directories exist on startup
const modelsDir = path.join(__dirname, "models");
if (!fs.existsSync(modelsDir)) {
fs.mkdirSync(modelsDir, { recursive: true });
}
const logsDir = path.join(__dirname, "logs");
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}
// API Routes
app.use("/api", limiter); // Apply rate limiting only to API routes
app.get("/api/health", (req, res) => {
res.json({ status: "ok", timestamp: Date.now() });
});
// Apply API Key protection to sensitive endpoints
app.use("/api/system-info", apiKeyMiddleware);
app.use("/api/models", apiKeyMiddleware);
app.use("/api/predict-risk", apiKeyMiddleware);
app.use("/api/security-status", apiKeyMiddleware);
app.use("/api/security-attack", apiKeyMiddleware);
app.use("/api/train-model", apiKeyMiddleware);
app.use("/api/run-test", apiKeyMiddleware);
app.use("/api/monitoring-drift", apiKeyMiddleware);
app.use("/api/monitoring-performance", apiKeyMiddleware);
app.use("/api/forensics", apiKeyMiddleware);
app.use("/api/audit-logs", apiKeyMiddleware);
app.use("/api/reboot", apiKeyMiddleware);
app.get("/api/model-metrics", (req, res) => {
const metricsPath = path.join(__dirname, "models", "model_metrics.json");
if (fs.existsSync(metricsPath)) {
const metrics = JSON.parse(fs.readFileSync(metricsPath, "utf8"));
res.json(metrics);
} else {
res.json({
logistic_regression_accuracy: 0.9252,
random_forest_accuracy: 0.9418,
timestamp: new Date().toISOString()
});
}
});
app.get("/api/model-metadata", (req, res) => {
const metadataPath = path.join(__dirname, "models", "model_metadata.json");
if (fs.existsSync(metadataPath)) {
const metadata = JSON.parse(fs.readFileSync(metadataPath, "utf8"));
res.json(metadata);
} else {
res.json({
version: "1.0.0",
production_model: "random_forest",
trained_at: new Date().toISOString().split('T')[0]
});
}
});
app.get("/api/system-info", (req, res) => {
res.json({
nodeVersion: process.version,
platform: process.platform,
arch: process.arch,
uptime: process.uptime(),
memoryUsage: process.memoryUsage(),
env: process.env.NODE_ENV || 'development'
});
});
// Model API
app.get("/api/models", (req, res) => {
res.json({
status: "success",
data: [
{ id: 'm1', type: 'Logistic Regression', version: '1.0.0', status: 'Stable Baseline', role: 'Monitoring' },
{ id: 'm2', type: 'Random Forest', version: '1.0.1', status: 'Active', role: 'Production' }
]
});
});
// Alias for prediction as requested
app.post("/api/predict", (req, res) => {
// Forward to predict-risk
req.url = "/api/predict-risk";
app._router.handle(req, res, () => {});
});
app.post("/api/predict-risk", (req, res) => {
try {
const validatedData = PredictRiskSchema.parse(req.body);
const { applicant, modelId } = validatedData;
if (!trainedModels.scaler) {
return res.status(400).json({ status: "error", message: "Models not trained yet. Please run training first." });
}
// Preprocess input using the same feature order as training
const featureNames = trainedModels.featureNames || ['income', 'debtRatio', 'creditScore', 'loanAmount'];
const features = featureNames.map(feat => (applicant as any)[feat] ?? 0);
const scaledFeatures = features.map((val, i) => (val - trainedModels.scaler!.mean[i]) / trainedModels.scaler!.std[i]);
let riskProbability = 0.5;
if (modelId === 'm1' && trainedModels.logistic) {
// LogisticRegression from ml-logistic-regression only has predict() returning 0 or 1
// We'll use predict() and map it to a probability for the UI
const prediction = trainedModels.logistic.predict(new Matrix([scaledFeatures]))[0];
riskProbability = prediction === 1 ? 0.9 : 0.1;
} else if (modelId === 'm2' && trainedModels.randomForest) {
// RandomForestClassifier.predictProbability(matrix, label) returns an array of probabilities for THAT label
// label 1 is Reject
const probabilities = (trainedModels.randomForest as any).predictProbability([scaledFeatures], 1);
riskProbability = probabilities[0];
} else {
// Fallback to simulation if specific model not trained
riskProbability = Math.random();
}
const predictionResult = {
timestamp: new Date().toISOString(),
income: applicant.income,
creditScore: applicant.creditScore,
prediction: riskProbability < 0.5 ? 'Approve' : 'Reject',
probability: riskProbability,
modelId
};
// Log prediction
try {
const logsPath = path.join(__dirname, "logs", "predictions.json");
let logs: any[] = [];
if (fs.existsSync(logsPath)) {
const content = fs.readFileSync(logsPath, "utf8");
logs = JSON.parse(content || "[]");
}
logs.push(predictionResult);
fs.writeFileSync(logsPath, JSON.stringify(logs, null, 2));
} catch (err) {
console.error("Failed to log prediction", err);
}
res.json({
riskProbability,
decision: predictionResult.prediction,
modelId
});
} catch (error) {
if (error instanceof z.ZodError) {
return res.status(400).json({ status: "error", message: "Validation failed", errors: (error as any).errors });
}
console.error("Prediction failed", error);
res.status(500).json({ status: "error", message: "Internal server error" });
}
});
// Security API
app.get("/api/security-status", (req, res) => {
res.json({
threatLevel: threatState.level,
integrity: threatState.integrity,
lastScan: Date.now()
});
});
app.post("/api/security-attack", (req, res) => {
try {
const { type } = SecurityAttackSchema.parse(req.body);
// Update monitoring and security state based on attack type
if (type === 'INCOME_INFLATION') {
monitoringState.psi = 0.285;
monitoringState.status = 'Critical Drift';
threatState.level = 'Critical';
threatState.integrity = 'Compromised';
} else if (type === 'DATA_POISONING') {
monitoringState.psi = 0.154;
monitoringState.status = 'Warning';
threatState.level = 'Medium';
} else if (type === 'FEATURE_MASKING') {
monitoringState.psi = 0.095;
monitoringState.status = 'Suspicious';
threatState.level = 'Medium';
} else if (type === 'CUSTOM') {
monitoringState.psi = 0.420;
monitoringState.status = 'Attacked';
threatState.level = 'High';
}
res.json({
status: "alert",
message: `Simulated ${type} attack detected and mitigated.`,
timestamp: Date.now(),
newPsi: monitoringState.psi
});
} catch (error) {
if (error instanceof z.ZodError) {
return res.status(400).json({ status: "error", message: "Validation failed", errors: (error as any).errors });
}
res.status(500).json({ status: "error", message: "Internal server error" });
}
});
app.post("/api/run-test", (req, res) => {
try {
const { modelId } = req.body;
if (!trainedModels.scaler) {
return res.status(400).json({ status: "error", message: "Models not trained yet." });
}
// Load dataset
const csvData = fs.readFileSync(path.join(__dirname, "dataset.csv"), "utf8");
const records = parse(csvData, { columns: true, skip_empty_lines: true });
// Use last 200 records as test set
const testRecords = records.slice(-200);
const { X, y } = preprocessData(testRecords, trainedModels.scaler);
let correct = 0;
const predictions: any[] = [];
for (let i = 0; i < X.length; i++) {
let prob = 0.5;
if (modelId === 'm1' && trainedModels.logistic) {
const prediction = (trainedModels.logistic as any).predict(new Matrix([X[i]]))[0];
prob = prediction === 1 ? 0.9 : 0.1;
} else if (modelId === 'm2' && trainedModels.randomForest) {
const probabilities = (trainedModels.randomForest as any).predictProbability([X[i]], 1);
prob = probabilities[0];
}
const pred = prob < 0.5 ? 0 : 1;
if (pred === y[i]) correct++;
predictions.push({
actual: y[i] === 1 ? 'Approve' : 'Reject',
predicted: pred === 1 ? 'Approve' : 'Reject',
probability: prob
});
}
const calculateMetrics = (actual: number[], predicted: number[]) => {
let tp = 0, tn = 0, fp = 0, fn = 0;
for (let i = 0; i < actual.length; i++) {
if (actual[i] === 1 && predicted[i] === 1) tp++;
else if (actual[i] === 0 && predicted[i] === 0) tn++;
else if (actual[i] === 0 && predicted[i] === 1) fp++;
else if (actual[i] === 1 && predicted[i] === 0) fn++;
}
const accuracy = (tp + tn) / actual.length;
const precision = tp + fp > 0 ? tp / (tp + fp) : 0;
const recall = tp + fn > 0 ? tp / (tp + fn) : 0;
const f1 = precision + recall > 0 ? 2 * (precision * recall) / (precision + recall) : 0;
return { accuracy, precision, recall, f1 };
};
const metrics = calculateMetrics(y, predictions);
res.json({
status: "success",
metrics: {
accuracy: metrics.accuracy,
precision: metrics.precision,
recall: metrics.recall,
f1: metrics.f1,
sampleSize: X.length
},
results: predictions.slice(0, 10) // Return first 10 for UI display
});
} catch (error) {
console.error("Test execution failed", error);
res.status(500).json({ status: "error", message: "Internal server error" });
}
});
app.post("/api/train-model", (req, res) => {
try {
console.log(`[Decision DNA] Training request received:`, req.body);
const config = TrainModelSchema.parse(req.body);
// Load dataset
const csvData = fs.readFileSync(path.join(__dirname, "dataset.csv"), "utf8");
const records = parse(csvData, { columns: true, skip_empty_lines: true });
const { X, y, scaler, featureNames } = preprocessData(records);
// Split data (80/20)
const splitIdx = Math.floor(X.length * 0.8);
const trainX = X.slice(0, splitIdx);
const trainY = y.slice(0, splitIdx);
const testX = X.slice(splitIdx);
const testY = y.slice(splitIdx);
// Train Logistic Regression
const logModel = new LogisticRegression({
numSteps: config.epochs * 10, // Scale epochs as ml-logistic-regression expects thousands
learningRate: config.learningRate
});
logModel.train(new Matrix(trainX), Matrix.columnVector(trainY));
// Train Random Forest
const rfModel = new RandomForestClassifier({
nEstimators: 50,
// Random Forest in this lib doesn't use epochs/learningRate in the same way
});
rfModel.train(trainX, trainY);
// Calculate real metrics on validation set
const calculateMetrics = (actual: number[], predicted: number[]) => {
let tp = 0, tn = 0, fp = 0, fn = 0;
for (let i = 0; i < actual.length; i++) {
if (actual[i] === 1 && predicted[i] === 1) tp++;
else if (actual[i] === 0 && predicted[i] === 0) tn++;
else if (actual[i] === 0 && predicted[i] === 1) fp++;
else if (actual[i] === 1 && predicted[i] === 0) fn++;
}
const accuracy = (tp + tn) / actual.length;
const precision = tp + fp > 0 ? tp / (tp + fp) : 0;
const recall = tp + fn > 0 ? tp / (tp + fn) : 0;
const f1 = precision + recall > 0 ? 2 * (precision * recall) / (precision + recall) : 0;
return { accuracy, precision, recall, f1 };
};
const logPreds = [];
const rfPreds = [];
for (let i = 0; i < testX.length; i++) {
const logPred = (logModel as any).predict(new Matrix([testX[i]]))[0];
logPreds.push(logPred);
const rfProbabilities = (rfModel as any).predictProbability([testX[i]], 1);
const rfProb = rfProbabilities[0];
rfPreds.push(rfProb < 0.5 ? 0 : 1);
}
const logMetrics = calculateMetrics(testY, logPreds);
const rfMetrics = calculateMetrics(testY, rfPreds);
// Save metrics to file as requested (Issue 3)
const metricsPath = path.join(__dirname, "models", "model_metrics.json");
const metricsData = {
logistic_regression_accuracy: logMetrics.accuracy,
random_forest_accuracy: rfMetrics.accuracy,
timestamp: new Date().toISOString()
};
try {
if (!fs.existsSync(path.join(__dirname, "models"))) {
fs.mkdirSync(path.join(__dirname, "models"), { recursive: true });
}
fs.writeFileSync(metricsPath, JSON.stringify(metricsData, null, 4));
} catch (err) {
console.error("Failed to save metrics", err);
}
// Update global state
trainedModels = {
logistic: logModel,
randomForest: rfModel,
scaler: scaler,
featureNames: featureNames
};
res.json({
status: "success",
message: "Models trained successfully on dataset.csv",
jobId: Math.random().toString(36).substring(7),
metrics: {
logistic: logMetrics,
rf: rfMetrics
}
});
} catch (error) {
if (error instanceof z.ZodError) {
return res.status(400).json({ status: "error", message: "Validation failed", errors: (error as any).errors });
}
console.error("Training failed", error);
res.status(500).json({ status: "error", message: "Internal server error" });
}
});
// Monitoring API
app.get("/api/monitoring-drift", (req, res) => {
res.json({
...monitoringState,
timestamp: Date.now()
});
});
app.post("/api/reboot", (req, res) => {
monitoringState = {
psi: 0.042,
klDivergence: 0.015,
status: 'Stable'
};
threatState = {
level: 'Low',
integrity: 'Verified'
};
console.log("[Decision DNA] System rebooted - states reset to baseline.");
res.json({ status: "success", message: "System rebooted. Monitoring and security states reset to baseline." });
});
app.get("/api/monitoring-performance", (req, res) => {
res.json({
latency: "12ms",
throughput: "450 req/s",
errorRate: "0.01%",
uptime: "99.99%"
});
});
app.get("/api/forensics", (req, res) => {
res.json([
{ id: 'f1', type: 'Adversarial', severity: 'Low', details: 'Unusual feature perturbation detected in sample #452' },
{ id: 'f2', type: 'Integrity', severity: 'None', details: 'Model hash verified successfully' }
]);
});
app.get("/api/audit-logs", (req, res) => {
const limit = parseInt(req.query.limit as string) || 50;
res.json({
count: 0,
logs: [] // In a real app, fetch from DB
});
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
// Serve static files in production
app.use(express.static(path.join(__dirname, "dist")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`[Decision DNA] Server running on http://localhost:${PORT}`);
});
}
startServer();