Private
Public Access
1
0

fix: extend time range for statistics retrieval to 90 minutes and enhance fallback logic for missing data, improving accuracy in data representation

This commit is contained in:
2025-12-03 16:50:05 -05:00
parent 9701babcaa
commit 27fcac14d2

View File

@@ -1429,10 +1429,10 @@ class Statistics extends Component
} }
if ($targetTime) { if ($targetTime) {
// Find the statistic closest to the target time (within 30 minutes) // Find the statistic closest to the target time (within 90 minutes)
// Use database-agnostic approach // Using 90 minutes to ensure we catch hourly statistics even with timing variations
$minTime = $targetTime->copy()->subMinutes(30); $minTime = $targetTime->copy()->subMinutes(90);
$maxTime = $targetTime->copy()->addMinutes(30); $maxTime = $targetTime->copy()->addMinutes(90);
$stats = ServerStatistics::where('server_id', $this->server->id) $stats = ServerStatistics::where('server_id', $this->server->id)
->whereBetween('DateTime', [$minTime, $maxTime]) ->whereBetween('DateTime', [$minTime, $maxTime])
@@ -1440,7 +1440,29 @@ class Statistics extends Component
->get(); ->get();
if ($stats->isEmpty()) { if ($stats->isEmpty()) {
return null; // Fallback: get the closest stat before or after target time
$closestBefore = ServerStatistics::where('server_id', $this->server->id)
->where('DateTime', '<=', $targetTime)
->orderBy('DateTime', 'desc')
->first();
$closestAfter = ServerStatistics::where('server_id', $this->server->id)
->where('DateTime', '>=', $targetTime)
->orderBy('DateTime', 'asc')
->first();
if (!$closestBefore && !$closestAfter) {
return null;
}
if (!$closestBefore) return $closestAfter;
if (!$closestAfter) return $closestBefore;
// Return whichever is closer
$diffBefore = abs($closestBefore->DateTime->diffInSeconds($targetTime));
$diffAfter = abs($closestAfter->DateTime->diffInSeconds($targetTime));
return $diffBefore <= $diffAfter ? $closestBefore : $closestAfter;
} }
// Find the closest one to target time (database-agnostic) // Find the closest one to target time (database-agnostic)