Fix: Historical Performance date filter

Ensure date filter respects server ID and displays correct data for 1 day  date filter
This commit is contained in:
samang-dauth
2025-07-02 17:44:11 +07:00
parent bdcf5c75b9
commit d6793f104c
3 changed files with 147 additions and 12 deletions
@@ -28,9 +28,10 @@ export const ServerHistoryCharts = ({ serverId }: ServerHistoryChartsProps) => {
} = useQuery({
queryKey: ['server-metrics-history', serverId, timeRange],
queryFn: async () => {
console.log('ServerHistoryCharts: Fetching metrics for serverId:', serverId);
console.log('ServerHistoryCharts: Fetching metrics for serverId:', serverId, 'timeRange:', timeRange);
const result = await serverService.getServerMetrics(serverId, timeRange);
console.log('ServerHistoryCharts: Raw metrics result:', result);
console.log('ServerHistoryCharts: Raw metrics result for timeRange', timeRange, ':', result?.length || 0, 'records');
console.log('ServerHistoryCharts: First 3 records:', result?.slice(0, 3));
return result;
},
enabled: !!serverId,
@@ -47,7 +48,9 @@ export const ServerHistoryCharts = ({ serverId }: ServerHistoryChartsProps) => {
timeRange
});
console.log('ServerHistoryCharts: About to format chart data with', metrics?.length || 0, 'metrics for timeRange:', timeRange);
const chartData = formatChartData(metrics, timeRange);
console.log('ServerHistoryCharts: After formatting, got', chartData?.length || 0, 'chart data points');
const getGridColor = () => theme === 'dark' ? '#374151' : '#e5e7eb';
const getAxisColor = () => theme === 'dark' ? '#9ca3af' : '#6b7280';
@@ -55,11 +55,24 @@ export const filterMetricsByTimeRange = (metrics: any[], timeRange: TimeRange):
if (!selectedRange) return metrics;
const cutoffTime = new Date(now.getTime() - (selectedRange.hours * 60 * 60 * 1000));
console.log('filterMetricsByTimeRange: timeRange:', timeRange, 'cutoffTime:', cutoffTime.toISOString(), 'now:', now.toISOString());
return metrics.filter(metric => {
const filtered = metrics.filter(metric => {
const metricTime = new Date(metric.created || metric.timestamp);
return metricTime >= cutoffTime;
const isValid = metricTime >= cutoffTime;
if (!isValid && metrics.indexOf(metric) < 3) {
console.log('filterMetricsByTimeRange: Filtered out metric:', {
metricTime: metricTime.toISOString(),
cutoffTime: cutoffTime.toISOString(),
created: metric.created,
timestamp: metric.timestamp
});
}
return isValid;
});
console.log('filterMetricsByTimeRange: Filtered', metrics.length, 'to', filtered.length, 'metrics');
return filtered;
};
const formatTimestamp = (timestamp: string, timeRange: TimeRange): string => {
@@ -95,7 +108,19 @@ const formatTimestamp = (timestamp: string, timeRange: TimeRange): string => {
};
export const formatChartData = (metrics: any[], timeRange: TimeRange) => {
console.log('formatChartData: Input metrics count:', metrics?.length || 0, 'timeRange:', timeRange);
const filteredMetrics = filterMetricsByTimeRange(metrics, timeRange);
console.log('formatChartData: After time filtering:', filteredMetrics?.length || 0, 'metrics');
if (filteredMetrics.length > 0) {
console.log('formatChartData: First filtered metric:', filteredMetrics[0]);
console.log('formatChartData: Sample timestamps:', filteredMetrics.slice(0, 3).map(m => ({
created: m.created,
timestamp: m.timestamp,
both: new Date(m.created || m.timestamp).toISOString()
})));
}
return filteredMetrics.slice(0, 100).reverse().map((metric, index) => {
const cpuUsage = typeof metric.cpu_usage === 'string' ?