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

Refs #5039
This commit is contained in:
Gunnar Beutner 2013-11-09 22:16:53 +01:00
parent ec1a7bbfcb
commit e2292ce5f7
11 changed files with 70 additions and 73 deletions

View File

@ -78,7 +78,7 @@ void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner,
String id;
Comment::Ptr comment;
BOOST_FOREACH(boost::tie(id, comment), comments) {
if (Service::IsCommentExpired(comment))
if (comment->IsExpired())
continue;
if (type == CompatTypeHost)
@ -177,7 +177,7 @@ void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner
String id;
Downtime::Ptr downtime;
BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
if (Service::IsDowntimeExpired(downtime))
if (downtime->IsExpired())
continue;
if (type == CompatTypeHost)
@ -199,7 +199,7 @@ void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner
<< "\t" << "triggered_by=" << triggeredByLegacy << "\n"
<< "\t" << "fixed=" << static_cast<long>(downtime->GetFixed()) << "\n"
<< "\t" << "duration=" << static_cast<long>(downtime->GetDuration()) << "\n"
<< "\t" << "is_in_effect=" << (Service::IsDowntimeActive(downtime) ? 1 : 0) << "\n"
<< "\t" << "is_in_effect=" << (downtime->IsActive() ? 1 : 0) << "\n"
<< "\t" << "author=" << downtime->GetAuthor() << "\n"
<< "\t" << "comment=" << downtime->GetComment() << "\n"
<< "\t" << "trigger_time=" << downtime->GetTriggerTime() << "\n"

View File

@ -108,7 +108,7 @@ Value DowntimesTable::TypeAccessor(const Value& row)
{
Downtime::Ptr downtime = Service::GetDowntimeByID(row);
// 1 .. active, 0 .. pending
return (Service::IsDowntimeActive(downtime) ? 1 : 0);
return (downtime->IsActive() ? 1 : 0);
}
Value DowntimesTable::IsServiceAccessor(const Value& row)

View File

@ -1425,7 +1425,7 @@ Value HostsTable::DowntimesAccessor(const Value& row)
if (!downtime)
continue;
if (Service::IsDowntimeExpired(downtime))
if (downtime->IsExpired())
continue;
ids->Add(downtime->GetLegacyId());
@ -1460,7 +1460,7 @@ Value HostsTable::DowntimesWithInfoAccessor(const Value& row)
if (!downtime)
continue;
if (Service::IsDowntimeExpired(downtime))
if (downtime->IsExpired())
continue;
Array::Ptr downtime_info = make_shared<Array>();
@ -1499,7 +1499,7 @@ Value HostsTable::CommentsAccessor(const Value& row)
if (!comment)
continue;
if (Service::IsCommentExpired(comment))
if (comment->IsExpired())
continue;
ids->Add(comment->GetLegacyId());
@ -1534,7 +1534,7 @@ Value HostsTable::CommentsWithInfoAccessor(const Value& row)
if (!comment)
continue;
if (Service::IsCommentExpired(comment))
if (comment->IsExpired())
continue;
Array::Ptr comment_info = make_shared<Array>();
@ -1573,7 +1573,7 @@ Value HostsTable::CommentsWithExtraInfoAccessor(const Value& row)
if (!comment)
continue;
if (Service::IsCommentExpired(comment))
if (comment->IsExpired())
continue;
Array::Ptr comment_info = make_shared<Array>();

View File

@ -1050,7 +1050,7 @@ Value ServicesTable::DowntimesAccessor(const Value& row)
if (!downtime)
continue;
if (Service::IsDowntimeExpired(downtime))
if (downtime->IsExpired())
continue;
ids->Add(downtime->GetLegacyId());
@ -1079,7 +1079,7 @@ Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
if (!downtime)
continue;
if (Service::IsDowntimeExpired(downtime))
if (downtime->IsExpired())
continue;
Array::Ptr downtime_info = make_shared<Array>();
@ -1112,7 +1112,7 @@ Value ServicesTable::CommentsAccessor(const Value& row)
if (!comment)
continue;
if (Service::IsCommentExpired(comment))
if (comment->IsExpired())
continue;
ids->Add(comment->GetLegacyId());
@ -1141,7 +1141,7 @@ Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
if (!comment)
continue;
if (Service::IsCommentExpired(comment))
if (comment->IsExpired())
continue;
Array::Ptr comment_info = make_shared<Array>();
@ -1174,7 +1174,7 @@ Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
if (!comment)
continue;
if (Service::IsCommentExpired(comment))
if (comment->IsExpired())
continue;
Array::Ptr comment_info = make_shared<Array>();

View File

@ -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());
}

View File

@ -35,6 +35,8 @@ class I2_ICINGA_API Comment : public ObjectImpl<Comment>
{
public:
DECLARE_PTR_TYPEDEFS(Comment);
bool IsExpired(void) const;
};
}

View File

@ -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());
}

View File

@ -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;
};
}

View File

@ -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);
}
}

View File

@ -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++;
}

View File

@ -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());