/
toil_web.py
executable file
·311 lines (240 loc) · 7.32 KB
/
toil_web.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
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python2
"""
toil_web.py
Each job assigned an ID. THe job generates:
- $ID.json # metadata
- $ID.tsv # benchmarks/time.py output. Success/failure for each task.
- $ID.wwz # files
This script generates an index.html with a table of metadata and links to the
logs.
TODO:
- Use JSON Template to escape HTML
- Can we publish spec test numbers in JSON?
"""
from __future__ import print_function
import csv
import datetime
import json
import itertools
import os
import sys
from doctools import html_head
def log(msg, *args):
if args:
msg = msg % args
print(msg, file=sys.stderr)
# *** UNUSED because it only makes sense on a dynamic web page! ***
# Loosely based on
# https://stackoverflow.com/questions/1551382/user-friendly-time-format-in-python
SECS_IN_DAY = 86400
def PrettyTime(now, start_time):
"""
Return a pretty string like 'an hour ago', 'Yesterday', '3 months ago', 'just
now', etc
"""
delta = now - start_time
if delta < 10:
return "just now"
if delta < 60:
return "%d seconds ago" % delta
if delta < 120:
return "a minute ago"
if delta < 3600:
return "%d minutes ago" % (delta // 60)
if delta < 7200:
return "an hour ago"
if delta < SECS_IN_DAY:
return "%d hours ago" % (delta // 3600)
if delta < 2 * SECS_IN_DAY:
return "Yesterday"
if delta < 7 * SECS_IN_DAY:
return "%d days ago" % (delta // SECS_IN_DAY)
if day_diff < 31 * SECS_IN_DAY:
return "%d weeks ago" % (delta / SECS_IN_DAY / 7)
if day_diff < 365:
return "%d months ago" % (delta / SECS_IN_DAY / 30)
return "%d years ago" % (delta / SECS_IN_DAY / 365)
def ParseJobs(stdin):
for line in stdin:
json_path = line.strip()
log('%s', json_path)
with open(json_path) as f:
meta = json.load(f)
#print(meta)
tsv_path = json_path[:-5] + '.tsv'
log('%s', tsv_path)
failed_tasks = []
total_elapsed = 0.0
num_tasks = 0
with open(tsv_path) as f:
reader = csv.reader(f, delimiter='\t')
for row in reader:
status = int(row[0])
task_name = row[2]
if status != 0:
failed_tasks.append(task_name)
elapsed = float(row[1])
total_elapsed += elapsed
num_tasks += 1
num_failures = len(failed_tasks)
if num_failures == 0:
s_html = '<span class="pass">pass</span>'
else:
if num_failures == 1:
fail_html = 'task <code>%s</code>' % failed_tasks[0]
else:
fail_html = '%d of %d tasks' % (num_failures, num_tasks)
s_html = '<span class="fail">FAIL</span><br/><span class="fail-detail">%s</span>' % fail_html
meta['status_html'] = s_html
total_elapsed = int(total_elapsed)
minutes = total_elapsed / 60
seconds = total_elapsed % 60
meta['elapsed_str'] = '%d:%02d' % (minutes, seconds)
# Note: this isn't a Unix timestamp
#microseconds = int(meta['TRAVIS_TIMER_START_TIME']) / 1e6
#log('ts = %d', microseconds)
start_time = meta.get('TASK_RUN_START_TIME')
if start_time is None:
start_time_str = '?'
else:
# Note: this is different clock! Could be desynchronized.
# Doesn't make sense this is static!
#now = time.time()
start_time = int(start_time)
t = datetime.datetime.fromtimestamp(start_time)
# %-I avoids leading 0, and is 12 hour date.
# lower() for 'pm' instead of 'PM'.
start_time_str = t.strftime('%-m/%d at %-I:%M%p').lower()
#start_time_str = PrettyTime(now, start_time)
meta['start_time_str'] = start_time_str
try:
commit_line = meta['TRAVIS_COMMIT_MESSAGE'].splitlines()[0]
except AttributeError:
commit_line = '?'
meta['commit_line'] = commit_line
try:
commit_hash = meta['TRAVIS_COMMIT'][-8:] # last 8 chars
except TypeError:
commit_hash = '?'
meta['commit_hash'] = commit_hash
filename = os.path.basename(json_path)
basename, _ = os.path.splitext(filename)
meta['basename'] = basename
yield meta
BUILD_ROW_TEMPLATE = '''\
<tr class="spacer">
<td colspan=5><td/>
</tr>
<tr class="commit-row">
<td colspan=2>
<code>%(TRAVIS_BRANCH)s</code>
<code><a href="https://github.com/oilshell/oil/commit/%(TRAVIS_COMMIT)s">%(commit_hash)s</a></code>
</td>
<td class="commit-line" colspan=3>
<code>%(commit_line)s</code>
</td>
</tr>
<tr class="spacer">
<td colspan=5><td/>
</tr>
'''
JOB_ROW_TEMPLATE = '''\
<tr>
<td>%(TRAVIS_JOB_NUMBER)s</td>
<td> <code><a href="%(basename)s.wwz/">%(TRAVIS_JOB_NAME)s</a></code> </td>
<td><a href="%(TRAVIS_JOB_WEB_URL)s">%(start_time_str)s</a></td>
<td>%(elapsed_str)s</td>
<td>%(status_html)s</td>
<!-- todo; spec details
<td> </td>
-->
</tr>
'''
INDEX_TOP = '''
<body class="width50">
<p id="home-link">
<a href="/">travis-ci.oilshell.org</a>
| <a href="//oilshell.org/">oilshell.org</a>
</p>
<h1>Recent Jobs</h1>
<table>
<thead>
<tr>
<td>Job #</td>
<td>Job Name</td>
<td>Start Time</td>
<td>Elapsed</td>
<td>Status</td>
<!--
<td>Details</td>
-->
</tr>
</thead>
'''
def main(argv):
action = argv[1]
if action == 'srht-index':
# Bust cache (e.g. Safari iPad seems to cache aggressively and doesn't
# have Ctrl-F5)
html_head.Write(sys.stdout, 'Recent Jobs',
css_urls=['../web/base.css?cache=0', '../web/toil.css?cache=0'])
print(INDEX_TOP)
rows = list(ParseJobs(sys.stdin))
#print(json.dump(rows, indent=2))
elif action == 'travis-index':
# Bust cache (e.g. Safari iPad seems to cache aggressively and doesn't
# have Ctrl-F5)
html_head.Write(sys.stdout, 'Recent Jobs',
css_urls=['../web/base.css?cache=0', '../web/toil.css?cache=0'])
print(INDEX_TOP)
rows = list(ParseJobs(sys.stdin))
# Sort by descending build number
def ByBuildNum(row):
return int(row.get('TRAVIS_BUILD_NUMBER', 0))
def ByTaskRunStartTime(row):
return int(row.get('TASK_RUN_START_TIME', 0))
rows.sort(key=ByBuildNum, reverse=True)
groups = itertools.groupby(rows, key=ByBuildNum)
#print(list(groups))
for build_num, group in groups:
build_num = int(build_num)
log('build %d', build_num)
jobs = list(group)
# Sort by start time
jobs.sort(key=ByTaskRunStartTime, reverse=True)
# The first job should have the same branch/commit/commit_line
print(BUILD_ROW_TEMPLATE % jobs[0])
for job in jobs:
print(JOB_ROW_TEMPLATE % job)
print('''\
</table>
<p>
<a href="raw.html">raw data</a>
</p>
</body>
</html>
''')
elif action == 'cleanup':
prefixes = []
for line in sys.stdin:
json_path = line.strip()
log('%s', json_path)
prefixes.append(json_path[:-5])
# looks like 2020-03-20, so sort ascending means the oldest are first
prefixes.sort()
# Keep 200 jobs. We only display the last 100.
prefixes = prefixes[:-200]
# Show what to delete. Then the user can pipe to xargs rm to remove it.
for prefix in prefixes:
print(prefix + '.json')
print(prefix + '.tsv')
print(prefix + '.wwz')
else:
raise RuntimeError('Invalid action %r' % action)
if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)