handlers.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Tornado handlers for api specifications."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from itertools import chain
  5. import json
  6. from tornado import gen, web
  7. from ...base.handlers import IPythonHandler, APIHandler
  8. from notebook._tz import utcfromtimestamp, isoformat
  9. import os
  10. class APISpecHandler(web.StaticFileHandler, IPythonHandler):
  11. def initialize(self):
  12. web.StaticFileHandler.initialize(self, path=os.path.dirname(__file__))
  13. @web.authenticated
  14. def get(self):
  15. self.log.warning("Serving api spec (experimental, incomplete)")
  16. return web.StaticFileHandler.get(self, 'api.yaml')
  17. def get_content_type(self):
  18. return 'text/x-yaml'
  19. class APIStatusHandler(APIHandler):
  20. _track_activity = False
  21. @web.authenticated
  22. @gen.coroutine
  23. def get(self):
  24. # if started was missing, use unix epoch
  25. started = self.settings.get('started', utcfromtimestamp(0))
  26. started = isoformat(started)
  27. kernels = yield gen.maybe_future(self.kernel_manager.list_kernels())
  28. total_connections = sum(k['connections'] for k in kernels)
  29. last_activity = isoformat(self.application.last_activity())
  30. model = {
  31. 'started': started,
  32. 'last_activity': last_activity,
  33. 'kernels': len(kernels),
  34. 'connections': total_connections,
  35. }
  36. self.finish(json.dumps(model, sort_keys=True))
  37. default_handlers = [
  38. (r"/api/spec.yaml", APISpecHandler),
  39. (r"/api/status", APIStatusHandler),
  40. ]