From 2818245e01ddb2dbb9187c90c75712e23e81a498 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 29 Jul 2021 12:10:42 +0200 Subject: [PATCH] Introduce Checkable#GetLastComment() --- lib/icinga/checkable-comment.cpp | 15 +++++++++++++++ lib/icinga/checkable.hpp | 1 + 2 files changed, 16 insertions(+) diff --git a/lib/icinga/checkable-comment.cpp b/lib/icinga/checkable-comment.cpp index ea4f8b595..bb26a6d3c 100644 --- a/lib/icinga/checkable-comment.cpp +++ b/lib/icinga/checkable-comment.cpp @@ -7,6 +7,7 @@ #include "base/timer.hpp" #include "base/utility.hpp" #include "base/logger.hpp" +#include using namespace icinga; @@ -42,6 +43,20 @@ std::set Checkable::GetComments() const return m_Comments; } +Comment::Ptr Checkable::GetLastComment() const +{ + std::unique_lock lock (m_CommentMutex); + Comment::Ptr lastComment; + + for (auto& comment : m_Comments) { + if (!lastComment || comment->GetEntryTime() > lastComment->GetEntryTime()) { + lastComment = comment; + } + } + + return std::move(lastComment); +} + void Checkable::RegisterComment(const Comment::Ptr& comment) { std::unique_lock lock(m_CommentMutex); diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index 52229288e..46916ddf0 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -152,6 +152,7 @@ public: void RemoveCommentsByType(int type, const String& removedBy = String()); std::set GetComments() const; + Comment::Ptr GetLastComment() const; void RegisterComment(const Comment::Ptr& comment); void UnregisterComment(const Comment::Ptr& comment);