From 42f959874ef20be8e18796c815bc33274efb6e7b Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Thu, 7 Nov 2019 15:57:31 +0100 Subject: [PATCH 1/2] IcingaDB: Add user notification history --- lib/icingadb/icingadb-objects.cpp | 27 +++++++++++++++++++++------ lib/icingadb/icingadb.hpp | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index a151c0cbd..6161b138a 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -1219,7 +1219,7 @@ void IcingaDB::SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResu } void IcingaDB::SendSentNotification( - const Notification::Ptr& notification, const Checkable::Ptr& checkable, size_t users, + const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set& users, NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text ) { @@ -1233,9 +1233,12 @@ void IcingaDB::SendSentNotification( finalText = cr->GetOutput(); } + auto usersAmount (users.size()); + auto notificationHistoryId = Utility::NewUniqueID(); + std::vector xAdd ({ "XADD", "icinga:history:stream:notification", "*", - "id", Utility::NewUniqueID(), + "id", notificationHistoryId, "environment_id", SHA1(GetEnvironment()), "notification_id", GetObjectIdentifier(notification), "type", Convert::ToString(type), @@ -1243,7 +1246,7 @@ void IcingaDB::SendSentNotification( "previous_hard_state", Convert::ToString(GetPreviousState(checkable, service, StateTypeHard)), "author", Utility::ValidateUTF8(author), "text", Utility::ValidateUTF8(finalText), - "users_notified", Convert::ToString(users), + "users_notified", Convert::ToString(usersAmount), "event_time", Convert::ToString(TimestampToMilliseconds(Utility::GetTime())), "event_id", Utility::NewUniqueID(), "event_type", "notification" @@ -1269,6 +1272,19 @@ void IcingaDB::SendSentNotification( } m_Rcon->FireAndForgetQuery(std::move(xAdd)); + + for (const User::Ptr& user : users) { + auto userId = GetObjectIdentifier(user); + std::vector xAddUser ({ + "XADD", "icinga:history:stream:usernotification", "*", + "id", Utility::NewUniqueID(), + "environment_id", SHA1(GetEnvironment()), + "notification_history_id", notificationHistoryId, + "user_id", GetObjectIdentifier(user), + }); + + m_Rcon->FireAndForgetQuery(std::move(xAddUser)); + } } void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime) @@ -1751,12 +1767,11 @@ void IcingaDB::NotificationSentToAllUsersHandler( auto rws (ConfigType::GetObjectsByType()); if (!rws.empty()) { - auto usersAmount (users.size()); auto authorAndText (std::make_shared>(author, text)); for (auto& rw : rws) { - rw->m_WorkQueue.Enqueue([rw, notification, checkable, usersAmount, type, cr, authorAndText]() { - rw->SendSentNotification(notification, checkable, usersAmount, type, cr, authorAndText->first, authorAndText->second); + rw->m_WorkQueue.Enqueue([rw, notification, checkable, users, type, cr, authorAndText]() { + rw->SendSentNotification(notification, checkable, users, type, cr, authorAndText->first, authorAndText->second); }); } } diff --git a/lib/icingadb/icingadb.hpp b/lib/icingadb/icingadb.hpp index 4ebe26409..9140297c1 100644 --- a/lib/icingadb/icingadb.hpp +++ b/lib/icingadb/icingadb.hpp @@ -66,7 +66,7 @@ private: void SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type); void SendSentNotification( - const Notification::Ptr& notification, const Checkable::Ptr& checkable, size_t users, + const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set& users, NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text ); From 1e2739aed63d2f9982b8fdbdd008b58851c8e588 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 7 Nov 2019 17:23:48 +0100 Subject: [PATCH 2/2] IcingaDB::NotificationSentToAllUsersHandler(): copy set once, not N*(1+M) times --- lib/icingadb/icingadb-objects.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index 6161b138a..d2a7f5e55 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -7,6 +7,7 @@ #include "base/json.hpp" #include "base/logger.hpp" #include "base/serializer.hpp" +#include "base/shared.hpp" #include "base/tlsutility.hpp" #include "base/initialize.hpp" #include "base/convert.hpp" @@ -1759,6 +1760,13 @@ void IcingaDB::DowntimeRemovedHandler(const Downtime::Ptr& downtime) } } +struct ATU +{ + String Author; + String Text; + std::set Users; +}; + void IcingaDB::NotificationSentToAllUsersHandler( const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set& users, NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text @@ -1767,11 +1775,11 @@ void IcingaDB::NotificationSentToAllUsersHandler( auto rws (ConfigType::GetObjectsByType()); if (!rws.empty()) { - auto authorAndText (std::make_shared>(author, text)); + auto atu (Shared::Make(ATU{author, text, users})); for (auto& rw : rws) { - rw->m_WorkQueue.Enqueue([rw, notification, checkable, users, type, cr, authorAndText]() { - rw->SendSentNotification(notification, checkable, users, type, cr, authorAndText->first, authorAndText->second); + rw->m_WorkQueue.Enqueue([rw, notification, checkable, atu, type, cr]() { + rw->SendSentNotification(notification, checkable, atu->Users, type, cr, atu->Author, atu->Text); }); } }