Page MenuHomeSoftware Heritage
Paste P1283

origin_visit_find_latest_status_by_date
ActivePublic

Authored by ardumont on Feb 10 2022, 9:22 AM.
def origin_visit_find_latest_status_by_date(
origin_url: str, visit_date: datetime.datetime
) -> Optional[OriginVisitInfo]:
"""Retrieve origin visit status whose date is most recent than the provided visit_date.
Args:
origin_url: origin concerned by the visit
visit_date: provided visit date
Returns:
The dict origin_visit_status matching the criteria if any.
"""
visit = storage.origin_visit_find_by_date(origin_url, visit_date)
if not visit:
return None
if visit.date > visit_date: # found a visit matching the criteria
visit_id = visit.visit
else:
previous_visit = visit.visit - 1
for visit in _lookup_origin_visits(origin_url, last_visit=previous_visit):
if visit.date > visit_date: # found a visit matching the criteria
visit_id = visit.visit
break
else:
# No visit found matching the criteria
return None
assert visit_id is not None
visit_status = storage.origin_visit_status_get_latest(origin_url, visit_id)
return converters.from_origin_visit({**visit_status.to_dict(), "type": visit.type})

Event Timeline

ardumont updated the paste's language from autodetect to python.

Take 2 which sounds simpler:

def origin_visit_find_latest_status(
    origin_url: str,
) -> Optional[OriginVisitInfo]:
    """Retrieve origin visit status whose date is most recent than the provided visit_date.

    Args:
        origin_url: origin concerned by the visit
        visit_date: provided visit date

    Returns:
       The dict origin_visit_status matching the criteria if any.

    """
    visit_page = storage.origin_visit_get(
        origin_url, limit=1, order=ListOrder.DESC
    )
    if not visit_page.results:
        return None
    visit = visit_page.results[0]
    visit_status = storage.origin_visit_status_get_latest(origin_url, visit.visit)
    return converters.from_origin_visit({**visit_status.to_dict(), "type": visit.type})