]> granicus.if.org Git - icinga2/blob - lib/livestatus/stdaggregator.cpp
Docs: Explain across midnight time periods with an overlapping range
[icinga2] / lib / livestatus / stdaggregator.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "livestatus/stdaggregator.hpp"
4 #include <math.h>
5
6 using namespace icinga;
7
8 StdAggregator::StdAggregator(String attr)
9         : m_StdAttr(std::move(attr))
10 { }
11
12 StdAggregatorState *StdAggregator::EnsureState(AggregatorState **state)
13 {
14         if (!*state)
15                 *state = new StdAggregatorState();
16
17         return static_cast<StdAggregatorState *>(*state);
18 }
19
20 void StdAggregator::Apply(const Table::Ptr& table, const Value& row, AggregatorState **state)
21 {
22         Column column = table->GetColumn(m_StdAttr);
23
24         Value value = column.ExtractValue(row);
25
26         StdAggregatorState *pstate = EnsureState(state);
27
28         pstate->StdSum += value;
29         pstate->StdQSum += pow(value, 2);
30         pstate->StdCount++;
31 }
32
33 double StdAggregator::GetResultAndFreeState(AggregatorState *state) const
34 {
35         StdAggregatorState *pstate = EnsureState(&state);
36         double result = sqrt((pstate->StdQSum - (1 / pstate->StdCount) * pow(pstate->StdSum, 2)) / (pstate->StdCount - 1));
37         delete pstate;
38
39         return result;
40 }