Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/origin_visits.py
# Copyright (C) 2018-2022 The Software Heritage developers | # Copyright (C) 2018-2022 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 | ||||
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 archive | from swh.web.common import archive | ||||
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, lookup_similar_urls: bool = True | |||||
) -> 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 | ||||
in the swh web browse ui. | in the swh web browse ui. | ||||
The returned visits are sorted according to their date in | The returned visits are sorted according to their date in | ||||
ascending order. | ascending order. | ||||
Args: | Args: | ||||
origin_info: dict describing the origin to fetch visits from | origin_info: dict describing the origin to fetch visits from | ||||
lookup_similar_urls: if :const:`True`, lookup origin with and | |||||
without trailing slash in its URL | |||||
Returns: | Returns: | ||||
A list of dict describing the origin visits | A list of dict describing the origin visits | ||||
Raises: | Raises: | ||||
swh.web.common.exc.NotFoundExc: if the origin is not found | swh.web.common.exc.NotFoundExc: if the origin is not found | ||||
""" | """ | ||||
from swh.web.common import archive | from swh.web.common import archive | ||||
if "url" in origin_info: | origin_url = archive.lookup_origin( | ||||
origin_url = origin_info["url"] | origin_info, lookup_similar_urls=lookup_similar_urls | ||||
else: | )["url"] | ||||
origin_url = archive.lookup_origin(origin_info)["url"] | |||||
cache_entry_id = "origin_visits_%s" % origin_url | cache_entry_id = "origin_visits_%s" % origin_url | ||||
cache_entry = cache.get(cache_entry_id) | cache_entry = cache.get(cache_entry_id) | ||||
last_visit = 0 | last_visit = 0 | ||||
origin_visits = [] | origin_visits = [] | ||||
new_visits = [] | new_visits = [] | ||||
per_page = archive.MAX_LIMIT | per_page = archive.MAX_LIMIT | ||||
▲ Show 20 Lines • Show All 128 Lines • Show Last 20 Lines |