openrvdas.logger.transforms.timestamp_transform
1#!/usr/bin/env python3 2 3import logging 4 5from logger.utils import timestamp # noqa: E402 6from logger.utils.nmea_timestamp import NMEATimestampExtractor # noqa: E402 7from logger.transforms.transform import Transform # noqa: E402 8 9 10################################################################################ 11class TimestampTransform(Transform): 12 """Prepend a timestamp to a text record. 13 14 By default the system clock is used. When ``use_nmea_timestamp`` is 15 enabled, the transform first attempts to extract the timestamp 16 embedded in NMEA 0183 sentences (GGA, RMC, ZDA, and many others) 17 and only falls back to the system clock when no NMEA time is 18 available. 19 20 Parameters 21 ---------- 22 time_format : str 23 strftime format string for the prepended timestamp. Defaults 24 to ``logger.utils.timestamp.TIME_FORMAT`` 25 (``'%Y-%m-%dT%H:%M:%S.%fZ'``). 26 time_zone : datetime.timezone 27 Timezone applied to the timestamp. Defaults to UTC. 28 sep : str 29 Separator inserted between the timestamp and the record text. 30 Defaults to a single space. 31 32 use_nmea_timestamp : bool 33 When True, attempt to parse the NMEA time field from the 34 incoming record before falling back to the system clock. 35 Defaults to False. 36 nmea_timestamp_timeout : float 37 How many seconds a previously-extracted NMEA timestamp remains 38 valid for records that do not carry their own time field 39 (e.g. VTG, HDT). After this period the system clock is used 40 instead. Defaults to 1 s. 41 nmea_time_drift_threshold : float or None 42 If the absolute difference between the NMEA-derived time and 43 the system clock exceeds this value (in seconds), a warning is 44 logged. Set to None to disable the check. Defaults to 0.1 s. 45 quiet : bool 46 Inherited from Transform (passed via ``**kwargs``). When True, 47 suppresses all warnings from both the transform and the 48 underlying NMEATimestampExtractor (drift, staleness, and 49 fallback warnings). Defaults to False. 50 """ 51 def __init__(self, time_format=timestamp.TIME_FORMAT, 52 time_zone=timestamp.timezone.utc, sep=' ', 53 use_nmea_timestamp=False, 54 nmea_timestamp_timeout=1, 55 nmea_time_drift_threshold=0.1, **kwargs): 56 """Create a TimestampTransform.""" 57 super().__init__(**kwargs) # processes 'quiet' and type hints 58 59 self.time_format = time_format 60 self.time_zone = time_zone 61 self.sep = sep 62 self.use_nmea_timestamp = use_nmea_timestamp 63 self.nmea_extractor = None 64 if use_nmea_timestamp: 65 self.nmea_extractor = NMEATimestampExtractor( 66 timeout=nmea_timestamp_timeout, 67 quiet=self.quiet, 68 time_drift_threshold=nmea_time_drift_threshold) 69 70 ############################ 71 def transform(self, record: str) -> str: 72 """Prepend a timestamp""" 73 return self._add_timestamp(record) 74 75 ############################ 76 def _add_timestamp(self, record, ts=None): 77 """Do the work of transform(). Lists are handled by calling this 78 method on each element, passing along the timestamp to use.""" 79 # Why not just recurse on transform()? If we have a mirror_to writer, 80 # transform() is wrapped to mirror its result, and we only want to 81 # mirror the full list we hand downstream, not each element (#648). 82 83 # If they've not given us a timestamp, find one. 84 if ts is None: 85 if self.nmea_extractor is None: 86 # Things are simple if we're not trying to extract a timestamp from NMEA. 87 ts = timestamp.time_str(time_format=self.time_format, 88 time_zone=self.time_zone) 89 else: 90 # If we're going to try to extract ts from NMEA 91 ts = self.nmea_extractor.get_timestamp( 92 record, self.time_format, self.time_zone) 93 # Failed, so fall back to system time. 94 if ts is None and not self.quiet: 95 logging.warning( 96 'TimestampTransform: no NMEA timestamp for record, ' 97 'falling back to system time') 98 ts = ts or timestamp.time_str(time_format=self.time_format, 99 time_zone=self.time_zone) 100 101 # See if it's something we can process, and if not, try digesting 102 if not self.can_process_record(record): # inherited from BaseModule() 103 # Special case: if we can't process it, but it's a list, pass 104 # along the same initial timestamp so all elements in the list 105 # share the same timestamp. 106 if isinstance(record, list): 107 return [self._add_timestamp(r, ts) for r in record] 108 # If not str and not list, pass it along to digest_record() 109 # to let it try and/or complain. 110 else: 111 return self.digest_record(record) # inherited from BaseModule() 112 113 # If it is something we can process, put a timestamp on it. 114 return ts + self.sep + record
12class TimestampTransform(Transform): 13 """Prepend a timestamp to a text record. 14 15 By default the system clock is used. When ``use_nmea_timestamp`` is 16 enabled, the transform first attempts to extract the timestamp 17 embedded in NMEA 0183 sentences (GGA, RMC, ZDA, and many others) 18 and only falls back to the system clock when no NMEA time is 19 available. 20 21 Parameters 22 ---------- 23 time_format : str 24 strftime format string for the prepended timestamp. Defaults 25 to ``logger.utils.timestamp.TIME_FORMAT`` 26 (``'%Y-%m-%dT%H:%M:%S.%fZ'``). 27 time_zone : datetime.timezone 28 Timezone applied to the timestamp. Defaults to UTC. 29 sep : str 30 Separator inserted between the timestamp and the record text. 31 Defaults to a single space. 32 33 use_nmea_timestamp : bool 34 When True, attempt to parse the NMEA time field from the 35 incoming record before falling back to the system clock. 36 Defaults to False. 37 nmea_timestamp_timeout : float 38 How many seconds a previously-extracted NMEA timestamp remains 39 valid for records that do not carry their own time field 40 (e.g. VTG, HDT). After this period the system clock is used 41 instead. Defaults to 1 s. 42 nmea_time_drift_threshold : float or None 43 If the absolute difference between the NMEA-derived time and 44 the system clock exceeds this value (in seconds), a warning is 45 logged. Set to None to disable the check. Defaults to 0.1 s. 46 quiet : bool 47 Inherited from Transform (passed via ``**kwargs``). When True, 48 suppresses all warnings from both the transform and the 49 underlying NMEATimestampExtractor (drift, staleness, and 50 fallback warnings). Defaults to False. 51 """ 52 def __init__(self, time_format=timestamp.TIME_FORMAT, 53 time_zone=timestamp.timezone.utc, sep=' ', 54 use_nmea_timestamp=False, 55 nmea_timestamp_timeout=1, 56 nmea_time_drift_threshold=0.1, **kwargs): 57 """Create a TimestampTransform.""" 58 super().__init__(**kwargs) # processes 'quiet' and type hints 59 60 self.time_format = time_format 61 self.time_zone = time_zone 62 self.sep = sep 63 self.use_nmea_timestamp = use_nmea_timestamp 64 self.nmea_extractor = None 65 if use_nmea_timestamp: 66 self.nmea_extractor = NMEATimestampExtractor( 67 timeout=nmea_timestamp_timeout, 68 quiet=self.quiet, 69 time_drift_threshold=nmea_time_drift_threshold) 70 71 ############################ 72 def transform(self, record: str) -> str: 73 """Prepend a timestamp""" 74 return self._add_timestamp(record) 75 76 ############################ 77 def _add_timestamp(self, record, ts=None): 78 """Do the work of transform(). Lists are handled by calling this 79 method on each element, passing along the timestamp to use.""" 80 # Why not just recurse on transform()? If we have a mirror_to writer, 81 # transform() is wrapped to mirror its result, and we only want to 82 # mirror the full list we hand downstream, not each element (#648). 83 84 # If they've not given us a timestamp, find one. 85 if ts is None: 86 if self.nmea_extractor is None: 87 # Things are simple if we're not trying to extract a timestamp from NMEA. 88 ts = timestamp.time_str(time_format=self.time_format, 89 time_zone=self.time_zone) 90 else: 91 # If we're going to try to extract ts from NMEA 92 ts = self.nmea_extractor.get_timestamp( 93 record, self.time_format, self.time_zone) 94 # Failed, so fall back to system time. 95 if ts is None and not self.quiet: 96 logging.warning( 97 'TimestampTransform: no NMEA timestamp for record, ' 98 'falling back to system time') 99 ts = ts or timestamp.time_str(time_format=self.time_format, 100 time_zone=self.time_zone) 101 102 # See if it's something we can process, and if not, try digesting 103 if not self.can_process_record(record): # inherited from BaseModule() 104 # Special case: if we can't process it, but it's a list, pass 105 # along the same initial timestamp so all elements in the list 106 # share the same timestamp. 107 if isinstance(record, list): 108 return [self._add_timestamp(r, ts) for r in record] 109 # If not str and not list, pass it along to digest_record() 110 # to let it try and/or complain. 111 else: 112 return self.digest_record(record) # inherited from BaseModule() 113 114 # If it is something we can process, put a timestamp on it. 115 return ts + self.sep + record
Prepend a timestamp to a text record.
By default the system clock is used. When use_nmea_timestamp is
enabled, the transform first attempts to extract the timestamp
embedded in NMEA 0183 sentences (GGA, RMC, ZDA, and many others)
and only falls back to the system clock when no NMEA time is
available.
Parameters
time_format : str
strftime format string for the prepended timestamp. Defaults
to logger.utils.timestamp.TIME_FORMAT
('%Y-%m-%dT%H:%M:%S.%fZ').
time_zone : datetime.timezone
Timezone applied to the timestamp. Defaults to UTC.
sep : str
Separator inserted between the timestamp and the record text.
Defaults to a single space.
use_nmea_timestamp : bool
When True, attempt to parse the NMEA time field from the
incoming record before falling back to the system clock.
Defaults to False.
nmea_timestamp_timeout : float
How many seconds a previously-extracted NMEA timestamp remains
valid for records that do not carry their own time field
(e.g. VTG, HDT). After this period the system clock is used
instead. Defaults to 1 s.
nmea_time_drift_threshold : float or None
If the absolute difference between the NMEA-derived time and
the system clock exceeds this value (in seconds), a warning is
logged. Set to None to disable the check. Defaults to 0.1 s.
quiet : bool
Inherited from Transform (passed via **kwargs). When True,
suppresses all warnings from both the transform and the
underlying NMEATimestampExtractor (drift, staleness, and
fallback warnings). Defaults to False.
52 def __init__(self, time_format=timestamp.TIME_FORMAT, 53 time_zone=timestamp.timezone.utc, sep=' ', 54 use_nmea_timestamp=False, 55 nmea_timestamp_timeout=1, 56 nmea_time_drift_threshold=0.1, **kwargs): 57 """Create a TimestampTransform.""" 58 super().__init__(**kwargs) # processes 'quiet' and type hints 59 60 self.time_format = time_format 61 self.time_zone = time_zone 62 self.sep = sep 63 self.use_nmea_timestamp = use_nmea_timestamp 64 self.nmea_extractor = None 65 if use_nmea_timestamp: 66 self.nmea_extractor = NMEATimestampExtractor( 67 timeout=nmea_timestamp_timeout, 68 quiet=self.quiet, 69 time_drift_threshold=nmea_time_drift_threshold)
Create a TimestampTransform.