diff --git a/scripts/telegram/telegram_cron.php b/scripts/telegram/telegram_cron.php index 96e1448..719e74f 100644 --- a/scripts/telegram/telegram_cron.php +++ b/scripts/telegram/telegram_cron.php @@ -93,7 +93,7 @@ $tasks = [ } $messageText = buildTcrbLatestVMessage($latestObservation, $nowBerlin); - $chartObservations = fetchRecentTcrbObservationsByFilter($pdo, 'V', 80); + $chartObservations = fetchRecentTcrbObservationsByFilterWindow($pdo, 'V', '-2 months'); $chartSvg = buildTcrbVChartSvg($chartObservations, $latestObservation); $chartPackage = createTelegramChartPackage($chartSvg); $sentCount = 0; @@ -421,8 +421,12 @@ function fetchLatestTcrbObservationByFilter(PDO $pdo, string $filterCode): ?arra return is_array($row) ? $row : null; } -function fetchRecentTcrbObservationsByFilter(PDO $pdo, string $filterCode, int $limit = 80): array +function fetchRecentTcrbObservationsByFilterWindow(PDO $pdo, string $filterCode, string $windowSpec = '-2 months'): array { + $windowStartUtc = (new DateTimeImmutable('now', new DateTimeZone('UTC'))) + ->modify($windowSpec) + ->format('Y-m-d H:i:s'); + $stmt = $pdo->prepare(" SELECT `id`, @@ -434,17 +438,38 @@ function fetchRecentTcrbObservationsByFilter(PDO $pdo, string $filterCode, int $ `observer_code` FROM `aavso_tcrb_observations` WHERE `filter_code` = :filter_code - ORDER BY `observed_at_utc` DESC, `id` DESC - LIMIT :row_limit + AND `observed_at_utc` >= :window_start_utc + ORDER BY `observed_at_utc` ASC, `id` ASC "); - $stmt->bindValue(':filter_code', $filterCode, PDO::PARAM_STR); - $stmt->bindValue(':row_limit', max(5, $limit), PDO::PARAM_INT); - $stmt->execute(); + $stmt->execute([ + ':filter_code' => $filterCode, + ':window_start_utc' => $windowStartUtc, + ]); $rows = $stmt->fetchAll(); - usort($rows, static function (array $a, array $b): int { - return strcmp((string) ($a['observed_at_utc'] ?? ''), (string) ($b['observed_at_utc'] ?? '')); - }); + if ($rows === []) { + $fallbackStmt = $pdo->prepare(" + SELECT + `id`, + `observed_at_utc`, + `jd`, + `magnitude`, + `magnitude_error`, + `filter_code`, + `observer_code` + FROM `aavso_tcrb_observations` + WHERE `filter_code` = :filter_code + ORDER BY `observed_at_utc` DESC, `id` DESC + LIMIT 30 + "); + $fallbackStmt->execute([ + ':filter_code' => $filterCode, + ]); + $rows = $fallbackStmt->fetchAll(); + usort($rows, static function (array $a, array $b): int { + return strcmp((string) ($a['observed_at_utc'] ?? ''), (string) ($b['observed_at_utc'] ?? '')); + }); + } return $rows; } @@ -636,7 +661,7 @@ function buildTcrbVChartSvg(array $observations, array $latestObservation): stri T CrB Lichtkurve - Filter V - letzte 80 Messpunkte, kleinere Magnituden sind heller + letzte 2 Monate %s %s Aktuell: %s%s