Skip to content

Commit

Permalink
Add overwatcher switch
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Mar 25, 2024
1 parent 4b6ddaa commit f0a9ffb
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 2 deletions.
160 changes: 159 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ python-multipart = "^0.0.6"
astropy = "^6.0.0"
astroplan = "^0.9.1"
polars = "^0.20.16"
redis = {version = "^5.0.3", extras = ["hiredis"]}

[tool.poetry.group.dev.dependencies]
ipython = ">=8.0.0"
Expand Down
3 changes: 2 additions & 1 deletion src/lvmapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from fastapi import FastAPI

from lvmapi import auth
from lvmapi.routers import ephemeris, slack, spectrographs, telescopes
from lvmapi.routers import ephemeris, overwatcher, slack, spectrographs, telescopes


app = FastAPI()
Expand All @@ -20,6 +20,7 @@
app.include_router(spectrographs.router)
app.include_router(slack.router)
app.include_router(ephemeris.router)
app.include_router(overwatcher.router)


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

from __future__ import annotations

from fastapi import APIRouter

from lvmapi.tools import get_redis_connection


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


@router.get("/")
async def get_overwatcher():
"""Not implemented."""

return {}


@router.get("/enabled", description="Is the overwatcher enabled?")
def get_overwatcher_enabled() -> bool:
"""Returns whether the overwatcher is enabled."""

redis = get_redis_connection()

enabled = redis.get("overwatcher:enabled")
assert enabled is None or isinstance(enabled, str)

if enabled is None:
return False

return bool(int(enabled))


@router.put("/enabled/{enabled}", description="Enable or disable the overwatcher")
def put_overwatcher_enabled(enabled: bool) -> bool:
"""Enables or disables the overwatcher."""

redis = get_redis_connection()
redis.set("overwatcher:enabled", int(enabled))

return enabled
2 changes: 2 additions & 0 deletions src/lvmapi/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

from .influxdb import *
from .rabbitmq import *
from .redis import *
from .schedule import *
from .spectrograph import *
20 changes: 20 additions & 0 deletions src/lvmapi/tools/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2024-03-25
# @Filename: redis.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)

from __future__ import annotations

import redis


__all__ = ["get_redis_connection"]


def get_redis_connection() -> redis.Redis:
"""Returns a connection to the Redis server."""

return redis.Redis(host="10.8.38.26", port=6379, db=0, decode_responses=True)

0 comments on commit f0a9ffb

Please sign in to comment.