-
Notifications
You must be signed in to change notification settings - Fork 4
/
offline_run_history.py
84 lines (73 loc) · 3.16 KB
/
offline_run_history.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
import requests
import re
run_number = 5458
parameter_spec={"metadata.fcl": {
"components":"components",
"configuration":"config_name",
'projectversion':'sbndaq_commit_or_version'
#'start_time':'daqinterface_start_time',
#'stop_time':'daqinterface_stop_time'
}}
ucondb_uri = 'https://dbdata0vm.fnal.gov:9443/icarus_on_ucon_prod/app/data/run_records_pending/configuration/key=%d'
class RunHistoryiReader:
def __init__(self,
parameter_spec=parameter_spec,
ucondb_uri=ucondb_uri):
self.parameter_spec=parameter_spec
self.ucondb_uri=ucondb_uri
def unpack_clob(self,clob):
pattern = re.compile (r'#{5}[^#{5}](.*\.fcl):\s#{5}\s([\s\S]*?)(?=(#{5}|End of Record))')
for match in re.finditer(pattern, clob):
yield (match.group(1),match.group(2))
def fetch_clob(self,run_number):
try:
#in case of issues with RunHistory DB, a hotfix to disable SSL certificate checking is to add verify=False option to the get command below
response = requests.get(self.ucondb_uri%(run_number))
response.raise_for_status()
return (0, response.text)
except requests.exceptions.HTTPError as ex:
return (-1, "Http Error: %s"%(ex))
except requests.exceptions.ConnectionError as ex:
return (-2 , "Error Connecting: %s"% (ex))
except requests.exceptions.Timeout as ex:
return (-3, "Timeout Error: %s"%(ex))
except requests.exceptions.RequestException as ex:
return (-4, "Error: %s"%(ex))
def parse_fhicl(self,fhicl_file):
#Rewrite to use the fhiclpy libary
results = {}
error_count=0
for file_name, export_params in self.parameter_spec.items():
for name, key in export_params.items():
pattern=r'%s:\s+(.*)'%key
matches=re.findall(pattern,fhicl_file[1])
if matches is None or not matches:
results[name]='Error: no matches for the regex /%s/ in %s.'%(pattern,file_name)
error_count+=1
continue
if len(matches) > 1 :
results[name]='Error: too many matches for the regex /%s/ in %s.'%(pattern,file_name)
error_count+=1
continue
results[name]=matches[0].replace('"', '').strip()
return (error_count,results)
def read(self,run_number):
err, clob = self.fetch_clob(run_number)
if err != 0:
return(err,{'error':clob})
results = {}
error_count=0
for fhicl_file in self.unpack_clob(clob):
if fhicl_file[0] not in self.parameter_spec:
continue
tmp=self.parse_fhicl(fhicl_file)
error_count+=tmp[0]
results = {**results, **tmp[1]}
return (error_count, results)
if __name__ == '__main__':
my_existing_data={'run_number':run_number }
result=RunHistoryiReader().read(run_number)
if(result[0]!=0):
print("ErrorCode=%d."%result[0])
#merge and print
print({**my_existing_data,**result[1]})