forked from HSLdevcom/OTPQA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhreport.py
153 lines (115 loc) · 4.97 KB
/
hreport.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
from __future__ import print_function
from future.standard_library import install_aliases
import json
import numpy as np
import math
from datetime import datetime
import pprint
install_aliases()
from urllib.parse import urlparse, parse_qs
def parsetime(aa):
if aa is None:
return None
return float(aa.split()[0])
def humanize(lt):
minf = math.modf(lt/60.0)
hf = math.modf(minf[1]/60.0)
os = ''
if hf[1] > 0:
os += '%d h ' % (hf[1])
os += '%d min ' % (hf[0]*60)
else:
if minf[1] > 0:
os += '%d min ' % minf[1]
os += '%d sec' % (minf[0] * 60)
return os
def main(filenames, input_blob=None, dt_url=None):
if (filenames is None or len(filenames) == 0) and input_blob is None:
return
if input_blob is None:
datasets = []
for fn in filenames:
blob = json.load(open(fn))
dataset = dict([(response["id_tuple"], response) for response in blob['responses']])
datasets.append(dataset)
else:
datasets = [dict([(response["id_tuple"], response) for response in input_blob['responses']]), ]
id_tuples = datasets[0].keys()
if len(id_tuples) == 0:
print("Input does not contain any data")
exit()
yield "<html>"
yield """<head><style>table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
text-align: left;
vertical-align:top;
}</style></head>"""
yield """<table border="1">"""
dataset_total_times = dict(zip(range(len(datasets)), [[] for x in range(len(datasets))]))
dataset_avg_times = dict(zip(range(len(datasets)), [[] for x in range(len(datasets))]))
dataset_fails = dict(zip(range(len(datasets)), [0] * len(datasets)))
for id_tuple in id_tuples:
otpurl = ''
if dt_url is not None:
otpurl = datasets[0][id_tuple]['url']
otpurl_parsed = urlparse(otpurl)
otpurl_params = parse_qs(otpurl_parsed.query)
otptst = datetime.strptime('%s %s' % (otpurl_params['date'][0],otpurl_params['time'][0]),'%Y-%m-%d %H:%M')
otptst = (otptst - datetime(1970, 1, 1)).total_seconds()
dturl = 'http://' + dt_url + '/reitti/from::%s/to::%s?time=%d' % \
(datasets[0][id_tuple]['from'], datasets[0][id_tuple]['to'],otptst)
yield """<tr><td rowspan="2" width="120">OTP: <a href="%s">%s</a><br/>DT: <a href="%s">%s</a></td>""" % \
(otpurl, id_tuple, dturl, id_tuple)
for i, dataset in enumerate(datasets):
response = dataset[id_tuple]
if not 'total_time' in response:
continue
dataset_total_times[i].append(parsetime(response['total_time']))
dataset_avg_times[i].append(parsetime(response['avg_time']))
yield "<td>%s total, %s avg / %s</td>" % (
response['total_time'], response['avg_time'], datasets[i][id_tuple]['mode'])
yield "</tr>"
for i, dataset in enumerate(datasets):
yield "<td>"
response = dataset[id_tuple]
# Filter out long walks (OTP has only soft walk limitin)
yield "<table border=1 width=100%><tr>"
if not 'itins' in response:
yield "<td style=\"background-color:#EDA1A1\">FAIL</td></tr></table></tr>"
continue
if len(response['itins']) == 0:
dataset_fails[i] += 1
yield "<td style=\"background-color:#EDA1A1\">NONE</td></tr></table></tr>"
continue
if all((itin['walk_limit_exceeded'] for itin in response['itins'])):
dataset_fails[i] += 1
yield "<td style=\"background-color:#EDA1A1\">LONG WALK (%.1f km)</td></tr></table></tr>" % (min((itin['walk_distance'] for itin in response['itins']))/1000.0)
continue
for itin in response['itins']:
filling = list(zip(itin['leg_modes'],(humanize(lt) for lt in itin['leg_times'])))
if filling == "{}":
color = "#EDECA1"
else:
color = "#AEEDA1"
yield "<td style=\"background-color:%s\"><small>%s</small><br/>total walk distance: %.1f km - wait: %s</td>" % (color, filling,itin['walk_distance']/1000.0, humanize(itin['wait_time_sec']))
yield "</tr></table>"
yield "</td>"
yield "</tr>"
yield "<tr><td>stats</td>"
for i in range(len(datasets)):
yield "<td>fails: %s (%.2f%%). total time: median:%.2fs mean:%.2fs</td>" % (
dataset_fails[i], 100 * dataset_fails[i] / float(len(id_tuples)), np.median(dataset_total_times[i]),
np.mean(dataset_total_times[i]))
yield "</tr>"
yield "</table>"
yield "</html>"
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print("usage: cmd fn1[fn2[fn3...]]")
exit()
for line in main(sys.argv[1:]):
print(line)