Optimize line of code calculation

This commit is contained in:
anmol098
2021-12-01 17:41:29 +05:30
parent 8cdc2b3261
commit fc9188a8e6
2 changed files with 21 additions and 11 deletions

2
loc.py
View File

@@ -86,7 +86,7 @@ class LinesOfCode:
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']] += individualCommitResult["stats"]["additions"] yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] += (individualCommitResult["stats"]["additions"] - individualCommitResult["stats"]['deletions'])
# to find total # to find total

24
main.py
View File

@@ -389,10 +389,16 @@ def generate_language_per_repo(result):
return '**' + title + '** \n\n' + '```text\n' + make_list(data) + '\n\n```\n' return '**' + title + '** \n\n' + '```text\n' + make_list(data) + '\n\n```\n'
def get_line_of_code(): def get_yearly_data():
repositoryList = run_query(repositoryListQuery.substitute(username=username, id=id)) repository_list = run_query(repositoryListQuery.substitute(username=username, id=id))
loc = LinesOfCode(id, username, ghtoken, repositoryList, ignored_repos_name) loc = LinesOfCode(id, username, ghtoken, repository_list, ignored_repos_name)
yearly_data = loc.calculateLoc() yearly_data = loc.calculateLoc()
if showLocChart.lower() in truthy:
loc.plotLoc(yearly_data)
return yearly_data
def get_line_of_code(yearly_data):
total_loc = sum( 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][lang] for year in yearly_data for quarter in yearly_data[year] for lang in
yearly_data[year][quarter]]) yearly_data[year][quarter]])
@@ -441,6 +447,10 @@ def get_stats(github):
stats = '' stats = ''
repositoryList = run_query(repositoryListQuery.substitute(username=username, id=id)) repositoryList = run_query(repositoryListQuery.substitute(username=username, id=id))
if (show_loc.lower() or showLocChart.lower()) in truthy:
# This condition is written to calculate the lines of code because it is heavy process soo needs to be calculate once this will reduce the execution time
yearly_data = get_yearly_data()
if show_profile_view.lower() in truthy: if show_profile_view.lower() in truthy:
data = run_v3_api(get_profile_view.substitute(owner=username, repo=username)) data = run_v3_api(get_profile_view.substitute(owner=username, repo=username))
stats += '![Profile Views](http://img.shields.io/badge/' + quote(str(translate['Profile Views'])) + '-' + str( stats += '![Profile Views](http://img.shields.io/badge/' + quote(str(translate['Profile Views'])) + '-' + str(
@@ -449,7 +459,7 @@ def get_stats(github):
if show_loc.lower() in truthy: if show_loc.lower() in truthy:
stats += '![Lines of code](https://img.shields.io/badge/' + quote( stats += '![Lines of code](https://img.shields.io/badge/' + quote(
str(translate['From Hello World I have written'])) + '-' + quote( str(translate['From Hello World I have written'])) + '-' + quote(
str(get_line_of_code())) + '%20' + quote(str(translate['Lines of code'])) + '-blue)\n\n' str(get_line_of_code(yearly_data))) + '%20' + quote(str(translate['Lines of code'])) + '-blue)\n\n'
if show_short_info.lower() in truthy: if show_short_info.lower() in truthy:
stats += get_short_info(github) stats += get_short_info(github)
@@ -461,9 +471,6 @@ def get_stats(github):
stats = stats + generate_language_per_repo(repositoryList) + '\n\n' stats = stats + generate_language_per_repo(repositoryList) + '\n\n'
if showLocChart.lower() in truthy: if showLocChart.lower() in truthy:
loc = LinesOfCode(id, username, ghtoken, repositoryList, ignored_repos_name)
yearly_data = loc.calculateLoc()
loc.plotLoc(yearly_data)
stats += '**' + translate['Timeline'] + '**\n\n' stats += '**' + translate['Timeline'] + '**\n\n'
branch_name = github.get_repo(f'{username}/{username}').default_branch branch_name = github.get_repo(f'{username}/{username}').default_branch
stats = stats + '![Chart not found](https://raw.githubusercontent.com/' + username + '/' + username + '/' + branch_name + '/charts/bar_graph.png) \n\n' stats = stats + '![Chart not found](https://raw.githubusercontent.com/' + username + '/' + username + '/' + branch_name + '/charts/bar_graph.png) \n\n'
@@ -494,6 +501,7 @@ def generate_new_readme(stats: str, readme: str):
if __name__ == '__main__': if __name__ == '__main__':
try: try:
start_time = datetime.datetime.now().timestamp() * 1000
if ghtoken is None: if ghtoken is None:
raise Exception('Token not available') raise Exception('Token not available')
g = Github(ghtoken) g = Github(ghtoken)
@@ -531,6 +539,8 @@ if __name__ == '__main__':
content=new_readme, sha=contents.sha, branch='main', content=new_readme, sha=contents.sha, branch='main',
committer=committer) committer=committer)
print("Readme updated") print("Readme updated")
end_time = datetime.datetime.now().timestamp() * 1000
print("Program processed in {} miliseconds.".format(round(end_time - start_time, 0)))
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()
print("Exception Occurred " + str(e)) print("Exception Occurred " + str(e))