-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathanalytics.js
More file actions
471 lines (410 loc) · 13.4 KB
/
analytics.js
File metadata and controls
471 lines (410 loc) · 13.4 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
// analytics.js
// Handles analytics visualizations- speed and road quality
let analyticsCharts = {};
let analyticsData = null;
// Initialize analytics panel
function initAnalytics() {
// Panel toggle
const panelHandle = document.getElementById('panelHandle');
const panel = document.getElementById('analyticsPanel');
const toggleIcon = document.getElementById('panelToggleIcon');
const toggleText = document.getElementById('panelToggleText');
panelHandle.addEventListener('click', () => {
panel.classList.toggle('open');
if (panel.classList.contains('open')) {
toggleIcon.textContent = '▼';
toggleText.textContent = 'Hide Analytics';
} else {
toggleIcon.textContent = '▲';
toggleText.textContent = 'Show Analytics & Visualizations';
}
});
// Tab switching
document.querySelectorAll('.panel-tab').forEach(tab => {
tab.addEventListener('click', () => {
const targetTab = tab.dataset.tab;
// Update active states
document.querySelectorAll('.panel-tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-pane').forEach(p => p.classList.remove('active'));
tab.classList.add('active');
document.getElementById(`tab-${targetTab}`).classList.add('active');
});
});
}
// Process metadata and generate analytics
function processMetadataForAnalytics(metadata) {
if (!metadata) return null;
const trips = [];
const sensors = {};
Object.keys(metadata).forEach(tripId => {
const trip = metadata[tripId];
const gnss = trip['GNSS'];
if (!gnss) return;
const parts = gnss.split(',');
const duration = parts[1]; // "14:50"
const stops = parts[2]; // "01:12"
const distance = parseFloat(parts[3]) || 0;
const avgSpeed = parseFloat(parts[4]) || 0;
const avgSpeedWOS = parseFloat(parts[5]) || 0;
const maxSpeed = parseFloat(parts[6]) || 0;
// Extract sensor ID from trip ID (e.g., "602B3" from "602B3_Trip4")
const sensorId = tripId.split('_')[0];
// Parse timestamps
const tripStartEnd = trip['Trip start/end'];
let startTime = null;
if (tripStartEnd) {
const timestampMatch = tripStartEnd.match(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/);
if (timestampMatch) {
startTime = new Date(timestampMatch[1]);
}
}
const tripData = {
id: tripId,
sensorId,
distance,
avgSpeed,
avgSpeedWOS,
maxSpeed,
duration: parseDurationToSeconds(duration),
stopTime: parseDurationToSeconds(stops),
movingTime: parseDurationToSeconds(duration) - parseDurationToSeconds(stops),
startTime,
charge: trip['Charge(start | stop)']
};
trips.push(tripData);
// Group by sensor
if (!sensors[sensorId]) {
sensors[sensorId] = {
id: sensorId,
trips: [],
totalDistance: 0,
totalTime: 0,
avgSpeed: 0
};
}
sensors[sensorId].trips.push(tripData);
sensors[sensorId].totalDistance += distance;
sensors[sensorId].totalTime += tripData.duration;
});
// Calculate sensor averages
Object.values(sensors).forEach(sensor => {
const totalSpeed = sensor.trips.reduce((sum, t) => sum + t.avgSpeed, 0);
sensor.avgSpeed = totalSpeed / sensor.trips.length;
});
return { trips, sensors };
}
// Parse duration "MM:SS" to seconds
function parseDurationToSeconds(duration) {
if (!duration) return 0;
const [part1, part2] = duration.split(':').map(Number);
return part1 * 60 + part2;
}
// Format seconds to readable string
function formatDuration(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (hours > 0) return `${hours}h ${minutes}m`;
return `${minutes}m ${seconds % 60}s`;
}
// Create speed distribution chart
function createSpeedChart(trips) {
const ctx = document.getElementById('speedChart');
if (!ctx) return;
// Calculate speed distribution
const speedRanges = {
'Stopped (0-2)': 0,
'Very Slow (2-5)': 0,
'Slow (5-10)': 0,
'Moderate (10-15)': 0,
'Fast (15-20)': 0,
'Very Fast (20-25)': 0,
'Extreme (25+)': 0
};
trips.forEach(trip => {
const speed = trip.avgSpeed;
if (speed < 2) speedRanges['Stopped (0-2)']++;
else if (speed < 5) speedRanges['Very Slow (2-5)']++;
else if (speed < 10) speedRanges['Slow (5-10)']++;
else if (speed < 15) speedRanges['Moderate (10-15)']++;
else if (speed < 20) speedRanges['Fast (15-20)']++;
else if (speed < 25) speedRanges['Very Fast (20-25)']++;
else speedRanges['Extreme (25+)']++;
});
if (analyticsCharts.speedChart) {
analyticsCharts.speedChart.destroy();
}
analyticsCharts.speedChart = new Chart(ctx, {
type: 'pie',
data: {
labels: Object.keys(speedRanges),
datasets: [{
data: Object.values(speedRanges),
backgroundColor: [
'#808080', '#DC2626', '#F97316', '#FACC15',
'#22C55E', '#3B82F6', '#6366F1'
]
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { position: 'bottom' }
}
}
});
}
// Create stop vs move chart
function createStopMoveChart(trips) {
const ctx = document.getElementById('stopMoveChart');
if (!ctx) return;
const totalMoving = trips.reduce((sum, t) => sum + t.movingTime, 0);
const totalStopped = trips.reduce((sum, t) => sum + t.stopTime, 0);
if (analyticsCharts.stopMoveChart) {
analyticsCharts.stopMoveChart.destroy();
}
analyticsCharts.stopMoveChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Moving', 'Stopped'],
datasets: [{
data: [totalMoving / 60, totalStopped / 60], // Convert to minutes
backgroundColor: ['#22C55E', '#DC2626']
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { position: 'bottom' },
tooltip: {
callbacks: {
label: function(context) {
const label = context.label || '';
const value = context.parsed;
const hours = Math.floor(value / 60);
const mins = Math.round(value % 60);
return `${label}: ${hours}h ${mins}m`;
}
}
}
}
}
});
}
// Create time of day chart
function createTimeOfDayChart(trips) {
const ctx = document.getElementById('timeOfDayChart');
if (!ctx) return;
const timeSlots = {
'0-6': { trips: 0, totalSpeed: 0 },
'6-9': { trips: 0, totalSpeed: 0 },
'9-12': { trips: 0, totalSpeed: 0 },
'12-15': { trips: 0, totalSpeed: 0 },
'15-18': { trips: 0, totalSpeed: 0 },
'18-21': { trips: 0, totalSpeed: 0 },
'21-24': { trips: 0, totalSpeed: 0 }
};
trips.forEach(trip => {
if (!trip.startTime) return;
const hour = trip.startTime.getHours();
let slot;
if (hour < 6) slot = '0-6';
else if (hour < 9) slot = '6-9';
else if (hour < 12) slot = '9-12';
else if (hour < 15) slot = '12-15';
else if (hour < 18) slot = '15-18';
else if (hour < 21) slot = '18-21';
else slot = '21-24';
timeSlots[slot].trips++;
timeSlots[slot].totalSpeed += trip.avgSpeed;
});
const labels = Object.keys(timeSlots);
const tripCounts = labels.map(slot => timeSlots[slot].trips);
const avgSpeeds = labels.map(slot =>
timeSlots[slot].trips > 0 ? timeSlots[slot].totalSpeed / timeSlots[slot].trips : 0
);
if (analyticsCharts.timeOfDayChart) {
analyticsCharts.timeOfDayChart.destroy();
}
analyticsCharts.timeOfDayChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels.map(l => l + 'h'),
datasets: [{
label: 'Number of Trips',
data: tripCounts,
backgroundColor: '#3B82F6',
yAxisID: 'y'
}, {
label: 'Avg Speed (km/h)',
data: avgSpeeds,
backgroundColor: '#22C55E',
yAxisID: 'y1'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
type: 'linear',
position: 'left',
title: { display: true, text: 'Trips' }
},
y1: {
type: 'linear',
position: 'right',
title: { display: true, text: 'Speed (km/h)' },
grid: { drawOnChartArea: false }
}
}
}
});
}
// Create day of week chart
function createDayOfWeekChart(trips) {
const ctx = document.getElementById('dayOfWeekChart');
if (!ctx) return;
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const dayCounts = [0, 0, 0, 0, 0, 0, 0];
trips.forEach(trip => {
if (trip.startTime) {
dayCounts[trip.startTime.getDay()]++;
}
});
if (analyticsCharts.dayOfWeekChart) {
analyticsCharts.dayOfWeekChart.destroy();
}
analyticsCharts.dayOfWeekChart = new Chart(ctx, {
type: 'bar',
data: {
labels: days,
datasets: [{
label: 'Trips per Day',
data: dayCounts,
backgroundColor: '#E67E22'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false }
}
}
});
}
// Update summary cards
function updateSummaryCards(trips) {
const totalSpeed = trips.reduce((sum, t) => sum + t.avgSpeed, 0);
const avgSpeed = totalSpeed / trips.length;
const maxSpeed = Math.max(...trips.map(t => t.maxSpeed));
const totalDistance = trips.reduce((sum, t) => sum + t.distance, 0);
const avgDistance = totalDistance / trips.length;
const totalDuration = trips.reduce((sum, t) => sum + t.duration, 0);
const avgDuration = totalDuration / trips.length;
document.getElementById('avgSpeedCard').textContent = avgSpeed.toFixed(1) + ' km/h';
document.getElementById('maxSpeedCard').textContent = maxSpeed.toFixed(1) + ' km/h';
document.getElementById('avgDistanceCard').textContent = avgDistance.toFixed(2) + ' km';
document.getElementById('avgDurationCard').textContent = formatDuration(avgDuration);
}
// Create sensor cards
function createSensorCards(sensors) {
const container = document.getElementById('sensorCards');
if (!container) return;
container.innerHTML = '';
Object.values(sensors).forEach(sensor => {
const card = document.createElement('div');
card.className = 'sensor-card';
card.innerHTML = `
<div class="sensor-card-title">Sensor ${sensor.id}</div>
<div class="sensor-stat">
<span>Total Trips:</span>
<strong>${sensor.trips.length}</strong>
</div>
<div class="sensor-stat">
<span>Total Distance:</span>
<strong>${sensor.totalDistance.toFixed(2)} km</strong>
</div>
<div class="sensor-stat">
<span>Average Speed:</span>
<strong>${sensor.avgSpeed.toFixed(1)} km/h</strong>
</div>
<div class="sensor-stat">
<span>Total Time:</span>
<strong>${formatDuration(sensor.totalTime)}</strong>
</div>
`;
card.addEventListener('click', () => {
console.log('Filter by sensor:', sensor.id);
// You can add filtering logic here to show only this sensor's routes on map
});
container.appendChild(card);
});
}
// Create trip details list
function createTripsList(trips) {
const container = document.getElementById('tripsList');
if (!container) return;
container.innerHTML = '<div style="margin-bottom: 15px;"><strong>All Trips</strong></div>';
trips.sort((a, b) => (b.startTime || 0) - (a.startTime || 0)).forEach(trip => {
const card = document.createElement('div');
card.className = 'sensor-card';
card.innerHTML = `
<div class="sensor-card-title">${trip.id.replace(/_/g, ' ')}</div>
<div class="sensor-stat">
<span>Date:</span>
<strong>${trip.startTime ? trip.startTime.toLocaleDateString() : 'Unknown'}</strong>
</div>
<div class="sensor-stat">
<span>Time:</span>
<strong>${trip.startTime ? trip.startTime.toLocaleTimeString() : 'Unknown'}</strong>
</div>
<div class="sensor-stat">
<span>Distance:</span>
<strong>${trip.distance.toFixed(2)} km</strong>
</div>
<div class="sensor-stat">
<span>Avg Speed:</span>
<strong>${trip.avgSpeed.toFixed(1)} km/h</strong>
</div>
<div class="sensor-stat">
<span>Max Speed:</span>
<strong>${trip.maxSpeed.toFixed(1)} km/h</strong>
</div>
<div class="sensor-stat">
<span>Duration:</span>
<strong>${formatDuration(trip.duration)}</strong>
</div>
`;
container.appendChild(card);
});
}
// Main function to update all analytics
export function updateAnalytics(metadata) {
if (!metadata) {
console.warn('No metadata provided for analytics');
return;
}
analyticsData = processMetadataForAnalytics(metadata);
if (!analyticsData) return;
const { trips, sensors } = analyticsData;
console.log('Analytics data processed:', { tripCount: trips.length, sensorCount: Object.keys(sensors).length });
// Update all visualizations
updateSummaryCards(trips);
createSpeedChart(trips);
createStopMoveChart(trips);
createTimeOfDayChart(trips);
createDayOfWeekChart(trips);
createSensorCards(sensors);
createTripsList(trips);
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initAnalytics);
} else {
initAnalytics();
}
// Export for use in main app
window.updateAnalytics = updateAnalytics;