Commit 2733c672 authored by Alessio Netti's avatar Alessio Netti

Grafana: improved SensorNavigator usage

- Fixed a memory leak
- Improved exception management
- General refactoring of the code
parent f74909c0
......@@ -123,51 +123,30 @@ void RestAPI::POST_levels(endpointArgs) {
void RestAPI::POST_search(endpointArgs) {
res.body() = "[";
std::set<std::string> *treeOutput;
std::set<std::string> *treeOutput = nullptr;
//Get the element from the sensor navigator.
std::string parentNode = getQuery("node", queries);
if(parentNode == "") {
try {
treeOutput = _navigator->getNodes(0,false);
}
catch(std::domain_error &e) {
treeOutput = getQuery("sensors", queries)=="true" ? _navigator->getSensors(0, false) : _navigator->getNodes(0, false);
} catch(const std::exception &e) {
treeOutput = new std::set<std::string>();
treeOutput->insert("");
}
if(getQuery("sensors", queries) == "true") {
try {
treeOutput = _navigator->getSensors(0,false);
}
catch(std::domain_error &e) {
treeOutput->insert("");
}
}
}
else {
} else {
parentNode += "/";
// What does this do exactly?
if(_separator != "")
boost::replace_all(parentNode, "/", _separator);
try {
treeOutput = _navigator->getNodes(parentNode,false);
}
catch(std::domain_error &e) {
res.body() = "Unknown node: " + parentNode;
treeOutput = getQuery("sensors", queries)=="true" ? _navigator->getSensors(parentNode, false) : _navigator->getNodes(parentNode, false);
} catch(const std::exception &e) {
res.body() = "Encountered exception: " + std::string(e.what());
res.result(http::status::bad_request);
return;
}
if(getQuery("sensors", queries) == "true") {
try {
treeOutput = _navigator->getSensors(parentNode,false);
}
catch(std::domain_error &e) {
res.body() = "Unknown node: " + parentNode;
res.result(http::status::bad_request);
return;
}
}
}
//Format the data for Grafana
......@@ -177,14 +156,18 @@ void RestAPI::POST_search(endpointArgs) {
if(getQuery("sensors", queries) != "true") {
outputElement = boost::regex_replace((o),exp,"");
outputElement.erase(std::remove(outputElement.begin(), outputElement.end(), '/'), outputElement.end());
}
else
} else {
outputElement = o;
}
res.body() += "\"" + outputElement + "\",";
}
if(res.body().back() == ',')
//De-allocating the set
delete treeOutput;
if(res.body().back() == ',') {
res.body().pop_back();
}
res.body() += "]";
//Send the response to Grafana
......
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