[icinga-checkins] icinga.org: icinga2/feature/checkresult-5039: Refactor the Service:: Is{Comment, Downtime}* methods.

git at icinga.org git at icinga.org
Sat Nov 9 22:16:57 CET 2013


Module: icinga2
Branch: feature/checkresult-5039
Commit: 882d85c0b25fc68292b1603f2bd767169ec7242a
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=882d85c0b25fc68292b1603f2bd767169ec7242a

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Sat Nov  9 22:16:53 2013 +0100

Refactor the Service::Is{Comment,Downtime}* methods.

Refs #5039

---

 lib/icinga/comment.cpp          |    8 ++++++
 lib/icinga/comment.h            |    2 ++
 lib/icinga/downtime.cpp         |   34 ++++++++++++++++++++++++++
 lib/icinga/downtime.h           |    5 ++++
 lib/icinga/service-comment.cpp  |    9 +------
 lib/icinga/service-downtime.cpp |   51 +++++----------------------------------
 lib/icinga/service.h            |    6 -----
 7 files changed, 56 insertions(+), 59 deletions(-)

diff --git a/lib/icinga/comment.cpp b/lib/icinga/comment.cpp
index 895fcc4..b6e3f61 100644
--- a/lib/icinga/comment.cpp
+++ b/lib/icinga/comment.cpp
@@ -18,8 +18,16 @@
  ******************************************************************************/
 
 #include "icinga/comment.h"
+#include "base/utility.h"
 #include "base/dynamictype.h"
 
 using namespace icinga;
 
 REGISTER_TYPE(Comment);
+
+bool Comment::IsExpired(void) const
+{
+	double expire_time = GetExpireTime();
+
+	return (expire_time != 0 && expire_time < Utility::GetTime());
+}
diff --git a/lib/icinga/comment.h b/lib/icinga/comment.h
index a54a945..0e042c6 100644
--- a/lib/icinga/comment.h
+++ b/lib/icinga/comment.h
@@ -35,6 +35,8 @@ class I2_ICINGA_API Comment : public ObjectImpl<Comment>
 {
 public:
 	DECLARE_PTR_TYPEDEFS(Comment);
+
+	bool IsExpired(void) const;
 };
 
 }
diff --git a/lib/icinga/downtime.cpp b/lib/icinga/downtime.cpp
index 6361c3f..f6d6175 100644
--- a/lib/icinga/downtime.cpp
+++ b/lib/icinga/downtime.cpp
@@ -18,8 +18,42 @@
  ******************************************************************************/
 
 #include "icinga/downtime.h"
+#include "base/utility.h"
 #include "base/dynamictype.h"
 
 using namespace icinga;
 
 REGISTER_TYPE(Downtime);
+
+bool Downtime::IsActive(void) const
+{
+	double now = Utility::GetTime();
+
+	if (now < GetStartTime() ||
+		now > GetEndTime())
+		return false;
+
+	if (GetFixed())
+		return true;
+
+	double triggerTime = GetTriggerTime();
+
+	if (triggerTime == 0)
+		return false;
+
+	return (triggerTime + GetDuration() < now);
+}
+
+bool Downtime::IsTriggered(void) const
+{
+	double now = Utility::GetTime();
+
+	double triggerTime = GetTriggerTime();
+
+	return (triggerTime > 0 && triggerTime <= now);
+}
+
+bool Downtime::IsExpired(void) const
+{
+	return (GetEndTime() < Utility::GetTime());
+}
diff --git a/lib/icinga/downtime.h b/lib/icinga/downtime.h
index d67a2b9..0f702bb 100644
--- a/lib/icinga/downtime.h
+++ b/lib/icinga/downtime.h
@@ -35,6 +35,11 @@ class I2_ICINGA_API Downtime : public ObjectImpl<Downtime>
 {
 public:
 	DECLARE_PTR_TYPEDEFS(Downtime);
+
+	bool IsActive(void) const;
+	bool IsTriggered(void) const;
+	bool IsExpired(void) const;
+
 };
 
 }
diff --git a/lib/icinga/service-comment.cpp b/lib/icinga/service-comment.cpp
index 89b8c28..e07f2a0 100644
--- a/lib/icinga/service-comment.cpp
+++ b/lib/icinga/service-comment.cpp
@@ -163,13 +163,6 @@ Comment::Ptr Service::GetCommentByID(const String& id)
 	return Comment::Ptr();
 }
 
-bool Service::IsCommentExpired(const Comment::Ptr& comment)
-{
-	double expire_time = comment->GetExpireTime();
-
-	return (expire_time != 0 && expire_time < Utility::GetTime());
-}
-
 void Service::AddCommentsToCache(void)
 {
 	Log(LogDebug, "icinga", "Updating Service comments cache.");
@@ -227,7 +220,7 @@ void Service::RemoveExpiredComments(void)
 		String id;
 		Comment::Ptr comment;
 		BOOST_FOREACH(boost::tie(id, comment), comments) {
-			if (IsCommentExpired(comment))
+			if (comment->IsExpired())
 				expiredComments.push_back(id);
 		}
 	}
diff --git a/lib/icinga/service-downtime.cpp b/lib/icinga/service-downtime.cpp
index 1c983d5..31a06f5 100644
--- a/lib/icinga/service-downtime.cpp
+++ b/lib/icinga/service-downtime.cpp
@@ -163,26 +163,20 @@ void Service::TriggerDowntime(const String& id)
 	if (!downtime)
 		return;
 
-	if (IsDowntimeActive(downtime) && IsDowntimeTriggered(downtime)) {
+	if (downtime->IsActive() && downtime->IsTriggered()) {
 		Log(LogDebug, "icinga", "Not triggering downtime with ID '" + Convert::ToString(downtime->GetLegacyId()) + "': already triggered.");
 		return;
 	}
 
-	if (IsDowntimeExpired(downtime)) {
+	if (downtime->IsExpired()) {
 		Log(LogDebug, "icinga", "Not triggering downtime with ID '" + Convert::ToString(downtime->GetLegacyId()) + "': expired.");
 		return;
 	}
 
-	double now = Utility::GetTime();
-
-	if (now < downtime->GetStartTime() ||
-	    now > downtime->GetEndTime())
-		return;
-
 	Log(LogDebug, "icinga", "Triggering downtime with ID '" + Convert::ToString(downtime->GetLegacyId()) + "'.");
 
 	if (downtime->GetTriggerTime() == 0)
-		downtime->SetTriggerTime(now);
+		downtime->SetTriggerTime(Utility::GetTime());
 
 	Dictionary::Ptr triggers = downtime->GetTriggers();
 	ObjectLock olock(triggers);
@@ -227,39 +221,6 @@ Downtime::Ptr Service::GetDowntimeByID(const String& id)
 	return Downtime::Ptr();
 }
 
-bool Service::IsDowntimeActive(const Downtime::Ptr& downtime)
-{
-	double now = Utility::GetTime();
-
-	if (now < downtime->GetStartTime() ||
-	    now > downtime->GetEndTime())
-		return false;
-
-	if (downtime->GetFixed())
-		return true;
-
-	double triggerTime = downtime->GetTriggerTime();
-
-	if (triggerTime == 0)
-		return false;
-
-	return (triggerTime + downtime->GetDuration() < now);
-}
-
-bool Service::IsDowntimeTriggered(const Downtime::Ptr& downtime)
-{
-	double now = Utility::GetTime();
-
-	double triggerTime = downtime->GetTriggerTime();
-
-	return (triggerTime > 0 && triggerTime <= now);
-}
-
-bool Service::IsDowntimeExpired(const Downtime::Ptr& downtime)
-{
-	return (downtime->GetEndTime() < Utility::GetTime());
-}
-
 void Service::StartDowntimesExpiredTimer(void)
 {
         if (!l_DowntimesExpireTimer) {
@@ -305,7 +266,7 @@ void Service::RemoveExpiredDowntimes(void)
 		String id;
 		Downtime::Ptr downtime;
 		BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
-			if (IsDowntimeExpired(downtime))
+			if (downtime->IsExpired())
 				expiredDowntimes.push_back(id);
 		}
 	}
@@ -330,7 +291,7 @@ bool Service::IsInDowntime(void) const
 
 	Downtime::Ptr downtime;
 	BOOST_FOREACH(boost::tie(boost::tuples::ignore, downtime), downtimes) {
-		if (Service::IsDowntimeActive(downtime))
+		if (downtime->IsActive())
 			return true;
 	}
 
@@ -346,7 +307,7 @@ int Service::GetDowntimeDepth(void) const
 
 	Downtime::Ptr downtime;
 	BOOST_FOREACH(boost::tie(boost::tuples::ignore, downtime), downtimes) {
-		if (Service::IsDowntimeActive(downtime))
+		if (downtime->IsActive())
 			downtime_depth++;
 	}
 
diff --git a/lib/icinga/service.h b/lib/icinga/service.h
index 3a7cb01..1ae8fca 100644
--- a/lib/icinga/service.h
+++ b/lib/icinga/service.h
@@ -197,10 +197,6 @@ public:
 	static Service::Ptr GetOwnerByDowntimeID(const String& id);
 	static Downtime::Ptr GetDowntimeByID(const String& id);
 
-	static bool IsDowntimeActive(const Downtime::Ptr& downtime);
-	static bool IsDowntimeTriggered(const Downtime::Ptr& downtime);
-	static bool IsDowntimeExpired(const Downtime::Ptr& downtime);
-
 	void StartDowntimesExpiredTimer(void);
 
 	bool IsInDowntime(void) const;
@@ -220,8 +216,6 @@ public:
 	static Service::Ptr GetOwnerByCommentID(const String& id);
 	static Comment::Ptr GetCommentByID(const String& id);
 
-	static bool IsCommentExpired(const Comment::Ptr& comment);
-
 	/* Notifications */
 	bool GetEnableNotifications(void) const;
 	void SetEnableNotifications(bool enabled, const String& authority = String());





More information about the icinga-checkins mailing list