AggregatorAnalyzer.cpp 3.73 KB
Newer Older
Alessio Netti's avatar
Alessio Netti committed
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
//
// Created by Netti, Alessio on 16.01.19.
//

#include "AggregatorAnalyzer.h"

const std::string AggregatorAnalyzer::opNames[] = {"sum", "average", "maximum", "minimum"};

AggregatorAnalyzer::AggregatorAnalyzer(const std::string& name) : AnalyzerTemplate(name) { _window = 0; _op = SUM; }

AggregatorAnalyzer::~AggregatorAnalyzer() {
    if(_buffer)
        delete _buffer;
}

void AggregatorAnalyzer::printConfig(LOG_LEVEL ll) {
    LOG_VAR(ll) << "            Window:          " << _window;
    LOG_VAR(ll) << "            Operation:       " << opNames[_op];
    AnalyzerTemplate<SensorBase>::printConfig(ll);
}

void AggregatorAnalyzer::compute(int unitID) {
    switch(_op) {
        case SUM:
            computeSum(unitID);
            break;
        case AVG:
            computeAvg(unitID);
            break;
        case MIN:
            computeMin(unitID);
            break;
        case MAX:
            computeMax(unitID);
            break;
        default:
            break;
    }
}

void AggregatorAnalyzer::computeSum(int unitID) {
42
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
43
44
45
46

    for(const auto& in : _units[unitID]->getInputs()) {
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
47
        if(!_buffer || _buffer->empty()) {
Alessio Netti's avatar
Alessio Netti committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
            LOG(error) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
            return;
        }

        for(const auto& v : *_buffer)
            acc += v.value;
    }

    reading_t out;
    out.timestamp = getTimestamp();
    out.value = acc;
    _units[unitID]->getOutputs()[0]->storeReading(out);
}

void AggregatorAnalyzer::computeAvg(int unitID) {
63
    int64_t acc=0, ctr=0;
Alessio Netti's avatar
Alessio Netti committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

    for(const auto& in : _units[unitID]->getInputs()) {
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
        if(_buffer->empty()) {
            LOG(error) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
            return;
        }

        for(const auto& v : *_buffer) {
            acc += v.value;
            ctr++;
        }
    }

    acc = ctr > 0 ? acc/ctr : acc;

    reading_t out;
    out.timestamp = getTimestamp();
    out.value = acc;
    _units[unitID]->getOutputs()[0]->storeReading(out);
}

void AggregatorAnalyzer::computeMax(int unitID) {
88
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    for(const auto& in : _units[unitID]->getInputs()) {
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
        if(_buffer->empty()) {
            LOG(error) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
            return;
        }

        for(const auto& v : *_buffer)
            if( v.value>acc )
                acc = v.value;
    }

    reading_t out;
    out.timestamp = getTimestamp();
    out.value = acc;
    _units[unitID]->getOutputs()[0]->storeReading(out);
}

void AggregatorAnalyzer::computeMin(int unitID) {
110
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    bool minInit=false;

    for(const auto& in : _units[unitID]->getInputs()) {
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
        if(_buffer->empty()) {
            LOG(error) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
            return;
        }

        for(const auto& v : *_buffer)
            if( v.value<acc || !minInit ) {
                acc = v.value;
                minInit = true;
            }
    }

    reading_t out;
    out.timestamp = getTimestamp();
    out.value = acc;
    _units[unitID]->getOutputs()[0]->storeReading(out);

}