2222TODO add support for 3 files update
2323"""
2424
25+ import argparse
2526import subprocess
2627from datetime import datetime
28+
2729import matplotlib .pyplot as plt
28- import argparse
2930
3031
3132def convert_timestamp (ts ):
3233 """
3334 Converts the timestamp in milliseconds in human readable format
3435 """
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" )
3637
3738
3839def get_ccdb_obj (path , timestamp , dest = "/tmp/" , verbose = 0 ):
3940 """
4041 Gets the ccdb object from 'path' and 'timestamp' and downloads it into 'dest'
4142 """
4243 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+ )
4654 subprocess .run (cmd .split ())
4755
4856
@@ -61,7 +69,7 @@ def get_ccdb_obj_validity(path, dest="/tmp/", verbose=0):
6169 print (* output , "\n " )
6270 print ("err:" )
6371 print (error )
64- result = list (filter (lambda x : x .startswith (' Valid-' ), output ))
72+ result = list (filter (lambda x : x .startswith (" Valid-" ), output ))
6573 ValidFrom = result [0 ].split ()
6674 ValidUntil = result [1 ].split ()
6775 return [int (ValidFrom [- 1 ]), int (ValidUntil [- 1 ])]
@@ -71,12 +79,18 @@ def upload_ccdb_obj(path, timestamp_from, timestamp_until, dest="/tmp/", meta=""
7179 """
7280 Uploads a new object to CCDB in the 'path' using the validity timestamp specified
7381 """
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+ )
7690 key = path .split ("/" )[- 1 ]
7791 cmd = f"o2-ccdb-upload -f { dest } { path } /snapshot.root "
7892 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 } "'
8094 subprocess .run (cmd .split ())
8195
8296
@@ -87,9 +101,9 @@ def main(path, timestamp_from, timestamp_until, verbose=0, show=False):
87101 Gets the object from CCDB specified in 'path' and for 'timestamp_until+1'
88102 If required plots the situation before and after the update
89103 """
90- get_ccdb_obj (path , timestamp_from - 1 )
104+ get_ccdb_obj (path , timestamp_from - 1 )
91105 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 )
93107 val_after = get_ccdb_obj_validity (path , verbose = verbose )
94108 overlap_before = val_before [1 ] > timestamp_from
95109 overlap_after = val_after [0 ] < timestamp_until
@@ -98,48 +112,67 @@ def main(path, timestamp_from, timestamp_until, verbose=0, show=False):
98112 print ("Previous objects overalps" )
99113 if overlap_after :
100114 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+ )
105121 if show :
106122 fig , ax = plt .subplots ()
107123 fig
108124
109125 def bef_af (v , y ):
110126 return [v [0 ] - 1 ] + v + [v [1 ] + 1 ], [0 , y , y , 0 ]
127+
111128 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" )
114131 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" )
118135 xlim = 10000000
119- plt .xlim ([timestamp_from - xlim , timestamp_until + xlim ])
136+ plt .xlim ([timestamp_from - xlim , timestamp_until + xlim ])
120137 plt .ylim (0 , 2 )
121- plt .xlabel (' Timestamp' )
122- plt .ylabel (' Validity' )
138+ plt .xlabel (" Timestamp" )
139+ plt .ylabel (" Validity" )
123140 plt .legend ()
124141 plt .show ()
125142
126143
127144if __name__ == "__main__" :
128145 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 )
139170
140171 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