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})