-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatlabcache.py
229 lines (204 loc) · 7.55 KB
/
batlabcache.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
"""This file implements an on-disk cache for batlab run data. Top level
data is pulled via month.
"""
from __future__ import print_function
import os
import io
import sys
import time
from datetime import datetime
from html.parser import HTMLParser
if sys.version_info[0] > 2:
from urllib.parse import urlencode
from urllib.request import urlopen
else:
from urllib import urlencode
from urllib2 import urlopen
import numpy as np
import pandas
def datespace(starty, startm, stopy, stopm):
"""generator of year and month tuples on inclusive bounds."""
for year in range(starty, stopy+1):
month1 = startm if year == starty else 1
month12 = stopm if year == stopy else 12
for month in range(month1, month12 + 1):
yield year, month
class BatlabCache(object):
overview_base_url = "http://submit-1.batlab.org/nmi/results/overview?"
overview_base_query = {'storedSearch': 0, 'sortDirection': 'down',
'sortBy': 'start'}
def __init__(self, year, month, username="", cachedir="cache"):
self.username = username
self.cachedir = cachedir = os.path.abspath(cachedir)
if not os.path.isdir(cachedir):
os.makedirs(cachedir)
usr = username + "_" if username else ""
self.month_filename = os.path.join(cachedir, usr + "{year}-{month:02}.html")
now = time.gmtime()
self.ensure_dates(year, month, now.tm_year, now.tm_mon)
def ensure_dates(self, start_year, start_month, stop_year, stop_month):
"""Ensures monthly data files have been downloaded."""
for year, month in datespace(start_year, start_month, stop_year, stop_month):
fname = self.month_filename.format(year=year, month=month)
if os.path.exists(fname):
continue
self.download_month(year, month)
def download_month(self, year, month, retry=3):
"""Downloads a month summary file."""
url = self.overview_url(year, month)
fname = self.month_filename.format(year=year, month=month)
t1 = time.time()
print("downloading " + url)
try:
with urlopen(url) as r:
page = r.read().decode('utf-8')
except ConnectionResetError:
if retry > 0:
print("...failed to download file, retying {0}".format(retry))
self.download_month(year, month, retry=retry-1)
else:
print("...failed to download file. Maximum retries reached.")
raise
with io.open(fname, 'w') as f:
f.write(page)
print("...saved as {0} in {1:.1} s".format(fname, time.time() - t1))
def download_this_month(self):
now = time.gmtime()
self.download_month(now.tm_year, now.tm_mon)
def download_last_month(self):
now = time.gmtime()
y = now.tm_year
m = now.tm_mon
if m == 1:
y, m = y - 1, 12
else:
m = m - 1
self.download_month(y, m)
def overview_url(self, year, month):
query = dict(self.overview_base_query)
query['user'] = self.username
date = 'between {year}-{month}-01 and {nextyear}-{nextmonth}-01'
nextyear, nextmonth = (year+1, 1) if month == 12 else (year, month+1)
query['date'] = date.format(year=year, month=month,
nextyear=nextyear, nextmonth=nextmonth)
return self.overview_base_url + urlencode(query)
def overview(self, year, month, stop_year=None, stop_month=None):
"""Creates a pandas DataFrame for a month or a range of months."""
dfs = []
stop_year = stop_year or year
stop_month = stop_month or month
for y, m in datespace(year, month, stop_year, stop_month):
fname = self.month_filename.format(year=y, month=m)
op = OverviewParser(convert_charrefs=True)
op.parse(fname)
dfs.append(pandas.DataFrame(op.data, index=op.data['start']))
df = dfs[0]
if len(dfs) > 1:
for d in dfs[1:]:
df = df.append(d)
return df
null = lambda x: x
respace = lambda s: s.replace('\xa0', ' ')
class OverviewParser(HTMLParser):
statuses = frozenset([
'tableRow0',
'tableRow1',
'tableRow0StatusInProgress',
'tableRow1StatusInProgress',
'tableRow0StatusInternalError',
'tableRow1StatusInternalError',
'tableRow0StatusSucceeded',
'tableRow1StatusSucceeded',
'tableRow0StatusFailed',
'tableRow1StatusFailed',
'tableRow0StatusRemoved',
'tableRow1StatusRemoved',
'tableRow0StatusNotCompleted',
'tableRow1StatusNotCompleted',
'tableRow0StatusError',
'tableRow1StatusError',
'tableRow0StatusTimedOut',
'tableRow1StatusTimedOut',
])
fields = (None, 'id', 'result', 'user', 'type', 'project', None,
#'project_version', 'component' , 'component_version',
'start', 'duration', 'description', 'platforms')
colparsers = {
'id': lambda s: int(s.split('>')[1].split('<')[0] if '>' in s else s),
'result': respace,
'user': null,
'type': null,
'project': null,
'project_version': null,
'component': null,
'component_version': null,
'start': lambda s: datetime.strptime(s, "%Y-%m-%d %H:%M:%S"),
'duration': lambda s: 0 if s == 'Unfinished' else \
sum([int(x)*d for x, d in zip(s.split(':'), [3600, 60, 1])]),
'description': respace,
'platforms': respace,
}
def reset(self):
"""Sort of a constructor."""
super(OverviewParser, self).reset()
self.data = {
'id': [],
'result': [],
'user': [],
'type': [],
'project': [],
#'project_version': [],
#'component': [],
#'component_version': [],
'start': [],
'duration': [],
'description': [],
'platforms': [],
}
self.inrow = False
self.col = -1
self.incol = False
self.nrows = 0
self.coldata = None
def parse(self, fname):
"""parses a file"""
with io.open(fname) as f:
s = f.read()
self.feed(s)
def handle_starttag(self, tag, attrs):
if tag == 'tr':
attrs = dict(attrs)
if 'class' not in attrs:
return
if attrs['class'] not in self.statuses:
return
self.inrow = True
self.nrows += 1
self.col = -1
elif self.inrow and tag == 'td':
self.col += 1
self.incol = True
self.coldata = None
def handle_data(self, data):
if self.inrow and self.incol:
col = self.col
colname = self.fields[col]
if colname is None:
return
if self.coldata is None:
self.coldata = ''
self.coldata += data
def handle_endtag(self, tag):
if tag == 'tr' and self.inrow:
self.inrow = False
self.col = -1
return
elif self.inrow and tag == 'td':
self.incol = False
col = self.col
colname = self.fields[col]
if colname is None:
return
coldata = None if self.coldata is None \
else self.colparsers[colname](self.coldata)
self.data[colname].append(coldata)