From 80f7d34dc50b66a3c9fb67bb4a899ef6aa3b2625 Mon Sep 17 00:00:00 2001 From: JulienPeloton Date: Thu, 5 Dec 2024 15:43:45 +0100 Subject: [PATCH 1/4] Encapsulate profiler with context manager --- apps/routes/cutouts/profiling.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/routes/cutouts/profiling.py b/apps/routes/cutouts/profiling.py index cee0a71..689b02f 100644 --- a/apps/routes/cutouts/profiling.py +++ b/apps/routes/cutouts/profiling.py @@ -15,6 +15,9 @@ """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", @@ -22,4 +25,5 @@ "output-format": "array", } -format_and_send_cutout(payload) +with app.app_context(): + format_and_send_cutout(payload) From 78d9383985208ca1157102336b0fe4db8ceecf45 Mon Sep 17 00:00:00 2001 From: JulienPeloton Date: Thu, 5 Dec 2024 15:50:45 +0100 Subject: [PATCH 2/4] Isolate cutout download for profiling --- apps/routes/cutouts/utils.py | 46 ++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/apps/routes/cutouts/utils.py b/apps/routes/cutouts/utils.py index b2349d0..048f9e4 100644 --- a/apps/routes/cutouts/utils.py +++ b/apps/routes/cutouts/utils.py @@ -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": @@ -185,3 +175,35 @@ def format_and_send_cutout(payload: dict): return send_file( datab, mimetype="image/png", as_attachment=True, download_name=filename ) + +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(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) + return cutout + From 512c97c0f4808122c040c7e3c0656ccf4577054a Mon Sep 17 00:00:00 2001 From: JulienPeloton Date: Thu, 5 Dec 2024 15:51:50 +0100 Subject: [PATCH 3/4] Typo --- apps/routes/cutouts/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/routes/cutouts/utils.py b/apps/routes/cutouts/utils.py index 048f9e4..6b5a772 100644 --- a/apps/routes/cutouts/utils.py +++ b/apps/routes/cutouts/utils.py @@ -196,13 +196,13 @@ def request_cutout(json_payload, output_format, cutout_api_url): if output_format == "FITS": json_payload.update({"return_type": "FITS"}) r0 = requests.post( - "{}/api/v1/cutouts".format(user_config["CUTOUTAPIURL"]), json=json_payload + "{}/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(user_config["CUTOUTAPIURL"]), json=json_payload + "{}/api/v1/cutouts".format(cutout_api_url), json=json_payload ) cutout = json.loads(r0.content) return cutout From 14ff2ca45fe550c0e43acbd94db48759fbdeb096 Mon Sep 17 00:00:00 2001 From: JulienPeloton Date: Thu, 5 Dec 2024 15:52:26 +0100 Subject: [PATCH 4/4] Profiling --- apps/routes/cutouts/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/routes/cutouts/utils.py b/apps/routes/cutouts/utils.py index 6b5a772..b2e13a2 100644 --- a/apps/routes/cutouts/utils.py +++ b/apps/routes/cutouts/utils.py @@ -176,6 +176,8 @@ def format_and_send_cutout(payload: dict): 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 @@ -206,4 +208,3 @@ def request_cutout(json_payload, output_format, cutout_api_url): ) cutout = json.loads(r0.content) return cutout -