Skip to content

Commit

Permalink
Update GitStats (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
R055A committed Jul 20, 2024
1 parent 2bcc41c commit 1fed32c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def __init__(
more_collaborators: Optional[str] = getenv("MORE_COLLABS"),
manually_added_repos: Optional[str] = getenv("MORE_REPOS"),
only_included_repos: Optional[str] = getenv("ONLY_INCLUDED"),
only_included_collab_repos: Optional[str] = getenv("ONLY_INCLUDED_COLLAB_REPOS"),
only_included_collab_repos: Optional[str] = getenv(
"ONLY_INCLUDED_COLLAB_REPOS"
),
exclude_collab_repos: Optional[str] = getenv("EXCLUDED_COLLAB_REPOS"),
more_collab_repos: Optional[str] = getenv("MORE_COLLAB_REPOS"),
):
Expand Down
9 changes: 6 additions & 3 deletions src/generate_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,20 @@ async def generate_overview(self) -> None:
)[1]
output = sub("{{ lines_changed }}", f"{changed:,}", output)

avg_contribution_percent = await self.__stats.avg_contribution_percent
avg_contribution_percent = (
f"{await self.__stats.avg_contribution_percent} "
f"[{await self.__stats.avg_contribution_percent_weighted}]"
)
output = sub("{{ avg_contribution_percent }}", avg_contribution_percent, output)

num_repos = len(await self.__stats.repos)
# num_owned_repos = len(await self.__stats.owned_repos)
num_owned_repos = len(await self.__stats.owned_repos)
repos = (
num_repos
if len(str(num_repos)) < TXT_SPACER_MAX_LEN
else add_unit(num_repos)
)
repos = f"{repos:,}" # [{'%g' % round(num_owned_repos / num_repos * 100, 1)}%]"
repos = f"{repos:,} [{'%g' % round(num_owned_repos / num_repos * 100, 1)}%]"
output = sub("{{ repos }}", repos, output)

collaborators_and_contributors = f"{await self.__stats.collaborators:,}"
Expand Down
55 changes: 33 additions & 22 deletions src/github_repo_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, environment_vars: EnvironmentVariables, session: ClientSessio
self._owned_repos: Optional[Set[str]] = None
self._users_lines_changed: Optional[Tuple[int, int]] = None
self._avg_percent: Optional[str] = None
self._avg_percent_weighted: Optional[str] = None
self._views: Optional[int] = None
self._collaborators: Optional[int] = None
self._collaborator_set: Optional[Set[str]] = None
Expand All @@ -63,26 +64,9 @@ async def to_str(self) -> str:

users_lines_changed = await self.lines_changed
avg_percent = await self.avg_contribution_percent
avg_percent_weighted = await self.avg_contribution_percent_weighted
contributors = max(len(await self.contributors) - 1, 0)

# return f"""GitHub Repository Statistics:
# Stargazers: {await self.stargazers:,}
# Forks: {await self.forks:,}
# Pull requests: {await self.pull_requests:,}
# Issues: {await self.issues:,}
# All-time contributions: {await self.total_contributions:,}
# Repositories with contributions: {len(await self.repos):,}
# Lines of code added: {users_lines_changed[0]:,}
# Lines of code deleted: {users_lines_changed[1]:,}
# Total lines of code changed: {sum(users_lines_changed):,}
# Avg. % of contributions (per collab repo): {avg_percent}
# Project page views: {await self.views:,}
# Project page views from date: {await self.views_from_date}
# Project repository collaborators: {await self.collaborators:,}
# Project repository contributors: {contributors:,}
# Total number of languages: {len(list(languages.keys()))} (+{len(await self.excluded_languages):,})
# Languages:\n\t\t\t- {formatted_languages}"""

return f"""GitHub Repository Statistics:
Stargazers: {await self.stargazers:,}
Forks: {await self.forks:,}
Expand All @@ -92,6 +76,7 @@ async def to_str(self) -> str:
Lines of code deleted: {users_lines_changed[1]:,}
Total lines of code changed: {sum(users_lines_changed):,}
Avg. % of contributions (per collab repo): {avg_percent}
Avg. % of contributions (per collab repo) weighted by number of contributors (max 100): {avg_percent_weighted}
Project page views: {await self.views:,}
Project page views from date: {await self.views_from_date}
Project repository collaborators: {await self.collaborators:,}
Expand Down Expand Up @@ -423,6 +408,7 @@ async def lines_changed(self) -> Tuple[int, int]:
contributor_set = set()
repo_total_changes_arr = []
author_contribution_percentages = []
author_contribution_percentages_weighted = []
author_total_additions = 0
author_total_deletions = 0

Expand All @@ -431,7 +417,8 @@ async def lines_changed(self) -> Tuple[int, int]:
for repo in await self.repos:
if repo in self._empty_repos:
continue
num_repo_collabs = 1
repo_contributors = set()
repo_contributors.add(self.environment_vars.username)
other_authors_total_changes = 0
author_additions = 0
author_deletions = 0
Expand All @@ -454,7 +441,7 @@ async def lines_changed(self) -> Tuple[int, int]:
for week in author_obj.get("weeks", []):
other_authors_total_changes += week.get("a", 0)
other_authors_total_changes += week.get("d", 0)
num_repo_collabs += 1
repo_contributors.add(author)
else:
for week in author_obj.get("weeks", []):
author_additions += week.get("a", 0)
Expand All @@ -465,7 +452,11 @@ async def lines_changed(self) -> Tuple[int, int]:
# calculate average author's contributions to each repository with at least one other collaborator
if (
repo not in self.environment_vars.exclude_collab_repos
and (not exclusive_collab_repos or repo in exclusive_collab_repos or repo in slave_status_repos)
and (
not exclusive_collab_repos
or repo in exclusive_collab_repos
or repo in slave_status_repos
)
and (author_additions + author_deletions) > 0
and (
other_authors_total_changes > 0
Expand All @@ -480,14 +471,23 @@ async def lines_changed(self) -> Tuple[int, int]:
author_contribution_percentages.append(
(author_additions + author_deletions) / repo_total_changes
)
author_contribution_percentages_weighted.append(
author_contribution_percentages[-1]
/ (
1
/ len(repo_contributors)
* (2 if len(repo_contributors) > 1 else 1)
)
)
repo_total_changes_arr.append(repo_total_changes)

repos.append(repo)

if sum(author_contribution_percentages) > 0:
self._avg_percent = f"{(sum(author_contribution_percentages) / len(repo_total_changes_arr) * 100):0.2f}%"
self._avg_percent_weighted = f"{(sum(author_contribution_percentages_weighted) / len(repo_total_changes_arr) * 100):0.2f}%"
else:
self._avg_percent = "N/A"
self._avg_percent_weighted = self._avg_percent = "N/A"

self._contributors = contributor_set

Expand All @@ -505,6 +505,17 @@ async def avg_contribution_percent(self) -> str:
assert self._avg_percent is not None
return self._avg_percent

@property
async def avg_contribution_percent_weighted(self) -> str:
"""
:return: str representing the avg percent of user's repo contributions weighted by number of contributors
"""
if self._avg_percent_weighted is not None:
return self._avg_percent_weighted
await self.lines_changed
assert self._avg_percent_weighted is not None
return self._avg_percent_weighted

@property
async def views(self) -> int:
"""
Expand Down
4 changes: 2 additions & 2 deletions src/templates/overview.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/git_stats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
MORE_COLLABS = getenv("MORE_COLLABS") # or enter: "<int>"
MORE_REPOS = getenv("MORE_REPOS") # or enter: "[owner/repo],...,[owner/repo]"
ONLY_INCLUDED = getenv("ONLY_INCLUDED") # or enter: "[owner/repo],..."
ONLY_INCLUDED_COLLAB_REPOS = getenv(
"ONLY_INCLUDED_COLLAB_REPOS"
) # or enter: "[owner/repo],..."
EXCLUDED_COLLAB_REPOS = getenv("EXCLUDED_COLLAB_REPOS") # or enter: "[owner/repo],..."
MORE_COLLAB_REPOS = getenv("MORE_COLLAB_REPOS") # or enter: "[owner/repo],..."

Expand Down Expand Up @@ -62,6 +65,7 @@ async def main() -> None:
more_collaborators=MORE_COLLABS,
manually_added_repos=MORE_REPOS,
only_included_repos=ONLY_INCLUDED,
only_included_collab_repos=ONLY_INCLUDED_COLLAB_REPOS,
exclude_collab_repos=EXCLUDED_COLLAB_REPOS,
more_collab_repos=MORE_COLLAB_REPOS,
),
Expand Down

0 comments on commit 1fed32c

Please sign in to comment.