Fix hourly summary: default to today for realtime updates, add hour parameter for efficient updates

This commit is contained in:
mwpn
2025-12-17 17:41:27 +07:00
parent dae5e9d2d5
commit 1aa462d9da
4 changed files with 112 additions and 31 deletions

View File

@@ -20,10 +20,11 @@ class HourlySummaryService
* Aggregate hourly summary for a specific date
*
* @param string $date Format: Y-m-d
* @return array ['rows_processed' => int, 'date' => string]
* @param int|null $hour Optional: 0-23, if provided only aggregate for this hour
* @return array ['rows_processed' => int, 'date' => string, 'hour' => int|null]
* @throws PDOException
*/
public function aggregateForDate(string $date): array
public function aggregateForDate(string $date, ?int $hour = null): array
{
// Validate date format
$dateTime = \DateTime::createFromFormat('Y-m-d', $date);
@@ -31,6 +32,11 @@ class HourlySummaryService
throw new \InvalidArgumentException('Invalid date format. Expected Y-m-d');
}
// Validate hour if provided
if ($hour !== null && ($hour < 0 || $hour > 23)) {
throw new \InvalidArgumentException('Invalid hour. Must be between 0 and 23');
}
$this->db->beginTransaction();
try {
@@ -55,6 +61,17 @@ class HourlySummaryService
AND e.gate_code = t.gate_code
AND e.category = t.category
WHERE DATE(e.event_time) = ?
";
$params = [$date];
// Add hour filter if provided
if ($hour !== null) {
$sql .= " AND HOUR(e.event_time) = ?";
$params[] = $hour;
}
$sql .= "
GROUP BY
DATE(e.event_time),
HOUR(e.event_time),
@@ -65,7 +82,7 @@ class HourlySummaryService
";
$stmt = $this->db->prepare($sql);
$stmt->execute([$date]);
$stmt->execute($params);
$aggregated = $stmt->fetchAll();
$rowsProcessed = 0;
@@ -103,7 +120,8 @@ class HourlySummaryService
return [
'rows_processed' => $rowsProcessed,
'date' => $date
'date' => $date,
'hour' => $hour
];
} catch (PDOException $e) {