livestatus: more fixes on joins, ptrs

refs #4372
This commit is contained in:
Michael Friedrich 2013-07-10 16:11:40 +02:00
parent 8033586f66
commit 2a097d0b04
20 changed files with 84 additions and 71 deletions

View File

@ -26,10 +26,10 @@ using namespace livestatus;
AndFilter::AndFilter(void)
{ }
bool AndFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
bool AndFilter::Apply(const Table::Ptr& table, const Value& row)
{
BOOST_FOREACH(const Filter::Ptr& filter, m_Filters) {
if (!filter->Apply(table, object))
if (!filter->Apply(table, row))
return false;
}

View File

@ -37,7 +37,7 @@ public:
AndFilter(void);
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
virtual bool Apply(const Table::Ptr& table, const Value& row);
};
}

View File

@ -29,11 +29,11 @@ AttributeFilter::AttributeFilter(const String& column, const String& op, const S
: m_Column(column), m_Operator(op), m_Operand(operand)
{ }
bool AttributeFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
{
Column column = table->GetColumn(m_Column);
Value value = column.ExtractValue(object);
Value value = column.ExtractValue(row);
if (value.IsObjectType<Array>()) {
if (m_Operator == ">=") {

View File

@ -37,7 +37,7 @@ public:
AttributeFilter(const String& column, const String& op, const String& operand);
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
virtual bool Apply(const Table::Ptr& table, const Value& row);
protected:
String m_Column;

View File

@ -26,14 +26,14 @@ Column::Column(const ValueAccessor& valueAccessor, const ObjectAccessor& objectA
: m_ValueAccessor(valueAccessor), m_ObjectAccessor(objectAccessor)
{ }
Value Column::ExtractValue(const Object::Ptr& uobject) const
Value Column::ExtractValue(const Value& urow) const
{
Object::Ptr object;
Value row;
if (!m_ObjectAccessor.empty())
object = m_ObjectAccessor(uobject);
row = m_ObjectAccessor(urow);
else
object = uobject;
row = urow;
return m_ValueAccessor(object);
return m_ValueAccessor(row);
}

View File

@ -31,12 +31,12 @@ namespace livestatus
class Column
{
public:
typedef boost::function<Value (const Object::Ptr&)> ValueAccessor;
typedef boost::function<Object::Ptr (const Object::Ptr&)> ObjectAccessor;
typedef boost::function<Value (const Value&)> ValueAccessor;
typedef boost::function<Value (const Value&)> ObjectAccessor;
Column(const ValueAccessor& valueAccessor, const ObjectAccessor& objectAccessor);
Value ExtractValue(const Object::Ptr& uobject) const;
Value ExtractValue(const Value& urow) const;
private:
ValueAccessor m_ValueAccessor;

View File

@ -19,7 +19,6 @@
#include "livestatus/commentstable.h"
#include "livestatus/servicestable.h"
#include "livestatus/hoststable.h"
#include "icinga/service.h"
#include "base/dynamictype.h"
#include "base/objectlock.h"
@ -49,8 +48,6 @@ void CommentsTable::AddColumns(Table *table, const String& prefix,
table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
// TODO: Join hosts and services table with prefix
HostsTable::AddColumns(table, "host_", &CommentsTable::HostAccessor);
ServicesTable::AddColumns(table, "service_", &CommentsTable::ServiceAccessor);
}
@ -70,10 +67,6 @@ void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
ObjectLock olock(comments);
/*Value comment;
BOOST_FOREACH(boost::tie(boost::tuples::ignore, comment), comments) {
addRowFn(comment);
}*/
String id;
BOOST_FOREACH(boost::tie(id, boost::tuples::ignore), comments) {
addRowFn(id);
@ -81,16 +74,6 @@ void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
}
}
Object::Ptr CommentsTable::HostAccessor(const Value& row)
{
Service::Ptr svc = Service::GetOwnerByCommentID(row);
if (!svc)
return Value();
return svc->GetHost();
}
Object::Ptr CommentsTable::ServiceAccessor(const Value& row)
{
return Service::GetOwnerByCommentID(row);
@ -100,6 +83,9 @@ Value CommentsTable::AuthorAccessor(const Value& row)
{
Dictionary::Ptr comment = Service::GetCommentByID(row);
if (!comment)
return Value();
return comment->Get("author");
}
@ -107,6 +93,9 @@ Value CommentsTable::CommentAccessor(const Value& row)
{
Dictionary::Ptr comment = Service::GetCommentByID(row);
if (!comment)
return Value();
return comment->Get("text");
}
@ -114,6 +103,9 @@ Value CommentsTable::IdAccessor(const Value& row)
{
Dictionary::Ptr comment = Service::GetCommentByID(row);
if (!comment)
return Value();
return comment->Get("legacy_id");
}
@ -121,6 +113,9 @@ Value CommentsTable::EntryTimeAccessor(const Value& row)
{
Dictionary::Ptr comment = Service::GetCommentByID(row);
if (!comment)
return Value();
return comment->Get("entry_time");
}
@ -134,6 +129,9 @@ Value CommentsTable::IsServiceAccessor(const Value& row)
{
Service::Ptr svc = Service::GetOwnerByCommentID(row);
if (!svc)
return Value();
return (svc->IsHostCheck() ? 0 : 1);
}
@ -153,6 +151,9 @@ Value CommentsTable::EntryTypeAccessor(const Value& row)
{
Dictionary::Ptr comment = Service::GetCommentByID(row);
if (!comment)
return Value();
return comment->Get("entry_type");
}
@ -160,6 +161,9 @@ Value CommentsTable::ExpiresAccessor(const Value& row)
{
Dictionary::Ptr comment = Service::GetCommentByID(row);
if (!comment)
return Value();
return comment->Get("expires");
}
@ -167,5 +171,8 @@ Value CommentsTable::ExpireTimeAccessor(const Value& row)
{
Dictionary::Ptr comment = Service::GetCommentByID(row);
if (!comment)
return Value();
return comment->Get("expire_time");
}

View File

@ -45,7 +45,7 @@ public:
protected:
virtual void FetchRows(const AddRowFunction& addRowFn);
static Object::Ptr HostAccessor(const Value& row);
private:
static Object::Ptr ServiceAccessor(const Value& row);
static Value AuthorAccessor(const Value& row);

View File

@ -18,6 +18,7 @@
******************************************************************************/
#include "livestatus/downtimestable.h"
#include "livestatus/servicestable.h"
#include "icinga/service.h"
#include "base/dynamictype.h"
#include "base/objectlock.h"
@ -47,7 +48,7 @@ void DowntimesTable::AddColumns(Table *table, const String& prefix,
table->AddColumn(prefix + "duration", Column(&DowntimesTable::DurationAccessor, objectAccessor));
table->AddColumn(prefix + "triggered_by", Column(&DowntimesTable::TriggeredByAccessor, objectAccessor));
// TODO: Join services table.
ServicesTable::AddColumns(table, "service_", &DowntimesTable::ServiceAccessor);
}
String DowntimesTable::GetName(void) const
@ -66,10 +67,6 @@ void DowntimesTable::FetchRows(const AddRowFunction& addRowFn)
ObjectLock olock(downtimes);
/*Value downtime;
BOOST_FOREACH(boost::tie(boost::tuples::ignore, downtime), downtimes) {
addRowFn(downtime);
}*/
String id;
BOOST_FOREACH(boost::tie(id, boost::tuples::ignore), downtimes) {
addRowFn(id);
@ -77,6 +74,11 @@ void DowntimesTable::FetchRows(const AddRowFunction& addRowFn)
}
}
Object::Ptr DowntimesTable::ServiceAccessor(const Value& row)
{
return Service::GetOwnerByDowntimeID(row);
}
Value DowntimesTable::AuthorAccessor(const Value& row)
{
Dictionary::Ptr downtime = Service::GetDowntimeByID(row);
@ -142,18 +144,14 @@ Value DowntimesTable::FixedAccessor(const Value& row)
Value DowntimesTable::DurationAccessor(const Value& row)
{
/*
Dictionary::Ptr downtime = Service::GetDowntimeByID(row);
return downtime->Get("duration");
*/
}
Value DowntimesTable::TriggeredByAccessor(const Value& row)
{
/*
Dictionary::Ptr downtime = Service::GetDowntimeByID(row);
return downtime->Get("triggered_by");
*/
}

View File

@ -45,6 +45,9 @@ public:
protected:
virtual void FetchRows(const AddRowFunction& addRowFn);
private:
static Object::Ptr ServiceAccessor(const Value& row);
static Value AuthorAccessor(const Value& row);
static Value CommentAccessor(const Value& row);
static Value IdAccessor(const Value& row);

View File

@ -33,7 +33,7 @@ class Filter : public Object
public:
DECLARE_PTR_TYPEDEFS(Filter);
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object) = 0;
virtual bool Apply(const Table::Ptr& table, const Value& row) = 0;
protected:
Filter(void);

View File

@ -26,7 +26,7 @@ NegateFilter::NegateFilter(const Filter::Ptr& inner)
: m_Inner(inner)
{ }
bool NegateFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
bool NegateFilter::Apply(const Table::Ptr& table, const Value& row)
{
return !m_Inner->Apply(table, object);
return !m_Inner->Apply(table, row);
}

View File

@ -37,7 +37,7 @@ public:
NegateFilter(const Filter::Ptr& inner);
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
virtual bool Apply(const Table::Ptr& table, const Value& row);
private:
Filter::Ptr m_Inner;

View File

@ -26,13 +26,13 @@ using namespace livestatus;
OrFilter::OrFilter(void)
{ }
bool OrFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
bool OrFilter::Apply(const Table::Ptr& table, const Value& row)
{
if (m_Filters.empty())
return true;
BOOST_FOREACH(const Filter::Ptr& filter, m_Filters) {
if (filter->Apply(table, object))
if (filter->Apply(table, row))
return true;
}

View File

@ -37,7 +37,7 @@ public:
OrFilter(void);
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
virtual bool Apply(const Table::Ptr& table, const Value& row);
};
}

View File

@ -27,6 +27,7 @@
#include "base/convert.h"
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include "base/exception.h"
#include <boost/algorithm/string/classification.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/foreach.hpp>
@ -220,7 +221,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
return;
}
std::vector<Object::Ptr> objects = table->FilterRows(m_Filter);
std::vector<Value> objects = table->FilterRows(m_Filter);
std::vector<String> columns;
if (m_Columns.size() > 0)
@ -231,7 +232,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
Array::Ptr rs = boost::make_shared<Array>();
if (m_Stats.empty()) {
BOOST_FOREACH(const Object::Ptr& object, objects) {
BOOST_FOREACH(const Value& object, objects) {
Array::Ptr row = boost::make_shared<Array>();
BOOST_FOREACH(const String& columnName, columns) {
@ -245,7 +246,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
} else {
std::vector<int> stats(m_Stats.size(), 0);
BOOST_FOREACH(const Object::Ptr& object, objects) {
BOOST_FOREACH(const Value& object, objects) {
int index = 0;
BOOST_FOREACH(const Filter::Ptr filter, m_Stats) {
if (filter->Apply(table, object))
@ -316,6 +317,10 @@ bool Query::Execute(const Stream::Ptr& stream)
else
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid livestatus query verb."));
} catch (const std::exception& ex) {
StackTrace *st = Exception::GetLastStackTrace();
std::ostringstream info;
st->Print(info);
Log(LogWarning, "livestatus", info.str());
SendResponse(stream, 452, boost::diagnostic_information(ex));
}

View File

@ -119,7 +119,7 @@ void ServicesTable::AddColumns(Table *table, const String& prefix,
table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor));
table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor));
HostsTable::AddColumns(table, "host_", &ServicesTable::HostAccessor);
HostsTable::AddColumns(table, "host_", boost::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
}
String ServicesTable::GetName(void) const
@ -134,9 +134,9 @@ void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
}
}
Object::Ptr ServicesTable::HostAccessor(const Value& row)
Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
{
return static_cast<Service::Ptr>(row)->GetHost();
return static_cast<Service::Ptr>(parentObjectAccessor(row))->GetHost();
}
Value ServicesTable::ShortNameAccessor(const Value& row)

View File

@ -45,7 +45,7 @@ public:
protected:
virtual void FetchRows(const AddRowFunction& addRowFn);
static Object::Ptr HostAccessor(const Value& row);
static Object::Ptr HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor);
static Value ShortNameAccessor(const Value& row);
static Value DisplayNameAccessor(const Value& row);

View File

@ -101,19 +101,19 @@ std::vector<String> Table::GetColumnNames(void) const
return names;
}
std::vector<Object::Ptr> Table::FilterRows(const Filter::Ptr& filter)
std::vector<Value> Table::FilterRows(const Filter::Ptr& filter)
{
std::vector<Object::Ptr> rs;
std::vector<Value> rs;
FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, _1));
return rs;
}
void Table::FilteredAddRow(std::vector<Object::Ptr>& rs, const Filter::Ptr& filter, const Object::Ptr& object)
void Table::FilteredAddRow(std::vector<Value>& rs, const Filter::Ptr& filter, const Value& row)
{
if (!filter || filter->Apply(GetSelf(), object))
rs.push_back(object);
if (!filter || filter->Apply(GetSelf(), row))
rs.push_back(row);
}
Value Table::ZeroAccessor(const Object::Ptr&)

View File

@ -45,7 +45,7 @@ public:
virtual String GetName(void) const = 0;
std::vector<Object::Ptr> FilterRows(const shared_ptr<Filter>& filter);
std::vector<Value> FilterRows(const shared_ptr<Filter>& filter);
void AddColumn(const String& name, const Column& column);
Column GetColumn(const String& name) const;
@ -65,7 +65,7 @@ protected:
private:
std::map<String, Column> m_Columns;
void FilteredAddRow(std::vector<Object::Ptr>& rs, const shared_ptr<Filter>& filter, const Object::Ptr& object);
void FilteredAddRow(std::vector<Value>& rs, const shared_ptr<Filter>& filter, const Value& row);
};
}