Skip to content

Commit

Permalink
Add event source for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed Aug 28, 2024
1 parent a4d3781 commit e9481f6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/middlewared/middlewared/plugins/apps/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import time

from middlewared.event import EventSource
from middlewared.schema import Dict, Int, Str
from middlewared.service import CallError
from middlewared.validators import Range

from .ix_apps.docker.stats import list_resources_stats_by_project


class AppStatsEventSource(EventSource):

"""
Retrieve statistics of apps.
"""
ACCEPTS = Dict(
Int('interval', default=2, validators=[Range(min_=2)]),
)
RETURNS = Dict(
Str('data', required=True),
Str('timestamp', required=True, null=True)
)

def run_sync(self):
if not self.middleware.call_sync('docker.state.validate', False):
raise CallError('Apps are not available')

interval = self.arg['interval']
while not self._cancel_sync.is_set():
self.send_event('ADDED', fields=list_resources_stats_by_project())
time.sleep(interval)


def setup(middleware):
middleware.register_event_source('app.stats', AppStatsEventSource, roles=['APPS_READ'])

0 comments on commit e9481f6

Please sign in to comment.