forked from pranavgade20/foss-leaderboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (77 loc) · 3.07 KB
/
app.py
File metadata and controls
97 lines (77 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import collections
import csv
import json
import time
import sys
import os
from threading import Thread
import requests as requests
from flask import Flask
from flask import send_from_directory
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
leaders = {}
remaining_hits = 1000
hits_reset = 0
hits_per_update = 1
headers = {
"Authorization": "Bearer " + os.environ["GH_TOKEN"]
}
def update_leaders():
ret = collections.defaultdict(lambda: 0)
with open('repos.csv') as csvfile:
reader = csv.reader(csvfile)
reader.__next__()
for user, repo in reader:
resp = []
page = 1
try:
f_resp = requests.get(f"https://api.github.com/repos/{user}/{repo}/pulls?state=all&per_page=100&page={page}", timeout=10.0, headers=headers)
t_resp = f_resp.json()
while len(t_resp) > 0:
resp.extend(t_resp)
if len(t_resp) < 90:
break # smol optimisation to reduce our number of calls
page += 1
f_resp = requests.get(f"https://api.github.com/repos/{user}/{repo}/pulls?state=all&per_page=100&page={page}", timeout=10.0, headers=headers)
t_resp = f_resp.json()
for pull in resp:
if any('points' in label['name'] for label in pull['labels']):
valid_pulls = [label['name'] for label in pull['labels'] if 'points - ' in label['name']]
ret[pull['user']['login']] += sum(map(lambda x: int(x.split(' - ')[-1]), valid_pulls))
except Exception:
print(f"ERROR AT: {user}, {repo}")
print(resp)
print(user, repo)
return 'ded'
global leaders, remaining_hits, hits_reset, hits_per_update
leaders = {k: v for k, v in sorted(ret.items(), key=lambda item: -item[1])}
initial_remaining_hits = remaining_hits
remaining_hits = int(f_resp.headers['X-RateLimit-Remaining'])
hits_reset = int(f_resp.headers['X-RateLimit-Reset'])
hits_per_update = initial_remaining_hits - remaining_hits
class UpdaterThread(Thread):
def run(self):
global last_updated, leaders, remaining_hits, hits_reset, hits_per_update
while True:
update_leaders()
number_updates = remaining_hits/hits_per_update
update_interval = 30 if hits_per_update < 0 else ((hits_reset - time.time()) / number_updates)
print(f"remaining_hits: {remaining_hits}")
print(f"hits_per_update: {hits_per_update}")
print(f"update_interval: {update_interval}")
sys.stdout.flush()
time.sleep(max(update_interval, 30))
@app.route('/leaderboard')
def leaderboard():
return json.dumps(leaders)
@app.route('/<path:path>')
def send_files(path):
return send_from_directory('.', path)
@app.route('/')
def root():
return send_from_directory('.', 'index.html')
if __name__ == '__main__':
UpdaterThread().start()
app.run(host='0.0.0.0', port=5000, debug=False)