Skip to content

Commit c0280e9

Browse files
committed
Improve ordered columns and hints
Change-Id: I37d71ce2a41dbf55f6486f81c957e76ea345c65c
1 parent 7b7c96d commit c0280e9

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

cmk/gui/views/inventory/_tree_renderer.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
# conditions defined in the file COPYING, which is part of this source code package.
55

66
import time
7+
from collections import OrderedDict
78
from collections.abc import Mapping, Sequence
89
from functools import total_ordering
9-
from typing import NamedTuple
10+
from typing import Iterable, NamedTuple
1011

1112
from livestatus import SiteId
1213

@@ -53,11 +54,16 @@ def make_table_view_name_of_host(view_name: str) -> str:
5354
return f"{view_name}_of_host"
5455

5556

56-
def _make_columns(
57+
def _make_columns_and_hints(
5758
rows: Sequence[Mapping[SDKey, SDValue]] | Sequence[Mapping[SDKey, SDDeltaValue]],
58-
key_order: Sequence[SDKey],
59-
) -> Sequence[SDKey]:
60-
return list(key_order) + sorted({k for r in rows for k in r} - set(key_order))
59+
hint: NodeDisplayHint,
60+
) -> OrderedDict[SDKey, ColumnDisplayHint]:
61+
return OrderedDict(
62+
{
63+
c: hint.get_column_hint(c)
64+
for c in list(hint.columns) + sorted({k for r in rows for k in r} - set(hint.columns))
65+
}
66+
)
6167

6268

6369
@total_ordering
@@ -84,16 +90,14 @@ def _sort_pairs(attributes: ImmutableAttributes, key_order: Sequence[SDKey]) ->
8490
]
8591

8692

87-
def _sort_rows(table: ImmutableTable, columns: Sequence[SDKey]) -> Sequence[Sequence[SDItem]]:
88-
def _sort_row(
89-
ident: SDRowIdent, row: Mapping[SDKey, SDValue], columns: Sequence[SDKey]
90-
) -> Sequence[SDItem]:
93+
def _sort_rows(table: ImmutableTable, columns: Iterable[SDKey]) -> Sequence[Sequence[SDItem]]:
94+
def _sort_row(ident: SDRowIdent, row: Mapping[SDKey, SDValue]) -> Sequence[SDItem]:
9195
return [SDItem(c, row.get(c), table.retentions.get(ident, {}).get(c)) for c in columns]
9296

9397
min_type = _MinType()
9498

9599
return [
96-
_sort_row(ident, row, columns)
100+
_sort_row(ident, row)
97101
for ident, row in sorted(
98102
table.rows_by_ident.items(),
99103
key=lambda t: tuple(t[1].get(c) or min_type for c in columns),
@@ -120,11 +124,9 @@ def _sort_delta_pairs(
120124

121125

122126
def _sort_delta_rows(
123-
table: ImmutableDeltaTable, columns: Sequence[SDKey]
127+
table: ImmutableDeltaTable, columns: Iterable[SDKey]
124128
) -> Sequence[Sequence[_SDDeltaItem]]:
125-
def _sort_row(
126-
row: Mapping[SDKey, SDDeltaValue], columns: Sequence[SDKey]
127-
) -> Sequence[_SDDeltaItem]:
129+
def _sort_row(row: Mapping[SDKey, SDDeltaValue]) -> Sequence[_SDDeltaItem]:
128130
return [
129131
_SDDeltaItem(c, v.old, v.new)
130132
for c in columns
@@ -137,7 +139,7 @@ def _sanitize(value: SDDeltaValue) -> tuple[_MinType | SDValue, _MinType | SDVal
137139
return (value.old or min_type, value.new or min_type)
138140

139141
return [
140-
_sort_row(row, columns)
142+
_sort_row(row)
141143
for row in sorted(
142144
table.rows,
143145
key=lambda r: tuple(_sanitize(r.get(c) or SDDeltaValue(None, None)) for c in columns),
@@ -356,21 +358,20 @@ def _show_table(
356358
class_="invtablelink",
357359
)
358360

359-
columns = _make_columns(table.rows, list(hint.columns))
360-
column_hints = {c: hint.get_column_hint(c) for c in columns}
361+
columns_and_hints = _make_columns_and_hints(table.rows, hint)
361362
sorted_rows: Sequence[Sequence[SDItem]] | Sequence[Sequence[_SDDeltaItem]] = (
362-
_sort_rows(table, columns)
363+
_sort_rows(table, columns_and_hints)
363364
if isinstance(table, ImmutableTable)
364-
else _sort_delta_rows(table, columns)
365+
else _sort_delta_rows(table, columns_and_hints)
365366
)
366367

367368
# TODO: Use table.open_table() below.
368369
html.open_table(class_="data")
369370
html.open_tr()
370-
for column in columns:
371+
for column, col_hint in columns_and_hints.items():
371372
html.th(
372373
self._get_header(
373-
column_hints[column].title,
374+
col_hint.title,
374375
f"{column}*" if column in table.key_columns else column,
375376
)
376377
)
@@ -379,7 +380,7 @@ def _show_table(
379380
for row in sorted_rows:
380381
html.open_tr(class_="even0")
381382
for item in row:
382-
col_hint = column_hints[item.key]
383+
col_hint = columns_and_hints[item.key]
383384
# TODO separate tdclass from rendered value
384385
if isinstance(item, SDItem):
385386
tdclass, rendered_value = compute_cell_spec(

0 commit comments

Comments
 (0)