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