Skip to content

Commit

Permalink
Use get request for logs instead of websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
UpstreamData committed Nov 25, 2024
1 parent 0ff256a commit 920a94b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 80 deletions.
3 changes: 1 addition & 2 deletions goosebit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from starlette.exceptions import HTTPException as StarletteHTTPException
from tortoise.exceptions import ValidationError

from goosebit import api, db, realtime, ui, updater
from goosebit import api, db, ui, updater
from goosebit.api.telemetry import metrics
from goosebit.auth import get_user_from_request, login_user, redirect_if_authenticated
from goosebit.settings import config
Expand Down Expand Up @@ -57,7 +57,6 @@ async def lifespan(_: FastAPI):
app.include_router(updater.router)
app.include_router(ui.router)
app.include_router(api.router)
app.include_router(realtime.router)
app.mount("/static", static, name="static")
Instrumentor.instrument_app(app)

Expand Down
1 change: 1 addition & 0 deletions goosebit/api/v1/devices/device/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class DeviceLogResponse(BaseModel):
log: str | None
progress: int | None


class DeviceResponse(DeviceSchema):
Expand Down
2 changes: 1 addition & 1 deletion goosebit/api/v1/devices/device/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ async def device_get(_: Request, device: Device = Depends(get_device)) -> Device
async def device_logs(_: Request, device: Device = Depends(get_device)) -> DeviceLogResponse:
if device is None:
raise HTTPException(404)
return DeviceLogResponse(log=device.last_log)
return DeviceLogResponse(log=device.last_log, progress=device.progress)
1 change: 0 additions & 1 deletion goosebit/realtime/__init__.py

This file was deleted.

41 changes: 0 additions & 41 deletions goosebit/realtime/logs.py

This file was deleted.

13 changes: 0 additions & 13 deletions goosebit/realtime/routes.py

This file was deleted.

1 change: 1 addition & 0 deletions goosebit/ui/bff/devices/device/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .routes import router # noqa : F401
16 changes: 16 additions & 0 deletions goosebit/ui/bff/devices/device/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from fastapi import APIRouter, Security

from goosebit.api.v1.devices.device import routes
from goosebit.auth import validate_user_permissions

router = APIRouter(prefix="/{dev_id}")

router.add_api_route(
"/log",
routes.device_logs,
methods=["GET"],
dependencies=[Security(validate_user_permissions, scopes=["device.read"])],
name="bff_device_logs",
)
2 changes: 2 additions & 0 deletions goosebit/ui/bff/devices/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from goosebit.ui.bff.common.util import parse_datatables_query

from ..common.responses import DTColumnDescription, DTColumns
from . import device
from .requests import DevicesPatchRequest
from .responses import BFFDeviceResponse

router = APIRouter(prefix="/devices")
router.include_router(device.router)


@router.get(
Expand Down
29 changes: 7 additions & 22 deletions goosebit/ui/static/js/logs.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
document.addEventListener("DOMContentLoaded", () => {
const logs_ws = create_ws(`/realtime/logs/${device}`);
document.addEventListener("DOMContentLoaded", async () => {
const res = await get_request(`/ui/bff/devices/${device}/log`);

logs_ws.addEventListener("message", (event) => {
const res = JSON.parse(event.data);
const logElem = document.getElementById("device-log");
logElem.textContent = res.log;

const logElem = document.getElementById("device-log");
if (res.clear) {
logElem.textContent = "";
}
logElem.textContent += res.log;

const progressElem = document.getElementById("install-progress");
progressElem.style.width = `${res.progress}%`;
progressElem.innerHTML = `${res.progress}%`;
});
const progressElem = document.getElementById("install-progress");
progressElem.style.width = `${res.progress}%`;
progressElem.innerHTML = `${res.progress}%`;
});

function create_ws(s) {
const l = window.location;
const protocol = l.protocol === "https:" ? "wss://" : "ws://";
const port = l.port !== "80" || l.port !== "443" ? l.port : "";
const url = `${protocol}${l.hostname}:${port}${s}`;
return new WebSocket(url);
}

0 comments on commit 920a94b

Please sign in to comment.