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 #2
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
Exemple #3
0
    def autocomplete_choices(cls, value: str, params: Dict) -> Choices:
        """Return the matching list of dropdown choices
        Called by the webservice with the current input field value and the
        completions_params to get the list of choices"""
        if not (params.get("host") or params.get("service")):
            choices: Iterable[TupleType[str, str]] = ((
                graph_id,
                graph_details.get(
                    "title",
                    graph_id,
                ),
            ) for graph_id, graph_details in graph_info.items())

        else:
            query = "\n".join([
                "GET services",
                "Columns: perf_data metrics check_command",
            ] + [
                f"Filter: {filter_name} = {livestatus.lqencode(filter_value)}"
                for filter_name, filter_value in (
                    ("host_name", params.get("host")),
                    ("service_description", params.get("service")),
                ) if filter_value
            ])
            with sites.set_limit(None):
                choices = set(
                    chain.from_iterable(
                        cls._graph_choices_from_livestatus_row(
                            perf_data,
                            metrics,
                            check_cmd,
                        ) for perf_data, metrics, check_cmd in
                        sites.live().query(query)))

        val_lower = value.lower()
        return sorted(
            (choice for choice in choices if val_lower in choice[1].lower()),
            key=lambda tuple_id_title: tuple_id_title[1],
        )