]> granicus.if.org Git - icinga2/blob - lib/livestatus/avgaggregator.cpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / livestatus / avgaggregator.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "livestatus/avgaggregator.hpp"
4
5 using namespace icinga;
6
7 AvgAggregator::AvgAggregator(String attr)
8         : m_AvgAttr(std::move(attr))
9 { }
10
11 AvgAggregatorState *AvgAggregator::EnsureState(AggregatorState **state)
12 {
13         if (!*state)
14                 *state = new AvgAggregatorState();
15
16         return static_cast<AvgAggregatorState *>(*state);
17 }
18
19 void AvgAggregator::Apply(const Table::Ptr& table, const Value& row, AggregatorState **state)
20 {
21         Column column = table->GetColumn(m_AvgAttr);
22
23         Value value = column.ExtractValue(row);
24
25         AvgAggregatorState *pstate = EnsureState(state);
26
27         pstate->Avg += value;
28         pstate->AvgCount++;
29 }
30
31 double AvgAggregator::GetResultAndFreeState(AggregatorState *state) const
32 {
33         AvgAggregatorState *pstate = EnsureState(&state);
34         double result = pstate->Avg / pstate->AvgCount;
35         delete pstate;
36
37         return result;
38 }