/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
+ * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
******************************************************************************/
#include "livestatus/commentstable.hpp"
+#include "livestatus/hoststable.hpp"
#include "livestatus/servicestable.hpp"
#include "icinga/service.hpp"
-#include "base/dynamictype.hpp"
+#include "base/configtype.hpp"
#include "base/objectlock.hpp"
#include <boost/tuple/tuple.hpp>
-#include <boost/foreach.hpp>
using namespace icinga;
-CommentsTable::CommentsTable(void)
+CommentsTable::CommentsTable()
{
AddColumns(this);
}
void CommentsTable::AddColumns(Table *table, const String& prefix,
- const Column::ObjectAccessor& objectAccessor)
+ const Column::ObjectAccessor& objectAccessor)
{
table->AddColumn(prefix + "author", Column(&CommentsTable::AuthorAccessor, objectAccessor));
table->AddColumn(prefix + "comment", Column(&CommentsTable::CommentAccessor, objectAccessor));
table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
- ServicesTable::AddColumns(table, "service_", boost::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor));
+ /* order is important - host w/o services must not be empty */
+ ServicesTable::AddColumns(table, "service_", std::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor));
+ HostsTable::AddColumns(table, "host_", std::bind(&CommentsTable::HostAccessor, _1, objectAccessor));
}
-String CommentsTable::GetName(void) const
+String CommentsTable::GetName() const
{
return "comments";
}
-String CommentsTable::GetPrefix(void) const
+String CommentsTable::GetPrefix() const
{
return "comment";
}
void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
{
- BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
- Dictionary::Ptr comments = host->GetComments();
-
- ObjectLock olock(comments);
-
- String id;
- Comment::Ptr comment;
- BOOST_FOREACH(tie(id, comment), comments) {
- addRowFn(comment);
- }
+ for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
+ if (!addRowFn(comment, LivestatusGroupByNone, Empty))
+ return;
}
+}
+
+Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
+{
+ Comment::Ptr comment = static_cast<Comment::Ptr>(row);
- BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
- Dictionary::Ptr comments = service->GetComments();
+ Checkable::Ptr checkable = comment->GetCheckable();
- ObjectLock olock(comments);
+ Host::Ptr host;
+ Service::Ptr service;
+ tie(host, service) = GetHostService(checkable);
- String id;
- Comment::Ptr comment;
- BOOST_FOREACH(tie(id, comment), comments) {
- addRowFn(comment);
- }
- }
+ return host;
}
Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
{
Comment::Ptr comment = static_cast<Comment::Ptr>(row);
- return Checkable::GetOwnerByCommentID(comment->GetId()); // XXX: this might return a Host object
+
+ Checkable::Ptr checkable = comment->GetCheckable();
+
+ Host::Ptr host;
+ Service::Ptr service;
+ tie(host, service) = GetHostService(checkable);
+
+ return service;
}
Value CommentsTable::AuthorAccessor(const Value& row)
Value CommentsTable::TypeAccessor(const Value& row)
{
Comment::Ptr comment = static_cast<Comment::Ptr>(row);
- Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
+ Checkable::Ptr checkable = comment->GetCheckable();
if (!checkable)
return Empty;
Value CommentsTable::IsServiceAccessor(const Value& row)
{
Comment::Ptr comment = static_cast<Comment::Ptr>(row);
- Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
+ Checkable::Ptr checkable = comment->GetCheckable();
if (!checkable)
return Empty;