Skip to content

Commit

Permalink
Add GET /api/status
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Aug 3, 2023
1 parent b3c3589 commit 7489e26
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
18 changes: 17 additions & 1 deletion jupyverse_api/jupyverse_api/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import logging
from datetime import datetime, timezone
from collections import defaultdict
from typing import Dict, List

from fastapi import FastAPI
from fastapi import FastAPI, Request

from ..exceptions import RedirectException, _redirect_exception_handler

Expand All @@ -27,6 +28,21 @@ def __init__(self, app: FastAPI, mount_path: str | None = None):
self._app = subapi
app.add_exception_handler(RedirectException, _redirect_exception_handler)
self._router_paths = defaultdict(list)
self._started_time = datetime.now(timezone.utc)
self._last_activity = self._started_time

@app.middleware("http")
async def get_last_activity(request: Request, call_next):
self._last_activity = datetime.now(timezone.utc)
return await call_next(request)

@property
def started_time(self) -> datetime:
return self._started_time

@property
def last_activity(self) -> datetime:
return self._last_activity

@property
def _paths(self):
Expand Down
13 changes: 13 additions & 0 deletions jupyverse_api/jupyverse_api/kernels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def __init__(self, app: App, auth: Auth):

router = APIRouter()

@router.get("/api/status")
async def get_status(
user: User = Depends(auth.current_user(permissions={"status": ["read"]})),
):
return await self.get_status(user)

@router.get("/api/kernelspecs")
async def get_kernelspecs(
user: User = Depends(auth.current_user(permissions={"kernelspecs": ["read"]})),
Expand Down Expand Up @@ -119,6 +125,13 @@ async def kernel_channels(
async def watch_connection_files(self, path: Path) -> None:
...

@abstractmethod
async def get_status(
self,
user: User,
):
...

@abstractmethod
async def get_kernelspecs(
self,
Expand Down
17 changes: 17 additions & 0 deletions plugins/kernels/fps_kernels/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ def __init__(
self.kernel_id_to_connection_file: Dict[str, str] = {}
self.sessions: Dict[str, Session] = {}
self.kernels = kernels
self._app = app

async def get_status(
self,
user: User,
):
started = self._app.started_time.isoformat().replace("+00:00", "Z")
last_activity = self._app.last_activity.isoformat().replace("+00:00", "Z")
connections = sum(
[kernel["server"].connections for kernel in kernels.values() if "server" in kernel]
)
return {
"started": started,
"last_activity": last_activity,
"kernels": len(kernels),
"connections": connections,
}

async def get_kernelspecs(
self,
Expand Down

0 comments on commit 7489e26

Please sign in to comment.