From 904830ee3e10033fbb2cf3444963f45079179aac Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 11 Jul 2013 17:52:06 +0200 Subject: [PATCH] livestatus: add sum aggregator, refactor Filter/Stats handling refs #4398 --- components/livestatus/Makefile.am | 2 + components/livestatus/livestatus.vcxproj | 6 ++ .../livestatus/livestatus.vcxproj.filters | 18 ++++++ components/livestatus/query.cpp | 56 +++++++++++++++++-- components/livestatus/sumaggregator.cpp | 42 ++++++++++++++ components/livestatus/sumaggregator.h | 49 ++++++++++++++++ 6 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 components/livestatus/sumaggregator.cpp create mode 100644 components/livestatus/sumaggregator.h diff --git a/components/livestatus/Makefile.am b/components/livestatus/Makefile.am index 3699921c6..aaac41339 100644 --- a/components/livestatus/Makefile.am +++ b/components/livestatus/Makefile.am @@ -55,6 +55,8 @@ liblivestatus_la_SOURCES = \ servicestable.h \ statustable.cpp \ statustable.h \ + sumaggregator.cpp \ + sumaggregator.h \ timeperiodstable.cpp \ timeperiodstable.h \ table.cpp \ diff --git a/components/livestatus/livestatus.vcxproj b/components/livestatus/livestatus.vcxproj index 2c9440eb5..96c391fdb 100644 --- a/components/livestatus/livestatus.vcxproj +++ b/components/livestatus/livestatus.vcxproj @@ -19,6 +19,9 @@ + + + @@ -44,6 +47,9 @@ + + + diff --git a/components/livestatus/livestatus.vcxproj.filters b/components/livestatus/livestatus.vcxproj.filters index 68f87a242..f65abb3d8 100644 --- a/components/livestatus/livestatus.vcxproj.filters +++ b/components/livestatus/livestatus.vcxproj.filters @@ -42,6 +42,15 @@ Headerdateien + + Headerdateien + + + Headerdateien + + + Headerdateien + Headerdateien @@ -113,6 +122,15 @@ Quelldateien + + Quelldateien + + + Quelldateien + + + Quelldateien + Quelldateien diff --git a/components/livestatus/query.cpp b/components/livestatus/query.cpp index 02ce92f4e..56f56013d 100644 --- a/components/livestatus/query.cpp +++ b/components/livestatus/query.cpp @@ -19,6 +19,7 @@ #include "livestatus/query.h" #include "livestatus/countaggregator.h" +#include "livestatus/sumaggregator.h" #include "livestatus/attributefilter.h" #include "livestatus/negatefilter.h" #include "livestatus/orfilter.h" @@ -81,10 +82,10 @@ Query::Query(const std::vector& lines) boost::algorithm::split(m_Columns, params, boost::is_any_of(" ")); else if (header == "ColumnHeaders") m_ColumnHeaders = (params == "on"); - else if (header == "Filter" || header == "Stats") { + else if (header == "Filter") { Filter::Ptr filter = ParseFilter(params); - + if (!filter) { m_Verb = "ERROR"; m_ErrorCode = 452; @@ -92,14 +93,57 @@ Query::Query(const std::vector& lines) return; } - std::deque& deq = (header == "Filter") ? filters : stats; + std::deque& deq = filters; deq.push_back(filter); - - if (deq == stats) { + } + else if (header == "Stats") { + + std::vector tokens; + boost::algorithm::split(tokens, params, boost::is_any_of(" ")); + + String aggregate_arg = tokens[0]; + String aggregate_attr = tokens[1]; + + if (aggregate_arg == "sum") { + Aggregator::Ptr aggregator = boost::make_shared(aggregate_attr); + aggregators.push_back(aggregator); + } + else if(aggregate_arg == "min") { + /* TODO */ + } + else if (aggregate_arg == "max") { + /* TODO */ + } + else if (aggregate_arg == "avg") { + /* TODO */ + } + else if (aggregate_arg == "std") { + /* TODO */ + } + else if (aggregate_arg == "suminv") { + /* TODO */ + } + else if (aggregate_arg == "avginv") { + /* TODO */ + + } else { + Filter::Ptr filter = ParseFilter(params); + + if (!filter) { + m_Verb = "ERROR"; + m_ErrorCode = 452; + m_ErrorMessage = "Invalid filter specification."; + return; + } + + std::deque& deq = stats; + deq.push_back(filter); + Aggregator::Ptr aggregator = boost::make_shared(); aggregator->SetFilter(filter); aggregators.push_back(aggregator); } + } else if (header == "Or" || header == "And") { std::deque& deq = (header == "Or" || header == "And") ? filters : stats; @@ -138,7 +182,7 @@ Query::Query(const std::vector& lines) filters.pop_back(); deq.push_back(boost::make_shared(filter)); - + if (deq == stats) { Aggregator::Ptr aggregator = aggregators.back(); aggregator->SetFilter(filter); diff --git a/components/livestatus/sumaggregator.cpp b/components/livestatus/sumaggregator.cpp new file mode 100644 index 000000000..8004a69c3 --- /dev/null +++ b/components/livestatus/sumaggregator.cpp @@ -0,0 +1,42 @@ +/****************************************************************************** + * 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 "livestatus/sumaggregator.h" + +using namespace livestatus; + +SumAggregator::SumAggregator(const String& attr) + : m_Sum(0) +{ + m_SumAttr = attr; +} + +void SumAggregator::Apply(const Table::Ptr& table, const Value& row) +{ + Column column = table->GetColumn(m_SumAttr); + + Value value = column.ExtractValue(row); + + m_Sum += value; +} + +double SumAggregator::GetResult(void) const +{ + return m_Sum; +} diff --git a/components/livestatus/sumaggregator.h b/components/livestatus/sumaggregator.h new file mode 100644 index 000000000..1c7142b1c --- /dev/null +++ b/components/livestatus/sumaggregator.h @@ -0,0 +1,49 @@ +/****************************************************************************** + * 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. * + ******************************************************************************/ + +#ifndef SUMAGGREGATOR_H +#define SUMAGGREGATOR_H + +#include "livestatus/table.h" +#include "livestatus/aggregator.h" + +namespace livestatus +{ + +/** + * @ingroup livestatus + */ +class SumAggregator : public Aggregator +{ +public: + DECLARE_PTR_TYPEDEFS(SumAggregator); + + SumAggregator(const String& attr); + + virtual void Apply(const Table::Ptr& table, const Value& row); + virtual double GetResult(void) const; + +private: + double m_Sum; + String m_SumAttr; +}; + +} + +#endif /* SUMAGGREGATOR_H */ -- 2.40.0