4141from .factory .dispatch import Dispatcher
4242from .executor import Executor , get_current_shell
4343from .metrics import SystemResourceMeasurement
44- from .models import FOLDER_REGEX , NAME_REGEX , MetricKeyString
44+ from .models import simvue_timestamp , FOLDER_REGEX , NAME_REGEX , MetricKeyString
4545from .system import get_system
4646from .metadata import git_info , environment
4747from .eco import CO2Monitor
4848from .utilities import (
4949 skip_if_failed ,
5050 validate_timestamp ,
51- simvue_timestamp ,
5251)
5352from .api .objects import (
5453 Run as RunObject ,
@@ -281,11 +280,6 @@ def duration(self) -> float:
281280 """Return current run duration"""
282281 return time .time () - self ._start_time
283282
284- @property
285- def time_stamp (self ) -> str :
286- """Return current timestamp"""
287- return simvue_timestamp ()
288-
289283 @property
290284 def processes (self ) -> list [psutil .Process ]:
291285 """Create an array containing a list of processes"""
@@ -1256,22 +1250,30 @@ def update_tags(self, tags: list[str]) -> bool:
12561250
12571251 @skip_if_failed ("_aborted" , "_suppress_errors" , False )
12581252 @check_run_initialised
1259- @pydantic .validate_call
1260- def log_event (self , message : str , timestamp : str | None = None ) -> bool :
1253+ @pydantic .validate_call (config = pydantic .ConfigDict (validate_default = True ))
1254+ def log_event (
1255+ self ,
1256+ message : str ,
1257+ timestamp : typing .Annotated [
1258+ str | None , pydantic .BeforeValidator (simvue_timestamp )
1259+ ] = None ,
1260+ ) -> bool :
12611261 """Log event to the server
12621262
12631263 Parameters
12641264 ----------
12651265 message : str
12661266 event message to log
12671267 timestamp : str, optional
1268- manually specify the time stamp for this log, by default None
1268+ manually specify the Simvue time stamp for this log, by default None
12691269
12701270 Returns
12711271 -------
12721272 bool
12731273 whether event log was successful
12741274 """
1275+ if not timestamp :
1276+ raise Exception
12751277 if self ._aborted :
12761278 return False
12771279
@@ -1287,22 +1289,24 @@ def log_event(self, message: str, timestamp: str | None = None) -> bool:
12871289 self ._error ("Cannot log events when not in the running state" )
12881290 return False
12891291
1290- if timestamp and not validate_timestamp (timestamp ):
1291- self ._error ("Invalid timestamp format" )
1292- return False
1293-
1294- _data = {"message" : message , "timestamp" : timestamp or self .time_stamp }
1292+ _data = {
1293+ "message" : message ,
1294+ "timestamp" : timestamp ,
1295+ }
12951296 self ._dispatcher .add_item (_data , "events" , self ._queue_blocking )
12961297
12971298 return True
12981299
1300+ @pydantic .validate_call (config = pydantic .ConfigDict (validate_default = True ))
12991301 def _add_metrics_to_dispatch (
13001302 self ,
13011303 metrics : dict [str , int | float ],
13021304 * ,
13031305 step : int | None = None ,
13041306 time : float | None = None ,
1305- timestamp : str | None = None ,
1307+ timestamp : typing .Annotated [
1308+ str | None , pydantic .BeforeValidator (simvue_timestamp )
1309+ ] = None ,
13061310 join_on_fail : bool = True ,
13071311 ) -> bool :
13081312 if self ._user_config .run .mode == "disabled" :
@@ -1326,34 +1330,37 @@ def _add_metrics_to_dispatch(
13261330 )
13271331 return False
13281332
1329- if timestamp and not validate_timestamp (timestamp ):
1333+ if timestamp and not validate_timestamp (timestamp , raise_except = False ):
13301334 self ._error ("Invalid timestamp format" , join_on_fail )
13311335 return False
13321336
13331337 _data : dict [str , typing .Any ] = {
13341338 "values" : metrics ,
13351339 "time" : time if time is not None else self .duration ,
1336- "timestamp" : timestamp if timestamp is not None else self . time_stamp ,
1340+ "timestamp" : timestamp ,
13371341 "step" : step if step is not None else self ._step ,
13381342 }
13391343 self ._dispatcher .add_item (_data , "metrics_regular" , self ._queue_blocking )
13401344
13411345 return True
13421346
1347+ @pydantic .validate_call (config = pydantic .ConfigDict (validate_default = True ))
13431348 def _add_tensors_to_dispatch (
13441349 self ,
13451350 tensors : dict [str , int | float ],
13461351 * ,
13471352 step : int | None = None ,
13481353 time : float | None = None ,
1349- timestamp : str | None = None ,
1354+ timestamp : typing .Annotated [
1355+ str | None , pydantic .BeforeValidator (simvue_timestamp )
1356+ ] = None ,
13501357 join_on_fail : bool = True ,
13511358 ) -> bool :
13521359 for tensor , array in tensors .items ():
13531360 _data : dict [str , typing .Any ] = {
13541361 "array" : array ,
13551362 "time" : time if time is not None else self .duration ,
1356- "timestamp" : timestamp if timestamp is not None else self . time_stamp ,
1363+ "timestamp" : timestamp ,
13571364 "step" : step if step is not None else self ._step ,
13581365 "grid" : self ._grids [tensor ]["id" ],
13591366 "metric" : tensor ,
@@ -1389,7 +1396,7 @@ def _add_values_to_dispatch(
13891396 )
13901397 return False
13911398
1392- if timestamp and not validate_timestamp (timestamp ):
1399+ if timestamp and not validate_timestamp (timestamp , raise_except = False ):
13931400 self ._error ("Invalid timestamp format" , join_on_fail )
13941401 return False
13951402
@@ -1549,7 +1556,7 @@ def log_metrics(
15491556 )
15501557 return False
15511558
1552- if timestamp and not validate_timestamp (timestamp ):
1559+ if timestamp and not validate_timestamp (timestamp , raise_except = False ):
15531560 self ._error ("Invalid timestamp format" , timestamp )
15541561 return False
15551562
0 commit comments