Skip to content

Commit 4032be6

Browse files
committed
Added admin stats retrieval
1 parent 988edf3 commit 4032be6

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

simvue/api/objects/stats.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,34 @@
99
import http
1010
import typing
1111

12+
from pydantic import BaseModel
13+
import pydantic
14+
1215
from .base import SimvueObject
1316
from simvue.api.request import get as sv_get, get_json_from_response
1417
from simvue.api.url import URL
1518

1619
__all__ = ["Stats"]
1720

1821

22+
class UserStatistics(BaseModel):
23+
"""Interface to per-user statistics output."""
24+
25+
runs: pydantic.NonNegativeInt
26+
alerts: pydantic.NonNegativeInt
27+
tags: pydantic.NonNegativeInt
28+
files: pydantic.NonNegativeInt
29+
volume: pydantic.NonNegativeInt
30+
events: pydantic.NonNegativeInt
31+
metrics: pydantic.NonNegativeInt
32+
33+
1934
class Stats(SimvueObject):
2035
"""Class for accessing Server statistics."""
2136

37+
_single: bool = True
38+
_tenant: str | None = None
39+
2240
def __init__(self) -> None:
2341
"""Initialise a statistics query object."""
2442
self.runs = RunStatistics(self)
@@ -122,15 +140,23 @@ def _get_visibility(self) -> dict[str, bool | list[str]]:
122140
return {}
123141

124142
def to_dict(self) -> dict[str, typing.Any]:
125-
"""Dictionary form of statistics.
143+
"""Dictionary form of current user statistics.
126144
127145
Returns
128146
-------
129-
doct[str, Any]
147+
dict[str, Any]
130148
statistics data as dictionary
131149
"""
132150
return {"runs": self._get_run_stats()}
133151

152+
def admin_stats(self, *, tenant: str | None = None) -> dict[str, dict[str, int]]:
153+
return {
154+
name: UserStatistics(**entry)
155+
for name, entry in self._get(
156+
single=False, **({"tenant": tenant} if tenant else {})
157+
).items()
158+
}
159+
134160
def commit(self) -> None:
135161
"""Does nothing, no data sendable to server."""
136162
pass

tests/unit/test_stats.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def test_stats() -> None:
1313
assert isinstance(_statistics.runs.data, int)
1414
assert _statistics.to_dict()
1515
assert _statistics.whoami()
16+
assert _statistics.admin_stats()
1617

1718
with pytest.raises(AttributeError):
1819
Stats.new()

0 commit comments

Comments
 (0)