forked from d2iq-archive/marathon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
review.py
executable file
·133 lines (99 loc) · 3.96 KB
/
review.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
#!/usr/bin/env python
import requests
import os
import pandas
from datetime import datetime
from tabulate import tabulate
ENDPOINT = "https://phabricator.mesosphere.com/api"
def pandas_frame_from(result):
d = pandas.io.json.json_normalize(result, [['result', 'data']])
# flatten 'fields' entry
fields = d.pop("fields").apply(pandas.Series)
return pandas.concat([d, fields], axis=1)
def life_time(data_frame):
"""
Calculates the life time of each row. Defined as 'dateModified' -
'dateCreated'.
:data_frame Pandas data frame holding the dateModified and dateCreated
columns
:return Life time series
"""
return data_frame.assign(
lifeTime=lambda x: x.dateModified - x.dateCreated)['lifeTime']
def stats(series, name, percentiles=[.25, .5, .75, .9]):
"""
Get statistics for series.
:series The series which statistics are caluclated.
:name Name for stats.
:return Statistics
"""
return series.describe(percentiles=percentiles).rename(name)
def beginning_of_this_month():
"""
:return Date for first day of this month.
"""
return datetime.now().replace(day=1, hour=0, minute=0, second=0)
def beginning_of_last_month():
"""
:return datetime for first day of last month.
"""
this_month = beginning_of_this_month()
month = this_month.month-1 if this_month.month > 1 else 12
if month < 12:
return this_month.replace(month=month)
else:
return this_month.replace(month=12, year=this_month.year-1)
def data_between(data_frame, start, end=datetime.now()):
"""
Selecte all rows that have 'dateCreated' after start and before end.
:param data_frame Data to select from
:param start The start date, including
:param end The end date, excluding
:return Sub series
"""
return data_frame.loc[(data_frame['dateCreated'] >= start) &
(data_frame['dateCreated'] < end)]
def query_open_reviews():
params = {'queryKey': 'active', 'order': 'newest',
'api.token': os.getenv('CONDUIT_TOKEN')}
result = requests.get(
"{}/differential.revision.search".format(ENDPOINT), params).json()
data_frame = pandas_frame_from(result)
data_frame.pop('attachments')
data_frame.pop('policy')
data_frame.pop('type')
data_frame.pop('jira.issues')
# Convert dates and calculate age
dates = data_frame[['dateCreated', 'dateModified']].applymap(
lambda d: datetime.fromtimestamp(d))
data_frame = data_frame.join(dates, rsuffix='.converted').assign(
age=lambda x: datetime.now() - x['dateCreated.converted'])
age_stats = data_frame[['age']].describe(percentiles=[.25, .5, .75, .9])
print(age_stats)
def query_closed_reviews():
conduit_token = os.getenv('CONDUIT_TOKEN')
if not conduit_token:
print("Please define a token with: CONDUIT_TOKEN=1234 ./review.py")
exit(1)
params = {'status': 'status-closed', 'api.token': conduit_token}
result = requests.get(
"{}/differential.query".format(ENDPOINT), params).json()
data_frame = pandas.io.json.json_normalize(result, 'result')
dates = data_frame[['dateCreated', 'dateModified']].apply(
pandas.to_numeric).applymap(lambda d: datetime.fromtimestamp(d))
total_life_time_stats = stats(life_time(dates), "All Time")
last_month = beginning_of_last_month()
this_month = beginning_of_this_month()
life_time_last_month = life_time(
data_between(dates, last_month, this_month))
last_month_stats = stats(
life_time_last_month, last_month.strftime("Last Month (%b)"))
life_time_this_month = life_time(
data_between(dates, this_month))
this_month_stats = stats(
life_time_this_month, this_month.strftime("This Month (%b)"))
all_stats = pandas.concat(
[total_life_time_stats, last_month_stats, this_month_stats], axis=1)
print(all_stats)
if __name__ == "__main__":
query_closed_reviews()