Commit f16102f6 authored by Dainius Simelevicius's avatar Dainius Simelevicius
Browse files

references #114: tag is assigned to empty string when autotag retrieval fails...

references #114: tag is assigned to empty string when autotag retrieval fails in elastic::timestream
parent c93d0d41
......@@ -279,12 +279,14 @@ void elastic::timestream::Application::actionPerformed(xdata::Event& event)
elastic::api::Cluster& cluster = member_->joinCluster(elasticsearchClusterUrl_.toString());
if ( autoTag_ != "" ) // retrieve tag from elasticsearch
{
tag_ = "";
std::string tagsIndex = elastic::api::ElasticMetaPrefix + zone + "-tags-registry";
json_t *tquery = json_pack("{s:i,s:{s: s},s:{s:{s:s}}}", "size", 1, "sort", "timestamp", "desc", "query", "term", "autotagtype", autoTag_.toString().c_str());
// debug
//char * test = json_dumps(tquery, 0);
//std::cout << "autotag query is: " << test << std::endl;
// debug
char * strQuery = json_dumps(tquery, 0);
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Tags index: '" << tagsIndex << "' auto tag query: '" << strQuery << "'");
free(strQuery);
json_t * tj = cluster.search(tagsIndex, "_doc", "", tquery);
nlohmann::json tags = elastic::api::janson2nlohmann(tj);
json_decref(tj);
......
......@@ -307,12 +307,14 @@ void xmas::probe::Application::actionPerformed (xdata::Event& event)
elastic::api::Cluster& cluster = member_->joinCluster(elasticsearchClusterUrl_.toString());
if ( autoTag_ != "" ) // retrieve tag from elasticsearch
{
tag_ = "";
std::string tagsIndex = elastic::api::ElasticMetaPrefix + zone + "-tags-registry";
json_t *tquery = json_pack("{s:i,s:{s: s},s:{s:{s:s}}}", "size", 1, "sort", "timestamp", "desc", "query", "term", "autotagtype", autoTag_.toString().c_str());
// debug
//char * test = json_dumps(tquery, 0);
//std::cout << "autotag query is: " << test << std::endl;
// debug
char * strQuery = json_dumps(tquery, 0);
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Tags index: '" << tagsIndex << "' auto tag query: '" << strQuery << "'");
free(strQuery);
json_t * tj = cluster.search(tagsIndex, "_doc", "", tquery);
nlohmann::json tags = elastic::api::janson2nlohmann(tj);
json_decref(tj);
......@@ -544,7 +546,6 @@ void xmas::probe::Application::actionPerformed (toolbox::Event& event)
if ( dynamicMetadata_ )
{
this->applySensorSettingsJSON(service, flashlistsJSONCache_);
}
else
{
......@@ -874,6 +875,15 @@ void xmas::probe::Application::schedulePulseEvents (toolbox::task::Timer * timer
void xmas::probe::Application::applySensorSettingsJSON (const std::string & service, nlohmann::json & fj )
{
if ( tag_ == "" )
{
std::stringstream msg;
msg << "Metasphere tag is not defined";
XCEPT_DECLARE(xmas::probe::exception::Exception, q, msg.str());
this->notifyQualified("fatal", q);
return;
}
std::string zone = this->getApplicationContext()->getDefaultZoneName();
std::string tag = tag_;
......@@ -883,88 +893,89 @@ void xmas::probe::Application::applySensorSettingsJSON (const std::string & serv
LOG4CPLUS_INFO(this->getApplicationLogger(), "applying sensor settings from elasticsearch cluster '" << elasticsearchClusterUrl_.toString() << "'");
nlohmann::json sj;
try
{
std::string squery = "size=2048&q=service:" + service;
std::string url = elastic::api::ElasticMetaPrefix + zone + "-" + tag + "-sensors-registry";
json_t* j = cluster.search(url, "", squery, 0);
nlohmann::json sj = elastic::api::janson2nlohmann(j);
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "search sensor setting for '" << service << "' total hits: " << sj["hits"]["total"]);
sj = elastic::api::janson2nlohmann(j);
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "search sensor setting for '" << service << "' total hits: " << sj["hits"]["total"]);
json_decref(j);
}
catch (elastic::api::exception::Exception & e)
{
std::stringstream msg;
msg << "Failed to load settings from cluster for "<< url;
XCEPT_DECLARE_NESTED(xmas::probe::exception::Exception, q, msg.str(), e);
this->notifyQualified("fatal", q);
}
flashListMonitorRegistry_.lock();
flashListMonitorRegistry_.lock();
nlohmann::json flashlists = fj["hits"]["hits"];
nlohmann::json sensors = sj["hits"]["hits"];
for (nlohmann::json::iterator its = sensors.begin(); its != sensors.end(); ++its)
nlohmann::json flashlists = fj["hits"]["hits"];
nlohmann::json sensors = sj["hits"]["hits"];
for (nlohmann::json::iterator its = sensors.begin(); its != sensors.end(); ++its)
{
std::string s = (*its)["_source"]["service"];
std::string id = (*its)["_id"];
//LOG4CPLUS_INFO(this->getApplicationLogger(), "iterator service: " << s);
if ( s == service)
{
std::string s = (*its)["_source"]["service"];
std::string id = (*its)["_id"];
//LOG4CPLUS_INFO(this->getApplicationLogger(), "iterator service: " << s);
if ( s == service)
LOG4CPLUS_INFO(this->getApplicationLogger(), "found settings for service '" << s << "' id '" << id << "'");
try
{
LOG4CPLUS_INFO(this->getApplicationLogger(), "found settings for service '" << s << "' id '" << id << "'");
std::vector<xmas::MonitorSettings*> settings = xmas::MonitorSettingsFactory::create((*its)["_source"],flashlists);
try
std::vector<xmas::MonitorSettings *>::iterator k;
for (k = settings.begin(); k != settings.end(); k++)
{
std::vector<xmas::MonitorSettings*> settings = xmas::MonitorSettingsFactory::create((*its)["_source"],flashlists);
std::vector<xmas::MonitorSettings *>::iterator k;
for (k = settings.begin(); k != settings.end(); k++)
// Only install a FlashListMonitor object if it is not already instantiated in the registry
//
std::string name = (*k)->getFlashListDefinition()->getProperty("name");
if (!flashListMonitorRegistry_.exists(name))
{
// Only install a FlashListMonitor object if it is not already instantiated in the registry
//
std::string name = (*k)->getFlashListDefinition()->getProperty("name");
if (!flashListMonitorRegistry_.exists(name))
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Applying xmas sensor settings for flashlist '" << name << "', creating monitor object");
xmas::probe::FlashListMonitor * monitor = flashListMonitorRegistry_.install(*k, &scheduler_);
std::list<xmas::probe::Sampler*> samplers = monitor->getSamplers();
for (std::list<xmas::probe::Sampler*>::iterator s = samplers.begin(); s != samplers.end(); s++)
{
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Applying xmas sensor settings for flashlist '" << name << "', creating monitor object");
xmas::probe::FlashListMonitor * monitor = flashListMonitorRegistry_.install(*k, &scheduler_);
std::list<xmas::probe::Sampler*> samplers = monitor->getSamplers();
for (std::list<xmas::probe::Sampler*>::iterator s = samplers.begin(); s != samplers.end(); s++)
{
#warning "the sampler is already active and could already receive data and interfere with this add listener call"
(*s)->addActionListener(this);
}
}
else
{
delete (*k); // the vector goes out of scope at the end of the function
(*s)->addActionListener(this);
}
}
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Loaded json sensor settings '" << (*its)["_id"] << "'");
}
catch (xmas::probe::exception::FlashListMonitorCreationFailed& e)
{
std::stringstream msg;
msg << "Failed to install monitor settings for flashlist from '" << (*its)["_id"] << "'";
XCEPT_DECLARE_NESTED(xmas::probe::exception::Exception, q, msg.str(), e);
this->notifyQualified("fatal", q);
flashListMonitorRegistry_.unlock();
return;
}
catch (xmas::exception::Exception& e)
{
std::stringstream msg;
msg << "Failed to parse configuration from '" << (*its)["_id"] << "'";
XCEPT_DECLARE_NESTED(xmas::probe::exception::Exception, q, msg.str(), e);
this->notifyQualified("fatal", q);
flashListMonitorRegistry_.unlock();
return;
else
{
delete (*k); // the vector goes out of scope at the end of the function
}
}
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Loaded json sensor settings '" << (*its)["_id"] << "'");
}
catch (xmas::probe::exception::FlashListMonitorCreationFailed& e)
{
std::stringstream msg;
msg << "Failed to install monitor settings for flashlist from '" << (*its)["_id"] << "'";
XCEPT_DECLARE_NESTED(xmas::probe::exception::Exception, q, msg.str(), e);
this->notifyQualified("fatal", q);
flashListMonitorRegistry_.unlock();
return;
}
catch (xmas::exception::Exception& e)
{
std::stringstream msg;
msg << "Failed to parse configuration from '" << (*its)["_id"] << "'";
XCEPT_DECLARE_NESTED(xmas::probe::exception::Exception, q, msg.str(), e);
this->notifyQualified("fatal", q);
flashListMonitorRegistry_.unlock();
return;
}
}
flashListMonitorRegistry_.unlock();
}
catch (elastic::api::exception::Exception & e)
{
std::stringstream msg;
msg << "Failed to load settings from cluster for "<< url;
XCEPT_DECLARE_NESTED(xmas::probe::exception::Exception, q, msg.str(), e);
this->notifyQualified("fatal", q);
}
flashListMonitorRegistry_.unlock();
}
void xmas::probe::Application::applySensorSettings (const std::string & path)
{
......
......@@ -241,12 +241,14 @@ void xmas::sensord::Application::actionPerformed (xdata::Event& event)
elastic::api::Cluster& cluster = member_->joinCluster(elasticsearchClusterUrl_.toString());
if ( autoTag_ != "" ) // retrieve tag from elasticsearch
{
tag_ = "";
std::string tagsIndex = elastic::api::ElasticMetaPrefix + zone + "-tags-registry";
json_t *tquery = json_pack("{s:i,s:{s: s},s:{s:{s:s}}}", "size", 1, "sort", "timestamp", "desc", "query", "term", "autotagtype", autoTag_.toString().c_str());
// debug
//char * test = json_dumps(tquery, 0);
//std::cout << "autotag query is: " << test << std::endl;
// debug
char * strQuery = json_dumps(tquery, 0);
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Tags index: '" << tagsIndex << "' auto tag query: '" << strQuery << "'");
free(strQuery);
json_t * tj = cluster.search(tagsIndex, "_doc", "", tquery);
nlohmann::json tags = elastic::api::janson2nlohmann(tj);
json_decref(tj);
......
......@@ -246,12 +246,14 @@ void xmas::slash2g::Application::actionPerformed( xdata::Event& event)
elastic::api::Cluster& cluster = member_->joinCluster(elasticsearchClusterUrl_.toString());
if ( autoTag_ != "" ) // retrieve tag from elasticsearch
{
tag_ = "";
std::string tagsIndex = elastic::api::ElasticMetaPrefix + zone + "-tags-registry";
json_t *tquery = json_pack("{s:i,s:{s: s},s:{s:{s:s}}}", "size", 1, "sort", "timestamp", "desc", "query", "term", "autotagtype", autoTag_.toString().c_str());
// debug
//char * test = json_dumps(tquery, 0);
//std::cout << "autotag query is: " << test << std::endl;
// debug
char * strQuery = json_dumps(tquery, 0);
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Tags index: '" << tagsIndex << "' auto tag query: '" << strQuery << "'");
free(strQuery);
json_t * tj = cluster.search(tagsIndex, "_doc", "", tquery);
nlohmann::json tags = elastic::api::janson2nlohmann(tj);
json_decref(tj);
......
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