From 4e7c43c8c82c46667b2df794df36bf65ec7a066c Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 15 Mar 2013 09:54:06 +0100 Subject: [PATCH] TimePeriod update function should return an array of time segments Fixes #3857 --- lib/icinga/timeperiod.cpp | 37 +++++++++++++++++++++++++------------ lib/icinga/timeperiod.h | 9 +++++---- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/icinga/timeperiod.cpp b/lib/icinga/timeperiod.cpp index a48917be6..8352b19bb 100644 --- a/lib/icinga/timeperiod.cpp +++ b/lib/icinga/timeperiod.cpp @@ -104,6 +104,11 @@ void TimePeriod::AddSegment(double begin, double end) Touch("segments"); } +void TimePeriod::AddSegment(const Dictionary::Ptr& segment) +{ + AddSegment(segment->Get("begin"), segment->Get("end")); +} + void TimePeriod::RemoveSegment(double begin, double end) { ASSERT(OwnsLock()); @@ -199,7 +204,16 @@ void TimePeriod::UpdateRegion(double begin, double end) } task->Start(); - task->GetResult(); + Array::Ptr segments = task->GetResult(); + + { + ObjectLock olock(this); + RemoveSegment(begin, end); + + BOOST_FOREACH(const Dictionary::Ptr& segment, segments) { + AddSegment(segment); + } + } } bool TimePeriod::IsInside(double ts) const @@ -274,10 +288,8 @@ void TimePeriod::EmptyTimePeriodUpdate(const ScriptTask::Ptr& task, const vector double begin = arguments[1]; double end = arguments[2]; - ObjectLock olock(tp); - tp->RemoveSegment(begin, end); - - task->FinishResult(Empty); + Array::Ptr segments = boost::make_shared(); + task->FinishResult(segments); } void TimePeriod::EvenMinutesTimePeriodUpdate(const ScriptTask::Ptr& task, const vector& arguments) @@ -289,16 +301,17 @@ void TimePeriod::EvenMinutesTimePeriodUpdate(const ScriptTask::Ptr& task, const double begin = arguments[1]; double end = arguments[2]; - { - ObjectLock olock(tp); + Array::Ptr segments = boost::make_shared(); - tp->RemoveSegment(begin, end); + for (long t = begin; t < end; t += 60) { + if ((t / 60) % 2 == 0) { + Dictionary::Ptr segment = boost::make_shared(); + segment->Set("begin", t); + segment->Set("end", t + 60); - for (long t = begin; t < end; t += 60) { - if ((t / 60) % 2 == 0) - tp->AddSegment(t, t + 60); + segments->Add(segment); } } - task->FinishResult(Empty); + task->FinishResult(segments); } diff --git a/lib/icinga/timeperiod.h b/lib/icinga/timeperiod.h index d27ee4298..5beb1a024 100644 --- a/lib/icinga/timeperiod.h +++ b/lib/icinga/timeperiod.h @@ -40,10 +40,6 @@ public: virtual void Start(void); - void AddSegment(double s, double end); - void RemoveSegment(double begin, double end); - void PurgeSegments(double end); - void UpdateRegion(double begin, double end); bool IsInside(double ts) const; @@ -57,6 +53,11 @@ private: Attribute m_ValidEnd; Attribute m_Segments; + void AddSegment(double s, double end); + void AddSegment(const Dictionary::Ptr& segment); + void RemoveSegment(double begin, double end); + void PurgeSegments(double end); + static Timer::Ptr m_UpdateTimer; static void UpdateTimerHandler(void); }; -- 2.40.0