Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET /api/status #337

Merged
merged 1 commit into from
Aug 3, 2023
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
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
Loading