Skip to content

Commit

Permalink
Merge pull request #334 from alercebroker/probability
Browse files Browse the repository at this point in the history
Probability API
  • Loading branch information
Demurest authored Oct 21, 2024
2 parents 8d072e9 + 19e93f5 commit b110ff0
Show file tree
Hide file tree
Showing 19 changed files with 2,763 additions and 45 deletions.
24 changes: 21 additions & 3 deletions lightcurve/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,34 @@ RUN poetry install --no-root --without=test
FROM node:22-alpine as tailwindcss_lightcurve
COPY ./lightcurve/ /lightcurve/
WORKDIR /lightcurve
RUN npx tailwindcss -i /lightcurve/src/lightcurve_api/templates/main.css -o /compiled/main.css
RUN \
wget -nc https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.14/tailwindcss-linux-x64 -O tailwindcss && \
chmod +x tailwindcss && \
./tailwindcss -i /lightcurve/src/lightcurve_api/templates/main.css -o /compiled/main.css

FROM node:22-alpine as tailwindcss_object
COPY ./lightcurve/ /lightcurve/
WORKDIR /lightcurve
RUN npx tailwindcss -i /lightcurve/src/object_api/templates/object.css -o /compiled/object.css
RUN \
wget -nc https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.13/tailwindcss-linux-x64 -O tailwindcss && \
chmod +x tailwindcss && \
./tailwindcss -i /lightcurve/src/object_api/templates/object.css -o /compiled/object.css

FROM node:22-alpine as tailwindcss_magstats
COPY ./lightcurve/ /lightcurve/
WORKDIR /lightcurve
RUN npx tailwindcss -i /lightcurve/src/magstats_api/templates/magstats.css -o /compiled/magstats.css
RUN \
wget -nc https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.13/tailwindcss-linux-x64 -O tailwindcss && \
chmod +x tailwindcss && \
./tailwindcss -i /lightcurve/src/magstats_api/templates/magstats.css -o /compiled/magstats.css

FROM node:22-alpine as tailwindcss_probability
COPY ./lightcurve/ /lightcurve/
WORKDIR /lightcurve
RUN \
wget -nc https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.13/tailwindcss-linux-x64 -O tailwindcss && \
chmod +x tailwindcss && \
./tailwindcss -i /lightcurve/src/probability_api/templates/probability.css -o /compiled/probability.css

FROM python:3.11-slim as production
RUN pip install poetry
Expand All @@ -45,6 +62,7 @@ COPY ./lightcurve/src /app/src
COPY --from=tailwindcss_lightcurve /compiled/main.css /app/src/api/static
COPY --from=tailwindcss_magstats /compiled/magstats.css /app/src/magstats_api/static
COPY --from=tailwindcss_object /compiled/object.css /app/src/object_api/static
COPY --from=tailwindcss_probability /compiled/probability.css /app/src/probability_api/static
# COPY --from=tailwindcss_crossmatch /compiled/crossmatch.css /app/src/crossmatch_api/static
RUN poetry install --only-root
CMD ["bash", "scripts/entrypoint.sh"]
22 changes: 17 additions & 5 deletions lightcurve/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ services:
environment:
SERVICE: lightcurve_api
API_URL: http://localhost:8001

object:
build:
context: ../
dockerfile: lightcurve/Dockerfile
ports:
- 8002:8000
env_file:
- variables.env
environment:
SERVICE: object_api
API_URL: http://localhost:8002

magstats:
build:
Expand All @@ -40,15 +52,15 @@ services:
environment:
SERVICE: magstats_api
API_URL: http://localhost:8003
object:

probability:
build:
context: ../
dockerfile: lightcurve/Dockerfile
ports:
- 8002:8000
- 8004:8000
env_file:
- variables.env
environment:
SERVICE: object_api
API_URL: http://localhost:8002
SERVICE: probability_api
API_URL: http://localhost:8004
2 changes: 2 additions & 0 deletions lightcurve/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ packages = [
{ include = "lightcurve_api", from = "src" },
{ include = "magstats_api", from = "src" },
{ include = "object_api", from = "src" },
{ include = "probability_api", from = "src" },
{ include = "database", from = "src" },
]

Expand Down Expand Up @@ -71,4 +72,5 @@ dev = "scripts.run_dev:run"
lightcurve = "scripts.run_dev:run_lightcurve"
magstats = "scripts.run_dev:run_magstats"
object = "scripts.run_dev:run_object"
probability = "scripts.run_dev:run_probability"
tunnel = "scripts.sshproxy:run_tunnel"
3 changes: 3 additions & 0 deletions lightcurve/scripts/run_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def run_object():
port = int(os.getenv("PORT", default=8000))
asyncio.run(run_service("object_api", port))

def run_probability():
port = int(os.getenv("PORT", default=8000))
asyncio.run(run_service("probability_api", port))

async def run_services(services, port):
tasks = []
Expand Down
12 changes: 12 additions & 0 deletions lightcurve/scripts/run_probability_dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

import uvicorn


def run():
port = os.getenv("PORT", default=8000)
uvicorn.run("probability_api.api:app", port=int(port), reload=True, reload_dirs=[".", "../libs"])


if __name__ == "__main__":
run()
21 changes: 6 additions & 15 deletions lightcurve/src/magstats_api/routes/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@
directory="src/object_api/templates", autoescape=True, auto_reload=True
)

@router.get("/")
def root():
return "this is the magstats module"

@router.get("/mag/{oid}", response_class=HTMLResponse)
async def object_mag_app(request: Request, oid: str):
try:
mag_stats = get_mag_stats(
oid, session_factory=request.app.state.psql_session
)
except ObjectNotFound:
raise HTTPException(status_code=404, detail="Object not found")

mag_stats_dict = {}
for i, mag_stat in enumerate(mag_stats):
mag_stats_dict[f"band_{i+1}"] = (
mag_stat.__dict__
) ## Es necesario cambiar el nombre de las keys por los fid y trabajar con el conversor que esta en probability en alerts-api

return {"request": request, "stat_r": mag_stats_dict}
@router.get("/healthcheck")
def healthcheck():
return "OK"
28 changes: 6 additions & 22 deletions lightcurve/src/object_api/routes/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@
directory="src/object_api/templates", autoescape=True, auto_reload=True
)

@router.get("/")
def root():
return "this is the object module"

@router.get("/object/{oid}", response_class=HTMLResponse)
async def object_info_app(request: Request, oid: str):
link = "https://acortar.link/ba5kba"

try:
object_data = get_object(
oid, session_factory=request.app.state.psql_session
)
except ObjectNotFound:
raise HTTPException(status_code=404, detail="Object ID not found")

return {
"request": request,
"object": object_data.oid,
"corrected": object_data.corrected,
"stellar": object_data.stellar,
"detections": object_data.ndet,
"discoveryDateMJD": object_data.firstmjd,
"lastDetectionMJD": object_data.lastmjd,
"ra": object_data.meanra,
"dec": object_data.meandec,
"link": link,
}
@router.get("/healthcheck")
def healthcheck():
return "OK"
Empty file.
35 changes: 35 additions & 0 deletions lightcurve/src/probability_api/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from prometheus_fastapi_instrumentator import Instrumentator

from .routes import htmx, rest
from database.mongo import connect as connect_mongo
from database.sql import connect as connect_sql, session_wrapper

app = FastAPI(openapi_url="/v2/object/openapi.json")
app.state.mongo_db = None
psql_engine = connect_sql()
app.state.psql_session = session_wrapper(psql_engine)
instrumentator = Instrumentator().instrument(app).expose(app)

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


app.include_router(rest.router)
app.include_router(prefix="/htmx", router=htmx.router)

app.mount("/static", StaticFiles(directory="src/probability_api/static"), name="static")


@app.get("/openapi.json")
def custom_swagger_route():
return app.openapi()

47 changes: 47 additions & 0 deletions lightcurve/src/probability_api/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import re


def update_config_dict(config_dict):
config_dict["FILTERS_MAP"] = {
"filter_atlas_detections": filter_atlas_detection_non_detection,
"filter_atlas_non_detections": filter_atlas_detection_non_detection,
"filter_atlas_lightcurve": filter_atlas_lightcurve,
"filter_atlas_forced_photometry": filter_atlas_detection_non_detection,
}


def get_filters_map():
return {
"filter_atlas_detections": filter_atlas_detection_non_detection,
"filter_atlas_non_detections": filter_atlas_detection_non_detection,
"filter_atlas_lightcurve": filter_atlas_lightcurve,
"filter_atlas_forced_photometry": filter_atlas_detection_non_detection,
}


def filter_atlas_detection_non_detection(lc_object):
pattern = re.compile("atlas*", re.IGNORECASE)
if pattern.match(lc_object["tid"]):
return False
return True


def filter_atlas_lightcurve(lc_object):
non_filtered_detections = []
non_filtered_non_detections = []
non_filtered_forced_photometry = []

for detection in lc_object["detections"]:
if filter_atlas_detection_non_detection(detection):
non_filtered_detections.append(detection)
for non_detecton in lc_object["non_detections"]:
if filter_atlas_detection_non_detection(non_detecton):
non_filtered_non_detections.append(non_detecton)
for forced_photometry in lc_object["forced_photometry"]:
if filter_atlas_detection_non_detection(forced_photometry):
non_filtered_forced_photometry.append(forced_photometry)

lc_object["detections"] = non_filtered_detections
lc_object["non_detections"] = non_filtered_non_detections
lc_object["forced_photometry"] = non_filtered_forced_photometry
return True
37 changes: 37 additions & 0 deletions lightcurve/src/probability_api/result_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import traceback
import logging

from fastapi import HTTPException

from core.exceptions import (
AtlasNonDetectionError,
DatabaseError,
ObjectNotFound,
SurveyIdError,
)


def handle_success(result):
return result


def _handle_client_error(err: BaseException, code=400):
raise HTTPException(status_code=code, detail=str(err))


def _handle_server_error(err: BaseException):
if err.__traceback__:
traceback.print_exception(err)
logging.error(err)
raise HTTPException(status_code=500, detail=str(err))


def handle_error(err: BaseException):
if isinstance(err, DatabaseError):
_handle_server_error(err)
if isinstance(err, SurveyIdError):
_handle_client_error(err)
if isinstance(err, AtlasNonDetectionError):
_handle_client_error(err)
if isinstance(err, ObjectNotFound):
_handle_client_error(err, code=404)
Empty file.
Loading

0 comments on commit b110ff0

Please sign in to comment.