Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/origin_visits.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import math | import math | ||||
from typing import List, Optional | from typing import List, Optional | ||||
from django.core.cache import cache | from django.core.cache import cache | ||||
from swh.web.common import service | |||||
from swh.web.common.exc import NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.common.typing import OriginInfo, OriginVisitInfo | from swh.web.common.typing import OriginInfo, OriginVisitInfo | ||||
from swh.web.common.utils import parse_iso8601_date_to_utc | from swh.web.common.utils import parse_iso8601_date_to_utc | ||||
def get_origin_visits(origin_info: OriginInfo) -> List[OriginVisitInfo]: | def get_origin_visits(origin_info: OriginInfo) -> List[OriginVisitInfo]: | ||||
"""Function that returns the list of visits for a swh origin. | """Function that returns the list of visits for a swh origin. | ||||
That list is put in cache in order to speedup the navigation | That list is put in cache in order to speedup the navigation | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | Args: | ||||
snapshot_id: a snapshot identifier | snapshot_id: a snapshot identifier | ||||
Returns: | Returns: | ||||
A dict containing the visit info. | A dict containing the visit info. | ||||
Raises: | Raises: | ||||
swh.web.common.exc.NotFoundExc: if no visit can be found | swh.web.common.exc.NotFoundExc: if no visit can be found | ||||
""" | """ | ||||
if not visit_ts and not visit_id and not snapshot_id: | |||||
from swh.web.common import service | |||||
# returns the latest full visit with a valid snapshot | # returns the latest full visit with a valid snapshot | ||||
visit = service.lookup_origin_visit_latest( | visit = service.lookup_origin_visit_latest( | ||||
origin_info["url"], allowed_statuses=["full"], require_snapshot=True | origin_info["url"], allowed_statuses=["full"], require_snapshot=True | ||||
) | ) | ||||
if not visit: | if not visit: | ||||
# or the latest partial visit with a valid snapshot otherwise | # or the latest partial visit with a valid snapshot otherwise | ||||
visit = service.lookup_origin_visit_latest( | visit = service.lookup_origin_visit_latest( | ||||
origin_info["url"], allowed_statuses=["partial"], require_snapshot=True | origin_info["url"], allowed_statuses=["partial"], require_snapshot=True | ||||
) | ) | ||||
if not visit_ts and not visit_id and not snapshot_id: | |||||
if visit: | if visit: | ||||
return visit | return visit | ||||
else: | else: | ||||
raise NotFoundExc( | raise NotFoundExc( | ||||
f"No valid visit for origin with url {origin_info['url']} found!" | f"No valid visit for origin with url {origin_info['url']} found!" | ||||
) | ) | ||||
# no need to fetch all visits list and search in it if the latest | |||||
# visit matches some criteria | |||||
if visit and (visit["snapshot"] == snapshot_id or visit["visit"] == visit_id): | |||||
return visit | |||||
visits = get_origin_visits(origin_info) | visits = get_origin_visits(origin_info) | ||||
if not visits: | if not visits: | ||||
raise NotFoundExc( | raise NotFoundExc( | ||||
f"No visits associated to origin with url {origin_info['url']}!" | f"No visits associated to origin with url {origin_info['url']}!" | ||||
) | ) | ||||
if snapshot_id: | if snapshot_id: | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |