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