Commit 563eb9f7 authored by root's avatar root
Browse files

Merge branch 'master' of https://github.com/tribe29/checkmk

parents 9f1ff24e bff9fbaa
......@@ -127,34 +127,19 @@ def _get_paint_function_from_globals(paint_name: str) -> PaintFunction:
return _PAINT_FUNCTIONS[_PAINT_FUNCTION_NAME_PREFIX + paint_name]
def _paint_host_inventory_tree(
row: Row,
invpath: SDRawPath = ".",
column: str = "host_inventory",
render_single_attribute: bool = False,
) -> CellSpec:
raw_hostname = row.get("host_name")
assert isinstance(raw_hostname, str)
if (
len(sites_with_same_named_hosts := _get_sites_with_same_named_hosts(HostName(raw_hostname)))
> 1
):
html.show_error(
_("Cannot display inventory tree of host '%s': Found this host on multiple sites: %s")
% (raw_hostname, ", ".join(sites_with_same_named_hosts))
)
def _paint_host_inventory_tree(row: Row, raw_path: SDRawPath) -> CellSpec:
try:
_validate_inventory_tree_uniqueness(row)
except MultipleInventoryTreesError:
return "", ""
struct_tree = row.get(column)
struct_tree = row.get("host_inventory")
if struct_tree is None:
return "", ""
assert isinstance(struct_tree, StructuredDataNode)
tree_renderer = _get_tree_renderer(row, column)
parsed_path, attribute_keys = inventory.parse_tree_path(invpath)
parsed_path, attribute_keys = inventory.parse_tree_path(raw_path)
child: Union[None, StructuredDataNode, Table, Attributes]
if attribute_keys is None:
......@@ -175,23 +160,49 @@ def _paint_host_inventory_tree(
if child is None:
return "", ""
painter_options = PainterOptions.get_instance()
tree_renderer = NodeRenderer(
row["site"],
row["host_name"],
show_internal_tree_paths=painter_options.get("show_internal_tree_paths"),
)
with output_funnel.plugged():
if isinstance(child, Attributes) and render_single_attribute:
if isinstance(child, Attributes):
# In host views like "Switch port statistics" the value is rendered as a single
# attribute and not within a table.
if len(attributes := list(child.pairs.items())) == 1:
key, value = attributes[-1]
tree_renderer.show_attribute(value, AttributeDisplayHint.make(parsed_path, key))
else:
child.show(tree_renderer)
else:
child.show(tree_renderer)
code = HTML(output_funnel.drain())
return td_class, code
def _get_sites_with_same_named_hosts(hostname: HostName) -> Sequence[SiteId]:
return _get_sites_with_same_named_hosts_cache().get(hostname, [])
class MultipleInventoryTreesError(Exception):
pass
def _validate_inventory_tree_uniqueness(row: Row) -> None:
raw_hostname = row.get("host_name")
assert isinstance(raw_hostname, str)
if (
len(
sites_with_same_named_hosts := _get_sites_with_same_named_hosts_cache().get(
HostName(raw_hostname), []
)
)
> 1
):
html.show_error(
_("Cannot display inventory tree of host '%s': Found this host on multiple sites: %s")
% (raw_hostname, ", ".join(sites_with_same_named_hosts))
)
raise MultipleInventoryTreesError()
@request_memoize()
......@@ -204,23 +215,6 @@ def _get_sites_with_same_named_hosts_cache() -> Mapping[HostName, Sequence[SiteI
return cache
def _get_tree_renderer(row: Row, column: str) -> ABCNodeRenderer:
if column == "host_inventory":
painter_options = PainterOptions.get_instance()
return NodeRenderer(
row["site"],
row["host_name"],
show_internal_tree_paths=painter_options.get("show_internal_tree_paths"),
)
tree_id = "/" + str(row["invhist_time"])
return DeltaNodeRenderer(
row["site"],
row["host_name"],
tree_id=tree_id,
)
def _get_filtered_attributes(
struct_tree: StructuredDataNode,
path: SDPath,
......@@ -266,9 +260,7 @@ def _declare_inv_column(raw_path: SDRawPath, raw_hint: InventoryHintSpec) -> Non
# not look good for the HW/SW inventory tree
"printable": isinstance(hint, AttributeDisplayHint),
"load_inv": True,
"paint": lambda row: _paint_host_inventory_tree(
row, raw_path, render_single_attribute=isinstance(hint, AttributeDisplayHint)
),
"paint": lambda row: _paint_host_inventory_tree(row, raw_path),
"sorter": name,
}
......@@ -401,7 +393,29 @@ class PainterInventoryTree(Painter):
return True
def render(self, row: Row, cell: Cell) -> CellSpec:
return _paint_host_inventory_tree(row)
try:
_validate_inventory_tree_uniqueness(row)
except MultipleInventoryTreesError:
return "", ""
struct_tree = row.get("host_inventory")
if struct_tree is None:
return "", ""
assert isinstance(struct_tree, StructuredDataNode)
painter_options = PainterOptions.get_instance()
tree_renderer = NodeRenderer(
row["site"],
row["host_name"],
show_internal_tree_paths=painter_options.get("show_internal_tree_paths"),
)
with output_funnel.plugged():
struct_tree.show(tree_renderer)
code = HTML(output_funnel.drain())
return "invtree", code
class ABCRowTable(RowTable):
......@@ -1911,7 +1925,28 @@ class PainterInvhistDelta(Painter):
return ["invhist_delta", "invhist_time"]
def render(self, row: Row, cell: Cell) -> CellSpec:
return _paint_host_inventory_tree(row, column="invhist_delta")
try:
_validate_inventory_tree_uniqueness(row)
except MultipleInventoryTreesError:
return "", ""
struct_tree = row.get("invhist_delta")
if struct_tree is None:
return "", ""
assert isinstance(struct_tree, StructuredDataNode)
tree_renderer = DeltaNodeRenderer(
row["site"],
row["host_name"],
tree_id="/" + str(row["invhist_time"]),
)
with output_funnel.plugged():
struct_tree.show(tree_renderer)
code = HTML(output_funnel.drain())
return "invtree", code
def _paint_invhist_count(row: Row, what: str) -> CellSpec:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment