You've already forked wakapi-readme-stats
FEAT: NEW GRAPH MODULE
This commit is contained in:
@@ -52,6 +52,15 @@ inputs:
|
|||||||
description: "Show the Total Lines of code written Badge till date"
|
description: "Show the Total Lines of code written Badge till date"
|
||||||
default: "False"
|
default: "False"
|
||||||
|
|
||||||
|
SHOW_LANGUAGE_PER_REPO:
|
||||||
|
required: false
|
||||||
|
description: ""
|
||||||
|
default: "True"
|
||||||
|
|
||||||
|
SHOW_LOC_CHART:
|
||||||
|
required: false
|
||||||
|
description: ""
|
||||||
|
default: "True"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
98
loc.py
98
loc.py
@@ -6,7 +6,7 @@ from github import Github
|
|||||||
import datetime
|
import datetime
|
||||||
from string import Template
|
from string import Template
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
from io import StringIO,BytesIO
|
from io import StringIO, BytesIO
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -15,70 +15,70 @@ from make_bar_graph import BarGraph
|
|||||||
|
|
||||||
class LinesOfCode:
|
class LinesOfCode:
|
||||||
|
|
||||||
def __init__(self,id,username,ghtoken, repositoryData):
|
def __init__(self, id, username, ghtoken, repositoryData):
|
||||||
self.id=id
|
self.id = id
|
||||||
self.username=username
|
self.username = username
|
||||||
|
|
||||||
self.g = Github(ghtoken)
|
self.g = Github(ghtoken)
|
||||||
self.headers = {"Authorization": "Bearer " + ghtoken}
|
self.headers = {"Authorization": "Bearer " + ghtoken}
|
||||||
self.repositoryData=repositoryData
|
self.repositoryData = repositoryData
|
||||||
|
|
||||||
def calculateLoc(self):
|
def calculateLoc(self):
|
||||||
result=self.repositoryData
|
result = self.repositoryData
|
||||||
yearly_data={}
|
yearly_data = {}
|
||||||
for repo in result['data']['user']['repositories']['edges']:
|
for repo in result['data']['user']['repositories']['edges']:
|
||||||
print(repo)
|
print(repo)
|
||||||
self.getCommitStat(repo['node'],yearly_data)
|
self.getCommitStat(repo['node'], yearly_data)
|
||||||
time.sleep(0.7)
|
time.sleep(0.7)
|
||||||
print("\n\n")
|
print("\n\n")
|
||||||
print(yearly_data)
|
print(yearly_data)
|
||||||
graph=BarGraph(yearly_data)
|
print("here")
|
||||||
graph_file=graph.build_graph()
|
graph = BarGraph(yearly_data)
|
||||||
|
graph_file = graph.build_graph()
|
||||||
self.pushChart()
|
self.pushChart()
|
||||||
|
|
||||||
def run_query_v3(self,nameWithOwner):
|
def run_query_v3(self, nameWithOwner):
|
||||||
endPoint='https://api.github.com/repos/'+nameWithOwner+'/stats/code_frequency'
|
endPoint = 'https://api.github.com/repos/' + nameWithOwner + '/stats/code_frequency'
|
||||||
# print(endPoint)
|
# print(endPoint)
|
||||||
request = requests.get(endPoint, headers=self.headers)
|
request = requests.get(endPoint, headers=self.headers)
|
||||||
if request.status_code == 401:
|
if request.status_code == 401:
|
||||||
raise Exception("Invalid token {}. {}".format(request.status_code, nameWithOwner))
|
raise Exception("Invalid token {}. {}".format(request.status_code, nameWithOwner))
|
||||||
return request.json()
|
return request.json()
|
||||||
|
|
||||||
def getQuarter(self,timeStamp):
|
def getQuarter(self, timeStamp):
|
||||||
month=datetime.datetime.fromtimestamp(timeStamp).month
|
month = datetime.datetime.fromtimestamp(timeStamp).month
|
||||||
if month>=1 and month<=4:
|
if month >= 1 and month <= 4:
|
||||||
return 1
|
return 1
|
||||||
elif month>=5 and month<=8:
|
elif month >= 5 and month <= 8:
|
||||||
return 2
|
return 2
|
||||||
elif month>=9 and month<=12:
|
elif month >= 9 and month <= 12:
|
||||||
return 3
|
return 3
|
||||||
|
|
||||||
|
def getCommitStat(self, repoDetails, yearly_data):
|
||||||
def getCommitStat(self,repoDetails,yearly_data):
|
result = self.run_query_v3(repoDetails['nameWithOwner'])
|
||||||
result= self.run_query_v3(repoDetails['nameWithOwner'])
|
this_year = datetime.datetime.utcnow().year
|
||||||
this_year=datetime.datetime.utcnow().year
|
|
||||||
|
|
||||||
for i in range(len(result)):
|
for i in range(len(result)):
|
||||||
curr_year=datetime.datetime.fromtimestamp(result[i][0]).year
|
curr_year = datetime.datetime.fromtimestamp(result[i][0]).year
|
||||||
# if curr_year != this_year:
|
# if curr_year != this_year:
|
||||||
quarter=self.getQuarter(result[i][0])
|
quarter = self.getQuarter(result[i][0])
|
||||||
if repoDetails['primaryLanguage'] is not None:
|
if repoDetails['primaryLanguage'] is not None:
|
||||||
|
|
||||||
if curr_year not in yearly_data:
|
if curr_year not in yearly_data:
|
||||||
yearly_data[curr_year]={}
|
yearly_data[curr_year] = {}
|
||||||
if quarter not in yearly_data[curr_year]:
|
if quarter not in yearly_data[curr_year]:
|
||||||
yearly_data[curr_year][quarter]={}
|
yearly_data[curr_year][quarter] = {}
|
||||||
if repoDetails['primaryLanguage']['name'] not in yearly_data[curr_year][quarter]:
|
if repoDetails['primaryLanguage']['name'] not in yearly_data[curr_year][quarter]:
|
||||||
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']]=0
|
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] = 0
|
||||||
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']]+=(result[i][1]+result[i][2])
|
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] += (result[i][1] + result[i][2])
|
||||||
|
|
||||||
#to find total
|
# to find total
|
||||||
|
|
||||||
# if 'total' not in yearly_data[curr_year]:
|
# if 'total' not in yearly_data[curr_year]:
|
||||||
# yearly_data[curr_year]['total']={}
|
# yearly_data[curr_year]['total']={}
|
||||||
# if repoDetails['primaryLanguage']['name'] not in yearly_data[curr_year]['total']:
|
# if repoDetails['primaryLanguage']['name'] not in yearly_data[curr_year]['total']:
|
||||||
# yearly_data[curr_year]['total'][repoDetails['primaryLanguage']['name']]=0
|
# yearly_data[curr_year]['total'][repoDetails['primaryLanguage']['name']]=0
|
||||||
# yearly_data[curr_year]['total'][repoDetails['primaryLanguage']['name']]+=(result[i][1]+result[i][2])
|
# yearly_data[curr_year]['total'][repoDetails['primaryLanguage']['name']]+=(result[i][1]+result[i][2])
|
||||||
|
|
||||||
def pushChart(self):
|
def pushChart(self):
|
||||||
repo = self.g.get_repo(f"{self.username}/{self.username}")
|
repo = self.g.get_repo(f"{self.username}/{self.username}")
|
||||||
@@ -88,13 +88,9 @@ class LinesOfCode:
|
|||||||
contents = repo.get_contents("charts/bar_graph.png")
|
contents = repo.get_contents("charts/bar_graph.png")
|
||||||
repo.update_file(contents.path, "Charts Added", data, contents.sha)
|
repo.update_file(contents.path, "Charts Added", data, contents.sha)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
repo.create_file("charts/bar_graph.png", "Initial Commit",data)
|
repo.create_file("charts/bar_graph.png", "Initial Commit", data)
|
||||||
print("pushed")
|
print("pushed")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# if __name__ == '__main__':
|
# if __name__ == '__main__':
|
||||||
# try:
|
# try:
|
||||||
# g = Github(ghtoken)
|
# g = Github(ghtoken)
|
||||||
|
|||||||
28
main.py
28
main.py
@@ -1,7 +1,7 @@
|
|||||||
'''
|
'''
|
||||||
Readme Development Metrics With waka time progress
|
Readme Development Metrics With waka time progress
|
||||||
'''
|
'''
|
||||||
|
import locale
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import base64
|
import base64
|
||||||
@@ -12,8 +12,6 @@ import requests
|
|||||||
from github import Github, GithubException
|
from github import Github, GithubException
|
||||||
import datetime
|
import datetime
|
||||||
from string import Template
|
from string import Template
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
from io import StringIO,BytesIO
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from loc import LinesOfCode
|
from loc import LinesOfCode
|
||||||
|
|
||||||
@@ -33,9 +31,10 @@ showOs = os.getenv('INPUT_SHOW_OS')
|
|||||||
showCommit = os.getenv('INPUT_SHOW_COMMIT')
|
showCommit = os.getenv('INPUT_SHOW_COMMIT')
|
||||||
showLanguage = os.getenv('INPUT_SHOW_LANGUAGE')
|
showLanguage = os.getenv('INPUT_SHOW_LANGUAGE')
|
||||||
show_loc = os.getenv('INPUT_SHOW_LINES_OF_CODE')
|
show_loc = os.getenv('INPUT_SHOW_LINES_OF_CODE')
|
||||||
showLanguagePerRepo= os.getenv('INPUT_LANGUAGE_PER_REPO')
|
|
||||||
showLocChart='y' if (os.getenv('LOC_CHART') is None) else os.getenv('LOC_CHART')
|
showLanguagePerRepo = os.getenv('INPUT_SHOW_LANGUAGE_PER_REPO')
|
||||||
show_waka_stats='n' if waka_key is None else 'y'
|
showLocChart = os.getenv('INPUT_SHOW_LOC_CHART')
|
||||||
|
show_waka_stats = 'y'
|
||||||
# The GraphQL query to get commit data.
|
# The GraphQL query to get commit data.
|
||||||
userInfoQuery = """
|
userInfoQuery = """
|
||||||
{
|
{
|
||||||
@@ -90,10 +89,11 @@ def run_v3_api(query):
|
|||||||
raise Exception(
|
raise Exception(
|
||||||
"Query failed to run by returning code of {}. {},... {}".format(request.status_code, query, request.json()))
|
"Query failed to run by returning code of {}. {},... {}".format(request.status_code, query, request.json()))
|
||||||
|
|
||||||
|
|
||||||
repositoryListQuery = Template("""
|
repositoryListQuery = Template("""
|
||||||
{
|
{
|
||||||
user(login: "$username") {
|
user(login: "$username") {
|
||||||
repositories(orderBy: {field: CREATED_AT, direction: ASC}, last: 100, affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER], isFork: false) {
|
repositories(orderBy: {field: CREATED_AT, direction: ASC}, last: 5, affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER], isFork: false) {
|
||||||
totalCount
|
totalCount
|
||||||
edges {
|
edges {
|
||||||
node {
|
node {
|
||||||
@@ -132,6 +132,7 @@ repositoryListQuery = Template("""
|
|||||||
}
|
}
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
||||||
def run_query(query):
|
def run_query(query):
|
||||||
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
|
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
|
||||||
if request.status_code == 200:
|
if request.status_code == 200:
|
||||||
@@ -278,11 +279,11 @@ def generate_commit_list(tz):
|
|||||||
string = string + '**' + title + '** \n\n' + '```text\n' + make_commit_list(one_day) + '\n\n```\n'
|
string = string + '**' + title + '** \n\n' + '```text\n' + make_commit_list(one_day) + '\n\n```\n'
|
||||||
string = string + '📅 **' + days_title + '** \n\n' + '```text\n' + make_commit_list(dayOfWeek) + '\n\n```\n'
|
string = string + '📅 **' + days_title + '** \n\n' + '```text\n' + make_commit_list(dayOfWeek) + '\n\n```\n'
|
||||||
|
|
||||||
|
|
||||||
def get_waka_time_stats():
|
def get_waka_time_stats():
|
||||||
stats=''
|
stats = ''
|
||||||
try:
|
request = requests.get(
|
||||||
request = requests.get(
|
f"https://wakatime.com/api/v1/users/current/stats/last_7_days?api_key={waka_key}")
|
||||||
f"https://wakatime.com/api/v1/users/current/stats/last_7_days?api_key={waka_key}")
|
|
||||||
|
|
||||||
if request.status_code != 401:
|
if request.status_code != 401:
|
||||||
data = request.json()
|
data = request.json()
|
||||||
@@ -366,9 +367,6 @@ def get_stats():
|
|||||||
'''Gets API data and returns markdown progress'''
|
'''Gets API data and returns markdown progress'''
|
||||||
|
|
||||||
stats = ''
|
stats = ''
|
||||||
if showCommit.lower() in ['true', '1', 't', 'y', 'yes']:
|
|
||||||
stats = stats + generate_commit_list() + '\n\n'
|
|
||||||
|
|
||||||
repositoryList = run_query(repositoryListQuery.substitute(username=username, id=id))
|
repositoryList = run_query(repositoryListQuery.substitute(username=username, id=id))
|
||||||
|
|
||||||
if showLanguagePerRepo.lower() in ['true', '1', 't', 'y', 'yes']:
|
if showLanguagePerRepo.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||||
@@ -386,6 +384,7 @@ def get_stats():
|
|||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|
||||||
def decode_readme(data: str):
|
def decode_readme(data: str):
|
||||||
'''Decode the contets of old readme'''
|
'''Decode the contets of old readme'''
|
||||||
decoded_bytes = base64.b64decode(data)
|
decoded_bytes = base64.b64decode(data)
|
||||||
@@ -420,4 +419,3 @@ if __name__ == '__main__':
|
|||||||
print("Readme updated")
|
print("Readme updated")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Exception Occurred" + str(e))
|
print("Exception Occurred" + str(e))
|
||||||
|
|
||||||
|
|||||||
@@ -2,106 +2,111 @@ import pandas as pd
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import altair as alt
|
import altair as alt
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
alt.data_transformers.enable('data_server_proxied')
|
||||||
|
|
||||||
|
|
||||||
# npm install vega-lite vega-cli canvas
|
# npm install vega-lite vega-cli canvas
|
||||||
|
|
||||||
|
|
||||||
class BarGraph:
|
class BarGraph:
|
||||||
|
|
||||||
def __init__(self, yearly_data):
|
def __init__(self, yearly_data):
|
||||||
self.yearly_data=yearly_data
|
self.yearly_data = yearly_data
|
||||||
|
|
||||||
def build_graph(self):
|
def build_graph(self):
|
||||||
|
|
||||||
with open('colors.json') as f:
|
with open('colors.json') as f:
|
||||||
colors = json.load(f)
|
colors = json.load(f)
|
||||||
allColorsValues=[]
|
allColorsValues = []
|
||||||
|
|
||||||
#filter data
|
# filter data
|
||||||
max_languages=5
|
max_languages = 5
|
||||||
top_languages={}
|
top_languages = {}
|
||||||
for year in self.yearly_data.keys():
|
for year in self.yearly_data.keys():
|
||||||
for quarter in self.yearly_data[year].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]:
|
for language in sorted(list(self.yearly_data[year][quarter].keys()),
|
||||||
if 'top' not in self.yearly_data[year][quarter]:
|
key=lambda lang: self.yearly_data[year][quarter][lang], reverse=True)[
|
||||||
self.yearly_data[year][quarter]['top']={}
|
0:max_languages]:
|
||||||
if self.yearly_data[year][quarter][language]!=0:
|
if 'top' not in self.yearly_data[year][quarter]:
|
||||||
self.yearly_data[year][quarter]['top'][language]=self.yearly_data[year][quarter][language]
|
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]
|
||||||
|
|
||||||
if language not in top_languages:
|
if language not in top_languages:
|
||||||
top_languages[language] =1
|
top_languages[language] = 1
|
||||||
top_languages[language]+=1
|
top_languages[language] += 1
|
||||||
|
|
||||||
# print(self.yearly_data)
|
print("here")
|
||||||
|
print(self.yearly_data)
|
||||||
|
|
||||||
all_languages=list(top_languages.keys())
|
all_languages = list(top_languages.keys())
|
||||||
|
|
||||||
for language in all_languages:
|
for language in all_languages:
|
||||||
if colors[language]['color'] is not None:
|
if colors[language]['color'] is not None:
|
||||||
allColorsValues.append(colors[language]['color'])
|
allColorsValues.append(colors[language]['color'])
|
||||||
|
|
||||||
languages_all_loc={}
|
languages_all_loc = {}
|
||||||
|
|
||||||
for language in all_languages:
|
for language in all_languages:
|
||||||
language_year=[]
|
language_year = []
|
||||||
for year in self.yearly_data.keys():
|
for year in self.yearly_data.keys():
|
||||||
language_quarter=[0,0,0]
|
language_quarter = [0, 0, 0]
|
||||||
for quarter in self.yearly_data[year].keys():
|
for quarter in self.yearly_data[year].keys():
|
||||||
if language in self.yearly_data[year][quarter]['top']:
|
if language in self.yearly_data[year][quarter]['top']:
|
||||||
language_quarter[quarter-1]=self.yearly_data[year][quarter]['top'][language]
|
language_quarter[quarter - 1] = self.yearly_data[year][quarter]['top'][language]
|
||||||
else:
|
else:
|
||||||
language_quarter[quarter-1]=0
|
language_quarter[quarter - 1] = 0
|
||||||
language_year.append(language_quarter)
|
language_year.append(language_quarter)
|
||||||
languages_all_loc[language]=language_year
|
languages_all_loc[language] = language_year
|
||||||
|
|
||||||
print(languages_all_loc)
|
print("here 2")
|
||||||
|
print(languages_all_loc)
|
||||||
|
|
||||||
language_df={}
|
language_df = {}
|
||||||
|
|
||||||
def prep_df(df, name):
|
def prep_df(df, name):
|
||||||
df = df.stack().reset_index()
|
df = df.stack().reset_index()
|
||||||
df.columns = ['c1', 'c2', 'values']
|
df.columns = ['c1', 'c2', 'values']
|
||||||
df['Language'] = name
|
df['Language'] = name
|
||||||
return df
|
return df
|
||||||
|
|
||||||
for language in languages_all_loc.keys():
|
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"])
|
language_df[language] = pd.DataFrame(languages_all_loc[language], index=list(self.yearly_data.keys()),
|
||||||
|
columns=["Q1", "Q2", "Q3"])
|
||||||
|
|
||||||
for language in language_df.keys():
|
for language in language_df.keys():
|
||||||
language_df[language]=prep_df(language_df[language], language)
|
language_df[language] = prep_df(language_df[language], language)
|
||||||
|
|
||||||
|
df = pd.concat(list(language_df.values()))
|
||||||
|
|
||||||
|
# print(df)
|
||||||
|
|
||||||
df=pd.concat(list(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),
|
||||||
|
|
||||||
# print(df)
|
# 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),
|
||||||
|
|
||||||
chart=alt.Chart(df).mark_bar().encode(
|
# tell Altair which field to use for color segmentation
|
||||||
|
color=alt.Color('Language:N',
|
||||||
# tell Altair which field to group columns on
|
scale=alt.Scale(
|
||||||
x=alt.X('c2:N', title=None),
|
domain=all_languages,
|
||||||
|
# make it look pretty with an enjoyable color pallet
|
||||||
# tell Altair which field to use as Y values and how to calculate
|
range=allColorsValues,
|
||||||
y=alt.Y('sum(values):Q',
|
),
|
||||||
axis=alt.Axis(
|
)) \
|
||||||
grid=False,
|
.configure_view(
|
||||||
title='LOC added')),
|
# remove grid lines around column clusters
|
||||||
|
strokeOpacity=0
|
||||||
# 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),
|
chart.save('bar_graph.png')
|
||||||
|
return 'bar_graph.png'
|
||||||
# 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'
|
|
||||||
|
|||||||
Reference in New Issue
Block a user