AggregatorAnalyzer.cpp 4.9 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

#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);
}

44
void AggregatorAnalyzer::compute(U_Ptr unit) {
Alessio Netti's avatar
Alessio Netti committed
45
46
    switch(_op) {
        case SUM:
47
            computeSum(unit);
Alessio Netti's avatar
Alessio Netti committed
48
49
            break;
        case AVG:
50
            computeAvg(unit);
Alessio Netti's avatar
Alessio Netti committed
51
52
            break;
        case MIN:
53
            computeMin(unit);
Alessio Netti's avatar
Alessio Netti committed
54
55
            break;
        case MAX:
56
            computeMax(unit);
Alessio Netti's avatar
Alessio Netti committed
57
58
59
60
61
62
            break;
        default:
            break;
    }
}

63
void AggregatorAnalyzer::computeSum(U_Ptr unit) {
64
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
65

66
    for(const auto& in : unit->getInputs()) {
Alessio Netti's avatar
Alessio Netti committed
67
68
        // 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
            return;
        }

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

    reading_t out;
    out.timestamp = getTimestamp();
    out.value = acc;
81
    unit->getOutputs()[0]->storeReading(out);
Alessio Netti's avatar
Alessio Netti committed
82
83
}

84
void AggregatorAnalyzer::computeAvg(U_Ptr unit) {
85
    int64_t acc=0, ctr=0;
Alessio Netti's avatar
Alessio Netti committed
86

87
    for(const auto& in : unit->getInputs()) {
Alessio Netti's avatar
Alessio Netti committed
88
89
        // 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
            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;
106
    unit->getOutputs()[0]->storeReading(out);
Alessio Netti's avatar
Alessio Netti committed
107
108
}

109
void AggregatorAnalyzer::computeMax(U_Ptr unit) {
110
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
111

112
    for(const auto& in : unit->getInputs()) {
Alessio Netti's avatar
Alessio Netti committed
113
114
        // 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
            return;
        }

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

    reading_t out;
    out.timestamp = getTimestamp();
    out.value = acc;
128
    unit->getOutputs()[0]->storeReading(out);
Alessio Netti's avatar
Alessio Netti committed
129
130
}

131
void AggregatorAnalyzer::computeMin(U_Ptr unit) {
132
    int64_t acc=0;
Alessio Netti's avatar
Alessio Netti committed
133
134
    bool minInit=false;

135
    for(const auto& in : unit->getInputs()) {
Alessio Netti's avatar
Alessio Netti committed
136
137
        // 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
            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;
153
    unit->getOutputs()[0]->storeReading(out);
Alessio Netti's avatar
Alessio Netti committed
154
155

}