Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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");
}
}
Loading