Display Processor progress on stdout
This commit is contained in:
parent
5b8d27e720
commit
6f7a7a6d79
1 changed files with 34 additions and 0 deletions
|
@ -15,6 +15,7 @@
|
|||
"""Base processor module."""
|
||||
|
||||
from collections import defaultdict
|
||||
import sys
|
||||
from typing import Callable
|
||||
from typing import Dict
|
||||
from typing import List
|
||||
|
@ -258,6 +259,9 @@ class Processor():
|
|||
expanded_handlers = self._expand_dependencies(*handlers, **kwargs)
|
||||
self._handler_multimap = self._get_handler_maps(expanded_handlers)
|
||||
self._register_with_handlers(expanded_handlers)
|
||||
self._progress_display = ProcessingProgressDisplay(
|
||||
[type(handler).__name__ for handler in expanded_handlers],
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _expand_dependencies(
|
||||
|
@ -306,8 +310,10 @@ class Processor():
|
|||
|
||||
:param events: the events to process
|
||||
"""
|
||||
self._progress_display.set_work_total(len(events))
|
||||
for event in events:
|
||||
self._process_event(event)
|
||||
self._progress_display.did_work()
|
||||
|
||||
def _process_event(self, event: DictEvent) -> None:
|
||||
"""Process a single event."""
|
||||
|
@ -339,3 +345,31 @@ class Processor():
|
|||
raise_if_not_found=False)
|
||||
metadata = EventMetadata(event_name, timestamp, cpu_id, procname, pid, tid)
|
||||
handler_function(event, metadata)
|
||||
|
||||
|
||||
class ProcessingProgressDisplay():
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
processing_elements: List[str],
|
||||
) -> None:
|
||||
self.__info_string = '[' + ', '.join(processing_elements) + ']'
|
||||
self.__progress_count = 0
|
||||
self.__total_work = 0
|
||||
self.__work_display_period = 1
|
||||
|
||||
def set_work_total(
|
||||
self,
|
||||
total: int,
|
||||
) -> None:
|
||||
self.__total_work = total
|
||||
self.__work_display_period = int(self.__total_work / 100.0)
|
||||
|
||||
def did_work(
|
||||
self,
|
||||
increment: int = 1,
|
||||
) -> None:
|
||||
self.__progress_count += increment
|
||||
if self.__progress_count % self.__work_display_period == 0:
|
||||
percentage = 100.0 * (float(self.__progress_count) / float(self.__total_work))
|
||||
sys.stdout.write(f' [{percentage:2.0f}%] {self.__info_string}\r')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue