Skip to content

Commit 8d86d8d

Browse files
committed
fix: final gemini performance tweaks (pre-calculate date + asyncio.gather)
1 parent c8dc7e7 commit 8d86d8d

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

modules/github_fetcher.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ async def fetch_user_profile(username: str) -> dict:
6262
raise ValueError(f"Invalid GitHub username: '{username}'")
6363

6464
one_year_ago = (datetime.now() - timedelta(days=365)).isoformat() + 'Z'
65+
one_year_ago_dt = datetime.now() - timedelta(days=365) # pre-calculated for loops
6566

6667
graphql_query = {
6768
"query": f"""
@@ -112,11 +113,11 @@ async def fetch_user_profile(username: str) -> dict:
112113

113114
pr_merged_last_year = sum(
114115
1 for pr in graphql_data.get('pullRequests', {}).get('nodes', [])
115-
if pr and datetime.strptime(pr['createdAt'], '%Y-%m-%dT%H:%M:%SZ') > datetime.now() - timedelta(days=365)
116+
if pr and datetime.strptime(pr['createdAt'], '%Y-%m-%dT%H:%M:%SZ') > one_year_ago_dt
116117
)
117118
issues_closed_last_year = sum(
118119
1 for issue in graphql_data.get('issues', {}).get('nodes', [])
119-
if issue and datetime.strptime(issue['createdAt'], '%Y-%m-%dT%H:%M:%SZ') > datetime.now() - timedelta(days=365)
120+
if issue and datetime.strptime(issue['createdAt'], '%Y-%m-%dT%H:%M:%SZ') > one_year_ago_dt
120121
)
121122

122123
return {

utils/user.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,36 @@ async def get_user_data(username: str, force: bool = True) -> dict:
6060
if res.status_code == 200:
6161
return res.json()
6262

63-
profile_data = await GitHubProfileFetcher.fetch_user_profile(username)
63+
# Parallel fetch profile + contributions
64+
profile_data, contributions_data = await asyncio.gather(
65+
GitHubProfileFetcher.fetch_user_profile(username),
66+
asyncio.to_thread(
67+
GitHubContributionsFetcher.fetch_recent_contributions,
68+
username,
69+
Settings.CONTRIBUTION_DAYS
70+
)
71+
)
6472

6573
# Early return if GitHub fetch failed
6674
if "error" in profile_data:
6775
return profile_data
6876

69-
contributions_data = await asyncio.to_thread(
70-
GitHubContributionsFetcher.fetch_recent_contributions,
71-
username,
72-
Settings.CONTRIBUTION_DAYS
73-
)
74-
7577
ai_generator = AIDescriptionGenerator()
78+
79+
# Parallel AI summary generation
7680
try:
77-
profile_summary = await asyncio.to_thread(
78-
ai_generator.generate_profile_summary, profile_data
81+
profile_summary, activity_summary = await asyncio.gather(
82+
asyncio.to_thread(ai_generator.generate_profile_summary, profile_data),
83+
asyncio.to_thread(ai_generator.generate_activity_summary, contributions_data) if contributions_data else None,
84+
return_exceptions=True
7985
)
8086
except Exception as e:
81-
logger.exception("Failed to generate profile summary for user %s", username)
87+
logger.exception("Failed to generate AI summaries for user %s", username)
8288
profile_summary = None
89+
activity_summary = None
8390

8491
if contributions_data:
85-
try:
86-
activity_summary = await asyncio.to_thread(
87-
ai_generator.generate_activity_summary, contributions_data
88-
)
89-
profile_data['activity_summary'] = activity_summary if activity_summary else {}
90-
except Exception as e:
91-
logger.exception("Failed to generate activity summary for user %s", username)
92-
profile_data['activity_summary'] = {}
92+
profile_data['activity_summary'] = activity_summary if activity_summary else {}
9393

9494
profile_data['profile_summary'] = profile_summary
9595
return profile_data

0 commit comments

Comments
 (0)