From e563addad94bd64be46f50dafa024bad27bf28c6 Mon Sep 17 00:00:00 2001 From: pseusys Date: Tue, 28 Feb 2023 00:28:51 +0100 Subject: [PATCH] graph height fixed, local yearly data caching added --- sources/graphics_chart_drawer.py | 4 +++- sources/main.py | 4 ++-- sources/manager_file.py | 33 +++++++++++++++++++++++++---- sources/manager_github.py | 6 ++++-- sources/yearly_commit_calculator.py | 11 +++++++++- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/sources/graphics_chart_drawer.py b/sources/graphics_chart_drawer.py index d408ae7..a8d9562 100644 --- a/sources/graphics_chart_drawer.py +++ b/sources/graphics_chart_drawer.py @@ -64,6 +64,8 @@ async def create_loc_graph(yearly_data: Dict, save_path: str): ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) - plt.ylim(top=1.1 * amax(cumulative[:, 0]), bottom=-1.5 * amax(cumulative[:, 1])) + joined = cumulative.reshape(-1, cumulative.shape[-1]) + plt.ylim(top=1.05 * amax(joined[:, 0]), bottom=-1.05 * amax(joined[:, 1])) + plt.savefig(save_path, bbox_inches="tight") plt.close(fig) diff --git a/sources/main.py b/sources/main.py index 7bab6ab..ff571ee 100644 --- a/sources/main.py +++ b/sources/main.py @@ -188,8 +188,8 @@ async def main(): if GHM.update_readme(stats): DBM.g("Readme updated!") else: - GHM.set_github_output(stats) - DBM.g("Debug run, readme not updated. Check the latest comment for the generated stats.") + if GHM.set_github_output(stats): + DBM.g("Debug run, readme not updated. Check the latest comment for the generated stats.") await DM.close_remote_resources() diff --git a/sources/manager_file.py b/sources/manager_file.py index 741b681..4ffe203 100644 --- a/sources/manager_file.py +++ b/sources/manager_file.py @@ -1,6 +1,7 @@ -from json import load -from os.path import join -from typing import Dict +from os.path import join, isfile +from pickle import load as load_pickle, dump as dump_pickle +from json import load as load_json +from typing import Dict, Optional from manager_environment import EnvironmentManager as EM @@ -29,7 +30,7 @@ class FileManager: :param file: Localization file path, related to current file (in sources root). """ with open(join("sources", file), encoding="utf-8") as config_file: - data = load(config_file) + data = load_json(config_file) FileManager._LOCALIZATION = data[EM.LOCALE] @staticmethod @@ -55,3 +56,27 @@ class FileManager: name = join("assets", name) if assets else name with open(name, "a" if append else "w", encoding="utf-8") as file: file.write(content) + + @staticmethod + def cache_binary(name: str, content: Optional[Dict] = None, assets: bool = False) -> Optional[Dict]: + """ + Save binary output file if provided or read if content is None. + + :param name: File name. + :param content: File content (utf-8 string) or None. + :param assets: True for saving to 'assets' directory, false otherwise. + :returns: File cache contents if content is None, None otherwise. + """ + name = join("assets", name) if assets else name + if content is None and not isfile(name): + return None + + with open(name, "rb" if content is None else "wb") as file: + if content is None: + try: + return load_pickle(file) + except Exception: + return None + else: + dump_pickle(content, file) + return None diff --git a/sources/manager_github.py b/sources/manager_github.py index 8bff82a..ba68cd3 100644 --- a/sources/manager_github.py +++ b/sources/manager_github.py @@ -106,7 +106,7 @@ class GitHubManager: return False @staticmethod - def set_github_output(stats: str): + def set_github_output(stats: str) -> bool: """ Outputs readme data as current action output instead of committing it. @@ -114,13 +114,15 @@ class GitHubManager: """ DBM.i("Setting README contents as action output...") if "GITHUB_OUTPUT" not in environ.keys(): - raise Exception("Not in GitHub environment ('GITHUB_OUTPUT' not defined)!") + DBM.p("Not in GitHub environment, not setting action output!") + return False prefix = "README stats current output:" eol = "".join(choice(ascii_letters) for _ in range(10)) FM.write_file(environ["GITHUB_OUTPUT"], f"README_CONTENT<<{eol}\n{prefix}\n\n{stats}\n{eol}\n", append=True) DBM.g("Action output set!") + return True @staticmethod def update_chart(chart_path: str) -> str: diff --git a/sources/yearly_commit_calculator.py b/sources/yearly_commit_calculator.py index 9fd528c..fb714da 100644 --- a/sources/yearly_commit_calculator.py +++ b/sources/yearly_commit_calculator.py @@ -19,9 +19,16 @@ async def calculate_yearly_commit_data(repositories: Dict) -> Dict: :returns: Commit quarter yearly data dictionary. """ DBM.i("Calculating yearly commit data...") + if EM.DEBUG_RUN: + content = FM.cache_binary("yearly_data.pick", assets=True) + if content is not None: + DBM.g("Yearly data restored from cache!") + return content + else: + DBM.w("No cached yearly data found, recalculating...") + yearly_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']}" @@ -30,7 +37,9 @@ async def calculate_yearly_commit_data(repositories: Dict) -> Dict: DBM.g("Yearly 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