Commit bb3f4462 authored by Carla Guillen's avatar Carla Guillen
Browse files

Factoring out statistic functions

parent 18e7d3da
/*
* CommonStatistics.h
*
* Created on: Aug 7, 2019
* Author: carla
*/
#ifndef ANALYTICS_INCLUDES_COMMONSTATISTICS_H_
#define ANALYTICS_INCLUDES_COMMONSTATISTICS_H_
#include <vector>
#include <cacheentry.h>
int64_t computeObs(std::vector<reading_t> &buffer) {
return buffer.size();
}
int64_t computeSum(std::vector<reading_t> &buffer) {
int64_t acc=0;
for(const auto& v : buffer)
acc += v.value;
return acc;
}
int64_t computeAvg(std::vector<reading_t> &buffer) {
int64_t acc=0, ctr=buffer.size();
for(const auto& v : buffer)
acc += v.value;
acc = ctr > 0 ? acc/ctr : acc;
return acc;
}
int64_t computeMax(std::vector<reading_t> &buffer) {
int64_t acc=0;
bool maxInit=false;
for(const auto& v : buffer)
if(v.value>acc || !maxInit) {
acc = v.value;
maxInit = true;
}
return acc;
}
int64_t computeMin(std::vector<reading_t> &buffer) {
int64_t acc=0;
bool minInit=false;
for(const auto& v : buffer)
if(v.value<acc || !minInit) {
acc = v.value;
minInit = true;
}
return acc;
}
int64_t computeStd(std::vector<reading_t> &buffer) {
int64_t avg = computeAvg(buffer);
int64_t acc=0, val=0, ctr=buffer.size();
for(const auto& v : buffer) {
val = v.value - avg;
acc += val*val;
}
acc = ctr > 0 ? sqrt(acc/ctr) : sqrt(acc);
return acc;
}
#endif /* ANALYTICS_INCLUDES_COMMONSTATISTICS_H_ */
......@@ -26,6 +26,7 @@
//================================================================================
#include "AggregatorOperator.h"
#include "../../includes/CommonStatistics.h"
AggregatorOperator::AggregatorOperator(const std::string& name) : OperatorTemplate(name) {
_window = 0;
......@@ -80,22 +81,22 @@ void AggregatorOperator::compute_internal(U_Ptr unit, vector<reading_t> *buffer)
if(op!=AggregatorSensorBase::QTL) {
switch (op) {
case AggregatorSensorBase::SUM:
reading.value = computeSum(_buffer);
reading.value = computeSum(*buffer);
break;
case AggregatorSensorBase::AVG:
reading.value = computeAvg(_buffer);
reading.value = computeAvg(*buffer);
break;
case AggregatorSensorBase::MIN:
reading.value = computeMin(_buffer);
reading.value = computeMin(*buffer);
break;
case AggregatorSensorBase::MAX:
reading.value = computeMax(_buffer);
reading.value = computeMax(*buffer);
break;
case AggregatorSensorBase::STD:
reading.value = computeStd(_buffer);
reading.value = computeStd(*buffer);
break;
case AggregatorSensorBase::OBS:
reading.value = computeObs(_buffer);
reading.value = computeObs(*buffer);
break;
default:
LOG(warning) << _name << ": Encountered unknown operation!";
......@@ -108,7 +109,7 @@ void AggregatorOperator::compute_internal(U_Ptr unit, vector<reading_t> *buffer)
}
if(!_quantileSensors.empty()) {
vector<int64_t> result = computeQuantiles(_buffer);
vector<int64_t> result = computeQuantiles(buffer);
for(unsigned idx=0; idx<result.size(); idx++) {
reading.value = result[idx];
_quantileSensors[idx]->storeReading(reading);
......@@ -116,58 +117,6 @@ void AggregatorOperator::compute_internal(U_Ptr unit, vector<reading_t> *buffer)
}
}
int64_t AggregatorOperator::computeObs(vector<reading_t> *buffer) {
return buffer->size();
}
int64_t AggregatorOperator::computeSum(vector<reading_t> *buffer) {
int64_t acc=0;
for(const auto& v : *buffer)
acc += v.value;
return acc;
}
int64_t AggregatorOperator::computeAvg(vector<reading_t> *buffer) {
int64_t acc=0, ctr=buffer->size();
for(const auto& v : *buffer)
acc += v.value;
acc = ctr > 0 ? acc/ctr : acc;
return acc;
}
int64_t AggregatorOperator::computeMax(vector<reading_t> *buffer) {
int64_t acc=0;
bool maxInit=false;
for(const auto& v : *_buffer)
if(v.value>acc || !maxInit) {
acc = v.value;
maxInit = true;
}
return acc;
}
int64_t AggregatorOperator::computeMin(vector<reading_t> *buffer) {
int64_t acc=0;
bool minInit=false;
for(const auto& v : *_buffer)
if(v.value<acc || !minInit) {
acc = v.value;
minInit = true;
}
return acc;
}
int64_t AggregatorOperator::computeStd(vector<reading_t> *buffer) {
int64_t avg = computeAvg(buffer);
int64_t acc=0, val=0, ctr=buffer->size();
for(const auto& v : *buffer) {
val = v.value - avg;
acc += val*val;
}
acc = ctr > 0 ? sqrt(acc/ctr) : sqrt(acc);
return acc;
}
vector<int64_t> AggregatorOperator::computeQuantiles(vector<reading_t> *buffer) {
size_t idx, mod;
vector<int64_t> result;
......
......@@ -62,12 +62,6 @@ protected:
void compute_internal(U_Ptr unit, vector<reading_t> *buffer);
// A separate method for each operation implies code redundancy, but also better efficiency and less useless
// variables used by specific operations lying around
int64_t computeObs(vector<reading_t> *buffer);
int64_t computeSum(vector<reading_t> *buffer);
int64_t computeAvg(vector<reading_t> *buffer);
int64_t computeMax(vector<reading_t> *buffer);
int64_t computeMin(vector<reading_t> *buffer);
int64_t computeStd(vector<reading_t> *buffer);
vector<int64_t> computeQuantiles(vector<reading_t> *buffer);
vector<reading_t> *_buffer;
......
......@@ -44,8 +44,8 @@ public:
}
private:
unsigned int _position;
unsigned int _scaling_factor;
unsigned int _position;
float _severity;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment