You've already forked wakapi-readme-stats
github api queries revised and refactored
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from json import dumps
|
||||
from re import search
|
||||
from datetime import datetime
|
||||
from typing import Dict
|
||||
from typing import Dict, Tuple
|
||||
|
||||
from manager_download import DownloadManager as DM
|
||||
from manager_environment import EnvironmentManager as EM
|
||||
@@ -10,7 +10,7 @@ from manager_file import FileManager as FM
|
||||
from manager_debug import DebugManager as DBM
|
||||
|
||||
|
||||
async def calculate_yearly_commit_data(repositories: Dict) -> Dict:
|
||||
async def calculate_commit_data(repositories: Dict) -> Tuple[Dict, Dict]:
|
||||
"""
|
||||
Calculate commit data by years.
|
||||
Commit data includes contribution additions and deletions in each quarter of each recorded year.
|
||||
@@ -18,38 +18,40 @@ async def calculate_yearly_commit_data(repositories: Dict) -> Dict:
|
||||
:param repositories: user repositories info dictionary.
|
||||
:returns: Commit quarter yearly data dictionary.
|
||||
"""
|
||||
DBM.i("Calculating yearly commit data...")
|
||||
DBM.i("Calculating commit data...")
|
||||
if EM.DEBUG_RUN:
|
||||
content = FM.cache_binary("yearly_data.pick", assets=True)
|
||||
content = FM.cache_binary("commits_data.pick", assets=True)
|
||||
if content is not None:
|
||||
DBM.g("Yearly data restored from cache!")
|
||||
return content
|
||||
DBM.g("Commit data restored from cache!")
|
||||
return tuple(content)
|
||||
else:
|
||||
DBM.w("No cached yearly data found, recalculating...")
|
||||
DBM.w("No cached commit data found, recalculating...")
|
||||
|
||||
yearly_data = dict()
|
||||
date_data = dict()
|
||||
total = len(repositories["data"]["user"]["repositories"]["nodes"])
|
||||
for ind, repo in enumerate(repositories["data"]["user"]["repositories"]["nodes"]):
|
||||
if repo["name"] not in EM.IGNORED_REPOS:
|
||||
repo_name = "[private]" if repo["isPrivate"] else f"{repo['owner']['login']}/{repo['name']}"
|
||||
DBM.i(f"\t{ind + 1}/{total} Retrieving repo: {repo_name}")
|
||||
await update_yearly_data_with_commit_stats(repo, yearly_data)
|
||||
DBM.g("Yearly commit data calculated!")
|
||||
await update_data_with_commit_stats(repo, yearly_data, date_data)
|
||||
DBM.g("Commit data calculated!")
|
||||
|
||||
if EM.DEBUG_RUN:
|
||||
FM.cache_binary("yearly_data.pick", yearly_data, assets=True)
|
||||
FM.write_file("yearly_data.json", dumps(yearly_data), assets=True)
|
||||
DBM.g("Yearly data saved to cache!")
|
||||
return yearly_data
|
||||
FM.cache_binary("commits_data.pick", [yearly_data, date_data], assets=True)
|
||||
FM.write_file("commits_data.json", dumps([yearly_data, date_data]), assets=True)
|
||||
DBM.g("Commit data saved to cache!")
|
||||
return yearly_data, date_data
|
||||
|
||||
|
||||
async def update_yearly_data_with_commit_stats(repo_details: Dict, yearly_data: Dict):
|
||||
async def update_data_with_commit_stats(repo_details: Dict, yearly_data: Dict, date_data: Dict):
|
||||
"""
|
||||
Updates yearly commit data with commits from given repository.
|
||||
Skips update if the commit isn't related to any repository.
|
||||
|
||||
:param repo_details: Dictionary with information about the given repository.
|
||||
:param yearly_data: Yearly data dictionary to update.
|
||||
:param date_data: Commit date dictionary to update.
|
||||
"""
|
||||
owner = repo_details["owner"]["login"]
|
||||
branch_data = await DM.get_remote_graphql("repo_branch_list", owner=owner, name=repo_details["name"])
|
||||
@@ -64,6 +66,12 @@ async def update_yearly_data_with_commit_stats(repo_details: Dict, yearly_data:
|
||||
curr_year = datetime.fromisoformat(date).year
|
||||
quarter = (datetime.fromisoformat(date).month - 1) // 3 + 1
|
||||
|
||||
if repo_details["name"] not in date_data:
|
||||
date_data[repo_details["name"]] = dict()
|
||||
if branch["name"] not in date_data[repo_details["name"]]:
|
||||
date_data[repo_details["name"]][branch["name"]] = dict()
|
||||
date_data[repo_details["name"]][branch["name"]][commit["oid"]] = commit["committedDate"]
|
||||
|
||||
if repo_details["primaryLanguage"] is not None:
|
||||
if curr_year not in yearly_data:
|
||||
yearly_data[curr_year] = dict()
|
||||
|
||||
Reference in New Issue
Block a user