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

Add support for units in json export, fix minor bugs with empty readings

parent 5f46b600
......@@ -77,7 +77,7 @@ bool JsonExporterConfigurator::operatorAttributes(JsonExporterOperator& op, CFG_
}
}
if (op.getNodeSubstitution().empty()) {
if (!op.getTemplate() && op.getNodeSubstitution().empty()) {
LOG(error) << _operatorName << ": NodeRegex has not been set but is required";
return false;
}
......
......@@ -48,7 +48,13 @@ void JsonExporterOperator::compute(U_Ptr unit) {
}
void JsonExporterOperator::computeAsync() {
std::map<std::tuple<std::string, std::string, std::string>, reading_t> readings;
typedef struct {
int64_t value;
uint64_t timestamp;
std::string unit;
} jsonreading_t;
std::map<std::tuple<std::string, std::string, std::string>, jsonreading_t> readings;
for (unsigned i = 0; i < _units.size(); i++) {
for(unsigned int j=0; j<_units[i]->getInputs().size(); j++) {
std::string inputName = _units[i]->getInputs()[j]->getName();
......@@ -63,67 +69,76 @@ void JsonExporterOperator::computeAsync() {
} else {
size_t pos = inputName.find_last_of("/");
std::string name = pos == string::npos ? inputName : inputName.substr(pos+1);
reading_t r = _buffer[_buffer.size()-1];
jsonreading_t r;
r.value = _buffer[_buffer.size()-1].value;
r.timestamp = _buffer[_buffer.size()-1].timestamp;
if (_units[i]->getInputs()[j]->getMetadata() && _units[i]->getInputs()[j]->getMetadata()->getUnit()) {
r.unit = *(_units[i]->getInputs()[j]->getMetadata()->getUnit());
}
readings[std::make_tuple(node, device, name)] = r;
}
}
}
std::string nodeName, deviceName;
boost::property_tree::ptree nodes, node, devices, device;
for (auto &r: readings) {
if (nodeName != std::get<0>(r.first)) {
if (!deviceName.empty()) {
devices.push_back(std::make_pair("", device));
device.clear();
if (!readings.empty()) {
std::string nodeName, deviceName;
boost::property_tree::ptree nodes, node, devices, device;
for (auto &r: readings) {
if (nodeName != std::get<0>(r.first)) {
if (!deviceName.empty()) {
devices.push_back(std::make_pair("", device));
device.clear();
}
if (!nodeName.empty()) {
node.add_child("device", devices);
devices.clear();
nodes.add_child(nodeName, node);
node.clear();
}
nodeName = std::get<0>(r.first);
//node.add("name", nodeName);
deviceName = "";
}
if (!nodeName.empty()) {
node.add_child("device", devices);
devices.clear();
nodes.add_child(nodeName, node);
node.clear();
if (deviceName != std::get<1>(r.first)) {
if (!deviceName.empty()) {
devices.push_back(std::make_pair("", device));
device.clear();
}
deviceName = std::get<1>(r.first);
device.add("name", deviceName);
}
nodeName = std::get<0>(r.first);
//node.add("name", nodeName);
deviceName = "";
}
if (deviceName != std::get<1>(r.first)) {
if (!deviceName.empty()) {
devices.push_back(std::make_pair("", device));
device.clear();
boost::property_tree::ptree reading;
reading.add("timestamp", r.second.timestamp);
reading.add("value", r.second.value);
if (!r.second.unit.empty()) {
reading.add("unit", r.second.unit);
}
if (deviceName.empty()) {
node.push_back(std::make_pair(std::get<2>(r.first), reading));
} else {
device.push_back(std::make_pair(std::get<2>(r.first), reading));
}
deviceName = std::get<1>(r.first);
device.add("name", deviceName);
}
boost::property_tree::ptree reading;
reading.add("timestamp", r.second.timestamp);
reading.add("value", r.second.value);
if (deviceName.empty()) {
node.push_back(std::make_pair(std::get<2>(r.first), reading));
} else {
device.push_back(std::make_pair(std::get<2>(r.first), reading));
if (!deviceName.empty()) {
devices.push_back(std::make_pair("", device));
}
}
if (!deviceName.empty()) {
devices.push_back(std::make_pair("", device));
}
node.add_child("device", devices);
nodes.add_child(nodeName, node);
std::ofstream outFile;
std::string tmpFileName = _fileName + ".tmp";
outFile.open(tmpFileName);
if (outFile.is_open()) {
boost::property_tree::write_json(outFile, nodes, true);
outFile.close();
if (rename(tmpFileName.c_str(), _fileName.c_str()) != 0) {
LOG(error) << "Error renaming temporary output file " << tmpFileName << " => " << _fileName;
node.add_child("device", devices);
nodes.add_child(nodeName, node);
std::ofstream outFile;
std::string tmpFileName = _fileName + ".tmp";
outFile.open(tmpFileName);
if (outFile.is_open()) {
boost::property_tree::write_json(outFile, nodes, true);
outFile.close();
if (rename(tmpFileName.c_str(), _fileName.c_str()) != 0) {
LOG(error) << "Error renaming temporary output file " << tmpFileName << " => " << _fileName;
}
} else {
LOG(error) << "Error opening temporary output file " << tmpFileName;
}
} else {
LOG(error) << "Error opening temporary output file " << tmpFileName;
}
if (_timer && _keepRunning && !_disabled) {
_scheduledTime = nextReadingTime();
......
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