-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not modify the global sorter_registry with config based tag sorters
The registries are global states and must not be modified by the request processing logic. Doing so can lead to inconsistent situations and lead to race conditions in threaded environments. Change-Id: I042a828fa716d76ce3d35c965a90493bcf7bb6c3
- Loading branch information
1 parent
1c6f4be
commit 89c613b
Showing
8 changed files
with
107 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2 | ||
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and | ||
# conditions defined in the file COPYING, which is part of this source code package. | ||
|
||
"""Dynamic sorters based on the site configuration""" | ||
|
||
from collections.abc import Mapping, Sequence | ||
from functools import partial | ||
|
||
from cmk.utils.tags import TagGroup, TagGroupID | ||
|
||
from cmk.gui.config import Config | ||
from cmk.gui.http import Request | ||
from cmk.gui.i18n import _ | ||
from cmk.gui.painter.v0.helpers import get_tag_groups | ||
from cmk.gui.type_defs import Row | ||
|
||
from .base import Sorter | ||
|
||
|
||
def host_tag_config_based_sorters(tag_groups: Sequence[TagGroup]) -> dict[str, Sorter]: | ||
return { | ||
(ident := f"host_tag_{tag_group.id}"): Sorter( | ||
ident=ident, | ||
title=_("Host tag:") + " " + tag_group.title, | ||
columns=["host_tags"], | ||
load_inv=False, | ||
sort_function=partial(_cmp_host_tag, tag_group_id=tag_group.id), | ||
) | ||
for tag_group in tag_groups | ||
} | ||
|
||
|
||
def _cmp_host_tag( | ||
r1: Row, | ||
r2: Row, | ||
*, | ||
parameters: Mapping[str, object] | None, | ||
config: Config, | ||
request: Request, | ||
tag_group_id: TagGroupID, | ||
) -> int: | ||
host_tag_1 = _get_tag_group_value(r1, "host", tag_group_id, config=config) | ||
host_tag_2 = _get_tag_group_value(r2, "host", tag_group_id, config=config) | ||
return (host_tag_1 > host_tag_2) - (host_tag_1 < host_tag_2) | ||
|
||
|
||
def _get_tag_group_value(row: Row, what: str, tag_group_id: TagGroupID, *, config: Config) -> str: | ||
tag_id = get_tag_groups(row, what).get(tag_group_id) | ||
|
||
tag_group = config.tags.get_tag_group(tag_group_id) | ||
if tag_group: | ||
label = dict(tag_group.get_tag_choices()).get(tag_id, _("N/A")) | ||
else: | ||
label = tag_id or _("N/A") | ||
|
||
return label or _("N/A") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.