File size: 4,830 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Cognitive Error Intelligence API Routes
 * ========================================
 * Advanced AI-powered error handling endpoints
 */

import express from 'express';
import { cognitiveErrorIntelligence } from '../services/CognitiveErrorIntelligence.js';

const router = express.Router();

// ═══════════════════════════════════════════════════════════════════════════
// INTELLIGENT ERROR PROCESSING
// ═══════════════════════════════════════════════════════════════════════════

/**
 * POST /api/intelligence/process
 * Process an error with full cognitive analysis
 */
router.post('/process', async (req, res) => {
  try {
    const { error, service, context, stackTrace } = req.body;

    if (!error || typeof error !== 'string') {
      return res.status(400).json({ error: 'error message is required' });
    }

    const result = await cognitiveErrorIntelligence.processError(
      error,
      service || 'unknown',
      context || {},
      stackTrace
    );

    res.json({
      success: true,
      ...result,
      solutionCount: result.solutions.length,
      correlationCount: result.correlatedErrors.length,
      hasAutoRemediation: result.autoRemediation?.queued || false
    });
  } catch (error) {
    console.error('Error processing with CEI:', error);
    res.status(500).json({ error: 'Failed to process error' });
  }
});

/**
 * GET /api/intelligence/stats
 * Get CEI system statistics
 */
router.get('/stats', (_req, res) => {
  try {
    const stats = cognitiveErrorIntelligence.getStats();
    res.json(stats);
  } catch (error) {
    console.error('Error getting CEI stats:', error);
    res.status(500).json({ error: 'Failed to get stats' });
  }
});

/**
 * GET /api/intelligence/correlations
 * Get learned error correlations
 */
router.get('/correlations', (_req, res) => {
  try {
    const correlations = cognitiveErrorIntelligence.getCorrelations();
    res.json({
      total: correlations.length,
      correlations: correlations.slice(0, 50) // Limit to top 50
    });
  } catch (error) {
    console.error('Error getting correlations:', error);
    res.status(500).json({ error: 'Failed to get correlations' });
  }
});

/**
 * POST /api/intelligence/metric
 * Record a metric for predictive analysis
 */
router.post('/metric', (req, res) => {
  try {
    const { metric, value } = req.body;

    if (!metric || typeof value !== 'number') {
      return res.status(400).json({ error: 'metric and numeric value required' });
    }

    cognitiveErrorIntelligence.recordMetric(metric, value);
    res.json({ success: true, metric, value });
  } catch (error) {
    console.error('Error recording metric:', error);
    res.status(500).json({ error: 'Failed to record metric' });
  }
});

/**
 * POST /api/intelligence/persist-correlations
 * Persist learned correlations to Neo4j
 */
router.post('/persist-correlations', async (_req, res) => {
  try {
    const count = await cognitiveErrorIntelligence.persistCorrelationsToNeo4j();
    res.json({
      success: true,
      persisted: count,
      message: `Persisted ${count} correlations to Neo4j`
    });
  } catch (error) {
    console.error('Error persisting correlations:', error);
    res.status(500).json({ error: 'Failed to persist correlations' });
  }
});

/**
 * POST /api/intelligence/approve-remediation/:actionId
 * Approve a pending remediation action
 */
router.post('/approve-remediation/:actionId', (req, res) => {
  try {
    const { actionId } = req.params;
    const approved = cognitiveErrorIntelligence.approveRemediation(actionId);

    if (approved) {
      res.json({ success: true, message: `Remediation ${actionId} approved` });
    } else {
      res.status(404).json({ error: 'Remediation action not found' });
    }
  } catch (error) {
    console.error('Error approving remediation:', error);
    res.status(500).json({ error: 'Failed to approve remediation' });
  }
});

/**
 * POST /api/intelligence/context
 * Update system context for better recommendations
 */
router.post('/context', (req, res) => {
  try {
    const { load, services } = req.body;

    const updates: any = {};
    if (load) updates.load = load;
    if (services) {
      updates.services = new Map(Object.entries(services));
    }

    cognitiveErrorIntelligence.updateSystemContext(updates);
    res.json({ success: true, message: 'Context updated' });
  } catch (error) {
    console.error('Error updating context:', error);
    res.status(500).json({ error: 'Failed to update context' });
  }
});

export default router;