Commit 3831fea4 authored by Michael Ott's avatar Michael Ott
Browse files

Fix unit conversions and scaling factor bugs in doQuery()

parent fd44b2a6
......@@ -117,9 +117,8 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
}
/* Convert the unit if requested */
if (DCDB::UnitConv::fromString(valueFormat.unit) != baseUnit) {
if (!DCDB::UnitConv::convert((*reading).value, baseUnit, DCDB::UnitConv::fromString(valueFormat.unit)))
if ((DCDB::UnitConv::fromString(valueFormat.unit) != DCDB::Unit_None) && (DCDB::UnitConv::fromString(valueFormat.unit) != baseUnit)) {
if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(valueFormat.unit)))
std::cerr << "Warning, cannot convert units ("
<< DCDB::UnitConv::toString(baseUnit) << " -> "
<< valueFormat.unit << ")"
......@@ -146,7 +145,7 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
if( DCDB::delta(current, previous, &result) == DCDB::DCDB_OP_OVERFLOW )
notifyOverflow = true;
if (DCDB::UnitConv::fromString(deltaFormat.unit) != baseUnit) {
if ((DCDB::UnitConv::fromString(deltaFormat.unit) != DCDB::Unit_None) && (DCDB::UnitConv::fromString(deltaFormat.unit) != baseUnit)) {
if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(deltaFormat.unit)))
std::cerr << "Warning, cannot convert units ("
<< DCDB::UnitConv::toString(baseUnit) << " -> "
......@@ -170,10 +169,10 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
if( DCDB::scale(&previous, deltaTFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
notifyOverflow = true;
}
if( DCDB::delta(current, prevT, &result) == DCDB::DCDB_OP_OVERFLOW)
if( DCDB::delta(current, previous, &result) == DCDB::DCDB_OP_OVERFLOW)
notifyOverflow = true;
if (DCDB::UnitConv::fromString(deltaTFormat.unit) != baseUnit) {
if ((DCDB::UnitConv::fromString(deltaTFormat.unit) != DCDB::Unit_None) && (DCDB::UnitConv::fromString(deltaTFormat.unit) != baseUnit)) {
if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(deltaTFormat.unit)))
std::cerr << "Warning, cannot convert units ("
<< DCDB::UnitConv::toString(baseUnit) << " -> "
......@@ -203,7 +202,7 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
if( DCDB::derivative(current, previous, (*reading).timeStamp.getRaw(), prevT, &result) == DCDB::DCDB_OP_OVERFLOW)
notifyOverflow = true;
if (DCDB::UnitConv::fromString(derivativeFormat.unit) != baseUnit) {
if ((DCDB::UnitConv::fromString(derivativeFormat.unit) != DCDB::Unit_None) && (DCDB::UnitConv::fromString(derivativeFormat.unit) != baseUnit)) {
if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(derivativeFormat.unit)))
std::cerr << "Warning, cannot convert units ("
<< DCDB::UnitConv::toString(baseUnit) << " -> "
......@@ -233,7 +232,7 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
if( DCDB::integral(current, previous, (*reading).timeStamp.getRaw(), prevT, &result) == DCDB::DCDB_OP_OVERFLOW)
notifyOverflow = true;
if (DCDB::UnitConv::fromString(integralFormat.unit) != baseUnit) {
if ((DCDB::UnitConv::fromString(integralFormat.unit) != DCDB::Unit_None) && (DCDB::UnitConv::fromString(integralFormat.unit) != baseUnit)) {
if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(integralFormat.unit)))
std::cerr << "Warning, cannot convert units ("
<< DCDB::UnitConv::toString(baseUnit) << " -> "
......@@ -252,7 +251,7 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
}
void DCDBQuery::checkModifier(std::list<std::string>::iterator it, double* scalingFactor, std::string* unit) {
void DCDBQuery::checkModifier(std::list<std::string>::iterator it, struct outputFormat *format) {
std::string modifierStr = it->substr(it->find('/')+1, it->length());
......@@ -261,10 +260,10 @@ void DCDBQuery::checkModifier(std::list<std::string>::iterator it, double* scali
boost::regex e("[0-9]*\.?[0-9]*", boost::regex::extended);
if (boost::regex_match(modifierStr, e))
sscanf(modifierStr.c_str(), "%lf", scalingFactor);
sscanf(modifierStr.c_str(), "%lf", &(format->scalingFactor));
else
unit = &modifierStr;
format->unit = modifierStr;
}
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
......@@ -282,7 +281,6 @@ void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DC
/* Iterate over list of sensors requested by the user */
for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
scalingFactor = 1;
unit = "none";
notifyOverflow = false;
......@@ -319,65 +317,46 @@ void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DC
/* Iterate over the list to detect all instances of the sensor with name "sensorName" */
while(it != sensors.end()) {
if(it->find(sensorName) != std::string::npos) {
/* Check first if the sensor we're looking is part of a function ...*/
if(boost::regex_search(*it, match, functExp)) {
if(match[0].compare("delta")==0) {
deltaFormat.printValue = true;
/* ...and for each function, check if we need a different scaling factor or unit... */
if(it->find('/') != std::string::npos) {
checkModifier(it, &scalingFactor, &unit);
deltaFormat.scalingFactor = scalingFactor;
deltaFormat.unit = unit;
checkModifier(it, &deltaFormat);
}
}
else if(match[0].compare("delta_t")==0) {
deltaTFormat.printValue = true;
if(it->find('/') != std::string::npos) {
checkModifier(it, &scalingFactor, &unit);
deltaTFormat.scalingFactor = scalingFactor;
deltaTFormat.unit = unit;
checkModifier(it, &deltaTFormat);
}
}
else if(match[0].compare("derivative")==0) {
derivativeFormat.printValue = true;
if(it->find('/') != std::string::npos) {
checkModifier(it, &scalingFactor, &unit);
derivativeFormat.scalingFactor = scalingFactor;
derivativeFormat.unit = unit;
checkModifier(it, &derivativeFormat);
}
}
else if(match[0].compare("integral")==0) {
integralFormat.printValue = true;
if(it->find('/') != std::string::npos) {
checkModifier(it, &scalingFactor, &unit);
integralFormat.scalingFactor = scalingFactor;
integralFormat.unit = unit;
checkModifier(it, &integralFormat);
}
}
}
/* ...otherwise, print just the sensor values and check if they are requested in a different scaling factor or unit... */
else {
valueFormat.printValue = true;
if(it->find('/') != std::string::npos) {
checkModifier(it, &scalingFactor, &unit);
valueFormat.scalingFactor = scalingFactor;
valueFormat.unit = unit;
checkModifier(it, &valueFormat);
}
}
/*...remove this instance of the sensor from the list */
......
......@@ -82,7 +82,7 @@ public:
void genOutput(std::list<DCDB::SensorDataStoreReading> &results);
void check(std::list<std::string>::iterator it , double* scalingFactor);
void checkModifier(std::list<std::string>::iterator it, double* scalingFactor, std::string* unit);
void checkModifier(std::list<std::string>::iterator it, struct outputFormat *format);
void doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end);
......
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