Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/origin_visits.py
Show All 18 Lines | def get_origin_visits(origin_info): | ||||
Args: | Args: | ||||
origin_info (dict): dict describing the origin to fetch visits from | origin_info (dict): dict describing the origin to fetch visits from | ||||
Returns: | Returns: | ||||
list: A list of dict describing the origin visits with the | list: A list of dict describing the origin visits with the | ||||
following keys: | following keys: | ||||
* **date**: UTC visit date in ISO format, | * **date**: UTC visit date in ISO format, | ||||
* **origin**: the origin id | * **origin**: the origin url | ||||
* **status**: the visit status, either **full**, **partial** | * **status**: the visit status, either **full**, **partial** | ||||
or **ongoing** | or **ongoing** | ||||
* **visit**: the visit id | * **visit**: the visit id | ||||
* **type**: the visit type | |||||
Raises: | Raises: | ||||
NotFoundExc: if the origin is not found | NotFoundExc: if the origin is not found | ||||
""" | """ | ||||
from swh.web.common import service | from swh.web.common import service | ||||
if 'url' in origin_info: | if 'url' in origin_info: | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | def get_origin_visit(origin_info, visit_ts=None, visit_id=None, | ||||
snapshot_id=None): | snapshot_id=None): | ||||
"""Function that returns information about a visit for | """Function that returns information about a visit for | ||||
a given origin. | a given origin. | ||||
The visit is retrieved from a provided timestamp. | The visit is retrieved from a provided timestamp. | ||||
The closest visit from that timestamp is selected. | The closest visit from that timestamp is selected. | ||||
Args: | Args: | ||||
origin_info (dict): a dict filled with origin information | origin_info (dict): a dict filled with origin information | ||||
(id, url, type) | |||||
visit_ts (int or str): an ISO date string or Unix timestamp to parse | visit_ts (int or str): an ISO date string or Unix timestamp to parse | ||||
Returns: | Returns: | ||||
A dict containing the visit info as described below:: | A dict containing the visit info as described below:: | ||||
{'origin': 2, | {'origin': 'https://forge.softwareheritage.org/source/swh-web/', | ||||
'date': '2017-10-08T11:54:25.582463+00:00', | 'date': '2017-10-08T11:54:25.582463+00:00', | ||||
'metadata': {}, | 'metadata': {}, | ||||
'visit': 25, | 'visit': 25, | ||||
'status': 'full'} | 'status': 'full'} | ||||
""" | """ | ||||
visits = get_origin_visits(origin_info) | visits = get_origin_visits(origin_info) | ||||
if not visits: | if not visits: | ||||
if 'url' in origin_info: | raise NotFoundExc(('No visit associated to origin with' | ||||
message = ('No visit associated to origin with' | ' url %s!' % origin_info['url'])) | ||||
' url %s!' % origin_info['url']) | |||||
else: | |||||
message = ('No visit associated to origin with' | |||||
' id %s!' % origin_info['id']) | |||||
raise NotFoundExc(message) | |||||
if snapshot_id: | if snapshot_id: | ||||
visit = [v for v in visits if v['snapshot'] == snapshot_id] | visit = [v for v in visits if v['snapshot'] == snapshot_id] | ||||
if len(visit) == 0: | if len(visit) == 0: | ||||
if 'type' in origin_info and 'url' in origin_info: | raise NotFoundExc(('Visit for snapshot with id %s for origin with' | ||||
message = ('Visit for snapshot with id %s for origin with type' | |||||
' url %s not found!' % | ' url %s not found!' % | ||||
(snapshot_id, origin_info['url'])) | (snapshot_id, origin_info['url']))) | ||||
else: | |||||
message = ('Visit for snapshot with id %s for origin with' | |||||
' id %s not found!' % | |||||
(snapshot_id, origin_info['id'])) | |||||
raise NotFoundExc(message) | |||||
return visit[0] | return visit[0] | ||||
if visit_id: | if visit_id: | ||||
visit = [v for v in visits if v['visit'] == int(visit_id)] | visit = [v for v in visits if v['visit'] == int(visit_id)] | ||||
if len(visit) == 0: | if len(visit) == 0: | ||||
if 'type' in origin_info and 'url' in origin_info: | raise NotFoundExc(('Visit with id %s for origin with' | ||||
message = ('Visit with id %s for origin with' | ' url %s not found!' % | ||||
' and url %s not found!' % | (visit_id, origin_info['url']))) | ||||
(visit_id, origin_info['url'])) | |||||
else: | |||||
message = ('Visit with id %s for origin with id %s' | |||||
' not found!' % (visit_id, origin_info['id'])) | |||||
raise NotFoundExc(message) | |||||
return visit[0] | return visit[0] | ||||
if not visit_ts: | if not visit_ts: | ||||
# returns the latest full visit when no timestamp is provided | # returns the latest full visit when no timestamp is provided | ||||
for v in reversed(visits): | for v in reversed(visits): | ||||
if v['status'] == 'full': | if v['status'] == 'full': | ||||
return v | return v | ||||
return visits[-1] | return visits[-1] | ||||
Show All 11 Lines | if visit_idx is not None: | ||||
# If multiple visits have the same date, select the one with | # If multiple visits have the same date, select the one with | ||||
# the largest id. | # the largest id. | ||||
while visit_idx < len(visits) - 1 and \ | while visit_idx < len(visits) - 1 and \ | ||||
visit['date'] == visits[visit_idx+1]['date']: | visit['date'] == visits[visit_idx+1]['date']: | ||||
visit_idx = visit_idx + 1 | visit_idx = visit_idx + 1 | ||||
visit = visits[visit_idx] | visit = visits[visit_idx] | ||||
return visit | return visit | ||||
else: | else: | ||||
if 'type' in origin_info and 'url' in origin_info: | raise NotFoundExc(('Visit with timestamp %s for origin with ' | ||||
message = ('Visit with timestamp %s for origin with ' | 'url %s not found!' % | ||||
'and url %s not found!' % | (visit_ts, origin_info['url']))) | ||||
(visit_ts, origin_info['url'])) | |||||
else: | |||||
message = ('Visit with timestamp %s for origin with id %s ' | |||||
'not found!' % (visit_ts, origin_info['id'])) | |||||
raise NotFoundExc(message) |