-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.py
executable file
·171 lines (138 loc) · 4.96 KB
/
index.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import autodqm.cfg
import cgi
import json
import os
import traceback
from autodqm import compare_hists
from autodqm.dqm import DQMSession
from autoref import sql
VARS = {}
def handle_request(req):
err = None
try:
load_vars()
if req['type'] == "fetch_run":
data = fetch_run(req['series'], req['sample'], req['run'])
elif req['type'] == "process":
data = process(req['subsystem'],
req['data_series'],
req['data_sample'],
req['data_run'],
req['ref_series'],
req['ref_sample'],
req['ref_run'])
elif req['type'] == "get_subsystems":
data = get_subsystems()
elif req['type'] == "get_series":
data = get_series()
elif req['type'] == "get_samples":
data = get_samples(req['series'])
elif req['type'] == "get_runs":
data = get_runs(req['series'], req['sample'])
elif req['type'] == "get_ref":
data = get_ref(req['subsystem'],
req['run'],
req['series'],
req['sample'])
else:
raise error
except Exception as e:
err = e
tb = traceback.format_exc()
finally:
res = {}
if err:
res['error'] = {
'message': str(err),
'traceback': tb
}
else:
res['data'] = data
return res
def fetch_run(series, sample, run):
with make_dqm() as dqm:
dqm.fetch_run(series, sample, run)
return {}
def process(subsystem,
data_series, data_sample, data_run,
ref_series, ref_sample, ref_run):
with make_dqm() as dqm:
# Get root file paths
data_path = dqm.fetch_run(data_series, data_sample, data_run)
ref_path = dqm.fetch_run(ref_series, ref_sample, ref_run)
# Get config and results/plugins directories
results_dir = os.path.join(VARS['PUBLIC'], 'results')
plugin_dir = VARS['PLUGINS']
config_dir = VARS['CONFIG']
# Process this query
results = compare_hists.process(config_dir, subsystem,
data_series, data_sample,
data_run, data_path,
ref_series, ref_sample,
ref_run, ref_path,
output_dir=results_dir,
plugin_dir=plugin_dir)
# Relativize the results paths
def relativize(p): return os.path.join(
'/results', os.path.relpath(p, results_dir))
for r in results:
r['pdf_path'] = relativize(r['pdf_path'])
r['json_path'] = relativize(r['json_path'])
r['png_path'] = relativize(r['png_path'])
return {'items': results}
def get_subsystems():
names = autodqm.cfg.list_subsystems(VARS['CONFIG'])
return {'items': [{"name": n} for n in names]}
def get_series():
with make_dqm() as dqm:
rows = dqm.fetch_series_list()
return {'items': [r._asdict() for r in rows]}
def get_samples(series):
with make_dqm() as dqm:
rows = dqm.fetch_sample_list(series)
return {'items': [r._asdict() for r in rows]}
def get_runs(series, sample):
with make_dqm() as dqm:
rows = dqm.fetch_run_list(series, sample)
return {'items': [r._asdict() for r in rows]}
def get_ref(subsystem, data_run, series, sample):
config_dir = VARS['CONFIG']
with make_dqm() as dqm:
rows = dqm.fetch_run_list(series, sample)
ref_runs = []
for row in [r._asdict() for r in rows]:
ref_runs.append(row['name'])
refs = sql.fetch_refs(autodqm.cfg.get_subsystem(config_dir, subsystem), data_run, ref_runs)
return {'items': refs['ref_data'], 'candidates':refs['ref_cands']}
def load_vars():
try:
VARS.update({
'SSLCERT': os.environ['ADQM_SSLCERT'],
'SSLKEY': os.environ['ADQM_SSLKEY'],
'DB': os.environ['ADQM_DB'],
'PUBLIC': os.environ['ADQM_PUBLIC'],
'CONFIG': os.environ['ADQM_CONFIG'],
'PLUGINS': os.environ['ADQM_PLUGINS']
})
VARS['CERT'] = (VARS['SSLCERT'], VARS['SSLKEY'])
VARS['CACHE'] = os.path.join(VARS['DB'], 'dqm_offline')
except Exception as e:
raise ServerError("Server incorrectly configured: {}".format(e))
def make_dqm():
return DQMSession(VARS['CERT'], VARS['DB'], cache=VARS['CACHE'])
class error(Exception):
pass
class ServerError(error):
pass
if __name__ == "__main__":
cgi_req = cgi.FieldStorage()
req = {}
for k in cgi_req.keys():
req[str(k)] = str(cgi_req[k].value)
res = handle_request(req)
print("Content-type: application/json")
print("Access-Control-Allow-Origin: *")
print("")
print(json.dumps(res))