This repository has been archived by the owner on Aug 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
249 lines (184 loc) · 6.4 KB
/
app.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Author: Samuel Guebo, Derick Alangi
# Description: Entry point of the application
# License: MIT
import requests
import json
import time
import itertools
from operator import itemgetter
from pprint import pprint
from datetime import datetime
from dateutil.relativedelta import relativedelta
from flask import Flask
from flask import render_template, request
from tinydb import TinyDB, Query
# instantiate Flask
app = Flask(__name__)
@app.route('/month/<month>')
@app.route('/')
def index(month=None):
""" Default / Home page of the application / tool. """
if month == None:
month = getCurrentMonth() # make month format human-readable
monthID = month
stats = getStatsFromDb(month)
contributors = getContributors(stats)
# check whether there are entries in db
formatted = datetime.strptime(month, ("%Y-%m"))
if dbHasMonth(month) == True:
return render_template('index.html', stats = stats,
month = formatted.strftime("%B, %Y"), contributors = contributors, monthID = monthID)
else:
return render_template('loader.html', monthID = monthID, formatted = formatted.strftime("%B, %Y"))
@app.route('/contributor/<username>/<month>')
def contributorPatchesByMonth(username, month):
""" REST endpoint for list of contributor's patche(s). """
Submitter = Query()
db = getDb()
# filter by username
patches = db.search(Submitter.username == username)
# grab previous url from flask.request.referrer
backUrl = request.referrer
return render_template('contributor.html', patches = patches, monthID = month, backUrl = backUrl)
def readContributorsFromFile():
""" Read through and get all contributors. """
file = open('contributors.json', "r")
jsonText = file.read()
response = json.loads(jsonText)
return response
def getContributorStats(username, month=None):
""" Fetch Gerrit API for patch contributor data. """
if month == None:
date = getCurrentMonth()
else:
date = month
previous_month = decrementMonth(date)
next_month = incrementMonth(date)
if username != "":
link = "https://gerrit.wikimedia.org/r/changes/?q=owner:"
# build the API requst url
url = link + username + "+after:" + previous_month + "+before:" + next_month
r = requests.get(url)
jsonArray = r.text
jsonArray = jsonArray.replace(")]}'", "", 1); # Fix this error in headers of json tree
return json.loads(jsonArray);
@app.route('/refresh/<month>')
@app.route('/refresh/')
def refreshStatsByMonth(month=None):
""" Force a deep refresh """
if month == None:
month = getCurrentMonth() # make month format human-readable
formatted = datetime.strptime(month, ("%Y-%m"))
# Let loader handle the refreshing process as this is its purpose
return render_template('loader.html', monthID = month, formatted = formatted.strftime("%B, %Y"))
def getCurrentMonth(format="%Y-%m"):
""" Get current month. """
currentMonth = time.strftime(format) # e.g. 2018-02
return currentMonth
@app.route('/raw/')
@app.route('/raw/<month>')
def raw(month=None):
""" Display Raw HTML stats """
if month == None:
month = getCurrentMonth()
# load and save contributors list
contributors = readContributorsFromFile()
# loop through contributors
for contributor in contributors:
username = contributor['username']
patches = getContributorStats(username, month)
# loop through contributor patches
db = getDb()
for patch in patches:
# prepare patch dictionary
patch['username'] = username;
patch['name'] = contributor['name'];
patch['country'] = contributor['country']
# make sure patch wasn't previously saved
if not patchExists(patch):
# persist patch to db
db.insert(patch)
# update patch status
else:
Patch = Query()
db.update({'status': patch['status']}, (Patch.username == patch['username']) & (Patch.created == patch['created']))
stats = getStatsFromDb(month)
contributors = getContributors(stats)
formatted = datetime.strptime(month, ("%Y-%m"))
return render_template('stats.html', stats = stats, monthID = month,
month = formatted.strftime("%B, %Y"), contributors = contributors)
def getStatsFromDb(month):
""" Get monthly statistics from DB. """
Patch = Query()
db = getDb()
#stats = db.search(Patch.created == month)
stats = db.search(Patch.created.test(filterMonth, month))
return stats
@app.template_filter()
def datetimeformat(value, inFormat="%Y-%m-%d %H:%M:%S.000000000", outFormat = '%Y-%m-%d, %H:%M'):
""" Custom Flask filter for datetimeformating. """
formattedString = datetime.strptime(value, inFormat)
return formattedString.strftime(outFormat) # simple formatting
def getDb():
""" DB object to be used independently. """
# setting the tinydb location
db = TinyDB('db/db.json')
return db
def getContributors(patches):
""" Get the list of patch contributors. """
data = []
contributors = []
# group contributions by username
data = sorted(patches, key=itemgetter('username'))
for k, g in itertools.groupby(data, key=lambda x:x['username']):
contributors.append(list(g)) # Store group iterator as a list
return contributors
def filterMonth(string, month):
""" Filter month. """
if month in string:
return True
else:
return False
def patchExists(patch):
""" Check whether patch(es) exists in the DB. """
db = getDb()
Patch = Query()
rows = db.search((Patch.created == patch['created'])
& (Patch.username == patch['username']))
# if the patch was previously saved
if len(rows) > 0:
return True
else:
return False
def monthToDate(month):
""" Convert month to date format. """
month = datetime.strptime(month, ("%Y-%m"))
date = month.strftime("%Y-%m-%d"); # eg 2018-02-01
date = datetime.strptime(date, ("%Y-%m-%d")) # return datetime object
return date
def incrementMonth(month, n=1):
""" Increment date by 'n' months. """
date = monthToDate(month)
next_month = date + relativedelta(months=n)
return next_month.strftime("%Y-%m-%d")
def decrementMonth(month, n=1):
""" Decrement date by 'n' months. """
date = monthToDate(month)
previous_month = date - relativedelta(months=n)
return previous_month.strftime("%Y-%m-%d")
@app.route('/test')
def sample_request():
""" Test API endpoint with hardcoded data. """
pprint.pprint(getContributorStats('D3r1ck01', '2018-01'))
return ''
def dbHasMonth(month):
""" Check whether month has entries in the DB. """
stats = getStatsFromDb(month)
# if there is at least one entry
if len(stats) > 0:
return True
else:
return False
# Execute the application
if __name__ == '__main__':
app.run()