Changeset View
Changeset View
Standalone View
Standalone View
swh/scheduler/journal_client.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime | from datetime import datetime | ||||
from typing import Dict, List, Optional, Tuple | from typing import Dict, List, Optional, Tuple | ||||
import attr | import attr | ||||
from swh.scheduler.interface import SchedulerInterface | from swh.scheduler.interface import SchedulerInterface | ||||
from swh.scheduler.model import OriginVisitStats | from swh.scheduler.model import OriginVisitStats | ||||
msg_type = "origin_visit_status" | msg_type = "origin_visit_status" | ||||
def max_date(d1: Optional[datetime], d2: Optional[datetime]) -> datetime: | def max_date(*dates: Optional[datetime]) -> datetime: | ||||
"""Return the max date of the visit stats | """Return the max date of given (possibly None) dates | ||||
At least one date must be not None. | |||||
""" | """ | ||||
if d1 is None and d2 is None: | datesok: Tuple[datetime, ...] = tuple(d for d in dates if d is not None) | ||||
if not datesok: | |||||
raise ValueError("At least one date should be a valid datetime") | raise ValueError("At least one date should be a valid datetime") | ||||
if d1 is None: | |||||
assert d2 is not None # make mypy happy | maxdate = datesok[0] | ||||
return d2 | if len(datesok) == 1: | ||||
if d2 is None: | return maxdate | ||||
return d1 | |||||
return max(d1, d2) | for d in datesok[1:]: | ||||
maxdate = max(d, maxdate) | |||||
return maxdate | |||||
def process_journal_objects( | def process_journal_objects( | ||||
messages: Dict[str, List[Dict]], *, scheduler: SchedulerInterface | messages: Dict[str, List[Dict]], *, scheduler: SchedulerInterface | ||||
) -> None: | ) -> None: | ||||
"""Read messages from origin_visit_status journal topics, then inserts them in the | """Read messages from origin_visit_status journal topics, then inserts them in the | ||||
scheduler "origin_visit_stats" table. | scheduler "origin_visit_stats" table. | ||||
▲ Show 20 Lines • Show All 86 Lines • Show Last 20 Lines |