From f6f5fd5ddafa317a6d3f6b4220742134918d3433 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 26 Feb 2020 15:42:03 +0100 Subject: [PATCH] TimePeriod: consult GetIncludes() and GetExcludes() in IsInside(), not UpdateRegion() refs #7398 --- lib/icinga/timeperiod.cpp | 77 +++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/lib/icinga/timeperiod.cpp b/lib/icinga/timeperiod.cpp index 0cf7fe1d2..c70227f94 100644 --- a/lib/icinga/timeperiod.cpp +++ b/lib/icinga/timeperiod.cpp @@ -233,43 +233,13 @@ void TimePeriod::UpdateRegion(double begin, double end, bool clearExisting) Array::Ptr segments = GetUpdate()->Invoke({ this, begin, end }); - { - ObjectLock olock(this); - RemoveSegment(begin, end); + ObjectLock olock(this); + RemoveSegment(begin, end); - if (segments) { - ObjectLock dlock(segments); - for (const Dictionary::Ptr& segment : segments) { - AddSegment(segment); - } - } - } - - bool preferInclude = GetPreferIncludes(); - - /* First handle the non preferred timeranges */ - Array::Ptr timeranges = preferInclude ? GetExcludes() : GetIncludes(); - - if (timeranges) { - ObjectLock olock(timeranges); - for (const String& name : timeranges) { - const TimePeriod::Ptr timeperiod = TimePeriod::GetByName(name); - - if (timeperiod) - Merge(timeperiod, !preferInclude); - } - } - - /* Preferred timeranges must be handled at the end */ - timeranges = preferInclude ? GetIncludes() : GetExcludes(); - - if (timeranges) { - ObjectLock olock(timeranges); - for (const String& name : timeranges) { - const TimePeriod::Ptr timeperiod = TimePeriod::GetByName(name); - - if (timeperiod) - Merge(timeperiod, preferInclude); + if (segments) { + ObjectLock dlock(segments); + for (const Dictionary::Ptr& segment : segments) { + AddSegment(segment); } } } @@ -279,10 +249,45 @@ bool TimePeriod::GetIsInside() const return IsInside(Utility::GetTime()); } +static bool IsInsideTimePeriods(double ts, Array::Ptr tps) +{ + if (tps) { + ObjectLock oLock (tps); + + for (const String& name : tps) { + auto timeperiod (TimePeriod::GetByName(name)); + + if (timeperiod && timeperiod->IsInside(ts)) { + return true; + } + } + } + + return false; +} + bool TimePeriod::IsInside(double ts) const { ObjectLock olock(this); + if (GetPreferIncludes()) { + if (IsInsideTimePeriods(ts, GetIncludes())) { + return true; + } + + if (IsInsideTimePeriods(ts, GetExcludes())) { + return false; + } + } else { + if (IsInsideTimePeriods(ts, GetExcludes())) { + return false; + } + + if (IsInsideTimePeriods(ts, GetIncludes())) { + return true; + } + } + if (GetValidBegin().IsEmpty() || ts < GetValidBegin() || GetValidEnd().IsEmpty() || ts > GetValidEnd()) return true; /* Assume that all invalid regions are "inside". */