Skip to content

Commit

Permalink
Add transparency endpoing (#14)
Browse files Browse the repository at this point in the history
* Stub for transparency route

* Complete code

* Update changelog
  • Loading branch information
albireox authored Nov 12, 2024
1 parent 2ae960d commit 76b4b04
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Next version

### 🚀 New

* [#14](https://github.com/sdss/lvmapi/pull/14) Add `/transparency` endpoint to retrieve transparency data.

### 🔧 Fixed

* Correctly calculate time lost in night metrics while the night is ongoing.
Expand Down
2 changes: 2 additions & 0 deletions src/lvmapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
spectrographs,
tasks,
telescopes,
transparency,
weather,
)
from lvmapi.tools.kubernetes import Kubernetes
Expand All @@ -51,6 +52,7 @@
app.include_router(actors.router)
app.include_router(logs.router)
app.include_router(notifications.router)
app.include_router(transparency.router)


@app.get("/id")
Expand Down
77 changes: 77 additions & 0 deletions src/lvmapi/routers/transparency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2024-11-11
# @Filename: transparency.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

from __future__ import annotations

from datetime import datetime
from time import time

from typing import Annotated

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

from lvmapi.tools.transparency import get_transparency


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


class TransparencyResponse(BaseModel):
"""Response model for transparency measurements."""

start_time: Annotated[
float,
Field(description="Start time of the measurements as a UNIX timestamp"),
]
end_time: Annotated[
float,
Field(description="End time of the measurements as a UNIX timestamp"),
]
data: Annotated[
list[TransparencyData],
Field(description="Transparency data"),
]


class TransparencyData(BaseModel):
"""Model for transparency data."""

time: Annotated[datetime, Field(description="Time of the measurement")]
telescope: Annotated[str, Field(description="Telescope name")]
zero_point: Annotated[float, Field(description="Zero-point value")]


@router.get("/", summary="Transparency measurements")
async def route_get_transparency(
start_time: Annotated[
float | None,
Query(description="Start time as a UNIX timestamp"),
] = None,
end_time: Annotated[
float | None,
Query(description="End time as a UNIX timestamp"),
] = None,
) -> TransparencyResponse:
"""Returns transparency measurements.
Without any parameters, returns the transparency measurements for the last hour.
"""

if start_time is None or end_time is None:
end_time = time()
start_time = end_time - 3600

data = await get_transparency(start_time, end_time)

return TransparencyResponse(
start_time=start_time,
end_time=end_time,
data=[TransparencyData(**row) for row in data.to_dicts()],
)
36 changes: 36 additions & 0 deletions src/lvmapi/tools/transparency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2024-11-11
# @Filename: transparency.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

from __future__ import annotations

import polars

from lvmapi.tools.influxdb import query_influxdb


async def get_transparency(start_time: float, end_time: float):
"""Returns transparency measurements."""

query = rf"""
from(bucket: "actors")
|> range(start: {int(start_time)}, stop: {int(end_time)})
|> filter(fn: (r) => (r["_measurement"] =~ /lvm\.[a-z]+\.guider/) and
(r["_field"] == "measured_pointing.zero_point"))
|> yield(name: "mean")
"""

data = await query_influxdb(query)

# Clean up the dataframe.
data = data.select(
time=polars.col._time,
telescope=polars.col._measurement.str.extract(r"lvm\.([a-z]+)\.guider"),
zero_point=polars.col._value,
)

return data

0 comments on commit 76b4b04

Please sign in to comment.