From a545c3c8e3b461f9ce5e4182c456f0e6d859f9f3 Mon Sep 17 00:00:00 2001 From: Mahreen-Ahmed Date: Thu, 23 Apr 2026 19:28:52 +0500 Subject: [PATCH 1/3] fix: Fixed current streak showing 0 despite having commits today --- src/index.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/index.php b/src/index.php index 46f883e4..660457ca 100644 --- a/src/index.php +++ b/src/index.php @@ -55,6 +55,29 @@ if ($cachedStats !== null) { // Use cached stats - instant response! $stats = $cachedStats; + $today = date("Y-m-d"); + $currentStreakEnd = $stats["currentStreak"]["end"]; + $currentStreakLength = $stats["currentStreak"]["length"]; + + if ($currentStreakLength == 0 || $currentStreakEnd != $today) { + try { + $contributionGraphs = getContributionGraphs($user, $startingYear); + $contributions = getContributionDates($contributionGraphs); + + if ($mode === "weekly") { + $stats = getWeeklyContributionStats($contributions); + } else { + $excludeDays = normalizeDays(explode(",", $excludeDaysRaw)); + $stats = getContributionStats($contributions, $excludeDays); + } + + if ($useCache) { + setCachedStats($user, $cacheOptions, $stats); + } + } catch (Exception $e) { + error_log("Failed to fetch fresh stats for user {$user}: " . $e->getMessage()); + } + } } else { // Fetch fresh data from GitHub API $contributionGraphs = getContributionGraphs($user, $startingYear); From b722c4badd468ffc15b8583a9bca60c589a34b0b Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Mon, 1 Jun 2026 02:04:51 +0300 Subject: [PATCH 2/3] refactor: Reduce code repetition --- src/index.php | 52 ++++++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/src/index.php b/src/index.php index 660457ca..f56b1285 100644 --- a/src/index.php +++ b/src/index.php @@ -49,44 +49,15 @@ // Check if cache is disabled $useCache = !isset($_SERVER["DISABLE_CACHE"]) || strtolower($_SERVER["DISABLE_CACHE"]) !== "true"; - // Check for cached stats first (24 hour cache) unless cache is disabled - $cachedStats = $useCache ? getCachedStats($user, $cacheOptions) : null; - - if ($cachedStats !== null) { - // Use cached stats - instant response! - $stats = $cachedStats; - $today = date("Y-m-d"); - $currentStreakEnd = $stats["currentStreak"]["end"]; - $currentStreakLength = $stats["currentStreak"]["length"]; - - if ($currentStreakLength == 0 || $currentStreakEnd != $today) { - try { - $contributionGraphs = getContributionGraphs($user, $startingYear); - $contributions = getContributionDates($contributionGraphs); - - if ($mode === "weekly") { - $stats = getWeeklyContributionStats($contributions); - } else { - $excludeDays = normalizeDays(explode(",", $excludeDaysRaw)); - $stats = getContributionStats($contributions, $excludeDays); - } - - if ($useCache) { - setCachedStats($user, $cacheOptions, $stats); - } - } catch (Exception $e) { - error_log("Failed to fetch fresh stats for user {$user}: " . $e->getMessage()); - } - } - } else { - // Fetch fresh data from GitHub API + // Fetches fresh stats from the GitHub API and updates the cache if enabled + $fetchFreshStats = function () use ($user, $startingYear, $mode, $excludeDaysRaw, $useCache, $cacheOptions) { $contributionGraphs = getContributionGraphs($user, $startingYear); $contributions = getContributionDates($contributionGraphs); if ($mode === "weekly") { $stats = getWeeklyContributionStats($contributions); } else { - // split and normalize excluded days + // Split and normalize excluded days $excludeDays = normalizeDays(explode(",", $excludeDaysRaw)); $stats = getContributionStats($contributions, $excludeDays); } @@ -95,6 +66,23 @@ if ($useCache) { setCachedStats($user, $cacheOptions, $stats); } + + return $stats; + }; + + // Check for cached stats first (24 hour cache) unless cache is disabled + $stats = $useCache ? getCachedStats($user, $cacheOptions) : null; + + if ($stats === null) { + // No cached stats - fetch fresh data from GitHub API + $stats = $fetchFreshStats(); + } elseif ($stats["currentStreak"]["length"] == 0 || $stats["currentStreak"]["end"] != date("Y-m-d")) { + // Cached streak may be stale - try refreshing, but fall back to cache on failure + try { + $stats = $fetchFreshStats(); + } catch (Exception $e) { + error_log("Failed to fetch fresh stats for user {$user}: " . $e->getMessage()); + } } renderOutput($stats); From 89f826e59fcc107693380dc685a54d853564b983 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Tue, 2 Jun 2026 13:26:05 +0300 Subject: [PATCH 3/3] Finish merge and refactor --- src/generator.php | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/generator.php b/src/generator.php index 456fda4e..1619f07b 100644 --- a/src/generator.php +++ b/src/generator.php @@ -33,7 +33,7 @@ function generateStreakStats(string $user, array $params = []): array // Check for cached stats first (24 hour cache) unless cache is disabled $cachedStats = $useCache ? getCachedStats($user, $cacheOptions) : null; - if ($cachedStats !== null) { + if (!statsMissingOrStale($cachedStats)) { return $cachedStats; } @@ -56,3 +56,40 @@ function generateStreakStats(string $user, array $params = []): array return $stats; } + +/** + * Check if cached stats are missing or stale - Streak may be outdated if it ends before today + * + * @param array|null $cachedStats The cached stats to check + * @return bool True if the cached stats are stale and should be refreshed + */ +function statsMissingOrStale(?array $cachedStats): bool +{ + // If there are no cached stats, we need to refresh + if ($cachedStats === null) { + return true; + } + + // If the cached stats don't have the expected structure, consider them stale + if (!isset($cachedStats["currentStreak"]["end"]) || !isset($cachedStats["currentStreak"]["length"])) { + return true; + } + + // If the current streak length is 0, we can consider it stale to allow for new streaks to be detected without waiting for the cache to expire + $currentStreakLength = $cachedStats["currentStreak"]["length"]; + if ($currentStreakLength === 0) { + return true; + } + + // Check if the current streak ends before today (or before the start of the week for weekly mode) + // If the streak ends before today, it may be outdated and we should refresh to check for new contributions + $currentStreakEnd = $cachedStats["currentStreak"]["end"]; + $mode = $cachedStats["mode"] ?? "daily"; + + if ($mode === "weekly") { + $startOfWeek = date("Y-m-d", strtotime("last Sunday")); + return $currentStreakEnd < $startOfWeek; + } else { + return $currentStreakEnd < date("Y-m-d"); + } +}