Skip to content

Commit

Permalink
Add max history length
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBurchLog committed Jul 11, 2024
1 parent 066dc47 commit 1102e42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion brewtils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,14 @@ def __init__(self, heartbeat=None, history=None):
self.heartbeat = heartbeat
self.history = history or []

def set_status_heartbeat(self, status):
def set_status_heartbeat(self, status, max_history=None):

self.heartbeat = datetime.utcnow()
self.history.append(StatusHistory(status=copy.deepcopy(status), heartbeat=self.heartbeat))

if max_history and len(self.history) > max_history:
self.history = self.history[(max_history * -1):]

def __str__(self):
return self.heartbeat

Expand Down
22 changes: 22 additions & 0 deletions test/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
RequestTemplate,
LegacyRole,
Subscriber,
StatusInfo,
Topic,
)
from pytest_lazyfixture import lazy_fixture
Expand Down Expand Up @@ -713,3 +714,24 @@ def test_repr(self, topic1, subscriber1):
topic1.name,
[subscriber1],
)


class TestStatusInfo:

def test_max_history(self):
status_info = StatusInfo()

max_length = 5

for _ in range(10):
status_info.set_status_heartbeat("RUNNING", max_history=max_length)

assert len(status_info.history) == max_length

def test_history(self):
status_info = StatusInfo()

for _ in range(10):
status_info.set_status_heartbeat("RUNNING")

assert len(status_info.history) == 10

0 comments on commit 1102e42

Please sign in to comment.