66import re
77import time
88from abc import ABC
9- from collections import OrderedDict
109from typing import Any
1110from urllib .parse import quote_plus
1211
1817
1918
2019class StepIO :
21- def __init__ (self ):
20+ def __init__ (self ) -> None :
2221 self .bytes = 0
2322 self .records = 0
2423 self .sampled = False
2524
26- def get_stats (self , time_total ) :
25+ def get_stats (self , time_total : float ) -> dict [ str , Any ] :
2726 mb_s = None
2827 rec_s = None
2928 if self .bytes :
3029 mb_s = round (float (self .bytes ) / (1024 * 1024 ) / time_total , 2 )
3130 if self .records :
3231 rec_s = int (self .records / time_total )
33- return OrderedDict (
34- [
35- ("bytes" , self .bytes ),
36- ("records" , self .records ),
37- ("mb_s" , mb_s ),
38- ("rec_s" , rec_s ),
39- ("sampled" , self .sampled ),
40- ]
41- )
32+ return {
33+ "bytes" : self .bytes ,
34+ "records" : self .records ,
35+ "mb_s" : mb_s ,
36+ "rec_s" : rec_s ,
37+ "sampled" : self .sampled ,
38+ }
4239
4340
4441class Step :
45- def __init__ (self , name , task ) :
42+ def __init__ (self , name : str , task : MetrologyTask ) -> None :
4643 self .name = name
4744 self .task = task
4845 self .read = StepIO ()
@@ -57,22 +54,17 @@ def done(self):
5754 self .time_total = self .time_finished - self .time_started
5855
5956 def get_stats (self ):
60- stats = OrderedDict (
61- [
62- ("name" , self .name ),
63- ("metadata" , self .metadata ),
64- ("time_started" , self .time_started ),
65- ("time_finished" , self .time_finished ),
66- ("time_total" , self .time_total ),
67- ("read" , self .read .get_stats (self .time_total )),
68- ("write" , self .write .get_stats (self .time_total )),
69- ]
70- )
71- return stats
57+ stats = {
58+ "name" : self .name ,
59+ "metadata" : self .metadata ,
60+ "time_started" : self .time_started ,
61+ "time_finished" : self .time_finished ,
62+ "time_total" : self .time_total ,
63+ "read" : self .read .get_stats (self .time_total ),
64+ "write" : self .write .get_stats (self .time_total ),
65+ }
7266
73- def mset_metadata (self , kvs ):
74- for k , v in kvs :
75- self .metadata [k ] = v
67+ return stats
7668
7769
7870class StepExecution :
@@ -90,22 +82,22 @@ class MetrologyTask:
9082 context : dict [str , Any ]
9183 steps : list [Step ]
9284
93- def can_upload (self ):
85+ def can_upload (self ) -> bool :
9486 if not hasattr (self , "context" ):
9587 return False
9688 return all (c in self .context for c in ("workflow_id" , "run_id" , "activity_id" ))
9789
9890 @property
9991 def metrology_path (self ):
100- path = []
92+ path : list [ str ] = []
10193 if settings .METROLOGY_PATH_PREFIX is not None :
10294 path .append (settings .METROLOGY_PATH_PREFIX )
10395 path .append (self .context ["workflow_id" ])
10496 path .append (quote_plus (self .context ["run_id" ]))
10597 path .append (f"activity.{ self .context ['activity_id' ]} .json" )
10698 return str (os .path .join (* path ))
10799
108- def step (self , name ) :
100+ def step (self , name : str ) -> StepExecution :
109101 """
110102 To be called in a `with` execution
111103 Ex :
0 commit comments