1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Helpers for progress logging.
11 from datetime import datetime
13 LOG = logging.getLogger()
19 """ Tracks and prints progress for the indexing process.
20 `name` is the name of the indexing step being tracked.
21 `total` sets up the total number of items that need processing.
22 `log_interval` denotes the interval in seconds at which progress
26 def __init__(self, name: str, total: int, log_interval: int = 1) -> None:
28 self.total_places = total
30 self.rank_start_time = datetime.now()
31 self.log_interval = log_interval
32 self.next_info = INITIAL_PROGRESS if LOG.isEnabledFor(logging.WARNING) else total + 1
34 def add(self, num: int = 1) -> None:
35 """ Mark `num` places as processed. Print a log message if the
36 logging is at least info and the log interval has passed.
38 self.done_places += num
40 if self.done_places < self.next_info:
44 done_time = (now - self.rank_start_time).total_seconds()
47 self.next_info = self.done_places + INITIAL_PROGRESS
50 places_per_sec = self.done_places / done_time
51 eta = (self.total_places - self.done_places) / places_per_sec
53 LOG.warning("Done %d in %d @ %.3f per second - %s ETA (seconds): %.2f",
54 self.done_places, int(done_time),
55 places_per_sec, self.name, eta)
57 self.next_info += int(places_per_sec) * self.log_interval
59 def done(self) -> int:
60 """ Print final statistics about the progress.
62 rank_end_time = datetime.now()
64 if rank_end_time == self.rank_start_time:
66 places_per_sec = float(self.done_places)
68 diff_seconds = (rank_end_time - self.rank_start_time).total_seconds()
69 places_per_sec = self.done_places / diff_seconds
71 LOG.warning("Done %d/%d in %d @ %.3f per second - FINISHED %s\n",
72 self.done_places, self.total_places, int(diff_seconds),
73 places_per_sec, self.name)
75 return self.done_places