Skip to content

Commit

Permalink
Update code to use lvmopstools 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Nov 27, 2024
1 parent 6015a91 commit 174a681
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 835 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"sdsstools>=1.8.1",
"fastapi[standard]>=0.112.0",
"lvmgort>=1.1.2",
"lvmopstools>=0.3.9",
"lvmopstools[influxdb,kubernetes]>=0.4.0",
"gunicorn>=22.0.0",
"uvicorn[standard]>=0.24.0",
"sdss-clu>=2.2.1",
Expand Down
7 changes: 5 additions & 2 deletions src/lvmapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import taskiq_fastapi
from fastapi import FastAPI, HTTPException, Request

from lvmopstools.kubernetes import Kubernetes

from lvmapi import auth, config
from lvmapi.broker import broker, broker_shutdown, broker_startup
from lvmapi.routers import (
Expand All @@ -33,7 +35,6 @@
transparency,
weather,
)
from lvmapi.tools.kubernetes import Kubernetes


logger = logging.getLogger("uvicorn.error")
Expand Down Expand Up @@ -77,7 +78,9 @@ async def get_id_route(request: Request):
taskiq_fastapi.init(broker, "lvmapi.app:app")

# Add kubernetes API instance to state.
app.state.kubernetes = Kubernetes()
app.state.kubernetes = Kubernetes(
deployments_path=config["kubernetes.deployments_path"]
)

# Fake states for testing.
app.state.use_fake_states = os.environ.get("LVM_USE_FAKE_STATES", "0") != "0"
Expand Down
3 changes: 1 addition & 2 deletions src/lvmapi/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
influxdb:
host: lvm-webapp.lco.cl
port: 9999
url: http://lvm-webapp.lco.cl:9999
org: LVM

slack:
Expand Down
12 changes: 8 additions & 4 deletions src/lvmapi/routers/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
from __future__ import annotations

import asyncio
import time
import warnings

import polars
from fastapi import APIRouter, Request
from pydantic import BaseModel

from lvmopstools.weather import get_weather_data, is_weather_data_safe

from lvmapi.tools.alerts import enclosure_alerts, spec_temperature_alerts
from lvmapi.tools.weather import get_weather_data, is_measurament_safe


class AlertsSummary(BaseModel):
Expand Down Expand Up @@ -44,10 +46,12 @@ class AlertsSummary(BaseModel):
async def summary(request: Request) -> AlertsSummary:
"""Summary of alerts."""

now = time.time()

tasks: list[asyncio.Task] = []
tasks.append(asyncio.create_task(spec_temperature_alerts()))
tasks.append(asyncio.create_task(enclosure_alerts()))
tasks.append(asyncio.create_task(get_weather_data(start_time=3600)))
tasks.append(asyncio.create_task(get_weather_data(start_time=now - 3600)))

results = await asyncio.gather(*tasks, return_exceptions=True)

Expand All @@ -70,14 +74,14 @@ async def summary(request: Request) -> AlertsSummary:
dew_point_alert = None

if not isinstance(weather_data, BaseException) and weather_data.height > 0:
wind_alert = not is_measurament_safe(
wind_alert = not is_weather_data_safe(
weather_data,
"wind_speed_avg",
threshold=35,
reopen_value=30,
)

humidity_alert = not is_measurament_safe(
humidity_alert = not is_weather_data_safe(
weather_data,
"relative_humidity",
threshold=80,
Expand Down
2 changes: 1 addition & 1 deletion src/lvmapi/routers/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


if TYPE_CHECKING:
from lvmapi.tools.kubernetes import Kubernetes
from lvmopstools.kubernetes import Kubernetes


class DeploymentInfoResponse(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion src/lvmapi/routers/spectrographs.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CryostatsResponse(BaseModel):
Field(description="Pressures in Torr as reported by the ion pumps"),
]
thermistors: Annotated[
dict[str, bool | None],
dict[str, bool],
Field(description="Whether the thermistors are sensing LN2"),
]
purging: Annotated[
Expand Down
7 changes: 5 additions & 2 deletions src/lvmapi/routers/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

from __future__ import annotations

from time import time

from typing import Annotated

import polars
from fastapi import APIRouter, HTTPException, Query

from lvmapi.tools.weather import get_weather_data
from lvmopstools.weather import get_weather_data


router = APIRouter(prefix="/weather", tags=["weather"])
Expand Down Expand Up @@ -57,7 +59,8 @@ async def route_get_weather(
elif any([start_time, end_time]) and not all([start_time, end_time]):
raise HTTPException(400, "start_time and end_time must be both defined.")
else:
df = await get_weather_data(station=station, start_time=delta_time)
now = time.time()
df = await get_weather_data(station=station, start_time=now - delta_time)

if last and len(df) > 0:
df = df.tail(1)
Expand Down
55 changes: 0 additions & 55 deletions src/lvmapi/tools/influxdb.py

This file was deleted.

185 changes: 0 additions & 185 deletions src/lvmapi/tools/kubernetes.py

This file was deleted.

Loading

0 comments on commit 174a681

Please sign in to comment.