Commit b9a68de8 authored by Michael Ott's avatar Michael Ott
Browse files

Add rate operator to calculate rate per second of metrics

parent 233c373b
...@@ -58,7 +58,8 @@ namespace DCDB { ...@@ -58,7 +58,8 @@ namespace DCDB {
DCDB_OP_RESULT delta(uint64_t lh, uint64_t rh, int64_t* result); DCDB_OP_RESULT delta(uint64_t lh, uint64_t rh, int64_t* result);
DCDB_OP_RESULT derivative(int64_t lhx, int64_t rhx, uint64_t lht, uint64_t rht, int64_t* result, DCDB::Unit unit = DCDB::Unit_None); DCDB_OP_RESULT derivative(int64_t lhx, int64_t rhx, uint64_t lht, uint64_t rht, int64_t* result, DCDB::Unit unit = DCDB::Unit_None);
DCDB_OP_RESULT integral(int64_t x, uint64_t lht, uint64_t rht, int64_t* result, DCDB::Unit unit = DCDB::Unit_None); DCDB_OP_RESULT integral(int64_t x, uint64_t lht, uint64_t rht, int64_t* result, DCDB::Unit unit = DCDB::Unit_None);
DCDB_OP_RESULT rate(int64_t x, uint64_t lht, uint64_t rht, int64_t* result);
} /* End of namespace DCDB */ } /* End of namespace DCDB */
#endif /* sensoroperations_h */ #endif /* sensoroperations_h */
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <limits> #include <limits>
#include <cmath> #include <cmath>
#include "dcdb/sensoroperations.h" #include "dcdb/sensoroperations.h"
#include "timestamp.h"
namespace DCDB { namespace DCDB {
...@@ -203,4 +204,11 @@ DCDB_OP_RESULT integral(int64_t x, uint64_t lht, uint64_t rht, int64_t* result, ...@@ -203,4 +204,11 @@ DCDB_OP_RESULT integral(int64_t x, uint64_t lht, uint64_t rht, int64_t* result,
} }
}; };
DCDB_OP_RESULT rate(int64_t x, uint64_t lht, uint64_t rht, int64_t* result) {
int64_t dt = NS_TO_S(((int64_t) lht - rht));
*result = x / dt;
return DCDB_OP_SUCCESS;
};
} /* end DCDB namespace */ } /* end DCDB namespace */
...@@ -138,6 +138,9 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results, quer ...@@ -138,6 +138,9 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results, quer
case DCDB_OP_INTEGRAL: case DCDB_OP_INTEGRAL:
std::cout << ",Integral"; std::cout << ",Integral";
break; break;
case DCDB_OP_RATE:
std::cout << ",Rate";
break;
default: default:
break; break;
} }
...@@ -234,6 +237,14 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results, quer ...@@ -234,6 +237,14 @@ void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results, quer
} }
} }
break;} break;}
case DCDB_OP_RATE: {
int64_t prevValue = prevReading;
if (scaleAndConvert(value, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
if( (prevT > (uint64_t) 0) && DCDB::rate(value, ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS) {
resultOk = true;
}
}
break;}
default: default:
break; break;
} }
...@@ -285,6 +296,8 @@ void DCDBQuery::parseSensorSpecification(const std::string sensor, std::string& ...@@ -285,6 +296,8 @@ void DCDBQuery::parseSensorSpecification(const std::string sensor, std::string&
queryCfg.operation = DCDB_OP_DERIVATIVE; queryCfg.operation = DCDB_OP_DERIVATIVE;
} else if (boost::iequals(functName, "integral")) { } else if (boost::iequals(functName, "integral")) {
queryCfg.operation = DCDB_OP_INTEGRAL; queryCfg.operation = DCDB_OP_INTEGRAL;
} else if (boost::iequals(functName, "rate")) {
queryCfg.operation = DCDB_OP_RATE;
} else if (boost::iequals(functName, "min")) { } else if (boost::iequals(functName, "min")) {
queryCfg.aggregate = DCDB::AGGREGATE_MIN; queryCfg.aggregate = DCDB::AGGREGATE_MIN;
} else if (boost::iequals(functName, "max")) { } else if (boost::iequals(functName, "max")) {
......
...@@ -51,6 +51,7 @@ typedef enum { ...@@ -51,6 +51,7 @@ typedef enum {
DCDB_OP_DELTAT, DCDB_OP_DELTAT,
DCDB_OP_DERIVATIVE, DCDB_OP_DERIVATIVE,
DCDB_OP_INTEGRAL, DCDB_OP_INTEGRAL,
DCDB_OP_RATE,
DCDB_OP_WINTERMUTE, DCDB_OP_WINTERMUTE,
DCDB_OP_UNKNOWN, DCDB_OP_UNKNOWN,
} DCDB_OP_TYPE; } DCDB_OP_TYPE;
......
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