From 4dbc1f993b9a2757ad1b8dbcb9a41e83d972a81f Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 17 Sep 2013 15:30:21 +0200 Subject: [PATCH] ido: Fix downtimes/comments removal. fixes #4651 --- lib/ido/servicedbobject.cpp | 134 ++++++++++++++++++++++++++++++++---- lib/ido/servicedbobject.h | 2 + 2 files changed, 122 insertions(+), 14 deletions(-) diff --git a/lib/ido/servicedbobject.cpp b/lib/ido/servicedbobject.cpp index 8af8e5120..66f991ccd 100644 --- a/lib/ido/servicedbobject.cpp +++ b/lib/ido/servicedbobject.cpp @@ -251,6 +251,8 @@ void ServiceDbObject::OnConfigUpdate(void) } /* update comments and downtimes on config change */ + RemoveComments(service); + RemoveDowntimes(service); AddComments(service); AddDowntimes(service); @@ -345,12 +347,8 @@ void ServiceDbObject::AddCommentByType(const DynamicObject::Ptr& object, const D if (object->GetType() == DynamicType::GetByName("Host")) { fields1->Set("comment_type", 2); - /* this is obviously bullshit, but otherwise we would hit - * the unique constraint on the table for the same service - * comment. dynamically incremented/decremented numbers as - * unique constraint - wtf? - */ - fields1->Set("internal_comment_id", 0); + /* requires idoutils 1.10 schema fix */ + fields1->Set("internal_comment_id", comment->Get("legacy_id")); } else if (object->GetType() == DynamicType::GetByName("Service")) { fields1->Set("comment_type", 1); fields1->Set("internal_comment_id", comment->Get("legacy_id")); @@ -375,9 +373,65 @@ void ServiceDbObject::AddCommentByType(const DynamicObject::Ptr& object, const D OnQuery(query1); } +void ServiceDbObject::RemoveComments(const Service::Ptr& service) +{ + Host::Ptr host = service->GetHost(); + + if (!host) + return; + + Log(LogDebug, "ido", "removing service comments for '" + service->GetName() + "'"); + + DbQuery query1; + query1.Table = "comments"; + query1.Type = DbQueryDelete; + query1.WhereCriteria = boost::make_shared(); + query1.WhereCriteria->Set("object_id", service); + OnQuery(query1); + + /* delete hostcheck service's host comments */ + if (host->GetHostCheckService() == service) { + DbQuery query2; + query2.Table = "comments"; + query2.Type = DbQueryDelete; + query2.WhereCriteria = boost::make_shared(); + query2.WhereCriteria->Set("object_id", host); + OnQuery(query2); + } +} + void ServiceDbObject::RemoveComment(const Service::Ptr& service, const Dictionary::Ptr& comment) { - /* TODO: implement */ + Host::Ptr host = service->GetHost(); + + if (!host) + return; + + if (!comment) { + Log(LogWarning, "ido", "comment does not exist. not adding it."); + return; + } + + Log(LogDebug, "ido", "removing service comment (id = " + comment->Get("legacy_id") + ") for '" + service->GetName() + "'"); + + DbQuery query1; + query1.Table = "comments"; + query1.Type = DbQueryDelete; + query1.WhereCriteria = boost::make_shared(); + query1.WhereCriteria->Set("object_id", service); + query1.WhereCriteria->Set("internal_comment_id", comment->Get("legacy_id")); + OnQuery(query1); + + /* delete hostcheck service's host comments */ + if (host->GetHostCheckService() == service) { + DbQuery query2; + query2.Table = "comments"; + query2.Type = DbQueryDelete; + query2.WhereCriteria = boost::make_shared(); + query2.WhereCriteria->Set("object_id", host); + query2.WhereCriteria->Set("internal_comment_id", comment->Get("legacy_id")); + OnQuery(query2); + } } void ServiceDbObject::AddDowntimes(const Service::Ptr& service) @@ -429,12 +483,8 @@ void ServiceDbObject::AddDowntimeByType(const DynamicObject::Ptr& object, const if (object->GetType() == DynamicType::GetByName("Host")) { fields1->Set("downtime_type", 2); - /* this is obviously bullshit, but otherwise we would hit - * the unique constraint on the table for the same service - * downtime. dynamically incremented/decremented numbers as - * unique constraint - wtf? - */ - fields1->Set("internal_downtime_id", 0); + /* requires idoutils 1.10 schema fix */ + fields1->Set("internal_downtime_id", downtime->Get("legacy_id")); } else if (object->GetType() == DynamicType::GetByName("Service")) { fields1->Set("downtime_type", 1); fields1->Set("internal_downtime_id", downtime->Get("legacy_id")); @@ -463,9 +513,65 @@ void ServiceDbObject::AddDowntimeByType(const DynamicObject::Ptr& object, const OnQuery(query1); } +void ServiceDbObject::RemoveDowntimes(const Service::Ptr& service) +{ + Host::Ptr host = service->GetHost(); + + if (!host) + return; + + Log(LogDebug, "ido", "removing service downtimes for '" + service->GetName() + "'"); + + DbQuery query1; + query1.Table = "scheduleddowntime"; + query1.Type = DbQueryDelete; + query1.WhereCriteria = boost::make_shared(); + query1.WhereCriteria->Set("object_id", service); + OnQuery(query1); + + /* delete hostcheck service's host downtimes */ + if (host->GetHostCheckService() == service) { + DbQuery query2; + query2.Table = "scheduleddowntime"; + query2.Type = DbQueryDelete; + query2.WhereCriteria = boost::make_shared(); + query2.WhereCriteria->Set("object_id", host); + OnQuery(query2); + } +} + void ServiceDbObject::RemoveDowntime(const Service::Ptr& service, const Dictionary::Ptr& downtime) { - /* TODO: implement */ + Host::Ptr host = service->GetHost(); + + if (!host) + return; + + if (!downtime) { + Log(LogWarning, "ido", "downtime does not exist. not adding it."); + return; + } + + Log(LogDebug, "ido", "removing service downtime (id = " + downtime->Get("legacy_id") + ") for '" + service->GetName() + "'"); + + DbQuery query1; + query1.Table = "scheduleddowntime"; + query1.Type = DbQueryDelete; + query1.WhereCriteria = boost::make_shared(); + query1.WhereCriteria->Set("object_id", service); + query1.WhereCriteria->Set("internal_downtime_id", downtime->Get("legacy_id")); + OnQuery(query1); + + /* delete hostcheck service's host comments */ + if (host->GetHostCheckService() == service) { + DbQuery query2; + query2.Table = "scheduleddowntime"; + query2.Type = DbQueryDelete; + query2.WhereCriteria = boost::make_shared(); + query2.WhereCriteria->Set("object_id", host); + query2.WhereCriteria->Set("internal_downtime_id", downtime->Get("legacy_id")); + OnQuery(query2); + } } void ServiceDbObject::TriggerDowntime(const Service::Ptr& service, const Dictionary::Ptr& downtime) diff --git a/lib/ido/servicedbobject.h b/lib/ido/servicedbobject.h index 9d3971b71..30499de55 100644 --- a/lib/ido/servicedbobject.h +++ b/lib/ido/servicedbobject.h @@ -54,11 +54,13 @@ private: static void AddComments(const Service::Ptr& service); static void AddComment(const Service::Ptr& service, const Dictionary::Ptr& comment); static void AddCommentByType(const DynamicObject::Ptr& object, const Dictionary::Ptr& comment); + static void RemoveComments(const Service::Ptr& service); static void RemoveComment(const Service::Ptr& service, const Dictionary::Ptr& comment); static void AddDowntimes(const Service::Ptr& service); static void AddDowntime(const Service::Ptr& service, const Dictionary::Ptr& downtime); static void AddDowntimeByType(const DynamicObject::Ptr& object, const Dictionary::Ptr& downtime); + static void RemoveDowntimes(const Service::Ptr& service); static void RemoveDowntime(const Service::Ptr& service, const Dictionary::Ptr& downtime); static void TriggerDowntime(const Service::Ptr& service, const Dictionary::Ptr& downtime); }; -- 2.40.0