Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion remoteappmanager/admin_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def _webapi_resources(self):
return [admin.ContainerHandler,
admin.ApplicationHandler,
admin.UserHandler,
admin.AccountingHandler]
admin.AccountingHandler,
admin.StatsHandler]

def _web_handlers(self):
base_urlpath = self.command_line_config.base_urlpath
Expand Down
1 change: 1 addition & 0 deletions remoteappmanager/webapi/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .application import ApplicationHandler # noqa
from .user import UserHandler # noqa
from .accounting import AccountingHandler # noqa
from .stats import StatsHandler # noqa
38 changes: 38 additions & 0 deletions remoteappmanager/webapi/admin/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from tornado import gen

from tornadowebapi.traitlets import Unicode, Int
from tornadowebapi.singleton_resource import SingletonResource
from tornadowebapi.resource_handler import ResourceHandler

from remoteappmanager.webapi.decorators import authenticated


class Stats(SingletonResource):
#: The current realm of the application
realm = Unicode(allow_empty=False, strip=True)
#: Total number of users on the system
num_total_users = Int()
#: Total number of users currently running at least one container
num_active_users = Int()
#: Total number of available applications.
num_applications = Int()
#: Total number of running containers.
num_running_containers = Int()


class StatsHandler(ResourceHandler):
"""Provides statistics about the service."""
resource_class = Stats

@gen.coroutine
@authenticated
def retrieve(self, resource, **kwargs):
app = self.application
manager = self.application.container_manager
containers = (yield manager.find_containers())

resource.realm = app.file_config.docker_realm
resource.num_total_users = len(app.db.list_users())
resource.num_active_users = len(set([c.user for c in containers]))
resource.num_applications = len(app.db.list_applications())
resource.num_running_containers = len(containers)
42 changes: 42 additions & 0 deletions remoteappmanager/webapi/admin/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from tornadowebapi.authenticator import NullAuthenticator
from tornadowebapi.http import httpstatus

from remoteappmanager.tests.webapi_test_case import WebAPITestCase
from remoteappmanager.tests.mocking import dummy


class TestStats(WebAPITestCase):
def get_app(self):
app = dummy.create_admin_application()
app.hub.verify_token.return_value = {
'pending': None,
'name': app.settings['user'],
'admin': False,
'server': app.settings['base_urlpath']}
return app

def test_get(self):
code, data = self.get("/user/johndoe/api/v1/stats/")
self.assertEqual(code, httpstatus.OK)
self.assertEqual(data, {
'num_active_users': 1,
'num_applications': 2,
'num_running_containers': 1,
'num_total_users': 1,
'realm': 'remoteexec'})

def cookie_auth_token(self):
return "jupyter-hub-token-johndoe=johndoe"


class TestContainerNoUser(WebAPITestCase):
def get_app(self):
app = dummy.create_admin_application()
app.registry.authenticator = NullAuthenticator
return app

def test_get_no_user(self):
self.get("/user/johndoe/api/v1/stats/", httpstatus.NOT_FOUND)

def cookie_auth_token(self):
return "jupyter-hub-token-johndoe=johndoe"