icinga2/lib/icinga/service-comment.cpp

212 lines
5.6 KiB
C++
Raw Normal View History

2013-01-29 16:29:09 +01:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "i2-icinga.h"
using namespace icinga;
int Service::m_NextCommentID = 1;
map<int, String> Service::m_LegacyCommentCache;
map<String, Service::WeakPtr> Service::m_CommentCache;
bool Service::m_CommentCacheValid;
Timer::Ptr Service::m_CommentExpireTimer;
2013-01-29 16:29:09 +01:00
int Service::GetNextCommentID(void)
2013-01-29 16:29:09 +01:00
{
return m_NextCommentID;
}
String Service::AddComment(CommentType entryType, const String& author,
const String& text, double expireTime)
2013-01-29 16:29:09 +01:00
{
Dictionary::Ptr comment = boost::make_shared<Dictionary>();
comment->Set("entry_time", Utility::GetTime());
comment->Set("entry_type", entryType);
comment->Set("author", author);
comment->Set("text", text);
comment->Set("expire_time", expireTime);
comment->Set("legacy_id", m_NextCommentID++);
2013-01-29 16:29:09 +01:00
Dictionary::Ptr comments = Get("comments");
2013-01-29 16:29:09 +01:00
if (!comments)
comments = boost::make_shared<Dictionary>();
String id = Utility::NewUUID();
comments->Set(id, comment);
Set("comments", comments);
2013-01-29 16:29:09 +01:00
return id;
}
void Service::RemoveAllComments(void)
2013-01-29 16:29:09 +01:00
{
Set("comments", Empty);
2013-01-29 16:29:09 +01:00
}
void Service::RemoveComment(const String& id)
2013-01-29 16:29:09 +01:00
{
Service::Ptr owner = GetOwnerByCommentID(id);
2013-01-29 16:29:09 +01:00
if (!owner)
return;
2013-01-29 16:29:09 +01:00
Dictionary::Ptr comments = owner->Get("comments");
if (comments) {
comments->Remove(id);
2013-01-29 16:29:09 +01:00
owner->Touch("comments");
}
}
String Service::GetCommentIDFromLegacyID(int id)
{
map<int, String>::iterator it = m_LegacyCommentCache.find(id);
if (it == m_LegacyCommentCache.end())
return Empty;
return it->second;
}
Service::Ptr Service::GetOwnerByCommentID(const String& id)
2013-01-29 16:29:09 +01:00
{
ValidateCommentCache();
return m_CommentCache[id].lock();
}
Dictionary::Ptr Service::GetCommentByID(const String& id)
2013-01-29 16:29:09 +01:00
{
Service::Ptr owner = GetOwnerByCommentID(id);
2013-01-29 16:29:09 +01:00
if (!owner)
return Dictionary::Ptr();
2013-01-29 16:29:09 +01:00
Dictionary::Ptr comments = owner->Get("comments");
if (comments) {
Dictionary::Ptr comment = comments->Get(id);
2013-01-29 16:29:09 +01:00
return comment;
}
return Dictionary::Ptr();
}
bool Service::IsCommentExpired(const Dictionary::Ptr& comment)
2013-01-30 14:28:13 +01:00
{
double expire_time = comment->Get("expire_time");
return (expire_time != 0 && expire_time < Utility::GetTime());
}
void Service::InvalidateCommentCache(void)
2013-01-29 16:29:09 +01:00
{
m_CommentCacheValid = false;
m_CommentCache.clear();
m_LegacyCommentCache.clear();
2013-01-29 16:29:09 +01:00
}
void Service::AddCommentsToCache(void)
2013-01-29 16:29:09 +01:00
{
Dictionary::Ptr comments = Get("comments");
2013-01-29 16:29:09 +01:00
if (!comments)
return;
String id;
Dictionary::Ptr comment;
BOOST_FOREACH(tie(id, comment), comments) {
int legacy_id = comment->Get("legacy_id");
if (legacy_id >= m_NextCommentID)
m_NextCommentID = legacy_id + 1;
if (m_LegacyCommentCache.find(legacy_id) != m_LegacyCommentCache.end()) {
/* The legacy_id is already in use by another comment;
* this shouldn't usually happen - assign it a new ID */
2013-01-29 16:29:09 +01:00
legacy_id = m_NextCommentID++;
comment->Set("legacy_id", legacy_id);
Touch("comments");
}
2013-01-29 16:29:09 +01:00
m_LegacyCommentCache[legacy_id] = id;
m_CommentCache[id] = GetSelf();
2013-01-29 16:29:09 +01:00
}
}
void Service::ValidateCommentCache(void)
2013-01-29 16:29:09 +01:00
{
if (m_CommentCacheValid)
return;
m_CommentCache.clear();
m_LegacyCommentCache.clear();
2013-01-29 16:29:09 +01:00
DynamicObject::Ptr object;
BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
Service::Ptr service = dynamic_pointer_cast<Service>(object);
service->AddCommentsToCache();
2013-01-29 16:29:09 +01:00
}
m_CommentCacheValid = true;
2013-01-30 14:28:13 +01:00
if (!m_CommentExpireTimer) {
m_CommentExpireTimer = boost::make_shared<Timer>();
m_CommentExpireTimer->SetInterval(300);
m_CommentExpireTimer->OnTimerExpired.connect(boost::bind(&Service::CommentExpireTimerHandler));
2013-01-30 14:28:13 +01:00
m_CommentExpireTimer->Start();
}
}
void Service::RemoveExpiredComments(void)
2013-01-30 14:28:13 +01:00
{
Dictionary::Ptr comments = Get("comments");
2013-01-30 14:28:13 +01:00
if (!comments)
return;
vector<String> expiredComments;
String id;
Dictionary::Ptr comment;
BOOST_FOREACH(tie(id, comment), comments) {
if (IsCommentExpired(comment))
expiredComments.push_back(id);
}
if (expiredComments.size() > 0) {
BOOST_FOREACH(id, expiredComments) {
comments->Remove(id);
}
Touch("comments");
2013-01-30 14:28:13 +01:00
}
}
void Service::CommentExpireTimerHandler(void)
2013-01-30 14:28:13 +01:00
{
DynamicObject::Ptr object;
BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
Service::Ptr service = dynamic_pointer_cast<Service>(object);
service->RemoveExpiredComments();
2013-01-30 14:28:13 +01:00
}
2013-01-29 16:29:09 +01:00
}