Repository navigation
Expand file tree
/
Copy pathexample-dashboard.js
More file actions
375 lines (323 loc) · 12.6 KB
/
Copy pathexample-dashboard.js
File metadata and controls
375 lines (323 loc) · 12.6 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
/**
* Complete Dashboard Example for TESAIoT Analytics API (JavaScript)
*
* This example demonstrates building a comprehensive IoT analytics
* dashboard that combines all four analytics domains.
*
* Author: TESAIoT Team
* License: Apache 2.0
*/
import { AnalyticsClient, TimeRange } from './analytics-client.js';
/**
* Collect all dashboard data in parallel
*/
async function collectDashboardData(client) {
const [
anomalies,
timeline,
clusters,
insights,
connectivity,
latency,
throughput,
quality
] = await Promise.all([
client.getAnomalies({ limit: 100, severityFilter: ['critical', 'high', 'medium'] }),
client.getAnomalyTimeline({ days: 30 }),
client.getClusters({ metricName: 'temperature', nClusters: 5 }),
client.getInsights({ days: 7 }),
client.getConnectivityStatus(),
client.getLatencyStats({ hours: 24 }),
client.getThroughputStats({ hours: 24 }),
client.getConnectionQuality()
]);
const alerts = generateAlerts(anomalies, connectivity, latency, quality);
const fleetHealth = calculateFleetHealth(anomalies, connectivity, insights, latency);
return {
timestamp: new Date().toISOString(),
fleetHealth,
anomalies: {
summary: anomalies.summary || {},
recent: (anomalies.anomalies || []).slice(0, 10),
timeline: timeline.timeline || [],
trend: timeline.trend || {}
},
clusters: {
clusters: clusters.clusters || [],
silhouetteScore: clusters.silhouette_score || 0,
outliers: (clusters.outliers || []).slice(0, 5)
},
insights: insights.insights || [],
connectivity: {
status: connectivity.summary || {},
devices: (connectivity.devices || []).slice(0, 20),
latency: latency.summary || {},
throughput: throughput.summary || {},
quality: quality.summary || {}
},
alerts
};
}
/**
* Calculate overall fleet health score
*/
function calculateFleetHealth(anomalies, connectivity, insights, latency) {
const scores = {};
// Anomaly score
const anomalySummary = anomalies.summary || {};
const totalDevices = connectivity.summary?.total || 100;
const anomalyCount = anomalySummary.total || 0;
const anomalyRate = anomalyCount / Math.max(totalDevices, 1);
scores.anomaly = Math.max(0, 100 - (anomalyRate * 1000));
// Connectivity score
const connSummary = connectivity.summary || {};
const online = connSummary.online || 0;
const total = connSummary.total || 1;
scores.connectivity = (online / Math.max(total, 1)) * 100;
// Latency score
const p95Latency = latency.summary?.p95_latency_ms || 0;
scores.latency = Math.max(0, 100 - (p95Latency / 10));
// Insights severity score
const insightsList = insights.insights || [];
const criticalCount = insightsList.filter(i => i.severity === 'critical').length;
const warningCount = insightsList.filter(i => i.severity === 'warning').length;
scores.insights = Math.max(0, 100 - (criticalCount * 20) - (warningCount * 5));
// Calculate weighted overall score
const overall =
scores.anomaly * 0.3 +
scores.connectivity * 0.3 +
scores.latency * 0.2 +
scores.insights * 0.2;
return {
overallScore: Math.round(overall * 10) / 10,
componentScores: scores,
status: getHealthStatus(overall)
};
}
/**
* Convert health score to status label
*/
function getHealthStatus(score) {
if (score >= 90) return 'EXCELLENT';
if (score >= 70) return 'GOOD';
if (score >= 50) return 'FAIR';
if (score >= 30) return 'POOR';
return 'CRITICAL';
}
/**
* Generate alerts based on current data
*/
function generateAlerts(anomalies, connectivity, latency, quality) {
const alerts = [];
// Critical anomaly alerts
const criticalCount = anomalies.summary?.by_severity?.critical || 0;
if (criticalCount > 0) {
alerts.push({
level: 'critical',
type: 'anomaly',
title: `${criticalCount} Critical Anomalies Detected`,
description: 'Immediate investigation recommended'
});
}
// Offline device alerts
const offline = connectivity.summary?.offline || 0;
const total = connectivity.summary?.total || 1;
const offlinePct = (offline / Math.max(total, 1)) * 100;
if (offlinePct > 20) {
alerts.push({
level: 'critical',
type: 'connectivity',
title: `${offline} Devices Offline (${offlinePct.toFixed(0)}%)`,
description: 'Network connectivity issue detected'
});
} else if (offline > 0) {
alerts.push({
level: 'warning',
type: 'connectivity',
title: `${offline} Device(s) Offline`,
description: 'Some devices are not responding'
});
}
// Latency alerts
const p95 = latency.summary?.p95_latency_ms || 0;
if (p95 > 1000) {
alerts.push({
level: 'critical',
type: 'latency',
title: `High Latency: ${p95.toFixed(0)}ms P95`,
description: 'Network performance severely degraded'
});
} else if (p95 > 500) {
alerts.push({
level: 'warning',
type: 'latency',
title: `Elevated Latency: ${p95.toFixed(0)}ms P95`,
description: 'Network performance degraded'
});
}
// Quality alerts
const poorDevices = quality.summary?.distribution?.poor || 0;
if (poorDevices > 5) {
alerts.push({
level: 'warning',
type: 'quality',
title: `${poorDevices} Devices with Poor Connection Quality`,
description: 'Review device connections and network path'
});
}
return alerts;
}
/**
* Render dashboard to console
*/
function renderDashboard(data) {
console.log('\n' + '='.repeat(80));
console.log(' '.repeat(25) + 'TESAIoT ANALYTICS DASHBOARD');
console.log('='.repeat(80));
console.log(`Generated: ${data.timestamp}`);
// Fleet Health
console.log('\n' + '-'.repeat(80));
console.log(' FLEET HEALTH');
console.log('-'.repeat(80));
const { overallScore, status, componentScores } = data.fleetHealth;
const barLen = Math.floor(overallScore / 5);
const bar = '\u2588'.repeat(barLen) + '\u2591'.repeat(20 - barLen);
console.log(`\n Overall: [${bar}] ${overallScore}/100 (${status})`);
console.log('\n Component Scores:');
for (const [component, score] of Object.entries(componentScores)) {
console.log(` ${component.padEnd(15)} ${Math.round(score)}/100`);
}
// Alerts
if (data.alerts.length > 0) {
console.log('\n' + '-'.repeat(80));
console.log(' ACTIVE ALERTS');
console.log('-'.repeat(80));
for (const alert of data.alerts) {
const icon = alert.level === 'critical' ? '\u2757' : '\u26A0';
console.log(`\n [${icon}] ${alert.title}`);
console.log(` ${alert.description}`);
}
}
// Anomalies
console.log('\n' + '-'.repeat(80));
console.log(' ANOMALY DETECTION');
console.log('-'.repeat(80));
const anomSummary = data.anomalies.summary;
console.log(`\n Total Anomalies (7 days): ${anomSummary.total || 0}`);
if (anomSummary.by_severity) {
console.log(' By Severity:');
for (const [sev, count] of Object.entries(anomSummary.by_severity)) {
console.log(` ${sev.padEnd(10)} ${count}`);
}
}
const trend = data.anomalies.trend;
const trendIcon = trend.direction === 'decreasing' ? '\u2193' :
trend.direction === 'increasing' ? '\u2191' : '\u2194';
console.log(`\n Trend: ${trendIcon} ${trend.direction || 'stable'} (${(trend.percent_change || 0).toFixed(1)}%)`);
// Clusters
console.log('\n' + '-'.repeat(80));
console.log(' PATTERN RECOGNITION');
console.log('-'.repeat(80));
console.log(`\n Clusters: ${data.clusters.clusters.length}`);
console.log(` Silhouette Score: ${(data.clusters.silhouetteScore || 0).toFixed(3)}`);
for (const cluster of data.clusters.clusters) {
console.log(`\n Cluster ${cluster.cluster_id}: ${cluster.device_count} devices`);
}
if (data.clusters.outliers.length > 0) {
console.log(`\n Outliers: ${data.clusters.outliers.length} devices`);
}
// Insights
console.log('\n' + '-'.repeat(80));
console.log(' AI INSIGHTS');
console.log('-'.repeat(80));
console.log(`\n Total Insights: ${data.insights.length}`);
const criticalInsights = data.insights.filter(i => i.severity === 'critical');
if (criticalInsights.length > 0) {
console.log('\n Critical Insights:');
for (const i of criticalInsights.slice(0, 3)) {
console.log(` \u2757 ${i.title}`);
}
}
const actionable = data.insights.filter(i => i.actionable);
if (actionable.length > 0) {
console.log(`\n Actionable Recommendations (${actionable.length}):`);
for (const i of actionable.slice(0, 3)) {
console.log(` - ${i.title}`);
}
}
// Connectivity
console.log('\n' + '-'.repeat(80));
console.log(' CONNECTIVITY');
console.log('-'.repeat(80));
const connStatus = data.connectivity.status;
console.log(`\n Devices: ${connStatus.online || 0}/${connStatus.total || 0} online`);
const latencyStats = data.connectivity.latency;
console.log('\n Latency:');
console.log(` Average: ${(latencyStats.avg_latency_ms || 0).toFixed(1)} ms`);
console.log(` P95: ${(latencyStats.p95_latency_ms || 0).toFixed(1)} ms`);
const throughputStats = data.connectivity.throughput;
console.log('\n Throughput (24h):');
console.log(` Total Messages: ${(throughputStats.total_messages || 0).toLocaleString()}`);
console.log(` Avg/Hour: ${(throughputStats.avg_per_hour || 0).toLocaleString()}`);
const qualityStats = data.connectivity.quality;
console.log(`\n Connection Quality: ${(qualityStats.average_quality_score || 0).toFixed(0)}/100`);
// Footer
console.log('\n' + '='.repeat(80));
console.log(' '.repeat(30) + 'END OF REPORT');
console.log('='.repeat(80));
}
/**
* Dashboard refresh loop
*/
async function dashboardRefreshLoop(client, intervalSeconds = 60, maxIterations = 5) {
console.log('Starting dashboard refresh loop...');
console.log(`Refresh interval: ${intervalSeconds} seconds`);
console.log('Press Ctrl+C to stop\n');
for (let i = 0; i < maxIterations; i++) {
try {
const data = await collectDashboardData(client);
console.log('\n'.repeat(2));
renderDashboard(data);
console.log(`\n[Refresh ${i + 1}/${maxIterations} - Next refresh in ${intervalSeconds} seconds]`);
await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
} catch (error) {
console.error(`Error refreshing dashboard: ${error.message}`);
await new Promise(resolve => setTimeout(resolve, 10000));
}
}
console.log('\nDashboard loop completed.');
}
/**
* Export dashboard data to JSON
*/
async function exportDashboardJson(client, outputFile = 'dashboard_data.json') {
console.log(`Exporting dashboard data to ${outputFile}...`);
const data = await collectDashboardData(client);
const fs = await import('fs');
fs.writeFileSync(outputFile, JSON.stringify(data, null, 2));
console.log(`Dashboard data exported to ${outputFile}`);
}
// Main entry point
async function main() {
const client = new AnalyticsClient();
const args = process.argv.slice(2);
if (args.includes('--loop')) {
const interval = parseInt(args[args.indexOf('--loop') + 1]) || 60;
await dashboardRefreshLoop(client, interval);
} else if (args.includes('--export')) {
const output = args[args.indexOf('--export') + 1] || 'dashboard_data.json';
await exportDashboardJson(client, output);
} else if (args.includes('--help')) {
console.log('TESAIoT Analytics Dashboard');
console.log('\nUsage:');
console.log(' node example-dashboard.js # Single refresh');
console.log(' node example-dashboard.js --loop # Continuous refresh (60s)');
console.log(' node example-dashboard.js --loop 30 # Custom interval');
console.log(' node example-dashboard.js --export # Export to JSON');
} else {
console.log('Collecting dashboard data...');
const data = await collectDashboardData(client);
renderDashboard(data);
}
}
main().catch(console.error);