cli: refactor progress indication
[deliverable/lttng-analyses.git] / lttnganalyses / cli / progressbar.py
CommitLineData
4ed24f86
JD
1# The MIT License (MIT)
2#
a3fa57c0 3# Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
4ed24f86
JD
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
418527ca
JD
23import os
24import sys
dd2efe70 25import time
73f9d005
PP
26from . import mi
27from collections import namedtuple
dd2efe70
PP
28from ..common import format_utils
29
418527ca
JD
30
31try:
32 from progressbar import ETA, Bar, Percentage, ProgressBar
33 progressbar_available = True
34except ImportError:
35 progressbar_available = False
36
dd2efe70 37
418527ca 38# approximation for the progress bar
dd2efe70 39_BYTES_PER_EVENT = 30
418527ca
JD
40
41
b6d9132b 42def get_folder_size(folder):
418527ca
JD
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):
b6d9132b 49 total_size += get_folder_size(itempath)
418527ca
JD
50 return total_size
51
52
dd2efe70
PP
53class _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
73f9d005 64
dd2efe70
PP
65 self._at = 0
66 self._event_count = 0
67 self._last_event_count_check = 0
68 self._last_time_check = time.time()
73f9d005 69
dd2efe70
PP
70 def update(self, event):
71 self._event_count += 1
73f9d005 72
dd2efe70
PP
73 if self._use_time:
74 self._at = event.timestamp - self._ts_begin
75 else:
76 self._at = self._event_count
418527ca 77
dd2efe70
PP
78 if self._at > self._maxval:
79 self._at = self._maxval
418527ca 80
dd2efe70
PP
81 if self._event_count - self._last_event_count_check >= 101:
82 self._last_event_count_check = self._event_count
83 now = time.time()
418527ca 84
dd2efe70
PP
85 if now - self._last_time_check >= .1:
86 self._update_progress()
87 self._last_time_check = now
b6d9132b 88
dd2efe70 89 def _update_progress(self):
418527ca 90 pass
418527ca 91
dd2efe70
PP
92 def finalize(self):
93 pass
418527ca 94
73f9d005 95
dd2efe70
PP
96class 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
73f9d005 100
dd2efe70
PP
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)
73f9d005 111
dd2efe70
PP
112 def _update_progress(self):
113 if self._pbar is None:
114 return
73f9d005 115
dd2efe70 116 self._pbar.update(self._at)
73f9d005 117
dd2efe70
PP
118 def finalize(self):
119 if self._pbar is None:
120 return
73f9d005 121
dd2efe70 122 self._pbar.finish()
73f9d005 123
73f9d005 124
dd2efe70
PP
125class 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))
73f9d005 136
dd2efe70 137 mi.print_progress(0, msg)
73f9d005 138
dd2efe70
PP
139 def _update_progress(self):
140 if self._at == self._maxval:
141 mi.print_progress(1, 'Done!')
142 return
73f9d005 143
dd2efe70
PP
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)
73f9d005 151
dd2efe70 152 mi.print_progress(round(self._at / self._maxval, 4), msg)
73f9d005 153
dd2efe70
PP
154 def finalize(self):
155 mi.print_progress(1, 'Done!')
This page took 0.031674 seconds and 5 git commands to generate.