4
4
# conditions defined in the file COPYING, which is part of this source code package.
5
5
6
6
import time
7
+ from collections import OrderedDict
7
8
from collections .abc import Mapping , Sequence
8
9
from functools import total_ordering
9
- from typing import NamedTuple
10
+ from typing import Iterable , NamedTuple
10
11
11
12
from livestatus import SiteId
12
13
@@ -53,11 +54,16 @@ def make_table_view_name_of_host(view_name: str) -> str:
53
54
return f"{ view_name } _of_host"
54
55
55
56
56
- def _make_columns (
57
+ def _make_columns_and_hints (
57
58
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
+ )
61
67
62
68
63
69
@total_ordering
@@ -84,16 +90,14 @@ def _sort_pairs(attributes: ImmutableAttributes, key_order: Sequence[SDKey]) ->
84
90
]
85
91
86
92
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 ]:
91
95
return [SDItem (c , row .get (c ), table .retentions .get (ident , {}).get (c )) for c in columns ]
92
96
93
97
min_type = _MinType ()
94
98
95
99
return [
96
- _sort_row (ident , row , columns )
100
+ _sort_row (ident , row )
97
101
for ident , row in sorted (
98
102
table .rows_by_ident .items (),
99
103
key = lambda t : tuple (t [1 ].get (c ) or min_type for c in columns ),
@@ -120,11 +124,9 @@ def _sort_delta_pairs(
120
124
121
125
122
126
def _sort_delta_rows (
123
- table : ImmutableDeltaTable , columns : Sequence [SDKey ]
127
+ table : ImmutableDeltaTable , columns : Iterable [SDKey ]
124
128
) -> 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 ]:
128
130
return [
129
131
_SDDeltaItem (c , v .old , v .new )
130
132
for c in columns
@@ -137,7 +139,7 @@ def _sanitize(value: SDDeltaValue) -> tuple[_MinType | SDValue, _MinType | SDVal
137
139
return (value .old or min_type , value .new or min_type )
138
140
139
141
return [
140
- _sort_row (row , columns )
142
+ _sort_row (row )
141
143
for row in sorted (
142
144
table .rows ,
143
145
key = lambda r : tuple (_sanitize (r .get (c ) or SDDeltaValue (None , None )) for c in columns ),
@@ -356,21 +358,20 @@ def _show_table(
356
358
class_ = "invtablelink" ,
357
359
)
358
360
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 )
361
362
sorted_rows : Sequence [Sequence [SDItem ]] | Sequence [Sequence [_SDDeltaItem ]] = (
362
- _sort_rows (table , columns )
363
+ _sort_rows (table , columns_and_hints )
363
364
if isinstance (table , ImmutableTable )
364
- else _sort_delta_rows (table , columns )
365
+ else _sort_delta_rows (table , columns_and_hints )
365
366
)
366
367
367
368
# TODO: Use table.open_table() below.
368
369
html .open_table (class_ = "data" )
369
370
html .open_tr ()
370
- for column in columns :
371
+ for column , col_hint in columns_and_hints . items () :
371
372
html .th (
372
373
self ._get_header (
373
- column_hints [ column ] .title ,
374
+ col_hint .title ,
374
375
f"{ column } *" if column in table .key_columns else column ,
375
376
)
376
377
)
@@ -379,7 +380,7 @@ def _show_table(
379
380
for row in sorted_rows :
380
381
html .open_tr (class_ = "even0" )
381
382
for item in row :
382
- col_hint = column_hints [item .key ]
383
+ col_hint = columns_and_hints [item .key ]
383
384
# TODO separate tdclass from rendered value
384
385
if isinstance (item , SDItem ):
385
386
tdclass , rendered_value = compute_cell_spec (
0 commit comments