66from .tools import str2float as s2f
77from .tools import smartfact_time2datetime as sft2dt
88from .tools import smartfact2table
9- from .tools import extract_run_id_from_system_status
109from .tools import get_entry
1110
1211import re
2019Quantity = namedtuple ('Quantity' , ['value' , 'unit' ])
2120
2221
22+ run_re = re .compile (
23+ '([0-9]{2}:[0-9]{2}:[0-9]{2}) ' # match the time part
24+ '<#[a-z]+>' # html color
25+ '([a-zA-Z\-]+) ' # run type
26+ '\[([a-zA-Z0-9 ]+)\] ' # source name
27+ '\(Run (\d+)\)' # run number
28+ '</#>'
29+ )
30+ Run = namedtuple ('Run' , ['start' , 'type' , 'source' , 'id' ])
31+
32+
2333def to_namedtuple (name , dictionary ):
2434 return namedtuple (name , dictionary .keys ())(** dictionary )
2535
@@ -305,14 +315,19 @@ def main_page(url=None, timeout=None, fallback=False):
305315 get = partial (get_entry , fallback = fallback )
306316
307317 system_status = get (table , 1 , 1 )
318+ power_val , power_unit = get (table , 6 , 3 , default = 'nan nan' ).split ()
319+ trigger_val , trigger_unit , _ = get (table , 5 , 1 , default = 'nan nan nan' ).split ()
308320 return to_namedtuple ('MainPage' , {
309321 'timestamp_1' : sft2dt (get (table , 0 , 0 )),
310322 'timestamp_2' : sft2dt (get (table , 0 , 1 )),
311323 'system_status' : system_status ,
312- 'run_id' : extract_run_id_from_system_status (system_status ),
313324 'relative_camera_temperature' : Quantity (s2f (get (table , 3 , 1 )), 'deg_C' ),
314325 'humidity' : Quantity (s2f (get (table , 4 , 1 )), '%' ),
315326 'wind_speed' : Quantity (s2f (get (table , 4 , 2 )), 'km/h' ),
327+ 'trigger_rate' : Quantity (s2f (trigger_val ), trigger_unit ),
328+ 'median_current' : Quantity (s2f (get (table , 6 , 1 )), 'uA' ),
329+ 'max_current' : Quantity (s2f (get (table , 6 , 2 )), 'uA' ),
330+ 'power' : Quantity (s2f (power_val ), power_unit ),
316331 })
317332
318333
@@ -335,14 +350,48 @@ def errorhist(url=None, timeout=None, fallback=False):
335350
336351 table = smartfact2table (url , timeout = timeout )
337352 get = partial (get_entry , fallback = fallback )
338- timestamp = get (table , 0 , 0 )
339353
340354 history = [
341355 h
342356 for h in get (table , 1 , 1 , default = '' ).split ("<->" )[1 ].split ("<br/>" )
343357 if h
344358 ]
345359 return to_namedtuple ('ErrorHistPage' , {
346- 'timestamp' : sft2dt (timestamp ) if timestamp else None ,
360+ 'timestamp' : sft2dt (get ( table , 0 , 0 )) ,
347361 'history' : history ,
348362 })
363+
364+
365+ def build_run (tup ):
366+ start , run_type , source , run_id = tup
367+ run_id = int (run_id )
368+
369+ now = datetime .utcnow ()
370+ start = datetime .strptime (start , '%H:%M:%S' ).replace (
371+ year = now .year , month = now .month , day = now .day
372+ )
373+
374+ if start > now :
375+ start -= timedelta (hours = 24 )
376+
377+ return Run (start , run_type , source , run_id )
378+
379+
380+ def observations (url = None , timeout = None , fallback = False ):
381+ if url is None :
382+ url = os .path .join (smartfacturl , 'observations.data' )
383+
384+ table = smartfact2table (url , timeout = timeout )
385+ get = partial (get_entry , fallback = fallback )
386+
387+ run_list = get (table , 1 , 1 )
388+ if run_list is not None :
389+ runs = list (map (build_run , run_re .findall (table [1 ][1 ])))
390+ runs .sort (key = lambda r : r .id )
391+ else :
392+ runs = None
393+
394+ return to_namedtuple ('ErrorHistPage' , {
395+ 'timestamp' : sft2dt (get (table , 0 , 0 )),
396+ 'runs' : runs ,
397+ })
0 commit comments