Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 18x 18x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 1x 1x 3x 6x 1x 5x 5x 5x 11x 11x 8x 8x 5x 11x 11x 5x 5x 5x 5x 3x 2x 1x 11x 5x 2x 2x 1x 1x 1x 1x 1x 1x 8x 4x 1x 9x 3x 9x 9x 9x 9x 3x 3x 2x 1x 3x 3x 3x 3x 3x 3x 3x 1x 3x 1x 3x 1x | /**
* Risk Classification Service
* Classifies detections into Green/Yellow/Orange/Red risk levels
*/
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.simple(),
transports: [new winston.transports.Console()]
});
// Risk classification rules by object type and anomaly
const RISK_RULES = {
atm: {
// Critical components - any modification is high risk
skimmer: { level: 'red', reason: 'Skimmer device detected', action: 'immediate_alert' },
extra_device: { level: 'red', reason: 'Unauthorized device attached', action: 'immediate_alert' },
pin_pad_modified: { level: 'red', reason: 'PIN pad may be compromised', action: 'immediate_alert' },
card_reader_overlay: { level: 'red', reason: 'Card reader overlay detected', action: 'immediate_alert' },
// High risk
camera_blocked: { level: 'orange', reason: 'Security camera blocked', action: 'urgent_inspection' },
cash_dispenser_modified: { level: 'orange', reason: 'Cash dispenser modified', action: 'urgent_inspection' },
// Medium risk
display_changed: { level: 'yellow', reason: 'Display appearance changed', action: 'verify_maintenance' },
receipt_printer_changed: { level: 'yellow', reason: 'Receipt printer modified', action: 'verify_maintenance' },
// Low risk
speaker_changed: { level: 'green', reason: 'Speaker modified', action: 'log_only' },
cosmetic_change: { level: 'green', reason: 'Cosmetic change detected', action: 'log_only' }
},
charging_station: {
cable_cut: { level: 'red', reason: 'Charging cable severed', action: 'immediate_alert' },
connector_damaged: { level: 'orange', reason: 'Connector damaged', action: 'urgent_inspection' },
payment_terminal_changed: { level: 'orange', reason: 'Payment terminal modified', action: 'urgent_inspection' },
display_changed: { level: 'yellow', reason: 'Display changed', action: 'verify_maintenance' },
status_lights_off: { level: 'yellow', reason: 'Status lights not functioning', action: 'verify_maintenance' }
},
parking_meter: {
coin_slot_blocked: { level: 'orange', reason: 'Coin slot blocked', action: 'urgent_inspection' },
card_reader_modified: { level: 'red', reason: 'Card reader modified', action: 'immediate_alert' },
display_changed: { level: 'yellow', reason: 'Display changed', action: 'verify_maintenance' },
keypad_changed: { level: 'orange', reason: 'Keypad modified', action: 'urgent_inspection' }
},
defibrillator: {
cabinet_open: { level: 'red', reason: 'Cabinet is open/unlocked', action: 'immediate_alert' },
cabinet_damaged: { level: 'orange', reason: 'Cabinet damaged', action: 'urgent_inspection' },
status_indicator_off: { level: 'orange', reason: 'Status indicator off', action: 'urgent_inspection' },
access_handle_broken: { level: 'orange', reason: 'Access handle broken', action: 'urgent_inspection' },
instructions_removed: { level: 'yellow', reason: 'Instructions missing', action: 'verify_maintenance' }
}
};
// Risk level weights for aggregation
const RISK_WEIGHTS = {
green: 0,
yellow: 1,
orange: 2,
red: 3
};
class RiskClassifierService {
constructor(options = {}) {
this.customRules = options.customRules || {};
this.autoEscalate = options.autoEscalate !== false;
}
/**
* Classify a single detection
*/
classify(detection, objectType = 'atm') {
const rules = RISK_RULES[objectType] || {};
const customRules = this.customRules[objectType] || {};
// Get anomaly type
const anomalyType = detection.anomalyType || detection.componentType;
// Check custom rules first
let classification = customRules[anomalyType];
// Fall back to default rules
Eif (!classification) {
classification = rules[anomalyType];
}
// Default if no rule found
Iif (!classification) {
classification = {
level: 'yellow',
reason: `Unknown anomaly: ${anomalyType}`,
action: 'manual_review'
};
}
// Adjust based on confidence
const adjustedLevel = this.adjustForConfidence(
classification.level,
detection.confidence || detection.anomalyConfidence || 0.5
);
return {
...classification,
level: adjustedLevel,
originalLevel: classification.level,
confidence: detection.confidence || detection.anomalyConfidence,
component: anomalyType,
timestamp: new Date().toISOString()
};
}
/**
* Adjust risk level based on confidence
*/
adjustForConfidence(level, confidence) {
// High confidence anomalies escalate
if (confidence > 0.9 && level !== 'red') {
const escalation = {
green: 'yellow',
yellow: 'orange',
orange: 'red'
};
return escalation[level] || level;
}
// Low confidence anomalies de-escalate
if (confidence < 0.5 && level !== 'green') {
const deescalation = {
red: 'orange',
orange: 'yellow',
yellow: 'green'
};
return deescalation[level] || level;
}
return level;
}
/**
* Aggregate multiple classifications into overall risk
*/
aggregate(classifications) {
if (!classifications || classifications.length === 0) {
return {
level: 'green',
reason: 'No anomalies detected',
action: 'continue_monitoring'
};
}
// Find highest risk
let maxWeight = -1;
let highestRisk = null;
for (const classification of classifications) {
const weight = RISK_WEIGHTS[classification.level] || 0;
if (weight > maxWeight) {
maxWeight = weight;
highestRisk = classification;
}
}
// Count by level
const counts = classifications.reduce((acc, c) => {
acc[c.level] = (acc[c.level] || 0) + 1;
return acc;
}, {});
// Determine overall action
const action = this.determineAction(highestRisk.level, counts);
return {
level: highestRisk.level,
reason: highestRisk.reason,
action: action,
details: classifications,
counts: counts,
timestamp: new Date().toISOString()
};
}
/**
* Determine action based on highest risk and counts
*/
determineAction(highestLevel, counts) {
const rules = [
{ condition: () => highestLevel === 'red', action: 'immediate_alert' },
{ condition: () => highestLevel === 'orange', action: 'urgent_inspection' },
{ condition: () => (counts.yellow || 0) >= 3, action: 'scheduled_inspection' },
{ condition: () => highestLevel === 'yellow', action: 'verify_maintenance' },
{ condition: () => true, action: 'continue_monitoring' }
];
const matched = rules.find(r => r.condition());
return matched ? matched.action : 'manual_review';
}
/**
* Process full observation with detections
*/
processObservation(observation, detections, objectType = 'atm') {
// Separate normal and anomaly detections
const anomalies = detections.filter(d => d.isAnomaly || this.isAnomalous(d));
if (anomalies.length === 0) {
return {
level: 'green',
reason: 'No anomalies detected',
action: 'continue_monitoring',
details: []
};
}
// Classify each anomaly
const classifications = anomalies.map(d => this.classify(d, objectType));
// Aggregate
const result = this.aggregate(classifications);
// Add observation metadata
return {
...result,
observationId: observation.id,
objectId: observation.objectId,
timestamp: new Date().toISOString()
};
}
/**
* Check if detection is anomalous based on rules
*/
isAnomalous(detection) {
// Known anomaly types
const anomalyTypes = [
'skimmer', 'extra_device', 'overlay', 'modified',
'blocked', 'damaged', 'missing', 'new_object'
];
return anomalyTypes.some(type =>
detection.componentType?.includes(type) ||
detection.anomalyType?.includes(type)
);
}
/**
* Get risk history trend
*/
calculateTrend(historicalRisks) {
if (!historicalRisks || historicalRisks.length < 2) {
return { trend: 'stable', confidence: 0 };
}
// Convert to weights
const weights = historicalRisks.map(r => RISK_WEIGHTS[r.level] || 0);
// Calculate trend (simple linear regression)
const n = weights.length;
const sumX = weights.reduce((a, b, i) => a + i, 0);
const sumY = weights.reduce((a, b) => a + b, 0);
const sumXY = weights.reduce((a, b, i) => a + i * b, 0);
const sumXX = weights.reduce((a, b, i) => a + i * i, 0);
const slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
let trend;
if (slope > 0.1) trend = 'increasing';
else if (slope < -0.1) trend = 'decreasing';
else trend = 'stable';
return {
trend,
slope,
confidence: Math.min(Math.abs(slope) * 10, 1),
averageRisk: sumY / n
};
}
/**
* Validate custom rules
*/
validateRules(rules) {
const validLevels = ['green', 'yellow', 'orange', 'red'];
const validActions = [
'immediate_alert', 'urgent_inspection', 'scheduled_inspection',
'verify_maintenance', 'continue_monitoring', 'manual_review', 'log_only'
];
const errors = [];
for (const [objectType, objectRules] of Object.entries(rules)) {
for (const [anomalyType, rule] of Object.entries(objectRules)) {
if (!validLevels.includes(rule.level)) {
errors.push(`Invalid level '${rule.level}' for ${objectType}.${anomalyType}`);
}
if (!validActions.includes(rule.action)) {
errors.push(`Invalid action '${rule.action}' for ${objectType}.${anomalyType}`);
}
}
}
return {
valid: errors.length === 0,
errors
};
}
}
module.exports = { RiskClassifierService, RISK_RULES };
|