Skip to content

Commit

Permalink
New LanguageMetric subclass and bugfixes
Browse files Browse the repository at this point in the history
Signed-off-by: CreativeNick <[email protected]>
  • Loading branch information
CreativeNick committed Aug 6, 2024
1 parent 8b4132a commit 70e6f07
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
36 changes: 36 additions & 0 deletions scripts/metricsLib/metrics_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,39 @@ def parse_commits_by_month(**kwargs):
commits_by_month[month] = 1

return {"commits_by_month": commits_by_month}

class LanguageMetric(BaseMetric):
"""
Class to fetch and process language data for a GitHub repository.
...
This class overrides the get_values method to handle the GitHub API's
language endpoint, which returns a dictionary of languages and their
byte counts. It calculates the percentage of each language based on
the total bytes of code in the repository.
Attributes
----------
Inherits all attributes from BaseMetric.
Methods
----------
get_values(params=None): Fetches language data and calculates
percentages.
"""
def __init__(self, name, params, url, token=None):
super().__init__(name, params, url, {}, token)

def get_values(self, params=None):
languages_data = self.hit_metric(params=params)
total_bytes = sum(languages_data.values())

language_percentages = {}
for lang, bytes_count in languages_data.items():
if total_bytes > 0:
percentage = (bytes_count / total_bytes) * 100
else:
percentage = 0
language_percentages[lang] = percentage

return {"languages": language_percentages}
13 changes: 6 additions & 7 deletions scripts/metricsLib/metrics_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from metricsLib.metrics_data_structures import CustomMetric, parse_commits_by_month, RangeMetric
from metricsLib.metrics_data_structures import GraphQLMetric, LengthMetric, ResourceMetric, BaseMetric
from metricsLib.metrics_data_structures import ListMetric, parse_nadia_label_into_badge
from metricsLib.metrics_data_structures import BaseMetric
from metricsLib.metrics_data_structures import BaseMetric, LanguageMetric
from metricsLib.constants import TOKEN, AUGUR_HOST

# The general procedure is to execute all metrics against all repos and orgs
Expand All @@ -24,7 +24,7 @@
RESOURCE_METRICS = []

# Predominant Languages Endpoint (ex. https://api.github.com/repos/chaoss/augur/languages)
LANGUAGE_ENDPOINT = "https://api.github.com/repos/chaoss/augur/languages"
LANGUAGE_ENDPOINT = "https://api.github.com/repos/{owner}/{repo}/languages"

REPO_GITHUB_GRAPHQL_QUERY = """
query ($repo: String!, $owner: String!) {
Expand Down Expand Up @@ -119,11 +119,10 @@
{"total_project_blank_lines": ["blank_lines"],
"average_blank_lines": ["avg_blank_lines"]}))

SIMPLE_METRICS.append(ListMetric("repositoryLanguages",
["owner", "repo"],
LANGUAGE_ENDPOINT,
{"languages": None},
token=TOKEN))
SIMPLE_METRICS.append(LanguageMetric("repositoryLanguages",
["owner", "repo"],
LANGUAGE_ENDPOINT,
token=TOKEN))

SIMPLE_METRICS.append(GraphQLMetric("githubGraphqlSimpleCounts", ["repo", "owner"],
REPO_GITHUB_GRAPHQL_QUERY,
Expand Down

0 comments on commit 70e6f07

Please sign in to comment.