forked from AutoDQM/AutoDQM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
executable file
·198 lines (161 loc) · 6.27 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
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
#!/usr/bin/env python3
# -*- 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
import sys
sys.path.append('/var/www/cgi-bin/')
VARS = {}
SITE_BASENAME = "/dqm/autodqm"
def handle_request(req):
err = None
try:
load_vars()
if req['type'] == "fetch_run":
data = fetch_runs(req['dqmSource'], req['subsystem'], req['series'], req['sample'], req['run'])
elif req['type'] == "process":
data = process(int(req['chunk_index']),
int(req['chunk_size']),
req['dqmSource'],
req['subsystem'],
req['data_series'],
req['data_sample'],
req['data_run'],
req['ref_series'],
req['ref_sample'],
req['ref_run'])
elif req['type'] == "get_dqmSources":
data = get_dqmSources()
elif req['type'] == "get_subsystems":
data = get_subsystems()
elif req['type'] == "get_series":
data = get_series(req['dqmSource'])
elif req['type'] == "get_samples":
data = get_samples(req['dqmSource'], req['series'])
elif req['type'] == "get_runs":
data = get_runs(req['dqmSource'], req['subsystem'], req['series'], req['sample'])
elif req['type'] == "get_ref":
data = get_ref(req['dqmSource'],
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_runs(dqmSource, subsystem, series, sample, runs):
with make_dqm() as dqm:
for run in runs.split('_'):
dqm.fetch_run(dqmSource, subsystem, series, sample, run)
return {}
def process(chunk_index, chunk_size,
dqmSource, 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(dqmSource, subsystem, data_series, data_sample, data_run)
ref_runs = []
ref_paths = []
for one_ref_run in ref_run.split('_'):
ref_runs.append( one_ref_run )
ref_paths.append( dqm.fetch_run(dqmSource, subsystem, ref_series, ref_sample, one_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(chunk_index, chunk_size, config_dir,
dqmSource, subsystem,
data_series, data_sample,
data_run, data_path,
ref_series, ref_sample,
ref_runs, ref_paths,
output_dir=results_dir,
plugin_dir=plugin_dir)
# Relativize the results paths
def relativize(p): return os.path.join(
SITE_BASENAME + '/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'])
new_chunk_index = chunk_index + chunk_size;
if(len(results) == 0):
new_chunk_index = -1;
return {'items': results, "chunk_index": new_chunk_index}
def get_dqmSources():
names = ["Online", "Offline"]
return {'items': [{"name": n} for n in names]}
def get_subsystems():
names = autodqm.cfg.list_subsystems(VARS['CONFIG'])
return {'items': [{"name": n} for n in names]}
def get_series(dqmSource):
with make_dqm() as dqm:
rows = dqm.fetch_series_list(dqmSource)
return {'items': [r._asdict() for r in rows]}
def get_samples(dqmSource, series):
with make_dqm() as dqm:
rows = dqm.fetch_sample_list(dqmSource, series)
return {'items': [r._asdict() for r in rows]}
def get_runs(dqmSource, subsystem, series, sample):
with make_dqm() as dqm:
## TODO: truncate list to reasonable range, e.g. 2018 and later - AWB 2022.05.23
rows = dqm.fetch_run_list(dqmSource, subsystem, series, sample)
return {'items': [r._asdict() for r in rows]}
def get_ref(dqmSource, subsystem, data_run, series, sample):
config_dir = VARS['CONFIG']
with make_dqm() as dqm:
rows = dqm.fetch_run_list(dqmSource, subsystem, 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 list(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)))