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;
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
int Service::m_NextCommentID = 1;
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex Service::m_CommentMutex;
|
2013-02-13 22:36:24 +01:00
|
|
|
map<int, String> Service::m_LegacyCommentsCache;
|
|
|
|
map<String, Service::WeakPtr> Service::m_CommentsCache;
|
2013-02-27 15:23:25 +01:00
|
|
|
bool Service::m_CommentsCacheValid = true;
|
2013-02-13 22:36:24 +01:00
|
|
|
Timer::Ptr Service::m_CommentsExpireTimer;
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
int Service::GetNextCommentID(void)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_CommentMutex);
|
|
|
|
|
2013-01-29 16:29:09 +01:00
|
|
|
return m_NextCommentID;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 18:39:43 +01:00
|
|
|
Dictionary::Ptr Service::GetComments(void) const
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Comments;
|
2013-02-09 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
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);
|
2013-02-27 12:44:51 +01:00
|
|
|
|
|
|
|
int legacy_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_CommentMutex);
|
|
|
|
legacy_id = m_NextCommentID++;
|
|
|
|
}
|
|
|
|
|
|
|
|
comment->Set("legacy_id", legacy_id);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
Dictionary::Ptr comments;
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
comments = GetComments();
|
|
|
|
|
|
|
|
if (!comments)
|
|
|
|
comments = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
m_Comments = comments;
|
|
|
|
}
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-01-30 09:59:22 +01:00
|
|
|
String id = Utility::NewUUID();
|
|
|
|
comments->Set(id, comment);
|
2013-02-26 10:13:54 +01:00
|
|
|
|
|
|
|
Touch("comments");
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
void Service::RemoveAllComments(void)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-27 12:44:51 +01:00
|
|
|
m_Comments = Empty;
|
|
|
|
Touch("comments");
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
void Service::RemoveComment(const String& id)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::Ptr owner = GetOwnerByCommentID(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
if (!owner)
|
2013-01-31 16:23:02 +01:00
|
|
|
return;
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Dictionary::Ptr comments = owner->GetComments();
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
if (comments) {
|
2013-01-30 09:59:22 +01:00
|
|
|
comments->Remove(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
owner->Touch("comments");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
String Service::GetCommentIDFromLegacyID(int id)
|
2013-01-30 09:59:22 +01:00
|
|
|
{
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_CommentMutex);
|
|
|
|
|
2013-02-13 22:36:24 +01:00
|
|
|
map<int, String>::iterator it = m_LegacyCommentsCache.find(id);
|
2013-01-30 13:02:20 +01:00
|
|
|
|
2013-02-13 22:36:24 +01:00
|
|
|
if (it == m_LegacyCommentsCache.end())
|
2013-01-31 16:23:02 +01:00
|
|
|
return Empty;
|
2013-01-30 13:02:20 +01:00
|
|
|
|
|
|
|
return it->second;
|
2013-01-30 09:59:22 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::Ptr Service::GetOwnerByCommentID(const String& id)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_CommentMutex);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-13 22:36:24 +01:00
|
|
|
return m_CommentsCache[id].lock();
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
Dictionary::Ptr Service::GetCommentByID(const String& id)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::Ptr owner = GetOwnerByCommentID(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
if (!owner)
|
2013-01-31 16:23:02 +01:00
|
|
|
return Dictionary::Ptr();
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Dictionary::Ptr comments = owner->GetComments();
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (comments)
|
|
|
|
return comments->Get(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
return Dictionary::Ptr();
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-27 15:23:25 +01:00
|
|
|
void Service::InvalidateCommentsCache(void)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_CommentMutex);
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (m_CommentsCacheValid)
|
|
|
|
Utility::QueueAsyncCallback(boost::bind(&Service::RefreshCommentsCache));
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
m_CommentsCacheValid = false;
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-27 12:44:51 +01:00
|
|
|
void Service::RefreshCommentsCache(void)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_CommentMutex);
|
|
|
|
|
|
|
|
if (m_CommentsCacheValid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_CommentsCacheValid = true;
|
|
|
|
}
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
map<int, String> newLegacyCommentsCache;
|
|
|
|
map<String, Service::WeakPtr> newCommentsCache;
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
|
|
|
|
Service::Ptr service = dynamic_pointer_cast<Service>(object);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
Dictionary::Ptr comments = service->GetComments();
|
2013-01-30 13:02:20 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
if (!comments)
|
|
|
|
continue;
|
2013-01-30 09:59:22 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
ObjectLock olock(comments);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
String id;
|
|
|
|
Dictionary::Ptr comment;
|
|
|
|
BOOST_FOREACH(tie(id, comment), comments) {
|
|
|
|
int legacy_id = comment->Get("legacy_id");
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
if (legacy_id >= m_NextCommentID)
|
|
|
|
m_NextCommentID = legacy_id + 1;
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
if (newLegacyCommentsCache.find(legacy_id) != newLegacyCommentsCache.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
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
legacy_id = m_NextCommentID++;
|
|
|
|
comment->Set("legacy_id", legacy_id);
|
|
|
|
service->Touch("comments");
|
|
|
|
}
|
|
|
|
|
|
|
|
newLegacyCommentsCache[legacy_id] = id;
|
|
|
|
newCommentsCache[id] = service;
|
|
|
|
}
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_CommentMutex);
|
|
|
|
|
|
|
|
m_CommentsCache.swap(newCommentsCache);
|
|
|
|
m_LegacyCommentsCache.swap(newLegacyCommentsCache);
|
2013-01-30 14:28:13 +01:00
|
|
|
|
2013-02-13 22:36:24 +01:00
|
|
|
if (!m_CommentsExpireTimer) {
|
|
|
|
m_CommentsExpireTimer = boost::make_shared<Timer>();
|
|
|
|
m_CommentsExpireTimer->SetInterval(300);
|
|
|
|
m_CommentsExpireTimer->OnTimerExpired.connect(boost::bind(&Service::CommentsExpireTimerHandler));
|
|
|
|
m_CommentsExpireTimer->Start();
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 01:16:43 +01:00
|
|
|
void Service::RemoveExpiredComments(void)
|
2013-01-30 14:28:13 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
Dictionary::Ptr comments = GetComments();
|
2013-01-30 14:28:13 +01:00
|
|
|
|
|
|
|
if (!comments)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vector<String> expiredComments;
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(comments);
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
String id;
|
|
|
|
Dictionary::Ptr comment;
|
|
|
|
BOOST_FOREACH(tie(id, comment), comments) {
|
|
|
|
if (IsCommentExpired(comment))
|
|
|
|
expiredComments.push_back(id);
|
|
|
|
}
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (expiredComments.size() > 0) {
|
2013-03-04 15:52:42 +01:00
|
|
|
BOOST_FOREACH(const String& id, expiredComments) {
|
2013-01-30 14:28:13 +01:00
|
|
|
comments->Remove(id);
|
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
Touch("comments");
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-13 22:36:24 +01:00
|
|
|
void Service::CommentsExpireTimerHandler(void)
|
2013-01-30 14:28:13 +01:00
|
|
|
{
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
|
2013-02-09 01:16:43 +01:00
|
|
|
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
|
|
|
}
|