summaryrefslogtreecommitdiffstats
path: root/lib/livestatus/minaggregator.cpp
blob: 06cb76e2220f5487bb72d969ce3495b8336bafd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "livestatus/minaggregator.hpp"

using namespace icinga;

MinAggregator::MinAggregator(String attr)
	: m_MinAttr(std::move(attr))
{ }

MinAggregatorState *MinAggregator::EnsureState(AggregatorState **state)
{
	if (!*state)
		*state = new MinAggregatorState();

	return static_cast<MinAggregatorState *>(*state);
}

void MinAggregator::Apply(const Table::Ptr& table, const Value& row, AggregatorState **state)
{
	Column column = table->GetColumn(m_MinAttr);

	Value value = column.ExtractValue(row);

	MinAggregatorState *pstate = EnsureState(state);

	if (value < pstate->Min)
		pstate->Min = value;
}

double MinAggregator::GetResultAndFreeState(AggregatorState *state) const
{
	MinAggregatorState *pstate = EnsureState(&state);

	double result;

	if (pstate->Min == DBL_MAX)
		result = 0;
	else
		result = pstate->Min;

	delete pstate;

	return result;
}