Skip to content

Commit

Permalink
Return last_exposure_no in get_spectrogaph_status
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Jul 15, 2024
1 parent 7cc06f6 commit 39910a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
30 changes: 20 additions & 10 deletions src/lvmapi/routers/spectrographs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import polars
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel
from pydantic import BaseModel, Field

from lvmapi.tools import (
get_spectrograph_mechanics,
Expand All @@ -32,6 +32,17 @@ class SplitDataFrameToDict(BaseModel):
data: list[tuple]


class SpecStatusResponse(BaseModel):
status: dict[Spectrographs, SpecStatus] = Field(
...,
description="The status of each spectrograph",
)
last_exposure_no: int = Field(
...,
description="The last exposure number",
)


router = APIRouter(prefix="/spectrographs", tags=["spectrographs"])


Expand All @@ -42,17 +53,16 @@ async def get_cryostats() -> list[str]:
return list(get_args(Spectrographs))


@router.get("/status", summary="Returns the spectrograph status")
async def get_status(
spec: Spectrographs | None = None,
) -> dict[Spectrographs, SpecStatus]:
@router.get(
"/status",
summary="Returns the spectrograph status",
response_model=SpecStatusResponse,
)
async def get_status(spec: Spectrographs | None = None) -> SpecStatusResponse:
"""Returns the spectrograph status."""

status = await get_spectrogaph_status()
if spec:
return {spec: status[spec]}

return status
spec_status = await get_spectrogaph_status()
return SpecStatusResponse(status=spec_status[0], last_exposure_no=spec_status[1])


@router.get(
Expand Down
8 changes: 6 additions & 2 deletions src/lvmapi/tools/spectrograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ async def read_thermistors(
return df


async def get_spectrogaph_status():
async def get_spectrogaph_status() -> tuple[dict[Spectrographs, SpecStatus], int]:
"""Returns the status of the spectrograph (integrating, reading, etc.)"""

spec_names = get_args(Spectrographs)
Expand All @@ -288,6 +288,7 @@ async def get_spectrogaph_status():
)

result: dict[Spectrographs, SpecStatus] = {}
last_exposure_no: int = -1

for task in group.results():
if task.status.did_fail:
Expand All @@ -308,8 +309,11 @@ async def get_spectrogaph_status():
else:
result[controller] = "unknown"

if "last_exposure_no" in status and status[last_exposure_no] > last_exposure_no:
last_exposure_no = status["last_exposure_no"]

for spec in spec_names:
if spec not in result:
result[spec] = "unknown"

return result
return result, last_exposure_no

0 comments on commit 39910a4

Please sign in to comment.