Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/algos/origin.py
Show All 38 Lines | |||||
def origin_get_latest_visit_status( | def origin_get_latest_visit_status( | ||||
storage, | storage, | ||||
origin_url: str, | origin_url: str, | ||||
type: Optional[str] = None, | type: Optional[str] = None, | ||||
allowed_statuses: Optional[Iterable[str]] = None, | allowed_statuses: Optional[Iterable[str]] = None, | ||||
require_snapshot: bool = False, | require_snapshot: bool = False, | ||||
limit: Optional[int] = 100, | |||||
) -> Optional[Tuple[OriginVisit, OriginVisitStatus]]: | ) -> Optional[Tuple[OriginVisit, OriginVisitStatus]]: | ||||
"""Get the latest origin visit (and status) of an origin. Optionally, a combination of | """Get the latest origin visit (and status) of an origin. Optionally, a combination of | ||||
criteria can be provided, origin type, allowed statuses or if a visit has a | criteria can be provided, origin type, allowed statuses or if a visit has a | ||||
snapshot. | snapshot. | ||||
If no visit matching the criteria is found, returns None. Otherwise, returns a tuple | If no visit matching the criteria is found, returns None. Otherwise, returns a tuple | ||||
of origin visit, origin visit status. | of origin visit, origin visit status. | ||||
Show All 9 Lines | Args: | ||||
require_snapshot: If True, only a visit with a snapshot | require_snapshot: If True, only a visit with a snapshot | ||||
will be returned. | will be returned. | ||||
Returns: | Returns: | ||||
a tuple of (visit, visit_status) model object if the visit *and* the visit | a tuple of (visit, visit_status) model object if the visit *and* the visit | ||||
status exist (and match the search criteria), None otherwise. | status exist (and match the search criteria), None otherwise. | ||||
""" | """ | ||||
# visits order are from older visit to most recent. | visits = list(storage.origin_visit_get(origin_url, order="desc", limit=limit)) | ||||
ardumont: oh, no longer need to `list` it... ;) | |||||
Done Inline Actionswrong, need it for the no result case. ardumont: wrong, need it for the no result case. | |||||
visits = list(storage.origin_visit_get(origin_url)) | |||||
visits.reverse() | |||||
if not visits: | if not visits: | ||||
return None | return None | ||||
visit_status: Optional[OriginVisitStatus] = None | visit_status: Optional[OriginVisitStatus] = None | ||||
visit: Dict[str, Any] | visit: Dict[str, Any] | ||||
# Iterate over the visits in reverse order, so the most recent match is found first | # Iterate over the visits in reverse order, so the most recent match is found first | ||||
for visit in visits: | for visit in visits: | ||||
if type is not None and visit["type"] != type: | if type is not None and visit["type"] != type: | ||||
continue | continue | ||||
visit_status = storage.origin_visit_status_get_latest( | visit_status = storage.origin_visit_status_get_latest( | ||||
origin_url, | origin_url, | ||||
visit["visit"], | visit["visit"], | ||||
allowed_statuses=allowed_statuses, | allowed_statuses=allowed_statuses, | ||||
require_snapshot=require_snapshot, | require_snapshot=require_snapshot, | ||||
) | ) | ||||
if visit_status is not None: | if visit_status is not None: | ||||
break | break | ||||
if visit_status is None: | if visit_status is None: | ||||
return None | return None | ||||
return (OriginVisit.from_dict(visit), visit_status) | return (OriginVisit.from_dict(visit), visit_status) | ||||
Done Inline ActionsThis is not needed, we'll get out of the loop with line 79. ardumont: This is not needed, we'll get out of the loop with line 79. |
oh, no longer need to list it... ;)