From f58bd419ba9ae88ea374d75a2bb80333e970f93f Mon Sep 17 00:00:00 2001 From: pseusys Date: Mon, 13 Feb 2023 01:27:28 +0100 Subject: [PATCH 1/5] new graph drawing backend --- loc.py | 5 +- make_bar_graph.py | 138 +++++++++++++++++----------------------------- requirements.txt | 7 +-- 3 files changed, 53 insertions(+), 97 deletions(-) diff --git a/loc.py b/loc.py index f4c31f0..5e98026 100644 --- a/loc.py +++ b/loc.py @@ -10,7 +10,7 @@ from io import StringIO, BytesIO from dotenv import load_dotenv import time -from make_bar_graph import BarGraph +from make_bar_graph import build_graph class LinesOfCode: @@ -34,8 +34,7 @@ class LinesOfCode: return yearly_data def plotLoc(self, yearly_data): - graph = BarGraph(yearly_data) - graph.build_graph() + build_graph(yearly_data) self.pushChart() def run_query_v3(self, endPoint): diff --git a/make_bar_graph.py b/make_bar_graph.py index 94f06cd..207f402 100644 --- a/make_bar_graph.py +++ b/make_bar_graph.py @@ -1,106 +1,68 @@ -import os -import pandas as pd +from typing import Dict +from os.path import join, dirname +from json import load + import numpy as np -import altair as alt -import json -import os -# npm install vega-lite vega-cli canvas +import matplotlib.patches as mpatches +import matplotlib.pyplot as plt -class BarGraph: +MAX_LANGUAGES = 5 - def __init__(self, yearly_data): - self.yearly_data = yearly_data - def build_graph(self): - - with open(os.path.join(os.path.dirname(__file__), 'colors.json')) as f: - colors = json.load(f) - allColorsValues = [] +def build_graph(yearly_data: Dict) -> str: + """ + Draws graph of lines of code written by user by quarters of years. + Picks top `MAX_LANGUAGES` languages from each quarter only. - # filter data - max_languages = 5 - top_languages = {} - for year in self.yearly_data.keys(): - for quarter in self.yearly_data[year].keys(): - for language in sorted(list(self.yearly_data[year][quarter].keys()), - key=lambda lang: self.yearly_data[year][quarter][lang], reverse=True)[ - 0:max_languages]: - if 'top' not in self.yearly_data[year][quarter]: - self.yearly_data[year][quarter]['top'] = {} - if self.yearly_data[year][quarter][language] != 0: - self.yearly_data[year][quarter]['top'][language] = self.yearly_data[year][quarter][language] + :param yearly_data: GitHub user yearly data. + :return: String, path to graph file. + """ + with open(join(dirname(__file__), "colors.json")) as f: + colors = load(f) - if language not in top_languages: - top_languages[language] = 1 - top_languages[language] += 1 + languages_all_loc = dict() + years = len(yearly_data.keys()) + year_indexes = np.arange(years) - # print(self.yearly_data) + for i, y in enumerate(sorted(yearly_data.keys())): + for q in yearly_data[y].keys(): + langs = sorted(yearly_data[y][q].keys(), key=lambda l: yearly_data[y][q][l], reverse=True)[0:MAX_LANGUAGES] - all_languages = list(top_languages.keys()) + for lang in langs: + if lang not in languages_all_loc: + languages_all_loc[lang] = np.array([[0] * years] * 4) + languages_all_loc[lang][q - 1][i] = yearly_data[y][q][lang] - for language in all_languages: - if colors[language]['color'] is not None: - allColorsValues.append(colors[language]['color']) + fig = plt.figure() + ax = fig.add_axes([0, 0, 1.5, 1]) - languages_all_loc = {} + language_handles = [] + cumulative = np.array([[0] * years] * 4) - for language in all_languages: - language_year = [] - for year in self.yearly_data.keys(): - language_quarter = [0, 0, 0, 0] - for quarter in self.yearly_data[year].keys(): - if language in self.yearly_data[year][quarter]['top']: - language_quarter[quarter - 1] = self.yearly_data[year][quarter]['top'][language] - else: - language_quarter[quarter - 1] = 0 - language_year.append(language_quarter) - languages_all_loc[language] = language_year + for key, value in languages_all_loc.items(): + color = colors[key]["color"] if colors[key]["color"] is not None else "w" + language_handles += [mpatches.Patch(color=color, label=key)] - # print(languages_all_loc) + for quarter in range(4): + ax.bar(year_indexes + quarter * 0.21, value[quarter], 0.2, bottom=cumulative[quarter], color=color) + cumulative[quarter] = np.add(cumulative[quarter], value[quarter]) - language_df = {} + ax.set_ylabel("LOC added", fontdict=dict(weight="bold")) + ax.set_xticks(np.array([np.arange(i, i + 0.84, step=0.21) for i in year_indexes]).flatten(), labels=["Q1", "Q2", "Q3", "Q4"] * years) - def prep_df(df, name): - df = df.stack().reset_index() - df.columns = ['c1', 'c2', 'values'] - df['Language'] = name - return df + sax = ax.secondary_xaxis("top") + sax.set_xticks(year_indexes + 0.42, labels=sorted(yearly_data.keys())) + sax.spines["top"].set_visible(False) - for language in languages_all_loc.keys(): - language_df[language] = pd.DataFrame(languages_all_loc[language], index=list(self.yearly_data.keys()), - columns=["Q1", "Q2", "Q3", "Q4"]) + ax.legend(title="Language", handles=language_handles, loc="upper left", bbox_to_anchor=(1, 1), framealpha=0, title_fontproperties=dict(weight="bold")) - for language in language_df.keys(): - language_df[language] = prep_df(language_df[language], language) + sax.tick_params(axis="both", length=0) + sax.spines["top"].set_visible(False) + ax.spines["top"].set_visible(False) + ax.spines["right"].set_visible(False) - df = pd.concat(language_df.values()) - - chart = alt.Chart(df).mark_bar().encode( - - # tell Altair which field to group columns on - x=alt.X('c2:N', title=None), - - # tell Altair which field to use as Y values and how to calculate - y=alt.Y('sum(values):Q', - axis=alt.Axis( - grid=False, - title='LOC added')), - - # tell Altair which field to use to use as the set of columns to be represented in each group - column=alt.Column('c1:N', title=None), - - # tell Altair which field to use for color segmentation - color=alt.Color('Language:N', - scale=alt.Scale( - domain=all_languages, - # make it look pretty with an enjoyable color pallet - range=allColorsValues, - ), - )) \ - .configure_view( - # remove grid lines around column clusters - strokeOpacity=0 - ) - chart.save('bar_graph.png') - return 'bar_graph.png' + plt.ylim(0, 1.05 * np.amax(cumulative)) + plt.savefig("bar_graph.png", bbox_inches="tight") + plt.close(fig) + return "bar_graph.png" diff --git a/requirements.txt b/requirements.txt index cdc1bf0..e4795bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,3 @@ -altair==4.1.0 -altair-data-server==0.4.1 -altair-saver==0.5.0 -altair-viewer==0.3.0 attrs==20.3.0 certifi==2020.12.5 chardet==4.0.0 @@ -14,9 +10,8 @@ Jinja2==2.11.3 jsonschema==3.2.0 kiwisolver==1.3.1 MarkupSafe==1.1.1 -matplotlib==3.4.1 +matplotlib==3.6.3 numpy==1.20.2 -pandas==1.2.3 Pillow==8.2.0 portpicker==1.3.1 PyGithub==1.54.1 From 1a56b76d87e949f291cb1d8c4e0a7c740ab62b4c Mon Sep 17 00:00:00 2001 From: pseusys Date: Mon, 13 Feb 2023 02:18:13 +0100 Subject: [PATCH 2/5] docker image size reduced --- Dockerfile | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 81ea2b3..927289a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,9 @@ -FROM nikolaik/python-nodejs:python3.9-nodejs16 +FROM python:3.9-slim ADD requirements.txt /requirements.txt +RUN pip3 install --upgrade pip wheel setuptools +RUN apk add --no-cache --virtual .build-deps g++ jpeg-dev zlib-dev libjpeg make && pip3 install -r requirements.txt && apk del .build-deps + ADD main.py /main.py ADD loc.py /loc.py ADD make_bar_graph.py /make_bar_graph.py @@ -9,10 +12,4 @@ ADD translation.json /translation.json ENV PATH "$PATH:/home/root/.npm-global/bin" -RUN python -m pip install --upgrade pip wheel setuptools -RUN pip install -r requirements.txt -RUN npm -g config set user root -RUN npm i -g npm@next-8 -RUN npm i -g vega vega-lite vega-cli canvas - ENTRYPOINT ["python", "/main.py"] From bce3676e50bdfcb09fefcfebd88c21caba995171 Mon Sep 17 00:00:00 2001 From: pseusys Date: Mon, 13 Feb 2023 02:18:42 +0100 Subject: [PATCH 3/5] docker npm root removed from path --- Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 927289a..33e0e35 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,4 @@ ADD make_bar_graph.py /make_bar_graph.py ADD colors.json /colors.json ADD translation.json /translation.json -ENV PATH "$PATH:/home/root/.npm-global/bin" - ENTRYPOINT ["python", "/main.py"] From 3a2d343ab18817c08d93bb09e7c67e88fac89344 Mon Sep 17 00:00:00 2001 From: Alexander Sergeev Date: Mon, 13 Feb 2023 10:26:47 +0100 Subject: [PATCH 4/5] dockerfile updated, alpine distribution used --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 33e0e35..18e593e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9-slim +FROM python:3.9-alpine ADD requirements.txt /requirements.txt RUN pip3 install --upgrade pip wheel setuptools From 94865cc731eea01a73ee15e48f35f07d02a3565a Mon Sep 17 00:00:00 2001 From: pseusys Date: Mon, 13 Feb 2023 18:47:39 +0100 Subject: [PATCH 5/5] image building proved and tested --- Dockerfile | 3 +-- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 18e593e..df5fbc1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,7 @@ FROM python:3.9-alpine ADD requirements.txt /requirements.txt -RUN pip3 install --upgrade pip wheel setuptools -RUN apk add --no-cache --virtual .build-deps g++ jpeg-dev zlib-dev libjpeg make && pip3 install -r requirements.txt && apk del .build-deps +RUN apk add --no-cache g++ jpeg-dev zlib-dev libjpeg make && pip3 install -r requirements.txt ADD main.py /main.py ADD loc.py /loc.py diff --git a/requirements.txt b/requirements.txt index e4795bf..8f0244c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,7 +11,7 @@ jsonschema==3.2.0 kiwisolver==1.3.1 MarkupSafe==1.1.1 matplotlib==3.6.3 -numpy==1.20.2 +numpy==1.24.2 Pillow==8.2.0 portpicker==1.3.1 PyGithub==1.54.1