Skip to content
Snippets Groups Projects
Verified Commit 83f93270 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Populate the visit status value in save code now request

Related to T3266
parent 1042f73c
No related branches found
No related tags found
1 merge request!541Populate the visit status value in save code now request
......@@ -69,11 +69,17 @@ def api_save_origin(request, visit_type, origin_url):
:>json string save_task_status: the status of the origin saving task,
either **not created**, **not yet scheduled**, **scheduled**,
**succeeded** or **failed**
:>json string visit_date: the date (in iso format) of the visit if a visit
occurred, null otherwise.
:>json string visit_status: the status of the visit, either **full**,
**partial**, **not_found** or **failed** if a visit occurred, null
otherwise.
:statuscode 200: no error
:statuscode 400: an invalid visit type or origin url has been provided
:statuscode 403: the provided origin url is blacklisted
:statuscode 404: no save requests have been found for a given origin
"""
if request.method == "POST":
......
......@@ -184,7 +184,7 @@ def _get_visit_info_for_save_request(
if i != len(visit_dates):
visit_date = visit_dates[i]
visit_status = origin_visits[i]["status"]
if origin_visits[i]["status"] not in ("full", "partial", "not_found"):
if visit_status not in ("full", "partial", "not_found"):
visit_date = None
except Exception as exc:
sentry_sdk.capture_exception(exc)
......@@ -206,7 +206,8 @@ def _check_visit_update_status(
"""
visit_date, visit_status = _get_visit_info_for_save_request(save_request)
save_request.visit_date = visit_date
if visit_date and visit_status is not None:
save_request.visit_status = visit_status
if visit_date and visit_status in ("full", "partial"):
# visit has been performed, mark the saving task as succeeded
save_task_status = SAVE_TASK_SUCCEEDED
elif visit_status in ("created", "ongoing"):
......@@ -247,8 +248,8 @@ def _save_request_dict(
if task:
save_task_status = _save_task_status[task["status"]]
if task_run:
save_task_status = _save_task_run_status[task_run["status"]]
# Consider request from which a visit date has already been found
# as succeeded to avoid retrieving it again
if save_task_status == SAVE_TASK_SCHEDULED and visit_date:
......@@ -259,6 +260,7 @@ def _save_request_dict(
):
visit_date, visit_status = _get_visit_info_for_save_request(save_request)
save_request.visit_date = visit_date
save_request.visit_status = visit_status
if visit_status in ("failed", "not_found"):
save_task_status = SAVE_TASK_FAILED
must_save = True
......
# Copyright (C) 2018-2019 The Software Heritage developers
# Copyright (C) 2018-2021 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information
......@@ -18,6 +18,8 @@ from swh.web.common.models import (
SAVE_TASK_NOT_YET_SCHEDULED,
SAVE_TASK_SCHEDULED,
SAVE_TASK_SUCCEEDED,
VISIT_STATUS_FAILED,
VISIT_STATUS_FULL,
SaveOriginRequest,
SaveUnauthorizedOrigin,
)
......@@ -130,6 +132,7 @@ def check_save_request_status(
scheduler_task_status="next_run_not_scheduled",
scheduler_task_run_status=None,
visit_date=None,
visit_status=None,
):
mock_scheduler = mocker.patch("swh.web.common.origin_save.scheduler")
mock_scheduler.get_tasks.return_value = [
......@@ -166,12 +169,13 @@ def check_save_request_status(
mock_visit_date = mocker.patch(
("swh.web.common.origin_save." "_get_visit_info_for_save_request")
)
mock_visit_date.return_value = (visit_date, None)
mock_visit_date.return_value = (visit_date, visit_status)
response = check_api_get_responses(api_client, url, status_code=200)
save_request_data = response.data[0]
assert save_request_data["save_request_status"] == expected_request_status
assert save_request_data["save_task_status"] == expected_task_status
assert save_request_data["visit_status"] == visit_status
# Check that save task status is still available when
# the scheduler task has been archived
......@@ -179,6 +183,7 @@ def check_save_request_status(
response = check_api_get_responses(api_client, url, status_code=200)
save_request_data = response.data[0]
assert save_request_data["save_task_status"] == expected_task_status
assert save_request_data["visit_status"] == visit_status
def test_save_request_rejected(api_client, mocker):
......@@ -251,6 +256,7 @@ def test_save_request_succeed(api_client, mocker):
scheduler_task_status="completed",
scheduler_task_run_status="eventful",
visit_date=visit_date,
visit_status=VISIT_STATUS_FULL,
)
......@@ -280,6 +286,7 @@ def test_save_request_failed(api_client, mocker):
expected_task_status=SAVE_TASK_FAILED,
scheduler_task_status="disabled",
scheduler_task_run_status="failed",
visit_status=VISIT_STATUS_FAILED,
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment