Exemple #1
0
    def graph_identification(self, context: VisualContext) -> GraphIdentifier:
        single_context = get_singlecontext_vars(context, self.single_infos())
        host = single_context.get("host")
        if not host:
            raise MKUserError("host", _("Missing needed host parameter."))

        service = single_context.get("service")
        if not service:
            service = "_HOST_"

        site = get_only_sites_from_context(context) or self._resolve_site(host)
        if isinstance(site, list):
            site = "".join(site)

        # source changed from int (n'th graph) to the graph id in 2.0.0b6, but we cannot transform this, so we have to
        # handle this here
        raw_source = self._dashlet_spec["source"]
        if isinstance(raw_source, int):
            graph_spec = TemplateGraphSpec({
                "site": site,
                "host_name": host,
                "service_description": service,
                "graph_index": raw_source - 1,
            })
        else:
            graph_spec = TemplateGraphSpec({
                "site": site,
                "host_name": host,
                "service_description": service,
                "graph_id": raw_source,
            })

        return ("template", graph_spec)
def _sorted_unique_lq(query: str, limit: int, value: str,
                      params: Dict) -> Choices:
    """Livestatus query of single column of unique elements.
    Prepare dropdown choices"""
    selected_sites = get_only_sites_from_context(params.get("context", {}))
    with sites.only_sites(selected_sites), sites.set_limit(limit):
        choices = [(h, h)
                   for h in sorted(sites.live().query_column_unique(query),
                                   key=lambda h: h.lower())]

    if len(choices) > limit:
        choices.insert(
            0, (None, _("(Max suggestions reached, be more specific)")))

    if (value, value) not in choices and params["strict"] == "False":
        choices.insert(
            0, (value, value))  # User is allowed to enter anything they want
    return choices
Exemple #3
0
def __live_query_to_choices(
    query_callback: Callable[[MultiSiteConnection],
                             Collection[LivestatusColumn]],
    limit: int,
    value: str,
    params: Dict,
) -> Choices:
    selected_sites = get_only_sites_from_context(params.get("context", {}))
    with sites.only_sites(selected_sites), sites.set_limit(limit):
        query_result = query_callback(sites.live())
        choices = [(h, h)
                   for h in sorted(query_result, key=lambda h: h.lower())]

    if len(choices) > limit:
        choices.insert(
            0, (None, _("(Max suggestions reached, be more specific)")))

    if (value, value) not in choices and params["strict"] is False:
        choices.insert(
            0, (value, value))  # User is allowed to enter anything they want
    return choices
def test_get_only_sites_from_context(context, result):
    assert get_only_sites_from_context(context) == result