def ajax_render_tree(): aggr_group = html.request.get_unicode_input("group") aggr_title = html.request.get_unicode_input("title") omit_root = bool(html.request.var("omit_root")) only_problems = bool(html.request.var("only_problems")) rows = [] bi_manager = BIManager() bi_manager.status_fetcher.set_assumed_states(config.user.bi_assumptions) aggregation_id = html.request.get_str_input_mandatory("aggregation_id") bi_aggregation_filter = BIAggregationFilter([], [], [aggregation_id], [aggr_title] if aggr_title is not None else [], [aggr_group] if aggr_group is not None else [], []) rows = bi_manager.computer.compute_legacy_result_for_filter(bi_aggregation_filter) # TODO: Cleanup the renderer to use a class registry for lookup renderer_class_name = html.request.var("renderer") if renderer_class_name == "FoldableTreeRendererTree": renderer_cls: Type[ABCFoldableTreeRenderer] = FoldableTreeRendererTree elif renderer_class_name == "FoldableTreeRendererBoxes": renderer_cls = FoldableTreeRendererBoxes elif renderer_class_name == "FoldableTreeRendererBottomUp": renderer_cls = FoldableTreeRendererBottomUp elif renderer_class_name == "FoldableTreeRendererTopDown": renderer_cls = FoldableTreeRendererTopDown else: raise NotImplementedError() renderer = renderer_cls(rows[0], omit_root=omit_root, expansion_level=config.user.bi_expansion_level, only_problems=only_problems, lazy=False) html.write(renderer.render())
def page(self) -> AjaxPageResult: aggregations_var = html.request.get_str_input_mandatory("aggregations", "[]") filter_names = json.loads(aggregations_var) forced_layout_id = html.request.var("layout_id") if forced_layout_id not in BILayoutManagement.get_all_bi_template_layouts(): forced_layout_id = None bi_aggregation_filter = BIAggregationFilter([], [], [], filter_names, [], []) results = bi.get_cached_bi_manager().computer.compute_result_for_filter( bi_aggregation_filter) aggregation_info: Dict[str, Any] = {"aggregations": {}} aggregation_layouts = BILayoutManagement.get_all_bi_aggregation_layouts() for bi_compiled_aggregation, node_result_bundles in results: for node_result_bundle in node_result_bundles: branch = node_result_bundle.instance aggr_name = branch.properties.title visual_mapper = NodeVisualizationBIDataMapper( is_single_host_aggregation=len(branch.get_required_hosts()) == 1) hierarchy = visual_mapper.consume(node_result_bundle) data: Dict[str, Any] = {} data["type"] = "bi" data["hierarchy"] = hierarchy data["groups"] = bi_compiled_aggregation.groups.names data["data_timestamp"] = int(time.time()) aggr_settings = bi_compiled_aggregation.aggregation_visualization layout: Dict[str, Any] = {"config": {}} if forced_layout_id: layout["enforced_id"] = aggr_name layout["origin_type"] = "globally_enforced" layout["origin_info"] = _("Globally enforced") layout["use_layout"] = BILayoutManagement.load_bi_template_layout( forced_layout_id) else: if aggr_name in aggregation_layouts: layout["origin_type"] = "explicit" layout["origin_info"] = _("Explicit set") layout["explicit_id"] = aggr_name layout["config"] = aggregation_layouts[aggr_name] layout["config"]["ignore_rule_styles"] = True else: layout.update(self._get_template_based_layout_settings(aggr_settings)) if "ignore_rule_styles" not in layout["config"]: layout["config"]["ignore_rule_styles"] = aggr_settings.get( "ignore_rule_styles", False) if "line_config" not in layout["config"]: layout["config"]["line_config"] = self._get_line_style_config(aggr_settings) data["layout"] = layout aggregation_info["aggregations"][aggr_name] = data html.set_output_format("json") return aggregation_info
def compute_bi_aggregation_filter(all_active_filters): only_hosts = [] only_group = [] only_service = [] only_aggr_name = [] group_prefix = [] for active_filter in all_active_filters: if active_filter.ident == "aggr_host": host_match = active_filter.value() if host_match: only_hosts = [host_match] elif active_filter.ident == "aggr_group": aggr_group = active_filter.selected_group() if aggr_group: only_group = [aggr_group] elif active_filter.ident == "aggr_service": # TODO: this is broken, in every version # service_spec: site_id, host, service service_spec = active_filter.service_spec() if service_spec: only_service = [service_spec] elif active_filter.ident == "aggr_name": aggr_name = active_filter.value().get("aggr_name") if aggr_name: only_aggr_name = [aggr_name] elif active_filter.ident == "aggr_group_tree": group_name = active_filter.value().get("aggr_group_tree") if group_name: group_prefix = [group_name] # BIAggregationFilter #("hosts", List[HostSpec]), #("services", List[Tuple[HostSpec, ServiceName]]), #("aggr_ids", List[str]), #("aggr_names", List[str]), #("aggr_groups", List[str]), #("aggr_paths", List[List[str]]), return BIAggregationFilter( only_hosts, # hosts only_service, # services [], # ids only_aggr_name, # names only_group, # groups group_prefix, # paths )
def api_get_aggregation_state(filter_names=None, filter_groups=None): """ returns the computed aggregation states """ with aggregation_compute_auto_cleanup(): bi_aggregation_filter = BIAggregationFilter([], [], [], filter_names or [], filter_groups or [], []) legacy_results = bi_computer.compute_legacy_result_for_filter( bi_aggregation_filter) used_aggr_names = set() filter_groups_set = set(filter_groups or []) modified_rows = [] for result in legacy_results: aggr_name = result["aggr_compiled_branch"].properties.title group_names = set(result["aggr_compiled_aggregation"].groups.names) if aggr_name in used_aggr_names: continue used_aggr_names.add(aggr_name) groups = list( group_names if filter_groups is None else group_names - filter_groups_set) del result["aggr_compiled_branch"] del result["aggr_compiled_aggregation"] modified_rows.append({"groups": groups, "tree": result}) have_sites = {x[0] for x in bi_status_fetcher.states.keys()} missing_aggregations = [] required_sites = set() required_aggregations = bi_computer.get_required_aggregations( bi_aggregation_filter) for _bi_aggregation, branches in required_aggregations: for branch in branches: branch_sites = {x[0] for x in branch.required_elements()} required_sites.update(branch_sites) if branch.properties.title not in used_aggr_names: missing_aggregations.append(branch.properties.title) response = { "missing_sites": list(required_sites - have_sites), "missing_aggr": missing_aggregations, "rows": modified_rows } return response
def api_get_aggregation_state( filter_names: Optional[List[str]] = None, filter_groups: Optional[List[str]] = None ): bi_manager = BIManager() bi_aggregation_filter = BIAggregationFilter( [], [], [], filter_names or [], filter_groups or [], [], ) def collect_infos(node_result_bundle: NodeResultBundle, is_single_host_aggregation: bool): actual_result = node_result_bundle.actual_result own_infos = {} if actual_result.custom_infos: own_infos["custom"] = actual_result.custom_infos if actual_result.state not in [BIStates.OK, BIStates.PENDING]: node_instance = node_result_bundle.instance line_tokens = [] if isinstance(node_instance, BICompiledRule): line_tokens.append(node_instance.properties.title) else: node_info = [] if not is_single_host_aggregation: node_info.append(node_instance.host_name) if node_instance.service_description: node_info.append(node_instance.service_description) if node_info: line_tokens.append("/".join(node_info)) if actual_result.output: line_tokens.append(actual_result.output) own_infos["error"] = {"state": actual_result.state, "output": ", ".join(line_tokens)} nested_infos = [ x for y in node_result_bundle.nested_results for x in [collect_infos(y, is_single_host_aggregation)] if x is not None ] if own_infos or nested_infos: return [own_infos, nested_infos] return None aggregations = {} results = bi_manager.computer.compute_result_for_filter(bi_aggregation_filter) for _compiled_aggregation, node_result_bundles in results: for node_result_bundle in node_result_bundles: aggr_title = node_result_bundle.instance.properties.title required_hosts = [x[1] for x in node_result_bundle.instance.get_required_hosts()] is_single_host_aggregation = len(required_hosts) == 1 aggregations[aggr_title] = { "state": node_result_bundle.actual_result.state, "hosts": required_hosts, "acknowledged": node_result_bundle.actual_result.acknowledged, "in_downtime": node_result_bundle.actual_result.downtime_state != 0, "in_service_period": node_result_bundle.actual_result.in_service_period, "infos": collect_infos(node_result_bundle, is_single_host_aggregation), } have_sites = {x[0] for x in bi_manager.status_fetcher.states} missing_aggregations = [] required_sites = set() required_aggregations = bi_manager.computer.get_required_aggregations(bi_aggregation_filter) for _bi_aggregation, branches in required_aggregations: for branch in branches: branch_sites = {x[0] for x in branch.required_elements()} required_sites.update(branch_sites) if branch.properties.title not in aggregations: missing_aggregations.append(branch.properties.title) response = { "aggregations": aggregations, "missing_sites": list(required_sites - have_sites), "missing_aggr": missing_aggregations, } return response
only_aggr_name = [aggr_name] elif active_filter.ident == "aggr_group_tree": if group_name := conf.get("aggr_group_tree"): group_prefix = [group_name] # BIAggregationFilter # ("hosts", List[HostName]), # ("services", List[Tuple[HostName, ServiceName]]), # ("aggr_ids", List[str]), # ("aggr_names", List[str]), # ("aggr_groups", List[str]), # ("aggr_paths", List[List[str]]), return BIAggregationFilter( only_hosts, # hosts only_service, # services [], # ids only_aggr_name, # names only_group, # groups group_prefix, # paths ) def table( context: VisualContext, columns: List[ColumnName], query: str, only_sites: OnlySites, limit: Optional[int], all_active_filters: Iterable[Filter], ) -> List[Dict]: bi_aggregation_filter = compute_bi_aggregation_filter(context, all_active_filters) bi_manager = BIManager()