Skip to content

Commit 6cba830

Browse files
chore: improved the graph
1 parent 85fcac8 commit 6cba830

2 files changed

Lines changed: 103 additions & 65 deletions

File tree

src/routes/api.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,15 @@ def graph_data_api_v3_():
382382
# Create a new list for the time series data of this particular series
383383
series_data = {
384384
"metric": series.get("metric"),
385-
"values": []
385+
"values": {}
386386
}
387387

388388
# Iterate over the values for this series
389389
for value in series.get("values", []):
390390
timestamp = datetime.fromtimestamp(float(value[0]), tz=timezone.utc).isoformat()
391391
if timestamp not in time_data:
392392
time_data.append(timestamp)
393-
series_data["values"].append(value[1])
393+
series_data["values"][timestamp] = value[1]
394394

395395
# Append the series data to the metric
396396
metric_data[metric].append(series_data)
@@ -399,16 +399,21 @@ def graph_data_api_v3_():
399399
else:
400400
raise Exception(f"Failed to fetch data for {metric} from Prometheus: {response.text}")
401401

402-
# Ensure all metric data has the same length as time_data
402+
# Sort the time data for proper alignment
403+
time_data.sort()
404+
405+
# Ensure all metric data aligns with time_data
403406
for metric, series_list in metric_data.items():
404407
for series in series_list:
405-
while len(series["values"]) < len(time_data):
406-
series["values"].append(None)
408+
aligned_values = []
409+
for timestamp in time_data:
410+
aligned_values.append(series["values"].get(timestamp, None))
411+
series["values"] = aligned_values
407412

408413
# Return the data as JSON
409414
response_data = {
410415
"time": time_data,
411-
**metric_data,
416+
**{metric: [{"metric": s["metric"], "values": s["values"]} for s in series_list] for metric, series_list in metric_data.items()},
412417
"current_time": current_time
413418
}
414419

src/static/js/graphs_experimental.js

Lines changed: 92 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function createChart(ctx, labels, datasets, yLabel) {
149149
})),
150150
},
151151
options: {
152-
152+
responsive: true,
153153
scales: {
154154
x: {
155155
type: 'category',
@@ -179,97 +179,130 @@ function createChart(ctx, labels, datasets, yLabel) {
179179

180180
// Function to create charts with the fetched data
181181
function createCharts(cpuData, timeData, memoryData, batteryData, networkSentData, networkReceivedData, dashboardMemoryUsageData, cpuFrequencyData, currentTempData) {
182+
183+
// Function to generate dynamic colors based on index
184+
function generateColor(index) {
185+
const hue = (index * 40) % 360; // Adjust hue for unique colors
186+
return {
187+
borderColor: `hsl(${hue}, 70%, 50%)`, // Border color
188+
backgroundColor: `hsl(${hue}, 70%, 80%)` // Background color
189+
};
190+
}
191+
182192
// CPU Usage Chart
183193
const ctxCpu = document.getElementById('cpuTimeChart').getContext('2d');
184-
const cpuDatasets = cpuData.map((cpu, index) => ({
185-
label: `CPU Usage (%) ${cpu.metric.instance}`,
186-
data: cpu.values,
187-
borderColor: `rgba(${75 + index * 20}, 192, 192, 1)`,
188-
backgroundColor: `rgba(${75 + index * 20}, 192, 192, 0.2)`,
189-
tension: 0.4
190-
}));
194+
const cpuDatasets = cpuData.map((cpu, index) => {
195+
const { borderColor, backgroundColor } = generateColor(index);
196+
return {
197+
label: `CPU Usage (%) ${cpu.metric.instance}`,
198+
data: cpu.values,
199+
borderColor: borderColor,
200+
backgroundColor: backgroundColor,
201+
tension: 0.4
202+
};
203+
});
191204

192205
createChart(ctxCpu, timeData, cpuDatasets, 'CPU Usage (%)');
193206

194207
// Memory Usage Chart
195208
const ctxMemory = document.getElementById('memoryTimeChart').getContext('2d');
196-
const memoryDatasets = memoryData.map((memory, index) => ({
197-
label: `Memory Usage (%) ${memory.metric.instance}`,
198-
data: memory.values,
199-
borderColor: `rgba(${75 + index * 20}, 75, 192, 1)`,
200-
backgroundColor: `rgba(${75 + index * 20}, 75, 192, 0.2)`,
201-
tension: 0.4
202-
}));
209+
const memoryDatasets = memoryData.map((memory, index) => {
210+
const { borderColor, backgroundColor } = generateColor(index);
211+
return {
212+
label: `Memory Usage (%) ${memory.metric.instance}`,
213+
data: memory.values,
214+
borderColor: borderColor,
215+
backgroundColor: backgroundColor,
216+
tension: 0.4
217+
};
218+
});
203219

204-
createChart(ctxMemory, timeData, memoryDatasets, 'Memory Usage (%)');
220+
createChart(ctxMemory, timeData, memoryDatasets, 'Memory Usage (%)');
205221

206222
// Battery Percentage Chart
207223
const ctxBattery = document.getElementById('batteryTimeChart').getContext('2d');
208-
const batteryDatasets = batteryData.map((battery, index) => ({
209-
label: `Battery Usage (%) ${battery.metric.instance}`,
210-
data: battery.values,
211-
borderColor: `rgba(${192 + index * 20}, 75, 75, 1)`,
212-
backgroundColor: `rgba(${192 + index * 20}, 75, 75, 0.2)`,
213-
tension: 0.4
214-
}));
224+
const batteryDatasets = batteryData.map((battery, index) => {
225+
const { borderColor, backgroundColor } = generateColor(index);
226+
return {
227+
label: `Battery Usage (%) ${battery.metric.instance}`,
228+
data: battery.values,
229+
borderColor: borderColor,
230+
backgroundColor: backgroundColor,
231+
tension: 0.4
232+
};
233+
});
215234

216235
createChart(ctxBattery, timeData, batteryDatasets, 'Battery Percentage (%)');
217236

218237
// Network Sent & Received Chart
219238
const ctxNetwork = document.getElementById('networkTimeChart').getContext('2d');
220239
const networkDatasets = [
221-
...networkSentData.map((networkSent, index) => ({
222-
label: `Network Sent (MB) ${networkSent.metric.instance}`,
223-
data: networkSent.values,
224-
borderColor: `rgba(${75 + index * 20}, 75, 192, 1)`,
225-
backgroundColor: `rgba(${75 + index * 20}, 75, 192, 0.2)`,
226-
tension: 0.4
227-
})),
228-
...networkReceivedData.map((networkReceived, index) => ({
229-
label: `Network Received (MB) ${networkReceived.metric.instance}`,
230-
data: networkReceived.values,
231-
borderColor: `rgba(${192 + index * 20}, 75, 75, 1)`,
232-
backgroundColor: `rgba(${192 + index * 20}, 75, 75, 0.2)`,
233-
tension: 0.4
234-
}))
240+
...networkSentData.map((networkSent, index) => {
241+
const { borderColor, backgroundColor } = generateColor(index);
242+
return {
243+
label: `Network Sent (MB) ${networkSent.metric.instance}`,
244+
data: networkSent.values,
245+
borderColor: borderColor,
246+
backgroundColor: backgroundColor,
247+
tension: 0.4
248+
};
249+
}),
250+
...networkReceivedData.map((networkReceived, index) => {
251+
const { borderColor, backgroundColor } = generateColor(index + networkSentData.length);
252+
return {
253+
label: `Network Received (MB) ${networkReceived.metric.instance}`,
254+
data: networkReceived.values,
255+
borderColor: borderColor,
256+
backgroundColor: backgroundColor,
257+
tension: 0.4
258+
};
259+
})
235260
];
236261

237262
createChart(ctxNetwork, timeData, networkDatasets, 'Data Transferred (MB)');
238263

239264
// Dashboard Memory Usage Chart
240265
const ctxDashboardMemory = document.getElementById('dashboardMemoryTimeChart').getContext('2d');
241-
const dashboardMemoryDatasets = dashboardMemoryUsageData.map((dashboardMemory, index) => ({
242-
label: `Dashboard Memory Usage (%) ${dashboardMemory.metric.instance}`,
243-
data: dashboardMemory.values,
244-
borderColor: `rgba(${75 + index * 20}, 192, 75, 1)`,
245-
backgroundColor: `rgba(${75 + index * 20}, 192, 75, 0.2)`,
246-
tension: 0.4
247-
}));
266+
const dashboardMemoryDatasets = dashboardMemoryUsageData.map((dashboardMemory, index) => {
267+
const { borderColor, backgroundColor } = generateColor(index);
268+
return {
269+
label: `Dashboard Memory Usage (%) ${dashboardMemory.metric.instance}`,
270+
data: dashboardMemory.values,
271+
borderColor: borderColor,
272+
backgroundColor: backgroundColor,
273+
tension: 0.4
274+
};
275+
});
248276

249277
createChart(ctxDashboardMemory, timeData, dashboardMemoryDatasets, 'Dashboard Memory Usage (%)');
250278

251279
// CPU Frequency Chart
252280
const ctxCpuFrequency = document.getElementById('cpuFrequencyTimeChart').getContext('2d');
253-
const cpuFrequencyDatasets = cpuFrequencyData.map((cpuFrequency, index) => ({
254-
label: `CPU Frequency (GHz) ${cpuFrequency.metric.instance}`,
255-
data: cpuFrequency.values,
256-
borderColor: `rgba(${192 + index * 20}, 192, 75, 1)`,
257-
backgroundColor: `rgba(${192 + index * 20}, 192, 75, 0.2)`,
258-
tension: 0.4,
259-
260-
}));
281+
const cpuFrequencyDatasets = cpuFrequencyData.map((cpuFrequency, index) => {
282+
const { borderColor, backgroundColor } = generateColor(index);
283+
return {
284+
label: `CPU Frequency (GHz) ${cpuFrequency.metric.instance}`,
285+
data: cpuFrequency.values,
286+
borderColor: borderColor,
287+
backgroundColor: backgroundColor,
288+
tension: 0.4
289+
};
290+
});
261291

262292
createChart(ctxCpuFrequency, timeData, cpuFrequencyDatasets, 'CPU Frequency (GHz)');
263293

264294
// Current Temperature Chart
265295
const ctxCurrentTemp = document.getElementById('currentTempTimeChart').getContext('2d');
266-
const currentTempDatasets = currentTempData.map((currentTemp, index) => ({
267-
label: `Current Temperature (°C) ${currentTemp.metric.instance}`,
268-
data: currentTemp.values,
269-
borderColor: `rgba(${75 + index * 20}, 192, 192, 1)`,
270-
backgroundColor: `rgba(${75 + index * 20}, 192, 192, 0.2)`,
271-
tension: 0.4
272-
}));
296+
const currentTempDatasets = currentTempData.map((currentTemp, index) => {
297+
const { borderColor, backgroundColor } = generateColor(index);
298+
return {
299+
label: `Current Temperature (°C) ${currentTemp.metric.instance}`,
300+
data: currentTemp.values,
301+
borderColor: borderColor,
302+
backgroundColor: backgroundColor,
303+
tension: 0.4
304+
};
305+
});
273306

274307
createChart(ctxCurrentTemp, timeData, currentTempDatasets, 'Current Temperature (°C)');
275308
}

0 commit comments

Comments
 (0)