-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
103 lines (98 loc) · 2.75 KB
/
server.js
File metadata and controls
103 lines (98 loc) · 2.75 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
/*
* Cheng Tsz Hung (25017438D)
* Awwab Hamam (22103907D)
*/
const socket = io();
const revenueCtx = document.getElementById('revenueChart').getContext('2d');
const revenueChart = new Chart(revenueCtx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Hourly Revenue',
data: [],
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75,192,192,0.1)',
tension: 0.1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top'
},
tooltip: {
mode: 'index',
intersect: false
}
},
interaction: {
mode: 'nearest',
axis: 'x',
intersect: false
},
scales: {
x: {
display: true,
ticks: {
autoSkip: true,
maxRotation: 0,
minRotation: 0
}
},
y: {
display: true,
beginAtZero: false,
ticks: {
callback: function(value) {
if (typeof value === 'number') {
return value.toLocaleString();
}
return value;
}
}
}
}
}
});
socket.on('statsUpdate', (data) => {
updateCharts(data);
});
function updateCharts(data) {
revenueChart.data.labels = data.timeLabels;
revenueChart.data.datasets[0].data = data.revenue;
revenueChart.update('active');
}
document.addEventListener('DOMContentLoaded', () => {
try {
const raw = document.getElementById('analyticsData').value;
if (raw) {
const analyticsData = JSON.parse(raw);
updateCharts(analyticsData);
}
} catch (err) {
console.error('Failed to initialize analytics data', err);
}
});
document.getElementById('exportCSV').addEventListener('click', async () => {
try {
const response = await fetch('/api/analytics/export/csv');
if (!response.ok) {
throw new Error('Failed to fetch CSV');
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'booth-analytics.csv';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
} catch (err) {
console.error('CSV export failed', err);
alert('CSV export failed. Check console for details.');
}
});