PerSystSqlConfigurator.cpp 7.47 KB
Newer Older
1
//================================================================================
2
// Name        : PerSystSqlConfigurator.cpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Author      : Carla Guillen
// Contact     : info@dcdb.it
// Copyright   : Leibniz Supercomputing Centre
// Description : Template implementing features to use Units in Operators.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

28
29
#include "PerSystSqlConfigurator.h"

30
31
32
33
34
35
36
37
38
39
#include <boost/algorithm/string/predicate.hpp>
#include <boost/foreach.hpp>
#include <boost/property_tree/detail/ptree_implementation.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <string>
#include <vector>

#include "../../includes/OperatorConfiguratorTemplate.h"
#include "../../includes/UnitInterface.h"

40
PerSystSqlConfigurator::PerSystSqlConfigurator(): JobOperatorConfiguratorTemplate() {
41
42
	 _operatorName = "persystsql";
	 _baseName     = "sensor";
Carla Guillen Carias's avatar
Carla Guillen Carias committed
43
44
45
	 _rotation_map["EVERY_YEAR"] = MariaDB::EVERY_YEAR;
	 _rotation_map["EVERY_MONTH"] = MariaDB::EVERY_MONTH;
	 _rotation_map["EVERY_XDAYS"] = MariaDB::EVERY_XDAYS;
46
47
}

48
PerSystSqlConfigurator::~PerSystSqlConfigurator() {
49
50
}

51
void PerSystSqlConfigurator::sensorBase(AggregatorSensorBase& s, CFG_VAL config) {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	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!";
			}
		}
	}
69
70
}

71
void PerSystSqlConfigurator::operatorAttributes(PerSystSqlOperator& op, CFG_VAL config) {
72
73
74
75
	std::string host;
	std::string user;
	std::string password;
	std::string database_name;
76
	int port = -1;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
77
	MariaDB::Rotation_t rotation = MariaDB::EVERY_MONTH;
78
	PerSystSqlOperator::Backend_t backend = PerSystSqlOperator::DEFAULT;
79
	unsigned int every_x_days = 0;
80
81
82
83
	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());
			op.setNumberOfEvenQuantiles(num_quantiles);
Carla Guillen's avatar
Carla Guillen committed
84
85
86
87
		} else if (boost::iequals(val.first, "go_back_ms")) {
			auto go_back_ms = std::stoi(val.second.data());
			op.setGoBackInMs(go_back_ms);
		} else if (boost::iequals(val.first, "severity_threshold")){
88
			double threshold = std::stod(val.second.data());
89
90
			op.setSeverityThreshold(threshold);
		} else if (boost::iequals(val.first, "severity_exponent")){
91
92
			double exponent =  std::stod(val.second.data());
			op.setSeverityExponent(exponent);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
		} else if (boost::iequals(val.first, "severity_formula")){
			std::string formula = val.second.data();
			if(formula == "formula1"){
				op.setSeverityFormula(PerSystSqlOperator::FORMULA1);
			} else if (formula == "formula2") {
				op.setSeverityFormula(PerSystSqlOperator::FORMULA2);
			} else if (formula == "formula3") {
				op.setSeverityFormula(PerSystSqlOperator::FORMULA3);
			} else if (formula == "memory_formula"){
				op.setSeverityFormula(PerSystSqlOperator::MEMORY_FORMULA);
			}
		} else if (boost::iequals(val.first, "severity_max_memory")){
			auto max_memory =  std::stof(val.second.data());
			op.setSeverityMaxMemory(max_memory);
107
108
		} else if (boost::iequals(val.first, "backend")) {
			if(val.second.data() == "cassandra") {
109
				backend = PerSystSqlOperator::CASSANDRA;
110
111
				op.setBackend(PerSystSqlOperator::CASSANDRA);
			} else if(val.second.data() == "mariadb") {
112
				backend = PerSystSqlOperator::MARIADB;
113
114
				op.setBackend(PerSystSqlOperator::MARIADB);
			}
115
    	} else if(boost::iequals(val.first, "property_id")){
Carla Guillen's avatar
Carla Guillen committed
116
117
			auto property_id = std::stoi(val.second.data());
			op.setPropertyId(property_id);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
		} 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());
Carla Guillen's avatar
Carla Guillen committed
137
		}
138
139
140
	}
	if(backend == PerSystSqlOperator::MARIADB) {
		op.setMariaDBConnection(host, user, password, database_name, port, rotation, every_x_days);
141
142
143
	}
}

144
bool PerSystSqlConfigurator::unit(UnitTemplate<AggregatorSensorBase>& u) {
145
	if(!u.isTopUnit()) {
146
		LOG(error) << "    " << _operatorName << ": This operator type only supports hierarchical units!";
147
148
149
		return false;
	}
	if(u.getOutputs().empty()) {
150
		LOG(error) << "    " << _operatorName << ": At least one output sensor per unit must be defined!";
151
152
		return false;
	}
153
154
155
	return true;
}

156
bool PerSystSqlConfigurator::readUnits(PerSystSqlOperator& op,
157
		std::vector<shared_ptr<AggregatorSensorBase>>& protoInputs,
158
159
160
		std::vector<shared_ptr<AggregatorSensorBase>>& protoOutputs,
	    std::vector<shared_ptr<AggregatorSensorBase>>& protoGlobalOutputs,
	    inputMode_t inputMode) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
161

162
163

	int num_quantiles = op.getNumberOfEvenQuantiles();
164
	if(num_quantiles == 0){
Carla Guillen Carias's avatar
Carla Guillen Carias committed
165
166
		return false;
	}
167
	bool quantile_found = false;
Carla Guillen's avatar
Carla Guillen committed
168
	AggregatorSensorBase quantsensor("");
169
	for(auto &sensor: protoGlobalOutputs) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
170
		if(sensor->getOperation() == AggregatorSensorBase::QTL){
171
			quantile_found = true;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
172
173
174
175
176
177
			quantsensor = *(sensor.get());
			sensor->setPercentile(0);
			auto topic = sensor->getMqtt();
			sensor->setMqtt(topic + "0");
			break;
		}
178
	}
Carla Guillen Carias's avatar
Carla Guillen Carias committed
179

180
	for(int i = 1; i <= num_quantiles && quantile_found; ++i) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
181
182
		auto outputSensor = std::make_shared<AggregatorSensorBase>(quantsensor);
		outputSensor->setMqtt(outputSensor->getMqtt() + std::to_string(i));
183
184
		outputSensor->setOperation(AggregatorSensorBase::QTL);
		outputSensor->setPercentile(i);
185
		protoGlobalOutputs.push_back(outputSensor);
186
187
	}

188
	return JobOperatorConfiguratorTemplate::readUnits(op, protoInputs, protoOutputs, protoGlobalOutputs, inputMode);
189
}