Merge pull request #144 from aaditkamat/fix-loc-stats

feat: calculate loc using commit data
This commit is contained in:
Anmol Pratap Singh
2020-12-20 12:08:36 +05:30
committed by GitHub
2 changed files with 30 additions and 23 deletions

34
loc.py
View File

@@ -29,23 +29,25 @@ class LinesOfCode:
for repo in result['data']['user']['repositories']['edges']:
self.getCommitStat(repo['node'], yearly_data)
time.sleep(0.7)
return yearly_data
def plotLoc(self, yearly_data):
graph = BarGraph(yearly_data)
graph_file = graph.build_graph()
graph.build_graph()
self.pushChart()
def run_query_v3(self, nameWithOwner):
endPoint = 'https://api.github.com/repos/' + nameWithOwner + '/stats/code_frequency'
def run_query_v3(self, endPoint):
# print(endPoint)
request = requests.get(endPoint, headers=self.headers)
if request.status_code == 401:
raise Exception("Invalid token {}. {}".format(request.status_code, nameWithOwner))
raise Exception("Invalid token {}.".format(request.status_code))
elif request.status_code == 204:
return []
else:
return request.json()
def getQuarter(self, timeStamp):
month = datetime.datetime.fromtimestamp(timeStamp).month
month = datetime.datetime.fromisoformat(timeStamp).month
if month >= 1 and month <= 3:
return 1
elif month >= 4 and month <= 6:
@@ -56,13 +58,25 @@ class LinesOfCode:
return 4
def getCommitStat(self, repoDetails, yearly_data):
result = self.run_query_v3(repoDetails['nameWithOwner'])
allCommitsEndPoint = 'https://api.github.com/repos/' + repoDetails['nameWithOwner'] + '/commits'
allCommitsResult = self.run_query_v3(allCommitsEndPoint)
# This ignores the error message you get when you try to list commits for an empty repository
if not type(allCommitsResult) == list:
return
this_year = datetime.datetime.utcnow().year
for i in range(len(result)):
curr_year = datetime.datetime.fromtimestamp(result[i][0]).year
for i in range(len(allCommitsResult)):
author = allCommitsResult[i]["commit"]["author"]
if author["name"] != self.username:
continue
date = re.search(r'\d+-\d+-\d+', author["date"]).group(0)
curr_year = datetime.datetime.fromisoformat(date).year
# if curr_year != this_year:
quarter = self.getQuarter(result[i][0])
individualCommitEndPoint = allCommitsResult[i]["url"]
individualCommitResult = self.run_query_v3(individualCommitEndPoint)
quarter = self.getQuarter(date)
if repoDetails['primaryLanguage'] is not None:
if curr_year not in yearly_data:
@@ -71,7 +85,7 @@ class LinesOfCode:
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']] += (result[i][1])
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] += individualCommitResult["stats"]["additions"]
# to find total

19
main.py
View File

@@ -383,18 +383,10 @@ def generate_language_per_repo(result):
def get_line_of_code():
result = run_query(createContributedRepoQuery.substitute(username=username))
nodes = result["data"]["user"]["repositoriesContributedTo"]["nodes"]
repos = [d for d in nodes if d['isFork'] is False]
total_loc = 0
for repository in repos:
try:
time.sleep(0.7)
datas = run_v3_api(get_loc_url.substitute(owner=repository["owner"]["login"], repo=repository["name"]))
for data in datas:
total_loc = total_loc + data[1] - data[2]
except Exception as execp:
print(execp)
repositoryList = run_query(repositoryListQuery.substitute(username=username, id=id))
loc = LinesOfCode(id, username, ghtoken, repositoryList)
yearly_data = loc.calculateLoc()
total_loc = sum([yearly_data[year][quarter][lang] for year in yearly_data for quarter in yearly_data[year] for lang in yearly_data[year][quarter]])
return humanize.intword(int(total_loc))
@@ -459,7 +451,8 @@ def get_stats(github):
if showLocChart.lower() in truthy:
loc = LinesOfCode(id, username, ghtoken, repositoryList)
loc.calculateLoc()
yearly_data = loc.calculateLoc()
loc.plotLoc(yearly_data)
stats += '**' + translate['Timeline'] + '**\n\n'
stats = stats + '![Chart not found](https://raw.githubusercontent.com/' + username + '/' + username + '/master/charts/bar_graph.png) \n\n'