Commit 89cc1f57 authored by Alessio Netti's avatar Alessio Netti
Browse files

Bugfixes

- Fixed a bug in the QueryEngine that caused wrong cache
sections to be selected for sensor data
- Now AverageAnalyzer actually computes sensor averages
parent 94cf7193
......@@ -4,7 +4,7 @@
#include "AverageAnalyzer.h"
AverageAnalyzer::AverageAnalyzer(const std::string& name) : AnalyzerTemplate(name) {}
AverageAnalyzer::AverageAnalyzer(const std::string& name) : AnalyzerTemplate(name) { _window = 0; }
AverageAnalyzer::~AverageAnalyzer() {
if(_buffer)
......@@ -15,16 +15,18 @@ void AverageAnalyzer::compute(int unitID) {
unsigned long long max=0, sum=0, avg=0;
for(const auto& in : _units[unitID]->getInputs()) {
// Getting only the most recent value
_buffer = _queryEngine.querySensor(in->getName(), 0, 0, _buffer);
// 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;
}
sum += _buffer->at(0).value;
if( _buffer->at(0).value > max)
max = _buffer->at(0).value;
for(const auto& v : *_buffer) {
sum += v.value;
if (v.value > max)
max = v.value;
}
}
reading_t out;
......
......@@ -13,11 +13,15 @@ public:
AverageAnalyzer(const std::string& name);
virtual ~AverageAnalyzer();
void setWindow(unsigned long long w) { _window = w; }
unsigned long long getWindow() { return _window; }
private:
void compute(int unitID) override;
vector<reading_t> *_buffer = NULL;
unsigned long long _window;
};
......
......@@ -16,7 +16,11 @@ void AverageConfigurator::sensorBase(SensorBase& s, CFG_VAL config) {
}
void AverageConfigurator::analyzer(AverageAnalyzer& a, CFG_VAL config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config)
{
if (boost::iequals(val.first, "window"))
a.setWindow(stoull(val.second.data()) * 1000000);
}
}
bool AverageConfigurator::unit(UnitTemplate<SensorBase>& u) {
......
......@@ -102,7 +102,7 @@ public:
vector<reading_t>* querySensor(const string& name, const uint64_t startTs, const uint64_t endTs, vector<reading_t>* buffer, const bool rel=true) {
if(!_callback)
throw runtime_error("Query Engine: callback not set!");
if(startTs > endTs)
if((startTs > endTs && !rel) || (startTs < endTs && rel))
throw invalid_argument("Query Engine: invalid time range!");
return _callback(name, startTs, endTs, buffer, rel);
}
......
......@@ -122,6 +122,7 @@ public:
return sensors;
}
//TODO: make all and all-recursive add sensors to existing ones
/**
* @brief Computes and instantiates units associated to the input analyzer
*
......
......@@ -186,7 +186,7 @@ public:
if( t < 0)
return -1;
// Converting from milliseconds to nanoseconds
int64_t offset = ( ( _cacheSize * t ) / (_cacheInterval * 1000000 ) ) + 1;
int64_t offset = ( ( (int64_t)_cacheSize * t ) / ( (int64_t)_cacheInterval * 1000000 ) ) + 1;
if(offset > _cacheSize)
return -1;
return ( _cacheSize + _cacheIndex - offset ) % _cacheSize;
......
Supports Markdown
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