Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/postgresql/storage.py
Show First 20 Lines • Show All 902 Lines • ▼ Show 20 Lines | def origin_visit_status_get_latest( | ||||
cur=None, | cur=None, | ||||
) -> Optional[OriginVisitStatus]: | ) -> Optional[OriginVisitStatus]: | ||||
if allowed_statuses and not set(allowed_statuses).intersection(VISIT_STATUSES): | if allowed_statuses and not set(allowed_statuses).intersection(VISIT_STATUSES): | ||||
raise StorageArgumentException( | raise StorageArgumentException( | ||||
f"Unknown allowed statuses {','.join(allowed_statuses)}, only " | f"Unknown allowed statuses {','.join(allowed_statuses)}, only " | ||||
f"{','.join(VISIT_STATUSES)} authorized" | f"{','.join(VISIT_STATUSES)} authorized" | ||||
) | ) | ||||
row = db.origin_visit_status_get_latest( | row_d = db.origin_visit_status_get_latest( | ||||
origin_url, visit, allowed_statuses, require_snapshot, cur=cur | origin_url, visit, allowed_statuses, require_snapshot, cur=cur | ||||
) | ) | ||||
if not row: | if not row_d: | ||||
return None | return None | ||||
return OriginVisitStatus.from_dict(row) | return OriginVisitStatus(**row_d) | ||||
@timed | @timed | ||||
@db_transaction(statement_timeout=500) | @db_transaction(statement_timeout=500) | ||||
def origin_visit_get( | def origin_visit_get( | ||||
self, | self, | ||||
origin: str, | origin: str, | ||||
page_token: Optional[str] = None, | page_token: Optional[str] = None, | ||||
order: ListOrder = ListOrder.ASC, | order: ListOrder = ListOrder.ASC, | ||||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | ) -> PagedResult[OriginVisitStatus]: | ||||
date_from = datetime.datetime.fromisoformat(page_token) | date_from = datetime.datetime.fromisoformat(page_token) | ||||
visit_statuses: List[OriginVisitStatus] = [] | visit_statuses: List[OriginVisitStatus] = [] | ||||
# Take one more visit status so we can reuse it as the next page token if any | # Take one more visit status so we can reuse it as the next page token if any | ||||
for row in db.origin_visit_status_get_range( | for row in db.origin_visit_status_get_range( | ||||
origin, visit, date_from=date_from, order=order, limit=limit + 1, cur=cur, | origin, visit, date_from=date_from, order=order, limit=limit + 1, cur=cur, | ||||
): | ): | ||||
row_d = dict(zip(db.origin_visit_status_cols, row)) | row_d = dict(zip(db.origin_visit_status_cols, row)) | ||||
visit_statuses.append( | visit_statuses.append(OriginVisitStatus(**row_d)) | ||||
OriginVisitStatus( | |||||
origin=row_d["origin"], | |||||
visit=row_d["visit"], | |||||
date=row_d["date"], | |||||
type=row_d["type"], | |||||
status=row_d["status"], | |||||
snapshot=row_d["snapshot"], | |||||
metadata=row_d["metadata"], | |||||
) | |||||
) | |||||
if len(visit_statuses) > limit: | if len(visit_statuses) > limit: | ||||
# last visit status date is the next page token | # last visit status date is the next page token | ||||
next_page_token = str(visit_statuses[-1].date) | next_page_token = str(visit_statuses[-1].date) | ||||
# excluding that visit status from the result to respect the limit size | # excluding that visit status from the result to respect the limit size | ||||
visit_statuses = visit_statuses[:limit] | visit_statuses = visit_statuses[:limit] | ||||
return PagedResult(results=visit_statuses, next_page_token=next_page_token) | return PagedResult(results=visit_statuses, next_page_token=next_page_token) | ||||
@timed | @timed | ||||
@db_transaction() | @db_transaction() | ||||
def origin_visit_status_get_random( | def origin_visit_status_get_random( | ||||
self, type: str, db=None, cur=None | self, type: str, db=None, cur=None | ||||
) -> Optional[OriginVisitStatus]: | ) -> Optional[OriginVisitStatus]: | ||||
row = db.origin_visit_get_random(type, cur) | row = db.origin_visit_get_random(type, cur) | ||||
if row is not None: | if row is not None: | ||||
row_d = dict(zip(db.origin_visit_get_cols, row)) | row_d = dict(zip(db.origin_visit_status_cols, row)) | ||||
visit_status = OriginVisitStatus( | return OriginVisitStatus(**row_d) | ||||
origin=row_d["origin"], | |||||
visit=row_d["visit"], | |||||
date=row_d["date"], | |||||
type=row_d["type"], | |||||
status=row_d["status"], | |||||
metadata=row_d["metadata"], | |||||
snapshot=row_d["snapshot"], | |||||
) | |||||
return visit_status | |||||
return None | return None | ||||
@timed | @timed | ||||
@db_transaction(statement_timeout=2000) | @db_transaction(statement_timeout=2000) | ||||
def object_find_by_sha1_git( | def object_find_by_sha1_git( | ||||
self, ids: List[Sha1Git], db=None, cur=None | self, ids: List[Sha1Git], db=None, cur=None | ||||
) -> Dict[Sha1Git, List[Dict]]: | ) -> Dict[Sha1Git, List[Dict]]: | ||||
ret: Dict[Sha1Git, List[Dict]] = {id: [] for id in ids} | ret: Dict[Sha1Git, List[Dict]] = {id: [] for id in ids} | ||||
▲ Show 20 Lines • Show All 337 Lines • Show Last 20 Lines |