AggregatorAnalyzer.cpp 4.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//================================================================================
// Name        : AggregatorAnalyzer.cpp
// Author      : Alessio Netti
// Copyright   : Leibniz Supercomputing Centre
// Description :
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2019-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
Alessio Netti's avatar
Alessio Netti committed
16
//
17
18
19
20
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
Alessio Netti's avatar
Alessio Netti committed
21
//
22
23
24
25
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
Alessio Netti's avatar
Alessio Netti committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#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) {
64
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
65
66
67
68

    for(const auto& in : _units[unitID]->getInputs()) {
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
69
        if(!_buffer || _buffer->empty()) {
70
            LOG(debug) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
Alessio Netti's avatar
Alessio Netti committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
            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) {
85
    int64_t acc=0, ctr=0;
Alessio Netti's avatar
Alessio Netti committed
86
87
88
89

    for(const auto& in : _units[unitID]->getInputs()) {
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
90
        if(!_buffer || _buffer->empty()) {
91
            LOG(debug) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
Alessio Netti's avatar
Alessio Netti committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
            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) {
110
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
111
112
113
114

    for(const auto& in : _units[unitID]->getInputs()) {
        // Getting the most recent values as specified in _window
        _buffer = _queryEngine.querySensor(in->getName(), _window, 0, _buffer);
115
        if(!_buffer || _buffer->empty()) {
116
            LOG(debug) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
Alessio Netti's avatar
Alessio Netti committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
            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) {
132
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
133
134
135
136
137
    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);
138
        if(!_buffer || _buffer->empty()) {
139
            LOG(debug) << "Analyzer " << _name << " cannot read from sensor " << in->getName() << "!";
Alessio Netti's avatar
Alessio Netti committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
            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);

}