22
22
TODO add support for 3 files update
23
23
"""
24
24
25
+ import argparse
25
26
import subprocess
26
27
from datetime import datetime
28
+
27
29
import matplotlib .pyplot as plt
28
- import argparse
29
30
30
31
31
32
def convert_timestamp (ts ):
32
33
"""
33
34
Converts the timestamp in milliseconds in human readable format
34
35
"""
35
- return datetime .utcfromtimestamp (ts / 1000 ).strftime (' %Y-%m-%d %H:%M:%S' )
36
+ return datetime .utcfromtimestamp (ts / 1000 ).strftime (" %Y-%m-%d %H:%M:%S" )
36
37
37
38
38
39
def get_ccdb_obj (path , timestamp , dest = "/tmp/" , verbose = 0 ):
39
40
"""
40
41
Gets the ccdb object from 'path' and 'timestamp' and downloads it into 'dest'
41
42
"""
42
43
if verbose :
43
- print ("Getting obj" , path , "with timestamp" ,
44
- timestamp , convert_timestamp (timestamp ))
45
- cmd = f"o2-ccdb-downloadccdbfile --path { path } --dest { dest } --timestamp { timestamp } "
44
+ print (
45
+ "Getting obj" ,
46
+ path ,
47
+ "with timestamp" ,
48
+ timestamp ,
49
+ convert_timestamp (timestamp ),
50
+ )
51
+ cmd = (
52
+ f"o2-ccdb-downloadccdbfile --path { path } --dest { dest } --timestamp { timestamp } "
53
+ )
46
54
subprocess .run (cmd .split ())
47
55
48
56
@@ -61,7 +69,7 @@ def get_ccdb_obj_validity(path, dest="/tmp/", verbose=0):
61
69
print (* output , "\n " )
62
70
print ("err:" )
63
71
print (error )
64
- result = list (filter (lambda x : x .startswith (' Valid-' ), output ))
72
+ result = list (filter (lambda x : x .startswith (" Valid-" ), output ))
65
73
ValidFrom = result [0 ].split ()
66
74
ValidUntil = result [1 ].split ()
67
75
return [int (ValidFrom [- 1 ]), int (ValidUntil [- 1 ])]
@@ -71,12 +79,18 @@ def upload_ccdb_obj(path, timestamp_from, timestamp_until, dest="/tmp/", meta=""
71
79
"""
72
80
Uploads a new object to CCDB in the 'path' using the validity timestamp specified
73
81
"""
74
- print ("Uploading obj" , path , "with timestamp" , [timestamp_from , timestamp_until ],
75
- convert_timestamp (timestamp_from ), convert_timestamp (timestamp_until ))
82
+ print (
83
+ "Uploading obj" ,
84
+ path ,
85
+ "with timestamp" ,
86
+ [timestamp_from , timestamp_until ],
87
+ convert_timestamp (timestamp_from ),
88
+ convert_timestamp (timestamp_until ),
89
+ )
76
90
key = path .split ("/" )[- 1 ]
77
91
cmd = f"o2-ccdb-upload -f { dest } { path } /snapshot.root "
78
92
cmd += f"--key { key } --path { path } "
79
- cmd += f" --starttimestamp { timestamp_from } --endtimestamp { timestamp_until } --meta \ "{ meta } \" "
93
+ cmd += f' --starttimestamp { timestamp_from } --endtimestamp { timestamp_until } --meta "{ meta } "'
80
94
subprocess .run (cmd .split ())
81
95
82
96
@@ -87,9 +101,9 @@ def main(path, timestamp_from, timestamp_until, verbose=0, show=False):
87
101
Gets the object from CCDB specified in 'path' and for 'timestamp_until+1'
88
102
If required plots the situation before and after the update
89
103
"""
90
- get_ccdb_obj (path , timestamp_from - 1 )
104
+ get_ccdb_obj (path , timestamp_from - 1 )
91
105
val_before = get_ccdb_obj_validity (path , verbose = verbose )
92
- get_ccdb_obj (path , timestamp_until + 1 )
106
+ get_ccdb_obj (path , timestamp_until + 1 )
93
107
val_after = get_ccdb_obj_validity (path , verbose = verbose )
94
108
overlap_before = val_before [1 ] > timestamp_from
95
109
overlap_after = val_after [0 ] < timestamp_until
@@ -98,48 +112,67 @@ def main(path, timestamp_from, timestamp_until, verbose=0, show=False):
98
112
print ("Previous objects overalps" )
99
113
if overlap_after :
100
114
print ("Next objects overalps" )
101
- trimmed_before = val_before if not overlap_before else [
102
- val_before [0 ], timestamp_from - 1 ]
103
- trimmed_after = val_after if not overlap_after else [
104
- timestamp_until + 1 , val_after [1 ]]
115
+ trimmed_before = (
116
+ val_before if not overlap_before else [val_before [0 ], timestamp_from - 1 ]
117
+ )
118
+ trimmed_after = (
119
+ val_after if not overlap_after else [timestamp_until + 1 , val_after [1 ]]
120
+ )
105
121
if show :
106
122
fig , ax = plt .subplots ()
107
123
fig
108
124
109
125
def bef_af (v , y ):
110
126
return [v [0 ] - 1 ] + v + [v [1 ] + 1 ], [0 , y , y , 0 ]
127
+
111
128
if True :
112
- ax .plot (* bef_af (val_before , 0.95 ), label = ' before' )
113
- ax .plot (* bef_af (val_after , 1.05 ), label = ' after' )
129
+ ax .plot (* bef_af (val_before , 0.95 ), label = " before" )
130
+ ax .plot (* bef_af (val_after , 1.05 ), label = " after" )
114
131
if False :
115
- ax .plot (* bef_af (trimmed_before , 0.9 ), label = ' trimmed before' )
116
- ax .plot (* bef_af (trimmed_after , 1.1 ), label = ' trimmed after' )
117
- ax .plot (* bef_af ([timestamp_from , timestamp_until ], 1 ), label = ' object' )
132
+ ax .plot (* bef_af (trimmed_before , 0.9 ), label = " trimmed before" )
133
+ ax .plot (* bef_af (trimmed_after , 1.1 ), label = " trimmed after" )
134
+ ax .plot (* bef_af ([timestamp_from , timestamp_until ], 1 ), label = " object" )
118
135
xlim = 10000000
119
- plt .xlim ([timestamp_from - xlim , timestamp_until + xlim ])
136
+ plt .xlim ([timestamp_from - xlim , timestamp_until + xlim ])
120
137
plt .ylim (0 , 2 )
121
- plt .xlabel (' Timestamp' )
122
- plt .ylabel (' Validity' )
138
+ plt .xlabel (" Timestamp" )
139
+ plt .ylabel (" Validity" )
123
140
plt .legend ()
124
141
plt .show ()
125
142
126
143
127
144
if __name__ == "__main__" :
128
145
parser = argparse .ArgumentParser (
129
- description = "Uploads timestamp non overlapping objects to CCDB."
130
- "Basic example: `./update_ccdb.py qc/TOF/TOFTaskCompressed/hDiagnostic 1588956517161 1588986517161 --show --verbose`" )
131
- parser .add_argument ('path' , metavar = 'path_to_object' , type = str ,
132
- help = 'Path of the object in the CCDB repository' )
133
- parser .add_argument ('timestamp_from' , metavar = 'from_timestamp' , type = int ,
134
- help = 'Timestamp of start for the new object to use' )
135
- parser .add_argument ('timestamp_until' , metavar = 'until_timestamp' , type = int ,
136
- help = 'Timestamp of stop for the new object to use' )
137
- parser .add_argument ('--verbose' , '-v' , action = 'count' , default = 0 )
138
- parser .add_argument ('--show' , '-s' , action = 'count' , default = 0 )
146
+ description = "Uploads timestamp non overlapping objects to CCDB. "
147
+ "Basic example: `./update_ccdb.py qc/TOF/TOFTaskCompressed/hDiagnostic "
148
+ "1588956517161 1588986517161 --show --verbose`"
149
+ )
150
+ parser .add_argument (
151
+ "path" ,
152
+ metavar = "path_to_object" ,
153
+ type = str ,
154
+ help = "Path of the object in the CCDB repository" ,
155
+ )
156
+ parser .add_argument (
157
+ "timestamp_from" ,
158
+ metavar = "from_timestamp" ,
159
+ type = int ,
160
+ help = "Timestamp of start for the new object to use" ,
161
+ )
162
+ parser .add_argument (
163
+ "timestamp_until" ,
164
+ metavar = "until_timestamp" ,
165
+ type = int ,
166
+ help = "Timestamp of stop for the new object to use" ,
167
+ )
168
+ parser .add_argument ("--verbose" , "-v" , action = "count" , default = 0 )
169
+ parser .add_argument ("--show" , "-s" , action = "count" , default = 0 )
139
170
140
171
args = parser .parse_args ()
141
- main (path = args .path ,
142
- timestamp_from = args .timestamp_from ,
143
- timestamp_until = args .timestamp_until ,
144
- verbose = args .verbose ,
145
- show = args .show )
172
+ main (
173
+ path = args .path ,
174
+ timestamp_from = args .timestamp_from ,
175
+ timestamp_until = args .timestamp_until ,
176
+ verbose = args .verbose ,
177
+ show = args .show ,
178
+ )
0 commit comments