Skip to content

Commit a446fe7

Browse files
authored
feat: set entity timestamp (at source) if not provided (#36)
Signed-off-by: Michal Fiedorowicz <[email protected]>
1 parent 0729278 commit a446fe7

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @leoparente @mfiedorowicz
1+
* @leoparente @ltucker @mfiedorowicz

netboxlabs/diode/sdk/ingester.py

+5
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,11 @@ def __new__(
710710
virtual_machine, VirtualMachinePb, name=virtual_machine
711711
)
712712

713+
if timestamp is None:
714+
ts = _timestamp_pb2.Timestamp()
715+
ts.GetCurrentTime()
716+
timestamp = ts
717+
713718
return EntityPb(
714719
site=site,
715720
platform=platform,

tests/test_ingester.py

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# Copyright 2024 NetBox Labs Inc
33
"""NetBox Labs - Tests."""
4+
from google.protobuf import timestamp_pb2
45

56
# ruff: noqa: I001
67
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
@@ -658,6 +659,38 @@ def test_site_instantiation_with_all_fields():
658659
assert isinstance(tag, TagPb)
659660

660661

662+
def test_entity_instantiation_with_no_timestamp_provided():
663+
"""Check Entity instantiation with no timestamp provided."""
664+
entity = Entity(
665+
site="Site1",
666+
)
667+
assert isinstance(entity, EntityPb)
668+
assert isinstance(entity.site, SitePb)
669+
assert entity.site.name == "Site1"
670+
assert entity.timestamp is not None
671+
assert entity.timestamp.seconds > 0
672+
assert entity.timestamp.nanos > 0
673+
674+
675+
def test_entity_instantiation_with_timestamp_provided():
676+
"""Check Entity instantiation with timestamp provided."""
677+
current_ts = timestamp_pb2.Timestamp()
678+
current_ts.GetCurrentTime()
679+
680+
entity = Entity(
681+
site="Site1",
682+
timestamp=current_ts,
683+
)
684+
assert isinstance(entity, EntityPb)
685+
assert isinstance(entity.site, SitePb)
686+
assert entity.site.name == "Site1"
687+
assert entity.timestamp is not None
688+
assert entity.timestamp.seconds > 0
689+
assert entity.timestamp.nanos > 0
690+
assert entity.timestamp.seconds == current_ts.seconds
691+
assert entity.timestamp.nanos == current_ts.nanos
692+
693+
661694
def test_entity_instantiation_with_site():
662695
"""Check Entity instantiation with site."""
663696
entity = Entity(

0 commit comments

Comments
 (0)