Commit 2b20c72b authored by Dainius Simelevicius's avatar Dainius Simelevicius
Browse files

references #113: added protection for the case when autotag does not exist in a registry

parent ccadf402
......@@ -273,48 +273,57 @@ void elastic::timestream::Application::actionPerformed(xdata::Event& event)
member_ = new elastic::api::Member(this, properties);
// dynamicMetadata
if ( dynamicMetadata_)
if ( dynamicMetadata_ )
{
std::string zone = this->getApplicationContext()->getDefaultZoneName();
elastic::api::Cluster& cluster = member_->joinCluster(elasticsearchClusterUrl_.toString());
//curl -XPOST -H 'Content-Type: application/json' 'http://cmsos-iaas-cdaq.cms:9200/cmsos-meta-development-tags/_doc/_search?pretty' -d '{ "size": 1, "sort": { "timestamp": "desc"}, "query": { "match_all": {}}}'
if (autoTag_ != "") // retrieve tag from elasticsearch
if ( autoTag_ != "" ) // retrieve tag from elasticsearch
{
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());
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);
//char * test = json_dumps(tquery, 0);
//std::cout << "autotag query is: " << test << std::endl;
// debug
json_t * tj = cluster.search(tagsIndex, "_doc", "", tquery);
nlohmann::json tags = elastic::api::janson2nlohmann(tj);
nlohmann::json tags = elastic::api::janson2nlohmann(tj);
json_decref(tj);
json_decref(tquery);
//std::cout << "dump tags json: " << tags["hits"]["hits"].dump(4) << std::endl;
nlohmann::json::iterator itt = tags["hits"]["hits"].begin();
//std::cout << "dump doc json: " << (*itt).dump(4) << std::endl;
nlohmann::json entry = (*itt)["_source"];
if ( entry.find("tag") != entry.end() )
{
std::string tag = entry["tag"];
tag_ = tag;
LOG4CPLUS_INFO(this->getApplicationLogger(), "Found auto tag '" << tag_.toString() << "'");
}
else
nlohmann::json hits = tags["hits"]["hits"];
if (hits.empty())
{
std::stringstream msg;
msg << "Failed to retrieve auto tag";
msg << "Cannot find autotag '" << autoTag_.toString() << "'";
XCEPT_DECLARE(elastic::timestream::exception::Exception, q, msg.str());
this->notifyQualified("fatal", q);
return;
}
//std::cout << "dump tags json: " << hits.dump(4) << std::endl;
for (auto && hit:hits)
{
//std::cout << "dump doc json: " << (*itt).dump(4) << std::endl;
nlohmann::json entry = hit["_source"];
if ( entry.find("tag") != entry.end() )
{
std::string tag = entry["tag"];
tag_ = tag;
LOG4CPLUS_INFO(this->getApplicationLogger(), "Found auto tag '" << tag_.toString() << "'");
break;
}
else
{
std::stringstream msg;
msg << "Failed to retrieve auto tag";
XCEPT_DECLARE(elastic::timestream::exception::Exception, q, msg.str());
this->notifyQualified("fatal", q);
return;
}
}
}
}
if ( enable_ )
{
if ( dynamicMetadata_)
......
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