Skip to content

Commit

Permalink
refactor(routes): support legacy results
Browse files Browse the repository at this point in the history
Raster and vector results generated by previous version are stored as
single AsyncResult in the result store. These need to be retrieved
differently as it is now down with GroupResults.
  • Loading branch information
matthiasschaub committed Oct 17, 2024
1 parent c9da307 commit 718f2f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
31 changes: 19 additions & 12 deletions sketch_map_tool/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def digitize_results_post(lang="en") -> Response:
"""Upload files to create geodata results"""
# "consent" is a checkbox and value is only send if it is checked
consent: bool = "consent" in request.form.keys()

# No files uploaded
if "file" not in request.files:
return redirect(url_for("digitize", lang=lang))
Expand Down Expand Up @@ -185,7 +184,7 @@ def digitize_results_post(lang="en") -> Response:
bboxes_[uuid] = bbox
layers_[uuid] = layer

tasks_vector = [] # noqa: E741
tasks_vector = []
tasks_raster = []
for file_id, file_name, uuid in zip(file_ids, file_names, uuids):
tasks_vector.append(
Expand Down Expand Up @@ -231,7 +230,6 @@ def digitize_results_post(lang="en") -> Response:
"vector-results": str(async_result_vector.id),
}
db_client_flask.set_async_result_ids(uuid, map_)

return redirect(url_for("digitize_results_get", lang=lang, uuid=uuid))


Expand All @@ -254,10 +252,10 @@ def status(uuid: str, type_: REQUEST_TYPES, lang="en") -> Response:

id_ = db_client_flask.get_async_result_id(uuid, type_)

if type_ in ("sketch-map", "quality-report"):
# due to legacy support it is not possible to check only `type_`
async_result = celery_app.GroupResult.restore(id_)
if async_result is None:
async_result = celery_app.AsyncResult(id_)
else:
async_result = celery_app.GroupResult.restore(id_)

href = ""
info = ""
Expand Down Expand Up @@ -316,12 +314,13 @@ def download(uuid: str, type_: REQUEST_TYPES, lang="en") -> Response:

id_ = db_client_flask.get_async_result_id(uuid, type_)

if type_ in ("sketch-map", "quality-report"):
# due to legacy support it is not possible to check only `type_`
async_result = celery_app.GroupResult.restore(id_)
if async_result is None:
async_result = celery_app.AsyncResult(id_)
if not async_result.ready() or async_result.failed():
abort(500)
else:
async_result = celery_app.GroupResult.restore(id_)
if not async_result.ready() or all([r.failed() for r in async_result.results]):
abort(500)
match type_:
Expand All @@ -336,13 +335,21 @@ def download(uuid: str, type_: REQUEST_TYPES, lang="en") -> Response:
case "raster-results":
mimetype = "application/zip"
download_name = type_ + ".zip"
file: BytesIO = zip_(async_result.get(propagate=False))
if isinstance(async_result, GroupResult):
file: BytesIO = zip_(async_result.get(propagate=False))
else:
# support legacy results
file: BytesIO = async_result.get()
case "vector-results":
mimetype = "application/geo+json"
download_name = type_ + ".geojson"
result = async_result.get(propagate=False)
raw = geojson.dumps(merge(result))
file: BytesIO = BytesIO(raw.encode("utf-8"))
if isinstance(async_result, GroupResult):
result: list = async_result.get(propagate=False)
raw = geojson.dumps(merge(result))
file: BytesIO = BytesIO(raw.encode("utf-8"))
else:
# support legacy results
file = async_result.get()
return send_file(file, mimetype, download_name=download_name)


Expand Down
9 changes: 4 additions & 5 deletions tests/integration/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def test_api_status_uuid_digitize(uuid_digitize, type_, flask_client):
assert resp.json["href"] == f"/api/download/{uuid_digitize}/{type_}"


# TODO: Make test case work in a run of the whole test suite
@pytest.mark.skip("Only works in a single test run")
@pytest.mark.skip("Long execution time.")
@vcr_app.use_cassette
def test_api_status_uuid_digitize_info(sketch_map_marked, flask_client):
"""Test if custom task status information is return by /status."""
Expand All @@ -163,8 +162,7 @@ def test_api_status_uuid_digitize_info(sketch_map_marked, flask_client):
assert resp.json["info"] == {"current": 0, "total": 1}


# TODO: Make test case work in a run of the whole test suite
@pytest.mark.skip("Only works in a single test run")
@pytest.mark.skip("Only works in a single test run. Long execution time.")
@vcr_app.use_cassette
def test_api_status_uuid_digitize_info_multiple(sketch_map_marked, flask_client):
"""Test if custom task status information is return by /status."""
Expand All @@ -191,8 +189,9 @@ def test_api_status_uuid_digitize_info_multiple(sketch_map_marked, flask_client)
resp = flask_client.get(f"/api/status/{uuid}/vector-results")
if resp.json["status"] == "SUCCESS":
break
assert resp.json["status"] in ["PENDING", "PROGRESS"]
assert resp.json["status"] in ["PENDING", "STARTED"]
assert resp.json["info"]["current"] in [0, 1, 2]
assert resp.json["info"]["total"] == 2
assert resp.status_code == 200
assert resp.json["status"] == "SUCCESS"

Expand Down

0 comments on commit 718f2f3

Please sign in to comment.