Fix: Server history chart and date filter

Ensure the historical performance chart correctly displays server metric data and that the date filter dropdown functions as expected.
This commit is contained in:
Tola Leng
2025-07-01 16:23:25 +07:00
parent 460fcbc7dc
commit 30bba14e37
4 changed files with 297 additions and 188 deletions
@@ -57,11 +57,43 @@ export const filterMetricsByTimeRange = (metrics: any[], timeRange: TimeRange):
const cutoffTime = new Date(now.getTime() - (selectedRange.hours * 60 * 60 * 1000));
return metrics.filter(metric => {
const metricTime = new Date(metric.timestamp);
const metricTime = new Date(metric.created || metric.timestamp);
return metricTime >= cutoffTime;
});
};
const formatTimestamp = (timestamp: string, timeRange: TimeRange): string => {
const date = new Date(timestamp);
if (timeRange === '60m') {
// For 60 minutes, show time with seconds
return date.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
} else if (timeRange === '1d') {
// For 1 day, show date and time
return date.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
} else {
// For longer periods, show date and time
return date.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
}
};
export const formatChartData = (metrics: any[], timeRange: TimeRange) => {
const filteredMetrics = filterMetricsByTimeRange(metrics, timeRange);
@@ -85,11 +117,11 @@ export const formatChartData = (metrics: any[], timeRange: TimeRange) => {
const networkRxSpeed = metric.network_rx_speed || 0;
const networkTxSpeed = metric.network_tx_speed || 0;
const timestamp = metric.created || metric.timestamp;
return {
timestamp: new Date(metric.timestamp).toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit'
}),
timestamp: formatTimestamp(timestamp, timeRange),
fullTimestamp: timestamp,
cpuUsage: Math.round(cpuUsage * 100) / 100,
cpuCores: parseInt(metric.cpu_cores) || 0,
cpuFree: 100 - cpuUsage,