Commit 09063423 authored by Axel Auweter's avatar Axel Auweter
Browse files

Add infrastructure for a simple "pure C" wrapper API to DCDBLib.

Make Mont-Blanc SlurmPlugin link against this C API.
parent 976f8bd8
......@@ -10,7 +10,8 @@ OBJS = src/connection.o \
src/sensordatastore.o \
src/timestamp.o \
src/sensorconfig.o \
src/sensorid.o
src/sensorid.o \
src/c_api.o
# List of public header files necessary to use this libray
PUBHEADERS = $(shell find include -type f -iname "*.h")
......
/*
* c_api.h
*
* Created on: May 27, 2015
* Author: Axel Auweter
*/
/**
* @file
* @brief This file contains a reduced public API for the
* DCDBLib library using C instead of C++ bindings.
*/
#ifndef DCDB_C_API_H
#define DCDB_C_API_H
#ifdef __cplusplus
extern "C" {
#endif
void dcdb_test_c_api();
#ifdef __cplusplus
}
#endif
#endif /* DCDB_C_API_H */
......@@ -37,6 +37,14 @@ public:
SensorId sensorId;
DCDBTimeStamp timeStamp;
uint64_t value;
#if 0
inline bool operator == (const SensorDataStoreReading& rhs) const {
return (sensorId == rhs.sensorId) &&
(timeStamp == rhs.timeStamp) &&
(value == rhs.value);}
#endif
};
/**
......@@ -65,7 +73,7 @@ public:
void setTTL(uint64_t ttl);
/**
* @brief This function querie a sensor's values in
* @brief This function queries a sensor's values in
* the given time range.
* @param result The list where the results will be stored.
* @param sid The SensorId to query.
......@@ -74,6 +82,16 @@ public:
*/
void query(std::list<SensorDataStoreReading>& result, SensorId sid, DCDBTimeStamp start, DCDBTimeStamp end);
/**
* @brief This function queries the integrated value (val * sec)
* of a sensor for the given time range.
* @param result The resulting integrated value.
* @param sid The SensorId to query.
* @param start Start of the time series.
* @param end End of the time series.
*/
void querySum(int64_t& result, SensorId sid, DCDBTimeStamp start, DCDBTimeStamp end);
/**
* @brief A shortcut constructor for a SensorDataStore object
* that allows accessing the data store through a
......
......@@ -109,6 +109,12 @@ public:
*/
bool patternMatch(std::string pattern);
#if 0
inline bool operator == (const SensorId& rhs) const {
return (data.raw[0] == rhs.data.raw[0]) &&
(data.raw[1] == rhs.data.raw[1]);}
#endif
SensorId();
SensorId(std::string mqttTopic);
virtual ~SensorId();
......
......@@ -63,6 +63,16 @@ public:
*/
void query(std::list<SensorDataStoreReading>& result, SensorId sid, DCDBTimeStamp start, DCDBTimeStamp end);
/**
* @brief This function queries the integrated value
* of a sensor for the given time range.
* @param result The resulting integrated value.
* @param sid The SensorId to query.
* @param start Start of the time series.
* @param end End of the time series.
*/
void querySum(int64_t& result, SensorId sid, DCDBTimeStamp start, DCDBTimeStamp end);
/**
* @brief This is the standard constructor of the SensorDataStoreImpl class.
* @param csb A CassandraBackend object to do the raw database access.
......
/*
* c_api.cpp
*
* Created on: May 27, 2015
* Author: Axel Auweter
*/
#include <cstdio>
#include "c_api.h"
void dcdb_test_c_api()
{
printf("Hello from your DCDB C API.\n");
}
......@@ -151,7 +151,7 @@ void SensorDataStoreImpl::setTTL(uint64_t ttl)
* @details
* This function issues a regular query to the data store
* and creates a SensorDataStoreReading object for each
* entry which is stores in the result list.
* entry which is stored in the result list.
*/
void SensorDataStoreImpl::query(std::list<SensorDataStoreReading>& result, SensorId sid, DCDBTimeStamp start, DCDBTimeStamp end)
{
......@@ -223,6 +223,49 @@ void SensorDataStoreImpl::query(std::list<SensorDataStoreReading>& result, Senso
cass_prepared_free(prepared);
}
/**
* @details
* This function generates an integrated value of the time series
* by first querying for the result set list using query() and then
* summing up the result.
*/
void SensorDataStoreImpl::querySum(int64_t& result, SensorId sid, DCDBTimeStamp start, DCDBTimeStamp end)
{
std::list<SensorDataStoreReading> queryResult;
/* Issue a standard query */
query(queryResult, sid, start, end);
/* Integrate the result */
result = 0;
SensorDataStoreReading prev;
for (std::list<SensorDataStoreReading>::iterator it = queryResult.begin(); it != queryResult.end(); it++) {
if (!(it == queryResult.begin())) {
SensorDataStoreReading cur = *it;
/* Calculate average between two readings */
int64_t avg = (cur.value + prev.value) / 2;
/* Calculate time difference */
uint64_t dt = cur.timeStamp.getRaw() - prev.timeStamp.getRaw();
/* Sum up (with lousy attempt to keep it numerically stable - should probably use double instead) */
if (dt > 10000000000) {
/* dt > 10s => convert dt to s first */
dt /= 1000000000;
result += avg * dt;
}
else {
/* dt < 10s => multiply first */
avg *= dt;
result += avg / 1000000000;
}
}
prev = *it;
}
}
/**
* @details
* This constructor sets the internal connection variable to
......@@ -284,6 +327,17 @@ void SensorDataStore::query(std::list<SensorDataStoreReading>& result, SensorId
impl->query(result, sid, start, end);
}
/**
* @details
* Instead of doing the actual work, this function simply
* forwards to the insert function of the SensorDataStoreImpl
* class.
*/
void SensorDataStore::querySum(int64_t& result, SensorId sid, DCDBTimeStamp start, DCDBTimeStamp end)
{
impl->querySum(result, sid, start, end);
}
/**
* @details
* This constructor allocates the implementation class which
......
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