|
13 | 13 |
|
14 | 14 |
|
15 | 15 | class kpi_page(ABC):
|
16 |
| - |
17 | 16 | def __init__(self, kpi_logger: KpiLogger) -> None:
|
| 17 | + self.kpi_logger = kpi_logger |
| 18 | + |
18 | 19 | @ui.page('/kpis')
|
19 | 20 | def page():
|
20 |
| - if self.language == 'de': |
21 |
| - humanize.activate('de') |
22 |
| - else: |
23 |
| - humanize.deactivate() |
24 |
| - with ui.row().style('margin:1em'): |
25 |
| - ui.markdown(f'### {self.title}').style('margin:1.5em;margin-top:-1.2em;') |
26 |
| - toggle = ui.toggle(self.timespans, value=next(iter(self.timespans)), on_change=lambda e: show(e.value)) |
27 |
| - with ui.row().classes('w-full'): |
28 |
| - ui_charts = [] |
29 |
| - for chart in self.charts: |
30 |
| - ui_charts.append(ui.echart({ |
31 |
| - 'title': {'text': chart.title}, |
32 |
| - 'xAxis': {'type': 'category'}, |
33 |
| - 'yAxis': {'type': 'value', 'name': chart.unit}, |
34 |
| - 'legend': {'bottom': 10}, |
35 |
| - 'tooltip': {}, |
36 |
| - 'color': [to_hex(colormaps[chart.colormap or 'viridis'](i / 10)) for i in range(9, -1, -2)], |
37 |
| - 'series': [], |
38 |
| - })) |
39 |
| - ui.timer(5, lambda: show(toggle.value)) |
| 21 | + self._content() |
40 | 22 |
|
41 |
| - def show(num_days: int) -> None: |
42 |
| - time_buckets: Sequence[TimeBucket] |
43 |
| - if num_days <= 7: |
44 |
| - time_buckets = kpi_logger.days[-num_days:] |
45 |
| - elif num_days % 7 == 0: |
46 |
| - weeks = [list(v) for _, v in groupby(kpi_logger.days, |
47 |
| - key=lambda d: str_to_date(d.date).strftime('%y-%W'))][-num_days//7:] |
48 |
| - time_buckets = [Week.from_buckets(days_in_week) for days_in_week in weeks] |
49 |
| - elif num_days % 30 == 0 and num_days <= 90: |
50 |
| - months = [list(v) for _, v in groupby(kpi_logger.days, |
51 |
| - key=lambda d: str_to_date(d.date).strftime('%y-%m'))][-num_days//30:] |
52 |
| - time_buckets = [Month.from_buckets(days_in_month) for days_in_month in months] |
53 |
| - else: |
54 |
| - raise ValueError(f'Unsupported number of days: {num_days}') |
| 23 | + def _content(self) -> None: |
| 24 | + if self.language == 'de': |
| 25 | + humanize.activate('de') |
| 26 | + else: |
| 27 | + humanize.deactivate() |
| 28 | + with ui.row().style('margin:1em'): |
| 29 | + ui.markdown(f'### {self.title}').style('margin:1.5em;margin-top:-1.2em;') |
| 30 | + toggle = ui.toggle(self.timespans, value=next(iter(self.timespans)), on_change=lambda e: show(e.value)) |
| 31 | + with ui.row().classes('w-full'): |
| 32 | + ui_charts = [] |
| 33 | + for chart in self.charts: |
| 34 | + ui_charts.append(ui.echart({ |
| 35 | + 'title': {'text': chart.title}, |
| 36 | + 'xAxis': {'type': 'category'}, |
| 37 | + 'yAxis': {'type': 'value', 'name': chart.unit}, |
| 38 | + 'legend': {'bottom': 10}, |
| 39 | + 'tooltip': {}, |
| 40 | + 'color': [to_hex(colormaps[chart.colormap or 'viridis'](i / 10)) for i in range(9, -1, -2)], |
| 41 | + 'series': [], |
| 42 | + })) |
| 43 | + ui.timer(5, lambda: show(toggle.value)) |
| 44 | + |
| 45 | + def show(num_days: int) -> None: |
| 46 | + time_buckets: Sequence[TimeBucket] |
| 47 | + if num_days <= 7: |
| 48 | + time_buckets = self.kpi_logger.days[-num_days:] |
| 49 | + elif num_days % 7 == 0: |
| 50 | + weeks = [list(v) for _, v in groupby(self.kpi_logger.days, |
| 51 | + key=lambda d: str_to_date(d.date).strftime('%y-%W'))][-num_days//7:] |
| 52 | + time_buckets = [Week.from_buckets(days_in_week) for days_in_week in weeks] |
| 53 | + elif num_days % 30 == 0 and num_days <= 90: |
| 54 | + months = [list(v) for _, v in groupby(self.kpi_logger.days, |
| 55 | + key=lambda d: str_to_date(d.date).strftime('%y-%m'))][-num_days//30:] |
| 56 | + time_buckets = [Month.from_buckets(days_in_month) for days_in_month in months] |
| 57 | + else: |
| 58 | + raise ValueError(f'Unsupported number of days: {num_days}') |
55 | 59 |
|
56 |
| - for chart, ui_chart in zip(self.charts, ui_charts, strict=True): |
57 |
| - keys = set(key for day in time_buckets for key in day.incidents if key in chart.indicators) |
58 |
| - data = {chart.indicators[key]: [day.incidents.get(key, 0) for day in time_buckets] for key in keys} |
59 |
| - styling = {'type': 'bar', 'stack': 'total', 'emphasis': {'focus': 'series'}} |
60 |
| - ui_chart.options['xAxis']['data'] = [_label(b) for b in time_buckets] |
61 |
| - ui_chart.options['series'] = [ |
62 |
| - {**styling, 'name': k, 'data': [round(item * chart.scale, 2) for item in v]} |
63 |
| - for k, v in sorted(data.items()) |
64 |
| - ] |
65 |
| - ui_chart.update() |
| 60 | + for chart, ui_chart in zip(self.charts, ui_charts, strict=True): |
| 61 | + keys = set(key for day in time_buckets for key in day.incidents if key in chart.indicators) |
| 62 | + data = {chart.indicators[key]: [day.incidents.get(key, 0) for day in time_buckets] for key in keys} |
| 63 | + styling = {'type': 'bar', 'stack': 'total', 'emphasis': {'focus': 'series'}} |
| 64 | + ui_chart.options['xAxis']['data'] = [_label(b) for b in time_buckets] |
| 65 | + ui_chart.options['series'] = [ |
| 66 | + {**styling, 'name': k, 'data': [round(item * chart.scale, 2) for item in v]} |
| 67 | + for k, v in sorted(data.items()) |
| 68 | + ] |
| 69 | + ui_chart.update() |
66 | 70 |
|
67 | 71 | @property
|
68 | 72 | def language(self) -> str:
|
|
0 commit comments