Skip to content

Commit 860232e

Browse files
committed
Added offline metrics test
1 parent 5cc27c0 commit 860232e

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

simvue/api/objects/metrics.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(
3737
def new(
3838
cls, *, run: str, offline: bool = False, metrics: list[MetricSet], **kwargs
3939
):
40-
"""Create a new Events entry on the Simvue server"""
40+
"""Create a new Metrics entry on the Simvue server"""
4141
return Metrics(
4242
run=run,
4343
metrics=[metric.model_dump() for metric in metrics],
@@ -51,27 +51,23 @@ def get(
5151
cls,
5252
metrics: list[str],
5353
xaxis: typing.Literal["timestamp", "step", "time"],
54+
runs: list[str],
5455
*,
5556
count: pydantic.PositiveInt | None = None,
5657
offset: pydantic.PositiveInt | None = None,
5758
**kwargs,
5859
) -> typing.Generator[MetricSet, None, None]:
5960
_class_instance = cls(_read_only=True, _local=True)
60-
if (
61-
_data := cls._get_all_objects(
62-
count,
63-
offset,
64-
metrics=json.dumps(metrics),
65-
xaxis=xaxis,
66-
**kwargs,
67-
).get("data")
68-
) is None:
69-
raise RuntimeError(
70-
f"Expected key 'data' for retrieval of {_class_instance.__class__.__name__.lower()}s"
71-
)
72-
73-
for _entry in _data:
74-
yield MetricSet(**_entry)
61+
_data = cls._get_all_objects(
62+
count,
63+
offset,
64+
metrics=json.dumps(metrics),
65+
runs=json.dumps(runs),
66+
xaxis=xaxis,
67+
**kwargs,
68+
)
69+
# TODO: Temp fix, just return the dictionary. Not sure what format we really want this in...
70+
return _data
7571

7672
@pydantic.validate_call
7773
def span(self, run_ids: list[str]) -> dict[str, int | float]:

tests/unit/test_metrics.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import uuid
77

88
from simvue.api.objects import Metrics, Folder, Run
9+
from simvue.models import DATETIME_FORMAT
10+
from simvue.sender import sender
911

1012
@pytest.mark.api
1113
@pytest.mark.online
@@ -38,9 +40,60 @@ def test_metrics_creation_online() -> None:
3840
)
3941
assert _metrics.to_dict()
4042
_metrics.commit()
41-
assert _metrics.get(metrics=["x", "y", "z"], xaxis="step")
43+
assert _metrics.get(metrics=["x", "y", "z"], xaxis="step", runs=[_run.id])
4244
assert _metrics.span(run_ids=[_run.id])
4345
assert _metrics.names(run_ids=[_run.id])
4446
_run.delete()
4547
_folder.delete(recursive=True, delete_runs=True, runs_only=False)
4648

49+
@pytest.mark.api
50+
@pytest.mark.offline
51+
def test_metrics_creation_offline() -> None:
52+
_uuid: str = f"{uuid.uuid4()}".split("-")[0]
53+
_folder_name = f"/simvue_unit_testing/{_uuid}"
54+
_folder = Folder.new(path=_folder_name, offline=True)
55+
_run = Run.new(name="hello", folder=_folder_name, offline=True)
56+
_folder.commit()
57+
_run.commit()
58+
59+
_values = {
60+
"x": 1,
61+
"y": 2.0,
62+
"z": True
63+
}
64+
_time: int = 1
65+
_step: int = 1
66+
_timestamp = datetime.datetime.now().strftime(DATETIME_FORMAT)
67+
_metrics = Metrics.new(
68+
run=_run.id,
69+
metrics=[
70+
{
71+
"timestamp": _timestamp,
72+
"time": _time,
73+
"step": _step,
74+
"values": _values,
75+
}
76+
],
77+
offline=True
78+
)
79+
_metrics.commit()
80+
with _metrics._local_staging_file.open() as in_f:
81+
_local_data = json.load(in_f)
82+
83+
assert _local_data.get("run") == _run.id
84+
assert _local_data.get("metrics")[0].get("values") == _values
85+
assert _local_data.get("metrics")[0].get("timestamp") == _timestamp
86+
assert _local_data.get("metrics")[0].get("step") == _step
87+
assert _local_data.get("metrics")[0].get("time") == _time
88+
89+
_id_mapping = sender(_metrics._local_staging_file.parents[1], 1, 10, ["folders", "runs", "metrics"])
90+
time.sleep(1)
91+
92+
# Get online version of metrics
93+
_online_metrics = Metrics(_id_mapping.get(_metrics.id))
94+
_data = _online_metrics.get(metrics=["x", "y", "z"], runs=[_id_mapping.get(_run.id)], xaxis="step")
95+
assert sorted(_online_metrics.names(run_ids=[_id_mapping.get(_run.id)])) == sorted(_values.keys())
96+
assert _data.get(_id_mapping.get(_run.id)).get('y')[0].get('value') == 2.0
97+
assert _data.get(_id_mapping.get(_run.id)).get('y')[0].get('step') == 1
98+
_run.delete()
99+
_folder.delete(recursive=True, delete_runs=True, runs_only=False)

0 commit comments

Comments
 (0)