Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/storage.py
Show First 20 Lines • Show All 798 Lines • ▼ Show 20 Lines | class Storage(): | ||||
@timed | @timed | ||||
@db_transaction() | @db_transaction() | ||||
def snapshot_get_random(self, db=None, cur=None): | def snapshot_get_random(self, db=None, cur=None): | ||||
return db.snapshot_get_random(cur) | return db.snapshot_get_random(cur) | ||||
@timed | @timed | ||||
@db_transaction() | @db_transaction() | ||||
def origin_visit_add( | def origin_visit_add( | ||||
self, origin, date, type, db=None, cur=None | self, origin_url: str, date: Union[str, datetime.datetime], | ||||
) -> Optional[Dict[str, Union[str, int]]]: | type: str, db=None, cur=None) -> OriginVisit: | ||||
origin_url = origin | |||||
if isinstance(date, str): | if isinstance(date, str): | ||||
# FIXME: Converge on iso8601 at some point | # FIXME: Converge on iso8601 at some point | ||||
date = dateutil.parser.parse(date) | date = dateutil.parser.parse(date) | ||||
elif not isinstance(date, datetime.datetime): | |||||
raise StorageArgumentException( | |||||
'Date must be a datetime or a string') | |||||
origin = self.origin_get({'url': origin_url}) | |||||
if not origin: # Cannot add a visit without an origin | |||||
raise StorageArgumentException( | |||||
'Unknown origin %s', origin_url) | |||||
with convert_validation_exceptions(): | with convert_validation_exceptions(): | ||||
visit_id = db.origin_visit_add(origin_url, date, type, cur) | visit_id = db.origin_visit_add(origin_url, date, type, cur=cur) | ||||
# We can write to the journal only after inserting to the | # We can write to the journal only after inserting to the | ||||
# DB, because we want the id of the visit | # DB, because we want the id of the visit | ||||
visit = { | visit = OriginVisit.from_dict({ | ||||
'origin': origin_url, | 'origin': origin_url, | ||||
'date': date, | 'date': date, | ||||
'type': type, | 'type': type, | ||||
'visit': visit_id, | 'visit': visit_id, | ||||
'status': 'ongoing', | 'status': 'ongoing', | ||||
'metadata': None, | 'metadata': None, | ||||
'snapshot': None | 'snapshot': None | ||||
} | }) | ||||
self.journal_writer.origin_visit_add(visit) | self.journal_writer.origin_visit_add(visit) | ||||
send_metric('origin_visit:add', count=1, method_name='origin_visit') | send_metric('origin_visit:add', count=1, method_name='origin_visit') | ||||
return { | return visit | ||||
'origin': origin_url, | |||||
'visit': visit_id, | |||||
} | |||||
@timed | @timed | ||||
@db_transaction() | @db_transaction() | ||||
def origin_visit_update(self, origin: str, visit_id: int, | def origin_visit_update(self, origin: str, visit_id: int, | ||||
status: Optional[str] = None, | status: Optional[str] = None, | ||||
metadata: Optional[Dict] = None, | metadata: Optional[Dict] = None, | ||||
snapshot: Optional[bytes] = None, | snapshot: Optional[bytes] = None, | ||||
db=None, cur=None): | db=None, cur=None): | ||||
▲ Show 20 Lines • Show All 308 Lines • Show Last 20 Lines |