Module: check_mk
Branch: master
Commit: e2a1e6c1edc77677dd3d0a83bbb1d05597c8d6f1
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e2a1e6c1edc776…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Sep 20 13:23:02 2018 +0200
Configuring equal times for all weekdays is now easier
Instead of setting the same time ranges for each weekday it is now
possible to select "Same times for all week days". Then it is only
needed to configure the time range once for all.
CMK-522
Change-Id: Ibc83f9d42968be90ab8a125e86d40623779dd289
---
cmk/gui/wato/pages/timeperiods.py | 77 ++++++++++++++++++++++++++++++++-------
1 file changed, 64 insertions(+), 13 deletions(-)
diff --git a/cmk/gui/wato/pages/timeperiods.py b/cmk/gui/wato/pages/timeperiods.py
index cce9403..696c3d8 100644
--- a/cmk/gui/wato/pages/timeperiods.py
+++ b/cmk/gui/wato/pages/timeperiods.py
@@ -55,6 +55,7 @@ from cmk.gui.valuespec import (
ValueSpec,
TimeofdayRange,
ListChoice,
+ CascadingDropdown,
)
from cmk.gui.plugins.wato import (
@@ -679,15 +680,7 @@ class ModeEditTimeperiod(WatoMode):
size = 80,
validate = self._validate_alias,
)),
- ("weekdays", Dictionary(
- title = _("Weekdays"),
- help = _("For each weekday you can setup no, one or several
"
- "time ranges in the format <tt>23:39</tt>,
in which the time period "
- "should be active."),
- elements = self._weekday_elements(),
- optional_keys = None,
- indent = False,
- )),
+ ("weekdays", self._vs_weekdays()),
("exceptions", self._vs_exceptions()),
("exclude", self._vs_exclude()),
],
@@ -709,6 +702,24 @@ class ModeEditTimeperiod(WatoMode):
raise MKUserError("alias", message)
+ def _vs_weekdays(self):
+ return CascadingDropdown(
+ title = _("Active time range"),
+ help = _("For each weekday you can setup no, one or several "
+ "time ranges in the format <tt>23:39</tt>, in which
the time period "
+ "should be active."),
+ choices = [
+ ("whole_week", _("Same times for all weekdays"),
MultipleTimeRanges(
+ )),
+ ("day_specific", _("Weekday specific times"),
Dictionary(
+ elements = self._weekday_elements(),
+ optional_keys = None,
+ indent = False,
+ )),
+ ],
+ )
+
+
def _weekday_elements(self):
elements = []
for tp_id, tp_title in cmk.defines.weekdays_by_name():
@@ -851,8 +862,7 @@ class ModeEditTimeperiod(WatoMode):
vs_spec = {
"name": self._name,
"alias": tp_spec.get("alias", ""),
- "weekdays": { day: map(self._time_range_to_valuespec,
tp_spec.get(day, []))
- for day in defines.weekday_ids() },
+ "weekdays": self._weekdays_to_valuespec(tp_spec),
"exclude": tp_spec.get("exclude", []),
"exceptions": sorted(exceptions),
}
@@ -860,6 +870,22 @@ class ModeEditTimeperiod(WatoMode):
return vs_spec
+ def _weekdays_to_valuespec(self, tp_spec):
+ if self._has_same_time_specs_during_whole_week(tp_spec):
+ return ("whole_week", map(self._time_range_to_valuespec,
tp_spec.get("monday", [])))
+
+ return ("day_specific", { day: map(self._time_range_to_valuespec,
tp_spec.get(day, []))
+ for day in defines.weekday_ids() })
+
+
+ def _has_same_time_specs_during_whole_week(self, tp_spec):
+ """Put the time ranges of all weekdays into a set to reduce the
duplicates to see whether
+ or not all days have the same time spec and return True if they have the
same."""
+ unified_time_ranges = set([ tuple(tp_spec.get(day, []))
+ for day in defines.weekday_ids() ])
+ return len(unified_time_ranges) == 1
+
+
def _time_range_to_valuespec(self, time_range):
"""Convert a time range specification to valuespec format
e.g. ("00:30", "10:17") ->
((0,30),(10,17))"""
@@ -880,10 +906,35 @@ class ModeEditTimeperiod(WatoMode):
if vs_spec["exclude"]:
tp_spec["exclude"] = vs_spec["exclude"]
- for day, time_ranges in vs_spec["weekdays"].items() +
vs_spec["exceptions"]:
+ tp_spec.update(self._exceptions_from_valuespec(vs_spec))
+ tp_spec.update(self._weekdays_from_valuespec(vs_spec))
+
+ return tp_spec
+
+
+ def _exceptions_from_valuespec(self, vs_spec):
+ tp_spec = {}
+ for exception_name, time_ranges in vs_spec["exceptions"]:
if time_ranges:
- tp_spec[day] = map(self._time_range_from_valuespec, time_ranges)
+ tp_spec[exception_name] = map(self._time_range_from_valuespec,
time_ranges)
+ return tp_spec
+
+
+ def _weekdays_from_valuespec(self, vs_spec):
+ weekday_ty, weekday_values = vs_spec["weekdays"]
+ if weekday_ty == "whole_week":
+ # produce a data structure equal to the "day_specific" structure
+ weekday_values = { day: weekday_values for day in defines.weekday_ids() }
+
+ elif weekday_ty != "day_specific":
+ raise NotImplementedError()
+
+ tp_spec = {}
+ for day in defines.weekday_ids():
+ time_specs = map(self._time_range_from_valuespec, weekday_values[day])
+ if time_specs:
+ tp_spec[day] = time_specs
return tp_spec