| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com> |
| 4 | # |
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | # of this software and associated documentation files (the "Software"), to deal |
| 7 | # in the Software without restriction, including without limitation the rights |
| 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | # copies of the Software, and to permit persons to whom the Software is |
| 10 | # furnished to do so, subject to the following conditions: |
| 11 | # |
| 12 | # The above copyright notice and this permission notice shall be included in |
| 13 | # all copies or substantial portions of the Software. |
| 14 | # |
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | # SOFTWARE. |
| 22 | |
| 23 | import os |
| 24 | import sys |
| 25 | import time |
| 26 | from . import mi |
| 27 | from collections import namedtuple |
| 28 | from ..common import format_utils |
| 29 | |
| 30 | |
| 31 | try: |
| 32 | from progressbar import ETA, Bar, Percentage, ProgressBar |
| 33 | progressbar_available = True |
| 34 | except ImportError: |
| 35 | progressbar_available = False |
| 36 | |
| 37 | |
| 38 | # approximation for the progress bar |
| 39 | _BYTES_PER_EVENT = 30 |
| 40 | |
| 41 | |
| 42 | def get_folder_size(folder): |
| 43 | total_size = os.path.getsize(folder) |
| 44 | for item in os.listdir(folder): |
| 45 | itempath = os.path.join(folder, item) |
| 46 | if os.path.isfile(itempath): |
| 47 | total_size += os.path.getsize(itempath) |
| 48 | elif os.path.isdir(itempath): |
| 49 | total_size += get_folder_size(itempath) |
| 50 | return total_size |
| 51 | |
| 52 | |
| 53 | class _Progress: |
| 54 | def __init__(self, ts_begin, ts_end, path, use_size=False): |
| 55 | if ts_begin is None or ts_end is None or use_size: |
| 56 | size = get_folder_size(path) |
| 57 | self._maxval = size / _BYTES_PER_EVENT |
| 58 | self._use_time = False |
| 59 | else: |
| 60 | self._maxval = ts_end - ts_begin |
| 61 | self._ts_begin = ts_begin |
| 62 | self._ts_end = ts_end |
| 63 | self._use_time = True |
| 64 | |
| 65 | self._at = 0 |
| 66 | self._event_count = 0 |
| 67 | self._last_event_count_check = 0 |
| 68 | self._last_time_check = time.time() |
| 69 | |
| 70 | def update(self, event): |
| 71 | self._event_count += 1 |
| 72 | |
| 73 | if self._use_time: |
| 74 | self._at = event.timestamp - self._ts_begin |
| 75 | else: |
| 76 | self._at = self._event_count |
| 77 | |
| 78 | if self._at > self._maxval: |
| 79 | self._at = self._maxval |
| 80 | |
| 81 | if self._event_count - self._last_event_count_check >= 101: |
| 82 | self._last_event_count_check = self._event_count |
| 83 | now = time.time() |
| 84 | |
| 85 | if now - self._last_time_check >= .1: |
| 86 | self._update_progress() |
| 87 | self._last_time_check = now |
| 88 | |
| 89 | def _update_progress(self): |
| 90 | pass |
| 91 | |
| 92 | def finalize(self): |
| 93 | pass |
| 94 | |
| 95 | |
| 96 | class FancyProgressBar(_Progress): |
| 97 | def __init__(self, ts_begin, ts_end, path, use_size): |
| 98 | super().__init__(ts_begin, ts_end, path, use_size) |
| 99 | self._pbar = None |
| 100 | |
| 101 | if progressbar_available: |
| 102 | widgets = ['Processing the trace: ', Percentage(), ' ', |
| 103 | Bar(marker='#', left='[', right=']'), |
| 104 | ' ', ETA(), ' '] # see docs for other options |
| 105 | self._pbar = ProgressBar(widgets=widgets, |
| 106 | maxval=self._maxval) |
| 107 | self._pbar.start() |
| 108 | else: |
| 109 | print('Warning: progressbar module not available, ' |
| 110 | 'using --no-progress.', file=sys.stderr) |
| 111 | |
| 112 | def _update_progress(self): |
| 113 | if self._pbar is None: |
| 114 | return |
| 115 | |
| 116 | self._pbar.update(self._at) |
| 117 | |
| 118 | def finalize(self): |
| 119 | if self._pbar is None: |
| 120 | return |
| 121 | |
| 122 | self._pbar.finish() |
| 123 | |
| 124 | |
| 125 | class MiProgress(_Progress): |
| 126 | def __init__(self, ts_begin, ts_end, path, use_size): |
| 127 | super().__init__(ts_begin, ts_end, path, use_size) |
| 128 | |
| 129 | if self._use_time: |
| 130 | fmt = 'Starting analysis from {} to {}' |
| 131 | begin = format_utils.format_timestamp(self._ts_begin) |
| 132 | end = format_utils.format_timestamp(self._ts_end) |
| 133 | msg = fmt.format(begin, end) |
| 134 | else: |
| 135 | msg = 'Starting analysis: {} estimated events'.format(round(self._maxval)) |
| 136 | |
| 137 | mi.print_progress(0, msg) |
| 138 | |
| 139 | def _update_progress(self): |
| 140 | if self._at == self._maxval: |
| 141 | mi.print_progress(1, 'Done!') |
| 142 | return |
| 143 | |
| 144 | if self._use_time: |
| 145 | ts_at = self._at + self._ts_begin |
| 146 | at_ts = format_utils.format_timestamp(ts_at) |
| 147 | end = format_utils.format_timestamp(self._ts_end) |
| 148 | msg = '{}/{}; {} events processed'.format(at_ts, end, self._event_count) |
| 149 | else: |
| 150 | msg = '{} events processed'.format(self._event_count) |
| 151 | |
| 152 | mi.print_progress(round(self._at / self._maxval, 4), msg) |
| 153 | |
| 154 | def finalize(self): |
| 155 | mi.print_progress(1, 'Done!') |