Skip to content

Commit 6a5cafd

Browse files
committed
working proto
1 parent 54a7dda commit 6a5cafd

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

tools/last_user_activity.py

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@
1919
import argparse
2020

2121
orgs = [
22-
# "binder-examples",
23-
# "binderhub-ci-repos",
22+
"binder-examples",
23+
"binderhub-ci-repos",
2424
"ipython",
25-
# "jupyter",
26-
# "jupyter-book",
27-
# "jupyter-governance",
28-
# "jupyter-incubator",
29-
# "jupyter-server",
30-
# "jupyter-standards",
31-
# "jupyter-widgets",
32-
# "jupyterhub",
33-
# "jupyterlab",
34-
# "jupyter-xeus",
35-
# "jupytercon",
36-
# "voila-dashboards",
37-
# "voila-gallery",
25+
"jupyter",
26+
"jupyter-book",
27+
"jupyter-governance",
28+
"jupyter-incubator",
29+
"jupyter-server",
30+
"jupyter-standards",
31+
"jupyter-widgets",
32+
"jupyterhub",
33+
"jupyterlab",
34+
"jupyter-xeus",
35+
"jupytercon",
36+
"voila-dashboards",
37+
"voila-gallery",
3838
]
3939

4040
token = os.getenv("GH_TOKEN")
@@ -47,9 +47,32 @@
4747
"Accept": "application/vnd.github.v3+json",
4848
}
4949

50+
class DateTimeCache(diskcache.Cache):
51+
"""Custom cache class that handles datetime serialization."""
52+
53+
def __setitem__(self, key, value):
54+
"""Override to serialize datetime objects."""
55+
if isinstance(value, datetime):
56+
value = {'__datetime__': value.isoformat()}
57+
super().__setitem__(key, value)
58+
59+
def __getitem__(self, key):
60+
"""Override to deserialize datetime objects."""
61+
value = super().__getitem__(key)
62+
if isinstance(value, dict) and '__datetime__' in value:
63+
return datetime.fromisoformat(value['__datetime__'])
64+
return value
65+
66+
def get(self, key, default=None, retry=False):
67+
"""Override to handle datetime deserialization in get method with retry."""
68+
try:
69+
return super().get(key, default=default, retry=retry)
70+
except KeyError:
71+
return default
72+
5073
# Configure DiskCache in the current directory
5174
CACHE_DIR = "github_cache"
52-
cache = diskcache.Cache(CACHE_DIR)
75+
cache = DateTimeCache(CACHE_DIR)
5376

5477
async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict]:
5578
"""Fetch all members of a GitHub organization with caching.
@@ -77,8 +100,8 @@ async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict
77100
"""
78101
cache_key = f"org_members_{org}"
79102

80-
# Try to get from cache
81-
cached_data = cache.get(cache_key)
103+
# Try to get from cache with retry
104+
cached_data = cache.get(cache_key, retry=True)
82105
if cached_data is not None:
83106
print(f"[cyan]Cache hit for {org} members[/cyan]")
84107
return cached_data
@@ -101,7 +124,7 @@ async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict
101124
members.extend(page_members)
102125

103126
# Cache the results
104-
cache.set(cache_key, members, expire=3600 * 24) # 24 hours
127+
cache[cache_key] = members # Using __setitem__ instead of set()
105128
print(f"[green]Cached {len(members)} members for {org}[/green]")
106129
return members
107130

@@ -131,12 +154,12 @@ async def get_user_activity(session: aiohttp.ClientSession, username: str) -> Op
131154
if events:
132155
last_activity = datetime.fromisoformat(events[0]["created_at"].replace('Z', '+00:00'))
133156
# Cache the results
134-
cache.set(cache_key, last_activity, expire=3600 * 24) # 24 hours
157+
cache[cache_key] = last_activity # Using __setitem__ instead of set()
135158
print(f"[green]Cached activity for {username}[/green]")
136159
return last_activity
137160
else:
138161
print(f"[yellow]No activity found for {username}[/yellow]")
139-
cache.set(cache_key, None, expire=3600 * 24)
162+
cache[cache_key] = None # Using __setitem__ instead of set()
140163
else:
141164
print(f"[red]Error fetching activity for {username}: {response.status}[/red]")
142165
except Exception as e:
@@ -205,11 +228,10 @@ async def main():
205228
# Print results sorted by last activity
206229
user_activities = []
207230
for (username, _), last_activity in zip(tasks, results):
208-
if last_activity:
209-
user_activities.append((username, last_activity, all_members[username]))
231+
user_activities.append((username, last_activity, all_members[username]))
210232

211-
for username, last_activity, user_orgs in sorted(user_activities, key=lambda x: x[1], reverse=True):
212-
last_activity_ago = humanize.naturaltime(datetime.now(last_activity.tzinfo) - last_activity)
233+
for username, last_activity, user_orgs in sorted(user_activities, key=lambda x: x[1] if x[1] is not None else datetime.fromtimestamp(0).astimezone(datetime.now().tzinfo), reverse=True):
234+
last_activity_ago = humanize.naturaltime(datetime.now(last_activity.tzinfo) - last_activity) if last_activity else "[red]never[/red]"
213235
orgs_str = ", ".join(user_orgs)
214236
print(f"{username:<20}: Last activity {last_activity_ago} in orgs: {orgs_str}")
215237

0 commit comments

Comments
 (0)