IcingaDB: Add user notification history

This commit is contained in:
Noah Hilverling 2019-11-07 15:57:31 +01:00
parent 280dbf62c7
commit 42f959874e
2 changed files with 22 additions and 7 deletions

View File

@ -1219,7 +1219,7 @@ void IcingaDB::SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResu
} }
void IcingaDB::SendSentNotification( 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<User::Ptr>& users,
NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text
) )
{ {
@ -1233,9 +1233,12 @@ void IcingaDB::SendSentNotification(
finalText = cr->GetOutput(); finalText = cr->GetOutput();
} }
auto usersAmount (users.size());
auto notificationHistoryId = Utility::NewUniqueID();
std::vector<String> xAdd ({ std::vector<String> xAdd ({
"XADD", "icinga:history:stream:notification", "*", "XADD", "icinga:history:stream:notification", "*",
"id", Utility::NewUniqueID(), "id", notificationHistoryId,
"environment_id", SHA1(GetEnvironment()), "environment_id", SHA1(GetEnvironment()),
"notification_id", GetObjectIdentifier(notification), "notification_id", GetObjectIdentifier(notification),
"type", Convert::ToString(type), "type", Convert::ToString(type),
@ -1243,7 +1246,7 @@ void IcingaDB::SendSentNotification(
"previous_hard_state", Convert::ToString(GetPreviousState(checkable, service, StateTypeHard)), "previous_hard_state", Convert::ToString(GetPreviousState(checkable, service, StateTypeHard)),
"author", Utility::ValidateUTF8(author), "author", Utility::ValidateUTF8(author),
"text", Utility::ValidateUTF8(finalText), "text", Utility::ValidateUTF8(finalText),
"users_notified", Convert::ToString(users), "users_notified", Convert::ToString(usersAmount),
"event_time", Convert::ToString(TimestampToMilliseconds(Utility::GetTime())), "event_time", Convert::ToString(TimestampToMilliseconds(Utility::GetTime())),
"event_id", Utility::NewUniqueID(), "event_id", Utility::NewUniqueID(),
"event_type", "notification" "event_type", "notification"
@ -1269,6 +1272,19 @@ void IcingaDB::SendSentNotification(
} }
m_Rcon->FireAndForgetQuery(std::move(xAdd)); m_Rcon->FireAndForgetQuery(std::move(xAdd));
for (const User::Ptr& user : users) {
auto userId = GetObjectIdentifier(user);
std::vector<String> 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) void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime)
@ -1751,12 +1767,11 @@ void IcingaDB::NotificationSentToAllUsersHandler(
auto rws (ConfigType::GetObjectsByType<IcingaDB>()); auto rws (ConfigType::GetObjectsByType<IcingaDB>());
if (!rws.empty()) { if (!rws.empty()) {
auto usersAmount (users.size());
auto authorAndText (std::make_shared<std::pair<String, String>>(author, text)); auto authorAndText (std::make_shared<std::pair<String, String>>(author, text));
for (auto& rw : rws) { for (auto& rw : rws) {
rw->m_WorkQueue.Enqueue([rw, notification, checkable, usersAmount, type, cr, authorAndText]() { rw->m_WorkQueue.Enqueue([rw, notification, checkable, users, type, cr, authorAndText]() {
rw->SendSentNotification(notification, checkable, usersAmount, type, cr, authorAndText->first, authorAndText->second); rw->SendSentNotification(notification, checkable, users, type, cr, authorAndText->first, authorAndText->second);
}); });
} }
} }

View File

@ -66,7 +66,7 @@ private:
void SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type); void SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type);
void SendSentNotification( void SendSentNotification(
const Notification::Ptr& notification, const Checkable::Ptr& checkable, size_t users, const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text
); );