From c86059359b8afdb04b1f6115d9f68a8145d4c00f Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 7 Aug 2013 17:36:54 +0200 Subject: [PATCH] ido: Refactor comments code. refs #4380 --- lib/ido/servicedbobject.cpp | 195 ++++++++++++++++-------------------- lib/ido/servicedbobject.h | 4 + 2 files changed, 93 insertions(+), 106 deletions(-) diff --git a/lib/ido/servicedbobject.cpp b/lib/ido/servicedbobject.cpp index 33543e3da..6703830fe 100644 --- a/lib/ido/servicedbobject.cpp +++ b/lib/ido/servicedbobject.cpp @@ -205,7 +205,7 @@ void ServiceDbObject::OnConfigUpdate(void) } /* update comments */ - OnCommentsChanged(service, Empty, CommentChangedUpdated); + //OnCommentsChanged(service, Empty, CommentChangedUpdated); /* service host config update */ Host::Ptr host = service->GetHost(); @@ -245,141 +245,124 @@ void ServiceDbObject::OnStatusUpdate(void) void ServiceDbObject::CommentsChangedHandler(const Service::Ptr& svcfilter, const String& id, CommentChangedType type) { - unsigned long entry_time; - unsigned long entry_time_usec; - Service::Ptr service; - Host::Ptr host; - Dictionary::Ptr comment; - DbQuery query1, query2, query_del1; - Dictionary::Ptr fields1, fields2; - if (type == CommentChangedUpdated || type == CommentChangedDeleted) { /* we cannot determine which comment id is deleted * ido schema does not store legacy id */ BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) { - service = static_pointer_cast(object); + Service::Ptr service = static_pointer_cast(object); if (svcfilter && svcfilter != service) continue; - host = service->GetHost(); + Host::Ptr host = service->GetHost(); if (!host) continue; /* delete all comments associated for this host/service */ - Log(LogDebug, "ido", "delete comments for '" + service->GetName() + "'"); - - query_del1; - query_del1.Table = "comments"; - query_del1.Type = DbQueryDelete; - query_del1.WhereCriteria = boost::make_shared(); - if (host->GetHostCheckService() == service) - query_del1.WhereCriteria->Set("object_id", host); - else - query_del1.WhereCriteria->Set("object_id", service); - - OnQuery(query_del1); + DeleteComments(service); /* dump all comments */ - Dictionary::Ptr comments = service->GetComments(); + AddComments(service); + } + } else if (type == CommentChangedAdded) { - if (!comments) - continue; + Dictionary::Ptr comment = Service::GetCommentByID(id); + AddComment(svcfilter, comment); + } else { + Log(LogDebug, "ido", "invalid comment change type: " + type); + } +} - ObjectLock olock(comments); - - String cid; - BOOST_FOREACH(boost::tie(cid, comment), comments) { - Log(LogDebug, "ido", "adding service comment (id = " + cid + ") for '" + service->GetName() + "'"); - - entry_time = static_cast(comment->Get("entry_time")); - entry_time_usec = (comment->Get("entry_time") - entry_time) * 1000 * 1000; - - Dictionary::Ptr fields1 = boost::make_shared(); - fields1->Set("entry_time", DbValue::FromTimestamp(entry_time)); - fields1->Set("entry_time_usec", entry_time_usec); - fields1->Set("entry_type", comment->Get("entry_type")); - - if (host->GetHostCheckService() == service) { - fields1->Set("comment_type", 2); - fields1->Set("object_id", host); - } else { - fields1->Set("comment_type", 1); - fields1->Set("object_id", service); - } - - fields1->Set("comment_time", DbValue::FromTimestamp(Utility::GetTime())); - fields1->Set("internal_comment_id", comment->Get("legacy_id")); /* not sure if that's accurate? */ - fields1->Set("author_name", comment->Get("author")); - fields1->Set("comment_data", comment->Get("text")); - fields1->Set("is_persistent", 1); - fields1->Set("comment_source", 1); /* external */ - fields1->Set("expires", (comment->Get("expire_time") > 0) ? 1 : 0); - fields1->Set("expiration_time", comment->Get("expire_time")); - fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */ - - DbQuery query1; - query1.Table = "comments"; - query1.Type = DbQueryInsert; - query1.Fields = fields1; - OnQuery(query1); - } - } +void ServiceDbObject::AddComments(Service::Ptr const& service) +{ + /* dump all comments */ + Dictionary::Ptr comments = service->GetComments(); - } else if (type == CommentChangedAdded) { + if (!comments) + return; - service = svcfilter; - host = service->GetHost(); + ObjectLock olock(comments); - if (!host) - return; + String comment_id; + Dictionary::Ptr comment; + BOOST_FOREACH(boost::tie(comment_id, comment), comments) { + AddComment(service, comment); + } +} - Dictionary::Ptr add_comment = Service::GetCommentByID(id); +void ServiceDbObject::AddComment(Service::Ptr const& service, Dictionary::Ptr const& comment) +{ + Host::Ptr host = service->GetHost(); - if (!add_comment || id.IsEmpty()) { - Log(LogWarning, "ido", "comment with id '" + id + "' does not exist. not adding it."); - return; - } + if (!host) + return; - /* newly added comment */ - Log(LogDebug, "ido", "adding service comment (id = " + id + ") for '" + service->GetName() + "'"); + if (!comment) { + Log(LogWarning, "ido", "comment does not exist. not adding it."); + return; + } - entry_time = static_cast(add_comment->Get("entry_time")); - entry_time_usec = (add_comment->Get("entry_time") - entry_time) * 1000 * 1000; + String comment_id = comment->Get("id"); - fields2 = boost::make_shared(); - fields2->Set("entry_time", DbValue::FromTimestamp(entry_time)); - fields2->Set("entry_time_usec", entry_time_usec); - fields2->Set("entry_type", add_comment->Get("entry_type")); + Log(LogDebug, "ido", "adding service comment (id = " + comment_id + ") for '" + service->GetName() + "'"); - if (host->GetHostCheckService() == service) { - fields2->Set("comment_type", 2); - fields2->Set("object_id", host); - } else { - fields2->Set("comment_type", 1); - fields2->Set("object_id", service); - } + unsigned long entry_time = static_cast(comment->Get("entry_time")); + unsigned long entry_time_usec = (comment->Get("entry_time") - entry_time) * 1000 * 1000; - fields2->Set("comment_time", DbValue::FromTimestamp(Utility::GetTime())); - fields2->Set("internal_comment_id", add_comment->Get("legacy_id")); /* not sure if that's accurate? */ - fields2->Set("author_name", add_comment->Get("author")); - fields2->Set("comment_data", add_comment->Get("text")); - fields2->Set("is_persistent", 1); - fields2->Set("comment_source", 1); /* external */ - fields2->Set("expires", (add_comment->Get("expire_time") > 0) ? 1 : 0); - fields2->Set("expiration_time", add_comment->Get("expire_time")); - fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */ - - query2; - query2.Table = "comments"; - query2.Type = DbQueryInsert; - query2.Fields = fields2; - OnQuery(query2); + Dictionary::Ptr fields1 = boost::make_shared(); + fields1->Set("entry_time", DbValue::FromTimestamp(entry_time)); + fields1->Set("entry_time_usec", entry_time_usec); + fields1->Set("entry_type", comment->Get("entry_type")); + if (host->GetHostCheckService() == service) { + fields1->Set("comment_type", 2); + fields1->Set("object_id", host); } else { - Log(LogDebug, "ido", "invalid comment change type: " + type); + fields1->Set("comment_type", 1); + fields1->Set("object_id", service); } + + fields1->Set("comment_time", DbValue::FromTimestamp(Utility::GetTime())); + fields1->Set("internal_comment_id", comment->Get("legacy_id")); /* not sure if that's accurate? */ + fields1->Set("author_name", comment->Get("author")); + fields1->Set("comment_data", comment->Get("text")); + fields1->Set("is_persistent", 1); + fields1->Set("comment_source", 1); /* external */ + fields1->Set("expires", (comment->Get("expire_time") > 0) ? 1 : 0); + fields1->Set("expiration_time", comment->Get("expire_time")); + fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query1; + query1.Table = "comments"; + query1.Type = DbQueryInsert; + query1.Fields = fields1; + OnQuery(query1); + /* TODO add hostcheck service AND host comments */ +} + +void ServiceDbObject::DeleteComments(Service::Ptr const& service) +{ + /* delete all comments associated for this host/service */ + Log(LogDebug, "ido", "delete comments for '" + service->GetName() + "'"); + + Host::Ptr host = service->GetHost(); + + if (!host) + return; + + DbQuery query1; + query1.Table = "comments"; + query1.Type = DbQueryDelete; + query1.WhereCriteria = boost::make_shared(); + if (host->GetHostCheckService() == service) + query1.WhereCriteria->Set("object_id", host); + else + query1.WhereCriteria->Set("object_id", service); + + OnQuery(query1); + + /* TODO delete hostcheck service AND host comments */ } diff --git a/lib/ido/servicedbobject.h b/lib/ido/servicedbobject.h index f1e5218bf..24424d350 100644 --- a/lib/ido/servicedbobject.h +++ b/lib/ido/servicedbobject.h @@ -51,6 +51,10 @@ protected: virtual void OnStatusUpdate(void); static void CommentsChangedHandler(const Service::Ptr& service, const String& id, CommentChangedType type); + + static void AddComments(Service::Ptr const& service); + static void AddComment(Service::Ptr const& service, Dictionary::Ptr const& comment); + static void DeleteComments(Service::Ptr const& service); }; } -- 2.40.0