1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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()
18 """ Tracks and prints progress for the indexing process.
19 `name` is the name of the indexing step being tracked.
20 `total` sets up the total number of items that need processing.
21 `log_interval` denotes the interval in seconds at which progress
25 def __init__(self, name: str, total: int, log_interval: int = 1) -> None:
27 self.total_places = total
29 self.rank_start_time = datetime.now()
30 self.log_interval = log_interval
31 self.next_info = INITIAL_PROGRESS if LOG.isEnabledFor(logging.WARNING) else total + 1
33 def add(self, num: int = 1) -> None:
34 """ Mark `num` places as processed. Print a log message if the
35 logging is at least info and the log interval has passed.
37 self.done_places += num
39 if self.done_places < self.next_info:
43 done_time = (now - self.rank_start_time).total_seconds()
46 self.next_info = self.done_places + INITIAL_PROGRESS
49 places_per_sec = self.done_places / done_time
50 eta = (self.total_places - self.done_places) / places_per_sec
52 LOG.warning("Done %d in %d @ %.3f per second - %s ETA (seconds): %.2f",
53 self.done_places, int(done_time),
54 places_per_sec, self.name, eta)
56 self.next_info += int(places_per_sec) * self.log_interval
58 def done(self) -> None:
59 """ Print final statistics about the progress.
61 rank_end_time = datetime.now()
63 if rank_end_time == self.rank_start_time:
65 places_per_sec = float(self.done_places)
67 diff_seconds = (rank_end_time - self.rank_start_time).total_seconds()
68 places_per_sec = self.done_places / diff_seconds
70 LOG.warning("Done %d/%d in %d @ %.3f per second - FINISHED %s\n",
71 self.done_places, self.total_places, int(diff_seconds),
72 places_per_sec, self.name)