Skip to content

Commit

Permalink
Better profiling for /api/v1/cutout (#7)
Browse files Browse the repository at this point in the history
* Encapsulate profiler with context manager

* Isolate cutout download for profiling

* Typo

* Profiling
  • Loading branch information
JulienPeloton authored Dec 5, 2024
1 parent e680e5e commit b8de7fb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
6 changes: 5 additions & 1 deletion apps/routes/cutouts/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
"""Call format_and_send_cutout"""

from apps.routes.cutouts.utils import format_and_send_cutout
from flask import Flask

app = Flask("Profile cutouts")

payload = {
"objectId": "ZTF21abfmbix",
"kind": "All",
"output-format": "array",
}

format_and_send_cutout(payload)
with app.app_context():
format_and_send_cutout(payload)
47 changes: 35 additions & 12 deletions apps/routes/cutouts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,10 @@ def format_and_send_cutout(payload: dict):
as_attachment=True,
download_name=filename,
)

# Extract cutouts
user_config = extract_configuration("config.yml")
if output_format == "FITS":
json_payload.update({"return_type": "FITS"})
r0 = requests.post(
"{}/api/v1/cutouts".format(user_config["CUTOUTAPIURL"]), json=json_payload
)
cutout = io.BytesIO(r0.content)
elif output_format in ["PNG", "array"]:
json_payload.update({"return_type": "array"})
r0 = requests.post(
"{}/api/v1/cutouts".format(user_config["CUTOUTAPIURL"]), json=json_payload
)
cutout = json.loads(r0.content)
cutout = request_cutout(json_payload, output_format, user_config["CUTOUTAPIURL"])

# send the FITS file
if output_format == "FITS":
Expand Down Expand Up @@ -185,3 +175,36 @@ def format_and_send_cutout(payload: dict):
return send_file(
datab, mimetype="image/png", as_attachment=True, download_name=filename
)


@profile
def request_cutout(json_payload, output_format, cutout_api_url):
"""Request a cutout from the Fink cutout API
Parameters
----------
json_payload: dict
Dictionary with arguments for /api/v1/cutouts
output_format: str
Among: FITS, PNG, array
cutout_api_url: str
URL of the Fink cutout API service.
Returns
-------
cutout: Any
Output type depends on the `output_format` argument
"""
if output_format == "FITS":
json_payload.update({"return_type": "FITS"})
r0 = requests.post(
"{}/api/v1/cutouts".format(cutout_api_url), json=json_payload
)
cutout = io.BytesIO(r0.content)
elif output_format in ["PNG", "array"]:
json_payload.update({"return_type": "array"})
r0 = requests.post(
"{}/api/v1/cutouts".format(cutout_api_url), json=json_payload
)
cutout = json.loads(r0.content)
return cutout

0 comments on commit b8de7fb

Please sign in to comment.