Commit 398fa5ef authored by Carla Guillen Carias's avatar Carla Guillen Carias
Browse files

Finishing configuration parsing for mariadb

parent 5425c0f8
......@@ -65,9 +65,7 @@ public:
};
void PerSystDB::print_error(){
std::cout << "Error(" << mysql_errno(_mysql) << ") [" << mysql_sqlstate(_mysql) << "] \""<< mysql_error(_mysql) << "\"" << std::endl;
//mysql_close(mysql);
LOG(error) << "Error(" << mysql_errno(_mysql) << ") [" << mysql_sqlstate(_mysql) << "] \""<< mysql_error(_mysql) << "\"" ;
}
PerSystDB::PerSystDB(): _mysql(NULL), _rotation(EVERY_MONTH), _every_x_days(0), _internal_connection(false), _end_aggregate_timestamp(0) {
......@@ -124,9 +122,7 @@ bool PerSystDB::getDBJobIDs(std::vector<std::string> & job_id_strings, std::map<
}
build_query << ")";
auto query = build_query.str();
#if DEBUG
std::cout << query << std::endl;
#endif
LOG(debug) << query;
if(mysql_real_query(_mysql, query.c_str(), query.size())){
print_error();
return false;
......@@ -146,7 +142,6 @@ bool PerSystDB::getDBJobIDs(std::vector<std::string> & job_id_strings, std::map<
bool PerSystDB::getCurrentSuffixAggregateTable(std::string & suffix){
if(_end_aggregate_timestamp){
auto now_uts = getTimestamp();
if(now_uts < _end_aggregate_timestamp) { //suffix found, don't do anything
......@@ -162,9 +157,7 @@ bool PerSystDB::getCurrentSuffixAggregateTable(std::string & suffix){
build_query << "SELECT suffix, UNIX_TIMESTAMP(end_timestamp) FROM SuffixToAggregateTable WHERE begin_timestamp < \'";
build_query << date_time << "\' AND end_timestamp > \'" << date_time << "\'";
auto query = build_query.str();
#if DEBUG
std::cout << query << std::endl;
#endif
LOG(debug) << query;
if(mysql_real_query(_mysql, query.c_str(), query.size())){
print_error();
......@@ -195,9 +188,7 @@ bool PerSystDB::insertIntoJob(const std::string& job_id_string, unsigned long lo
build_insert << pass->pw_name << "\',\'";
build_insert << suffix << "\',\'" << suffix << "\')";
std::string query = build_insert.str();
#if DEBUG
std::cout << query << std::endl;
#endif
LOG(debug) << query;
if(mysql_real_query(_mysql, query.c_str(), query.size())){
print_error();
......@@ -220,9 +211,7 @@ bool PerSystDB::insertInAggregateTable(const std::string& suffix, Aggregate_info
}
build_insert << "\', \'" << agg.severity_average << "\')";
std::string query = build_insert.str();
#if DEBUG
std::cout << query << std::endl;
#endif
LOG(debug) << query;
if(mysql_real_query(_mysql, query.c_str(), query.size())){
print_error();
......@@ -264,9 +253,7 @@ bool PerSystDB::createNewAggregate(std::string& new_suffix){
build_insert << "INSERT INTO SuffixToAggregateTable VALUES(\'" << new_suff;
build_insert << "\', \'" << new_begin_timestamp << "\', \'" << new_end_timestamp << "\')";
auto query = build_insert.str();
#if DEBUG
std::cout << query << std::endl;
#endif
LOG(debug) << query;
if (mysql_real_query(_mysql, query.c_str(), query.size())){
print_error();
return false;
......@@ -275,9 +262,7 @@ bool PerSystDB::createNewAggregate(std::string& new_suffix){
std::stringstream build_create;
build_create << "CREATE TABLE Aggregate_" << new_suff << " LIKE Aggregate";
auto query2 = build_create.str();
#if DEBUG
std::cout << query2 << std::endl;
#endif
LOG(debug) << query2;
if(mysql_real_query(_mysql, query2.c_str(), query2.size() )){
if(mysql_errno(_mysql) == 1050){
return true; //table exists!
......@@ -304,9 +289,7 @@ bool PerSystDB::updateJobsLastSuffix(std::map<std::string, std::string>& job_map
count++;
}
auto query = build_update.str();
#if DEBUG
std::cout << query << std::endl;
#endif
LOG(debug) << query;
if (mysql_real_query(_mysql, query.c_str(), query.size())){
print_error();
......@@ -354,10 +337,8 @@ void PerSystDB::getNewDates(const std::string& last_end_timestamp, std::string &
_end_aggregate_timestamp = diff.total_seconds() * 1e9;
end_timestamp = to_iso_extended_string(d) + " 00:00:00";
#if DEBUG
std::cout << "boost_end_timestamp =" << _end_aggregate_timestamp << std::endl;
std::cout << "boost_end_aggregate_timestamp=" << end_timestamp << std::endl;
#endif
LOG(debug) << "_end_aggregate_timestamp =" << _end_aggregate_timestamp;
LOG(debug) << "end_timestamp =" << end_timestamp;
}
......@@ -29,6 +29,7 @@
#define ANALYTICS_OPERATORS_PERSYSTSQL_PERSYSTDB_H_
#include "mariadb/mysql.h"
#include "../../../common/include/logging.h"
#include <vector>
#include <string>
#include <map>
......@@ -60,7 +61,7 @@ protected:
bool _internal_connection;
unsigned long long _end_aggregate_timestamp;
std::string _current_table_suffix;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
void print_error();
public:
......
......@@ -40,33 +40,42 @@
PerSystSqlConfigurator::PerSystSqlConfigurator(): JobOperatorConfiguratorTemplate() {
_operatorName = "persystsql";
_baseName = "sensor";
_rotation_map["EVERY_YEAR"] = PerSystDB::EVERY_YEAR;
_rotation_map["EVERY_MONTH"] = PerSystDB::EVERY_MONTH;
_rotation_map["EVERY_XDAYS"] = PerSystDB::EVERY_XDAYS;
}
PerSystSqlConfigurator::~PerSystSqlConfigurator() {
}
void PerSystSqlConfigurator::sensorBase(AggregatorSensorBase& s, CFG_VAL config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config)
{
if (boost::iequals(val.first, "operation")) {
std::string opName = val.second.data();
if (opName == "average"){
s.setOperation(AggregatorSensorBase::AVG);
} else if (opName == "deciles" || opName == "percentiles" || opName == "quantile"){
s.setOperation(AggregatorSensorBase::QTL);
} else if (opName == "observations" || opName == "numobs") {
s.setOperation(AggregatorSensorBase::OBS);
} else if (opName == "average_severity"){
s.setOperation(AggregatorSensorBase::AVG_SEV);
} else {
LOG(error) << "PerSystSqlConfigurator operation " << opName << " not supported!" ;
}
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config)
{
if (boost::iequals(val.first, "operation")) {
std::string opName = val.second.data();
if (opName == "average") {
s.setOperation(AggregatorSensorBase::AVG);
} else if (opName == "deciles" || opName == "percentiles" || opName == "quantile") {
s.setOperation(AggregatorSensorBase::QTL);
} else if (opName == "observations" || opName == "numobs") {
s.setOperation(AggregatorSensorBase::OBS);
} else if (opName == "average_severity") {
s.setOperation(AggregatorSensorBase::AVG_SEV);
} else {
LOG(error) << "PerSystSqlConfigurator operation " << opName << " not supported!";
}
}
}
}
void PerSystSqlConfigurator::operatorAttributes(PerSystSqlOperator& op, CFG_VAL config) {
std::string host;
std::string user;
std::string password;
std::string database_name;
int port;
PerSystDB::Rotation_t rotation = PerSystDB::EVERY_MONTH;
unsigned int every_x_days = 0;
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "number_quantiles")) {
unsigned int num_quantiles = std::stoul(val.second.data());
......@@ -100,10 +109,30 @@ void PerSystSqlConfigurator::operatorAttributes(PerSystSqlOperator& op, CFG_VAL
} else if(val.second.data() == "mariadb") {
op.setBackend(PerSystSqlOperator::MARIADB);
}
} else if(boost::iequals(val.first, "property_id")){
} else if(boost::iequals(val.first, "property_id")){
auto property_id = std::stoi(val.second.data());
op.setPropertyId(property_id);
} else if (boost::iequals(val.first, "mariadb_host")) {
host = val.second.data();
} else if (boost::iequals(val.first, "mariadb_user")){
user = val.second.data();
} else if (boost::iequals(val.first, "mariadb_password")){
password = val.second.data();
} else if (boost::iequals(val.first, "mariadb_database_name")){
database_name = val.second.data();
} else if (boost::iequals(val.first, "mariadb_port")){
port = std::stoi(val.second.data());
} else if (boost::iequals(val.first, "mariadb_rotation")){
auto found = _rotation_map.find(val.second.data());
if (found != _rotation_map.end()){
rotation = found->second;
} else {
LOG(error) << " Rotation strategy (" << val.second.data() << ") not found.";
}
} else if (boost::iequals(val.first, "mariadb_every_x_days")){
every_x_days = std::stoi(val.second.data());
}
op.setMariaDBConnection(host, user, password, database_name, port, rotation, every_x_days);
}
}
......
......@@ -40,6 +40,7 @@ public:
PerSystSqlConfigurator();
virtual ~PerSystSqlConfigurator();
private:
std::map<std::string, PerSystDB::Rotation_t> _rotation_map;
void sensorBase(AggregatorSensorBase& s, CFG_VAL config) override;
void operatorAttributes(PerSystSqlOperator& op, CFG_VAL config) override;
bool unit(UnitTemplate<AggregatorSensorBase>& u) override;
......
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