Skip to content

Commit 2d6a10c

Browse files
committed
Refactor GitHub statistics calculations to streamline stargazers and forks retrieval. Introduce a new method for fetching total commits from all years, enhancing overall statistics accuracy. Update properties to utilize the new summary statistics method.
1 parent 3ee7179 commit 2d6a10c

File tree

1 file changed

+72
-21
lines changed

1 file changed

+72
-21
lines changed

github_stats.py

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ async def get_stats(self) -> None:
412412
"""
413413
Get lots of summary statistics using one big query. Sets many attributes
414414
"""
415-
self._stargazers = 0
416-
self._forks = 0
415+
# Não inicializar stargazers e forks aqui, pois usaremos get_summary_stats
417416
self._languages = dict()
418417
self._repos = set()
419418

@@ -468,9 +467,7 @@ async def get_stats(self) -> None:
468467
self._repos.add(name)
469468
processed_repos += 1
470469

471-
# Corrigir: descomentar e ajustar a contagem de stars e forks
472-
self._stargazers += repo.get("stargazers", {}).get("totalCount", 0)
473-
self._forks += repo.get("forkCount", 0)
470+
# Não contar stars e forks aqui - usamos get_summary_stats para isso
474471

475472
for lang in repo.get("languages", {}).get("edges", []):
476473
lang_name = lang.get("node", {}).get("name", "Other")
@@ -501,8 +498,6 @@ async def get_stats(self) -> None:
501498
break
502499

503500
print(f"Total repositories found: {len(self._repos)}")
504-
print(f"Total stars: {self._stargazers}")
505-
print(f"Total forks: {self._forks}")
506501
print(f"Languages found: {len(self._languages)}")
507502

508503
langs_total = sum([v.get("size", 0) for v in self._languages.values()])
@@ -527,26 +522,20 @@ async def stargazers(self) -> int:
527522
"""
528523
if self._stargazers is not None:
529524
return self._stargazers
530-
await self.get_stats()
525+
await self.get_summary_stats()
531526
assert self._stargazers is not None
532527
return self._stargazers
533528

534529
@property
535530
async def forks(self) -> int:
536531
"""
537-
:return: total number of forks on user's repos + forks made by user
532+
:return: total number of forks on user's repos
538533
"""
539-
if self._forks is None:
540-
await self.get_stats()
534+
if self._forks is not None:
535+
return self._forks
536+
await self.get_summary_stats()
541537
assert self._forks is not None
542-
forks_received = self._forks
543-
544-
forks_made = await self.forks_made
545-
546-
total_forks = forks_received + forks_made
547-
print(f"Total forks: {forks_received} received + {forks_made} made = {total_forks}")
548-
549-
return total_forks
538+
return self._forks
550539

551540
@property
552541
async def languages(self) -> Dict:
@@ -698,11 +687,11 @@ async def views(self) -> int:
698687
@property
699688
async def total_commits(self) -> int:
700689
"""
701-
Get the total number of commits made by the user.
690+
Get the total number of commits made by the user from all years.
702691
"""
703692
if self._total_commits is not None:
704693
return self._total_commits
705-
await self.get_summary_stats()
694+
await self.get_all_time_commits()
706695
assert self._total_commits is not None
707696
return self._total_commits
708697

@@ -763,6 +752,68 @@ async def forks_made(self) -> int:
763752
assert self._forks_made is not None
764753
return self._forks_made
765754

755+
async def get_all_time_commits(self) -> None:
756+
"""
757+
Get total commits from all years
758+
"""
759+
if self._total_commits is not None:
760+
return
761+
762+
print("Fetching commits from all years...")
763+
764+
# Primeiro, pegar os commits do contributionsCollection (ano atual)
765+
await self.get_summary_stats()
766+
current_year_commits = self._total_commits or 0
767+
768+
# Depois, buscar commits de todos os anos
769+
years = (
770+
(await self.queries.query(Queries.contrib_years()))
771+
.get("data", {})
772+
.get("viewer", {})
773+
.get("contributionsCollection", {})
774+
.get("contributionYears", [])
775+
)
776+
777+
total_commits = 0
778+
779+
# Para cada ano, buscar o total de commits
780+
for year in years:
781+
query = f"""
782+
query {{
783+
viewer {{
784+
contributionsCollection(from: "{year}-01-01T00:00:00Z", to: "{int(year) + 1}-01-01T00:00:00Z") {{
785+
totalCommitContributions
786+
restrictedContributionsCount
787+
}}
788+
}}
789+
}}
790+
"""
791+
result = await self.queries.query(query)
792+
if result:
793+
contrib = result.get("data", {}).get("viewer", {}).get("contributionsCollection", {})
794+
year_commits = (
795+
contrib.get("totalCommitContributions", 0) +
796+
contrib.get("restrictedContributionsCount", 0)
797+
)
798+
total_commits += year_commits
799+
print(f"Year {year}: {year_commits} commits")
800+
801+
self._total_commits = total_commits
802+
print(f"Total commits from all years: {total_commits}")
803+
804+
@property
805+
async def total_forks(self) -> int:
806+
"""
807+
:return: total number of forks on user's repos + forks made by user
808+
"""
809+
forks_received = await self.forks
810+
forks_made = await self.forks_made
811+
812+
total = forks_received + forks_made
813+
print(f"Total forks: {forks_received} received + {forks_made} made = {total}")
814+
815+
return total
816+
766817
###############################################################################
767818
# Main Function
768819
###############################################################################

0 commit comments

Comments
 (0)