File size: 1,633 Bytes
d1ceb73 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from typing import Dict, NamedTuple, Optional, Union
class Timestamp:
"""A nanosecond-resolution timestamp."""
def __init__(self, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
nsec = -nsec
self.sec: int = int(sec)
self.nsec: int = int(nsec)
def __str__(self) -> str:
return f"{self.sec}.{self.nsec:09d}"
def __repr__(self) -> str:
return f"Timestamp({self.sec}, {self.nsec})"
def __float__(self) -> float:
return float(self.sec) + float(self.nsec) / 1e9
def __eq__(self, other: object) -> bool:
return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec
def __ne__(self, other: object) -> bool:
return not self == other
def __gt__(self, other: "Timestamp") -> bool:
return self.sec > other.sec or self.nsec > other.nsec
def __lt__(self, other: "Timestamp") -> bool:
return self.sec < other.sec or self.nsec < other.nsec
# Timestamp and exemplar are optional.
# Value can be an int or a float.
# Timestamp can be a float containing a unixtime in seconds,
# a Timestamp object, or None.
# Exemplar can be an Exemplar object, or None.
class Exemplar(NamedTuple):
labels: Dict[str, str]
value: float
timestamp: Optional[Union[float, Timestamp]] = None
class Sample(NamedTuple):
name: str
labels: Dict[str, str]
value: float
timestamp: Optional[Union[float, Timestamp]] = None
exemplar: Optional[Exemplar] = None
|