Module: check_mk
Branch: master
Commit: 46fa60bdb147906f6cb31eeff789afe2e1787ec3
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=46fa60bdb14790…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Mar 4 14:05:59 2019 +0100
Moved only_sites handling to View()
Change-Id: I036c3964293c277bfb65884fb8db9eba7cfc242a
---
cmk/gui/mobile.py | 2 ++
cmk/gui/plugins/dashboard/view.py | 1 +
cmk/gui/views.py | 44 ++++++++++++++++++++++++++-------------
tests/unit/cmk/gui/test_views.py | 9 ++++++++
4 files changed, 42 insertions(+), 14 deletions(-)
diff --git a/cmk/gui/mobile.py b/cmk/gui/mobile.py
index e872aa8..3a06fcc 100644
--- a/cmk/gui/mobile.py
+++ b/cmk/gui/mobile.py
@@ -237,6 +237,7 @@ def page_index():
if view_spec.get("mobile") and not view_spec.get("hidden"):
view = views.View(view_name, view_spec)
view.row_limit = views.get_limit()
+ view.only_sites = views.get_only_sites()
url = "mobile_view.py?view_name=%s" % view_name
count = ""
@@ -284,6 +285,7 @@ def page_view():
view = views.View(view_name, view_spec)
view.row_limit = views.get_limit()
+ view.only_sites = views.get_only_sites()
title = views.view_title(view_spec)
mobile_html_head(title)
diff --git a/cmk/gui/plugins/dashboard/view.py b/cmk/gui/plugins/dashboard/view.py
index e6c0e93..f80f1f7 100644
--- a/cmk/gui/plugins/dashboard/view.py
+++ b/cmk/gui/plugins/dashboard/view.py
@@ -94,6 +94,7 @@ class ViewDashlet(IFrameDashlet):
view = views.View(self._dashlet_spec["name"], self._dashlet_spec)
view.row_limit = views.get_limit()
+ view.only_sites = views.get_only_sites()
view_renderer = views.GUIViewRenderer(view, show_buttons=False)
views.show_view(view, view_renderer)
diff --git a/cmk/gui/views.py b/cmk/gui/views.py
index 2185956..d5b68b3 100644
--- a/cmk/gui/views.py
+++ b/cmk/gui/views.py
@@ -31,7 +31,7 @@ import pprint
import traceback
import json
from collections import namedtuple
-from typing import Dict # pylint: disable=unused-import
+from typing import Dict, Optional, List # pylint: disable=unused-import
import livestatus
@@ -210,6 +210,7 @@ class View(object):
self.name = view_name
self.spec = view_spec
self._row_limit = None # type: Optional[int]
+ self._only_sites = None # type: Optional[List[str]]
@property
def datasource(self):
@@ -238,6 +239,19 @@ class View(object):
def row_limit(self, row_limit):
self._row_limit = row_limit
+ @property
+ def only_sites(self):
+ """Optional list of sites to query instead of all sites
+
+ This is a performance feature. It is highly recommended to set the only_sites
attribute
+ whenever it is possible. In the moment it is set a livestatus query is not sent
to all
+ sites anymore but only to the given list of sites."""
+ return self._only_sites
+
+ @only_sites.setter
+ def only_sites(self, only_sites):
+ self._only_sites = only_sites
+
class ViewRenderer(object):
__metaclass__ = abc.ABCMeta
@@ -1128,6 +1142,7 @@ def page_view():
view = View(view_name, view_spec)
view.row_limit = get_limit()
+ view.only_sites = get_only_sites()
# Gather the page context which is needed for the "add to visual" popup
menu
# to add e.g. views to dashboards or reports
@@ -1217,12 +1232,6 @@ def show_view(view, view_renderer, only_count=False):
continue
filterheaders += header
- # Apply the site hint / filter
- if html.request.var("site"):
- only_sites = [html.request.var("site")]
- else:
- only_sites = None
-
# Fork to availability view. We just need the filter headers, since we do not query
the normal
# hosts and service table, but "statehist". This is *not* true for BI
availability, though (see later)
if html.request.var("mode") == "availability" and
("aggr" not in view.datasource.infos or
@@ -1232,7 +1241,7 @@ def show_view(view, view_renderer, only_count=False):
context.update(visuals.get_singlecontext_html_vars(view.spec))
return cmk.gui.plugins.views.availability.render_availability_page(
- view.spec, view.datasource, context, filterheaders, only_sites,
view.row_limit)
+ view.spec, view.datasource, context, filterheaders, view.only_sites,
view.row_limit)
query = filterheaders + view.spec.get("add_headers", "")
@@ -1259,13 +1268,12 @@ def show_view(view, view_renderer, only_count=False):
# Fetch data. Some views show data only after pressing [Search]
if (only_count or (not view.spec.get("mustsearch")) or
html.request.var("filled_in") in ["filter",
'actions', 'confirm', 'painteroptions']):
- rows = view.datasource.table.query(view, columns, query, only_sites,
view.row_limit,
+ rows = view.datasource.table.query(view, columns, query, view.only_sites,
view.row_limit,
all_active_filters)
# Now add join information, if there are join columns
if join_cells:
- _do_table_join(view.datasource, rows, filterheaders, join_cells,
join_columns,
- only_sites)
+ _do_table_join(view, rows, filterheaders, join_cells, join_columns)
# If any painter, sorter or filter needs the information about the host's
# inventory, then we load it and attach it as column "host_inventory"
@@ -1458,8 +1466,8 @@ def columns_of_cells(cells):
return columns
-def _do_table_join(master_ds, master_rows, master_filters, join_cells, join_columns,
only_sites):
- join_table, join_master_column = master_ds.join
+def _do_table_join(view, master_rows, master_filters, join_cells, join_columns):
+ join_table, join_master_column = view.datasource.join
slave_ds = data_source_registry[join_table]()
join_slave_column = slave_ds.join_key
@@ -1474,7 +1482,7 @@ def _do_table_join(master_ds, master_rows, master_filters,
join_cells, join_colu
datasource=slave_ds,
columns=[join_master_column, join_slave_column] + join_columns,
add_headers=query,
- only_sites=only_sites,
+ only_sites=view.only_sites,
limit=None)
per_master_entry = {}
current_key = None
@@ -1537,6 +1545,14 @@ def play_alarm_sounds():
break # only one sound at one time
+def get_only_sites():
+ # type: () -> Optional[List[str]]
+ """Is the view limited to specific sites by
request?"""
+ if html.request.var("site"):
+ return [html.request.var("site")]
+ return None
+
+
def get_limit():
"""How many data rows may the user query?"""
limitvar = html.request.var("limit", "soft")
diff --git a/tests/unit/cmk/gui/test_views.py b/tests/unit/cmk/gui/test_views.py
index a1f84e0..23c9f94 100644
--- a/tests/unit/cmk/gui/test_views.py
+++ b/tests/unit/cmk/gui/test_views.py
@@ -5826,3 +5826,12 @@ def test_gui_view_row_limit(register_builtin_html, monkeypatch,
limit, permissio
for perm in permissions:
monkeypatch.setitem(config.user.permissions, perm, True)
assert cmk.gui.views.get_limit() == result
+
+def test_view_only_sites(register_builtin_html, load_view_plugins):
+ view_name = "allhosts"
+ view_spec = cmk.gui.views.multisite_builtin_views[view_name]
+
+ view = cmk.gui.views.View(view_name, view_spec)
+ assert view.only_sites is None
+ view.only_sites = ["unit"]
+ assert view.only_sites == ["unit"]