This repository has been archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
300 lines (241 loc) · 8.3 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import datetime
import pytz
import dateutil.parser
from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
import requests
import json
import lxml.html
import cachetools
from flask import (
Flask, request, make_response, jsonify, render_template)
APP_DATA = {
'azure-pipelines': {
'repos': cachetools.LRUCache(maxsize=128),
'rates': cachetools.LRUCache(maxsize=96),
},
'travis-ci': {
'repos': cachetools.LRUCache(maxsize=128),
'rates': cachetools.LRUCache(maxsize=96),
},
'github-actions': {
'repos': cachetools.LRUCache(maxsize=128),
'rates': cachetools.LRUCache(maxsize=96),
}
}
STATUS_UPDATE_DELAY = 300
NOSTATUS = 'No Status Available'
STATUS_UPDATED = None
STATUS_DATA = {
'azure': {
'status': NOSTATUS,
},
'webservices': {
'status': NOSTATUS,
},
}
START_TIME = datetime.datetime.fromisoformat("2020-01-01T00:00:00+00:00")
TIME_INTERVAL = 60*5 # five minutes
app = Flask(__name__)
def _make_time_key(uptime):
dt = uptime.timestamp() - START_TIME.timestamp()
return int(dt // TIME_INTERVAL)
# reload the cache
RELOAD_CACHE = True
def _reload_cache():
print(" ")
print("!!!!!!!!!!!!!! RELOADING THE CACHE !!!!!!!!!!!!!!")
global APP_DATA
data = requests.get(
("https://raw.githubusercontent.com/regro/cf-action-counter-db/"
"master/data/latest.json")).json()
for slug in APP_DATA:
print('reloading data for %s' % slug)
if slug not in data:
if slug != 'github-actions':
continue
else:
_data = data
else:
_data = data[slug]
for repo in _data['repos']:
APP_DATA[slug]['repos'][repo] = _data['repos'][repo]
for ts in _data['rates']:
t = datetime.datetime.fromisoformat(ts).astimezone(pytz.UTC)
key = _make_time_key(t)
APP_DATA[slug]['rates'][key] = _data['rates'][ts]
print(" reloaded %d repos" % len(APP_DATA[slug]['repos']))
print(" reloaded %d rates" % len(APP_DATA[slug]['rates']))
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print(" ")
if RELOAD_CACHE:
_reload_cache()
RELOAD_CACHE = False
class MyYAML(YAML):
"""dump yaml as string rippd from docs"""
def dump(self, data, stream=None, **kw):
inefficient = False
if stream is None:
inefficient = True
stream = StringIO()
YAML.dump(self, data, stream, **kw)
if inefficient:
return stream.getvalue()
def _make_est_from_time_key(key, iso=False):
est = pytz.timezone('US/Eastern')
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
dt = datetime.timedelta(seconds=key * TIME_INTERVAL)
t = dt + START_TIME
t = t.astimezone(est)
if iso:
return t.isoformat()
else:
return t.strftime(fmt)
def _make_report_data(iso=False):
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
know = _make_time_key(now)
report = {}
for key in APP_DATA:
rates = {}
for k in range(know, know-96, -1):
tstr = _make_est_from_time_key(k, iso=iso)
rates[tstr] = APP_DATA[key]['rates'].get(k, 0)
total = sum(v for v in rates.values())
report[key] = {
'total': total,
'rates': rates,
'repos': {k: v for k, v in APP_DATA[key]['repos'].items()},
}
return report
@app.route('/', methods=['GET'])
def index():
yaml = MyYAML()
return render_template(
'index.html',
report=yaml.dump(_make_report_data(iso=False)),
)
@app.route('/db', methods=['GET'])
def report():
data = _make_report_data(iso=True)
resp = make_response(jsonify(data))
resp.headers['Access-Control-Allow-Origin'] = "*"
return resp
@app.route('/report/<name>', methods=['GET'])
def report_name(name):
data = _make_report_data(iso=True)
resp = make_response(jsonify(data[name]))
resp.headers['Access-Control-Allow-Origin'] = "*"
return resp
@app.route('/status', methods=['GET'])
def status():
global STATUS_DATA
global STATUS_UPDATED
do_update = False
if STATUS_UPDATED is None:
do_update = True
else:
now = datetime.datetime.now().astimezone(pytz.UTC)
dt = now - STATUS_UPDATED
# five minutes
if dt.total_seconds() >= STATUS_UPDATE_DELAY:
do_update = True
if do_update:
try:
r = requests.get('https://status.dev.azure.com')
if r.status_code != 200:
STATUS_DATA['azure'] = NOSTATUS
else:
s = json.loads(
lxml
.html
.fromstring(r.content)
.get_element_by_id('dataProviders')
.text
)
def _rec_search(d):
if isinstance(d, dict):
if 'health' in d and 'message' in d:
return d['message']
else:
for v in d.values():
if isinstance(v, dict):
val = _rec_search(v)
if val is not None:
return val
return None
else:
return None
stat = _rec_search(s)
if stat is None:
stat = NOSTATUS
STATUS_DATA['azure'] = stat
except requests.exceptions.RequestException:
STATUS_DATA['azure'] = NOSTATUS
try:
r = requests.post(
(
'https://conda-forge.herokuapp.com'
'/conda-webservice-update/hook'
),
headers={'X-GitHub-Event': 'ping'}
)
if (
r.status_code != 200 or
r.elapsed.total_seconds() > 1 or
r.text != 'pong'
):
STATUS_DATA['webservices'] = 'Degraded Performance'
else:
STATUS_DATA['webservices'] = 'All Systems Operational'
except requests.exceptions.RequestException:
STATUS_DATA['webservices'] = 'Degraded Performance'
STATUS_UPDATED = datetime.datetime.now().astimezone(pytz.UTC)
STATUS_DATA['updated_at'] = STATUS_UPDATED.isoformat()
resp = make_response(jsonify(STATUS_DATA))
resp.headers['Access-Control-Allow-Origin'] = "*"
return resp
@app.route('/payload', methods=['POST'])
def payload():
global APP_DATA
if request.method == 'POST':
event_type = request.headers.get('X-GitHub-Event')
print(" ")
print("event:", event_type)
if event_type == 'ping':
return 'pong'
elif event_type == 'check_run':
repo = request.json['repository']['full_name']
cs = request.json['check_run']
print(" repo:", repo)
print(" app:", cs['app']['slug'])
print(" action:", request.json['action'])
print(" status:", cs['status'])
print(" conclusion:", cs['conclusion'])
if (
cs['app']['slug'] in APP_DATA and
cs['status'] == 'completed'
):
print(" completed_at:", cs['completed_at'])
key = cs['app']['slug']
uptime = dateutil.parser.isoparse(cs['completed_at'])
interval = _make_time_key(uptime)
if interval not in APP_DATA[key]['rates']:
APP_DATA[key]['rates'][interval] = 0
APP_DATA[key]['rates'][interval] = (
APP_DATA[key]['rates'][interval]
+ 1
)
if repo not in APP_DATA[key]['repos']:
APP_DATA[key]['repos'][repo] = 0
APP_DATA[key]['repos'][repo] = (
APP_DATA[key]['repos'][repo]
+ 1
)
return event_type
elif event_type == 'check_suite':
return event_type
else:
return make_response(
"could not handle event: '%s'" % event_type,
404,
)