Skip to content

Commit b32861f

Browse files
committed
fix script
1 parent ab4a21d commit b32861f

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

tools/last_user_activity.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
from datetime import datetime, timezone
1212
import humanize
1313
from itertools import count
14-
import aiosqlite
1514
import diskcache
16-
import json
1715
import pathlib
1816
from typing import Optional, List, Dict
1917
import argparse
@@ -77,7 +75,9 @@ def get(self, key, default=None, retry=False):
7775
cache = DateTimeCache(CACHE_DIR)
7876

7977

80-
async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict]:
78+
async def get_org_members(
79+
session: aiohttp.ClientSession, org: str, debug: bool
80+
) -> List[Dict]:
8181
"""Fetch all members of a GitHub organization with caching.
8282
8383
Parameters
@@ -106,7 +106,8 @@ async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict
106106
# Try to get from cache with retry
107107
cached_data = cache.get(cache_key, retry=True)
108108
if cached_data is not None:
109-
print(f"[cyan]Cache hit for {org} members[/cyan]")
109+
if debug:
110+
print(f"[cyan]Cache hit for {org} members[/cyan]")
110111
return cached_data
111112

112113
print(f"[yellow]Cache miss for {org} members - fetching from API[/yellow]")
@@ -139,25 +140,30 @@ async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict
139140

140141

141142
async def get_user_activity(
142-
session: aiohttp.ClientSession, username: str
143+
session: aiohttp.ClientSession, username: str, debug: bool
143144
) -> Optional[datetime]:
144145
"""Fetch the last public activity date for a GitHub user."""
145146
cache_key = f"user_activity_{username}"
146147

147148
# Try to get from cache
148149
cached_data = cache.get(cache_key)
149150
if cached_data is not None:
150-
print(f"[cyan]Cache hit for {username} activity[/cyan]")
151+
if debug:
152+
print(f"[cyan]Cache hit for {username} activity[/cyan]")
151153
return cached_data
152-
153-
print(f"[yellow]Cache miss for {username} activity - fetching from API[/yellow]")
154+
if debug:
155+
print(
156+
f"[yellow]Cache miss for {username} activity - fetching from API[/yellow]"
157+
)
154158

155159
try:
156-
print(f"Getting activity for {username}")
160+
if debug:
161+
print(f"[blue]Getting activity for {username}[/blue]")
157162
url = f"https://api.github.com/users/{username}/events/public"
158163
async with session.get(url, headers=headers) as response:
159164
if response.status == 200:
160-
print(f"Got activity for {username}")
165+
if debug:
166+
print(f"Got activity for {username}")
161167
events = await response.json()
162168
if events:
163169
last_activity = datetime.fromisoformat(
@@ -170,7 +176,8 @@ async def get_user_activity(
170176
print(f"[green]Cached activity for {username}[/green]")
171177
return last_activity
172178
else:
173-
print(f"[yellow]No activity found for {username}[/yellow]")
179+
if debug:
180+
print(f"[yellow]No activity found for {username}[/yellow]")
174181
cache[cache_key] = None # Using __setitem__ instead of set()
175182
else:
176183
print(
@@ -205,7 +212,7 @@ def clear_cache() -> None:
205212
print(f"[red]Error clearing cache: {str(e)}[/red]")
206213

207214

208-
async def main():
215+
async def main(debug: bool):
209216
"""Main execution function."""
210217
# Show cache status
211218
print(f"[blue]Cache directory: {CACHE_DIR} (size: {get_cache_size()})[/blue]")
@@ -236,7 +243,7 @@ async def main():
236243
# Get all members from all orgs
237244
all_members = {}
238245
for org in orgs:
239-
members = await get_org_members(session, org)
246+
members = await get_org_members(session, org, debug)
240247
for member in members:
241248
if member["login"] not in all_members:
242249
all_members[member["login"]] = []
@@ -245,7 +252,7 @@ async def main():
245252
# Get activity for each user
246253
tasks = []
247254
for username in all_members:
248-
task = get_user_activity(session, username)
255+
task = get_user_activity(session, username, debug)
249256
tasks.append((username, task))
250257

251258
results = await asyncio.gather(*(task for _, task in tasks))
@@ -258,14 +265,16 @@ async def main():
258265
username,
259266
datetime.fromisoformat(last_activity["__datetime__"])
260267
if last_activity is not None
261-
else datetime.fromtimestamp(0).replace(tzinfo=timezone.utc),
268+
else None,
262269
all_members[username],
263270
)
264271
)
265272

266273
for username, last_activity, user_orgs in sorted(
267274
user_activities,
268-
key=lambda x: x[1] if x[1] is not None else datetime.fromtimestamp(0),
275+
key=lambda x: (x[1], x[0])
276+
if x[1] is not None
277+
else (datetime.fromtimestamp(0).replace(tzinfo=timezone.utc), x[0]),
269278
reverse=True,
270279
):
271280
last_activity_ago = (
@@ -290,4 +299,4 @@ async def main():
290299
if args.clear_cache:
291300
clear_cache()
292301

293-
asyncio.run(main())
302+
asyncio.run(main(args.debug))

0 commit comments

Comments
 (0)