Skip to content

Commit ce3bf31

Browse files
Add caching for GitHub stats when API calls fail
Co-authored-by: Lackadaisical-Security <205846242+Lackadaisical-Security@users.noreply.github.com>
1 parent 7af9982 commit ce3bf31

4 files changed

Lines changed: 47 additions & 11 deletions

File tree

REALTIME_STATS.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"timestamp": "2026-01-05T14:32:59.155485Z",
2+
"timestamp": "2026-01-05T14:36:19.877520Z",
33
"local": {
4-
"commit_count": 3,
4+
"commit_count": 4,
55
"unique_contributors": 2,
6-
"last_commit_date": "2026-01-05 14:30:26 +0000",
7-
"last_commit_message": "Initial plan",
6+
"last_commit_date": "2026-01-05 14:33:19 +0000",
7+
"last_commit_message": "Improve GitHub API error handling and update auth format",
88
"current_branch": "copilot/fix-real-time-stats-api",
99
"tracked_files": 63,
10-
"timestamp": "2026-01-05T14:32:59.166468Z"
10+
"timestamp": "2026-01-05T14:36:19.888287Z"
1111
},
1212
"github": {}
1313
}

REALTIME_STATS.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Repository Statistics - Real-Time
22

3-
**Last Updated:** 2026-01-05T14:32:59.155485Z
3+
**Last Updated:** 2026-01-05T14:36:19.877520Z
44

55
## 📊 Current Statistics
66

@@ -9,11 +9,11 @@
99
| Metric | Value |
1010
|--------|-------|
1111
| 🌿 Current Branch | `copilot/fix-real-time-stats-api` |
12-
| 📝 Total Commits | 3 |
12+
| 📝 Total Commits | 4 |
1313
| 👥 Contributors | 2 |
1414
| 📄 Tracked Files | 63 |
15-
| 🕐 Last Commit | 2026-01-05 14:30:26 |
16-
| 💬 Last Message | Initial plan |
15+
| 🕐 Last Commit | 2026-01-05 14:33:19 |
16+
| 💬 Last Message | Improve GitHub API error handling and update auth format |
1717

1818
---
1919

@@ -22,7 +22,7 @@
2222
Add these to your README.md:
2323

2424
```markdown
25-
<img src="https://img.shields.io/badge/commits-3-orange?style=flat-square">
25+
<img src="https://img.shields.io/badge/commits-4-orange?style=flat-square">
2626
```
2727

2828
---

STATS_HISTORY.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,18 @@
141141
"timestamp": "2026-01-05T14:32:59.166468Z"
142142
},
143143
"github": {}
144+
},
145+
{
146+
"timestamp": "2026-01-05T14:36:19.877520Z",
147+
"local": {
148+
"commit_count": 4,
149+
"unique_contributors": 2,
150+
"last_commit_date": "2026-01-05 14:33:19 +0000",
151+
"last_commit_message": "Improve GitHub API error handling and update auth format",
152+
"current_branch": "copilot/fix-real-time-stats-api",
153+
"tracked_files": 63,
154+
"timestamp": "2026-01-05T14:36:19.888287Z"
155+
},
156+
"github": {}
144157
}
145158
]

realtime_stats_tracker.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,28 @@ def collect_stats(self):
174174
"""Collect all statistics"""
175175
print("🔍 Collecting repository statistics...\n")
176176

177+
# Try to load previous stats for fallback
178+
previous_github_stats = {}
179+
if self.stats_file.exists():
180+
try:
181+
with open(self.stats_file, 'r') as f:
182+
previous_data = json.load(f)
183+
previous_github_stats = previous_data.get('github', {})
184+
except:
185+
pass
186+
177187
stats = {
178188
"timestamp": datetime.now(UTC).strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
179189
"local": self.get_local_git_stats(),
180190
"github": self.get_github_stats()
181191
}
182192

193+
# If GitHub stats are empty but we have previous stats, preserve them
194+
if not stats["github"] and previous_github_stats:
195+
print("ℹ️ Using cached GitHub stats from previous successful fetch")
196+
stats["github"] = previous_github_stats
197+
stats["github_cached"] = True
198+
183199
return stats
184200

185201
def save_stats(self, stats):
@@ -249,6 +265,8 @@ def display_stats(self, stats):
249265
# GitHub stats
250266
if stats.get("github"):
251267
print("\n🌐 GITHUB STATISTICS:")
268+
if stats.get("github_cached"):
269+
print(" ℹ️ (Using cached data from previous successful fetch)")
252270
print("-" * 80)
253271
gh = stats["github"]
254272
print(f" ⭐ Stars: {gh.get('stars', 'N/A')}")
@@ -274,7 +292,12 @@ def generate_stats_markdown(self, stats):
274292
md = f"""# Repository Statistics - Real-Time
275293
276294
**Last Updated:** {stats['timestamp']}
277-
295+
"""
296+
297+
if stats.get("github_cached"):
298+
md += "\n> ℹ️ **Note:** GitHub metrics below are cached from previous successful fetch (current API call failed)\n"
299+
300+
md += """
278301
## 📊 Current Statistics
279302
280303
"""

0 commit comments

Comments
 (0)