Skip to content

Commit

Permalink
Merge pull request #495 from beer-garden/Pub_Sub_Counters
Browse files Browse the repository at this point in the history
Add counters for Topic and Subscriber
  • Loading branch information
TheBurchLog authored Aug 9, 2024
2 parents 554d725 + faed7e9 commit 209e1fb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Brewtils Changelog
------
TBD

- Expanded Topic/Subscriber models to include counters
- Formalized Status Info model and added helper features to track the history of the status changes.
- Added support models for tracking primary replication
- New Models for User, UserToken, Role, and AliasUserMap
Expand Down
13 changes: 10 additions & 3 deletions brewtils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,7 @@ def __init__(
instance=None,
command=None,
subscriber_type=None,
consumer_count=0,
):
self.garden = garden
self.namespace = namespace
Expand All @@ -1837,14 +1838,15 @@ def __init__(
self.instance = instance
self.command = command
self.subscriber_type = subscriber_type or "DYNAMIC"
self.consumer_count = consumer_count

def __str__(self):
return "%s" % self.__dict__

def __repr__(self):
return (
"<Subscriber: garden=%s, namespace=%s, system=%s, version=%s, instance=%s, "
"command=%s, subscriber_type=%s>"
"command=%s, subscriber_type=%s, consumer_count=%s>"
% (
self.garden,
self.namespace,
Expand All @@ -1853,6 +1855,7 @@ def __repr__(self):
self.instance,
self.command,
self.subscriber_type,
self.consumer_count,
)
)

Expand All @@ -1875,18 +1878,22 @@ def __eq__(self, other):
class Topic(BaseModel):
schema = "TopicSchema"

def __init__(self, id=None, name=None, subscribers=None): # noqa # shadows built-in
def __init__(
self, id=None, name=None, subscribers=None, publisher_count=0
): # noqa # shadows built-in
self.id = id
self.name = name
self.subscribers = subscribers or []
self.publisher_count = publisher_count

def __str__(self):
return "%s: %s" % (self.name, [str(s) for s in self.subscribers])

def __repr__(self):
return "<Topic: name=%s, subscribers=%s>" % (
return "<Topic: name=%s, subscribers=%s, publisher_count=%s>" % (
self.name,
self.subscribers,
self.publisher_count,
)


Expand Down
2 changes: 2 additions & 0 deletions brewtils/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,14 @@ class SubscriberSchema(BaseSchema):
instance = fields.Str(allow_none=True)
command = fields.Str(allow_none=True)
subscriber_type = fields.Str(allow_none=True)
consumer_count = fields.Int(allow_none=True)


class TopicSchema(BaseSchema):
id = fields.Str(allow_none=True)
name = fields.Str(allow_none=True)
subscribers = fields.List(fields.Nested(SubscriberSchema, allow_none=True))
publisher_count = fields.Int(allow_none=True)


class ReplicationSchema(BaseSchema):
Expand Down
8 changes: 7 additions & 1 deletion brewtils/test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ def subscriber_dict():
"instance": "inst",
"command": "run",
"subscriber_type": "DYNAMIC",
"consumer_count": 10,
}


Expand All @@ -1062,7 +1063,12 @@ def bg_subscriber(subscriber_dict):
@pytest.fixture
def topic_dict(subscriber_dict):
"""Topic as dict"""
return {"id": "5d174df1", "name": "foo", "subscribers": [subscriber_dict]}
return {
"id": "5d174df1",
"name": "foo",
"subscribers": [subscriber_dict],
"publisher_count": 10,
}


@pytest.fixture
Expand Down
7 changes: 5 additions & 2 deletions test/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def subscriber1():

@pytest.fixture
def topic1(subscriber1):
return Topic(name="foo.*", subscribers=[subscriber1])
return Topic(name="foo.*", subscribers=[subscriber1], publisher_count=10)


class TestSubscriber(object):
Expand All @@ -715,9 +715,12 @@ def test_str(self, topic1, subscriber1):
assert str(topic1) == "%s: %s" % (topic1.name, [str(subscriber1)])

def test_repr(self, topic1, subscriber1):
assert repr(topic1) == "<Topic: name=%s, subscribers=%s>" % (
assert repr(
topic1
) == "<Topic: name=%s, subscribers=%s, publisher_count=%s>" % (
topic1.name,
[subscriber1],
topic1.publisher_count,
)


Expand Down

0 comments on commit 209e1fb

Please sign in to comment.