PerSystSqlConfigurator.cpp 8.66 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
	BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
		if (boost::iequals(val.first, "number_quantiles")) {
74
75
76
77
78
79
    		try {
    			unsigned int num_quantiles = std::stoul(val.second.data());
    			op.setNumberOfEvenQuantiles(num_quantiles);
    		} catch (const std::exception &e) {
				LOG(error) << "  Error parsing number_quantiles \"" << val.second.data() << "\": " << e.what();
			}
Carla Guillen's avatar
Carla Guillen committed
80
		} else if (boost::iequals(val.first, "go_back_ms")) {
81
82
83
84
85
86
			try {
				auto go_back_ms = std::stoi(val.second.data());
				op.setGoBackInMs(go_back_ms);
			} catch (const std::exception &e) {
				LOG(error) << "  Error parsing go_back_ms \"" << val.second.data() << "\": " << e.what();
			}
Carla Guillen's avatar
Carla Guillen committed
87
		} else if (boost::iequals(val.first, "severity_threshold")){
88
89
90
91
92
93
			try {
				double threshold = std::stod(val.second.data());
				op.setSeverityThreshold(threshold);
			} catch (const std::exception &e) {
				LOG(error) << "  Error parsing severity_threshold \"" << val.second.data() << "\": " << e.what();
			}
94
		} else if (boost::iequals(val.first, "severity_exponent")){
95
96
97
98
99
100
			try {
				double exponent =  std::stod(val.second.data());
				op.setSeverityExponent(exponent);
			} catch (const std::exception &e) {
				LOG(error) << "  Error parsing severity_exponent \"" << val.second.data() << "\": " << e.what();
			}
101
102
		} else if (boost::iequals(val.first, "severity_formula")){
			std::string formula = val.second.data();
103
			if(formula == "formula1" || formula == "FORMULA1"){
104
				op.setSeverityFormula(PerSystSqlOperator::FORMULA1);
105
			} else if (formula == "formula2" || formula == "FORMULA2") {
106
				op.setSeverityFormula(PerSystSqlOperator::FORMULA2);
107
			} else if (formula == "formula3" || formula == "FORMULA3") {
108
				op.setSeverityFormula(PerSystSqlOperator::FORMULA3);
109
			} else if (formula == "memory_formula" || formula == "MEMORY_FORMULA"){
110
				op.setSeverityFormula(PerSystSqlOperator::MEMORY_FORMULA);
111
112
			} else {
				LOG(error) << "Unrecognized/unsupported severity formula: " << formula;
113
114
			}
		} else if (boost::iequals(val.first, "severity_max_memory")){
115
116
117
118
119
120
			try {
				auto max_memory =  std::stof(val.second.data());
				op.setSeverityMaxMemory(max_memory);
			} catch (const std::exception &e) {
				LOG(error) << "  Error parsing severity_max_memory \"" << val.second.data() << "\": " << e.what();
			}
121
122
123
124
125
		} else if (boost::iequals(val.first, "backend")) {
			if(val.second.data() == "cassandra") {
				op.setBackend(PerSystSqlOperator::CASSANDRA);
			} else if(val.second.data() == "mariadb") {
				op.setBackend(PerSystSqlOperator::MARIADB);
126
127
			} else {
				LOG(error) << "  Unrecognized/unsupported backend \"" << val.second.data();
128
			}
129
130
131
132
133
134
135
	    } else if(boost::iequals(val.first, "property_id")){
	    	try {
	    		auto property_id = std::stoi(val.second.data());
	    		op.setPropertyId(property_id);
	    	} catch (const std::exception &e) {
	    		LOG(error) << "  Error parsing property_id \"" << val.second.data() << "\": " << e.what();
	    	}
136
		} else if (boost::iequals(val.first, "mariadb_host")) {
137
			op.setMariaDBHost(val.second.data());
138
		} else if (boost::iequals(val.first, "mariadb_user")){
139
			op.setMariaDBUser(val.second.data());
140
		} else if (boost::iequals(val.first, "mariadb_password")){
141
			op.setMariaDBPassword(val.second.data());
142
		} else if (boost::iequals(val.first, "mariadb_database_name")){
143
			op.setMariaDBDatabaseName(val.second.data());
144
		} else if (boost::iequals(val.first, "mariadb_port")){
145
146
147
148
149
150
			try {
				int port = std::stoi(val.second.data());
				op.setMariaDBPort(port);
			} catch (const std::exception &e) {
	    		LOG(error) << "  Error parsing mariadb_port \"" << val.second.data() << "\": " << e.what();
	    	}
151
152
153
		} else if (boost::iequals(val.first, "mariadb_rotation")){
			auto found = _rotation_map.find(val.second.data());
			if (found != _rotation_map.end()){
154
				op.setMariaDBRotation(found->second);
155
156
157
158
			} else {
				LOG(error) << " Rotation strategy (" << val.second.data() << ") not found.";
			}
		} else if (boost::iequals(val.first, "mariadb_every_x_days")){
159
160
161
162
163
164
			try {
				unsigned int every_x_days = std::stoi(val.second.data());
				op.setMariaDBEveryXDays(every_x_days);
			} catch (const std::exception &e) {
	    		LOG(error) << "  Error parsing mariadb_every_x_days \"" << val.second.data() << "\": " << e.what();
	    	}
Carla Guillen's avatar
Carla Guillen committed
165
		}
166
	}
167
168
}

169
bool PerSystSqlConfigurator::unit(UnitTemplate<AggregatorSensorBase>& u) {
170
	if(!u.isTopUnit()) {
171
		LOG(error) << "    " << _operatorName << ": This operator type only supports hierarchical units!";
172
173
174
		return false;
	}
	if(u.getOutputs().empty()) {
175
		LOG(error) << "    " << _operatorName << ": At least one output sensor per unit must be defined!";
176
177
		return false;
	}
178
179
180
	return true;
}

181
bool PerSystSqlConfigurator::readUnits(PerSystSqlOperator& op,
182
		std::vector<shared_ptr<AggregatorSensorBase>>& protoInputs,
183
184
185
		std::vector<shared_ptr<AggregatorSensorBase>>& protoOutputs,
	    std::vector<shared_ptr<AggregatorSensorBase>>& protoGlobalOutputs,
	    inputMode_t inputMode) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
186

187
188

	int num_quantiles = op.getNumberOfEvenQuantiles();
189
	if(num_quantiles == 0){
Carla Guillen Carias's avatar
Carla Guillen Carias committed
190
191
		return false;
	}
192
	bool quantile_found = false;
Carla Guillen's avatar
Carla Guillen committed
193
	AggregatorSensorBase quantsensor("");
194
	for(auto &sensor: protoGlobalOutputs) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
195
		if(sensor->getOperation() == AggregatorSensorBase::QTL){
196
			quantile_found = true;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
197
198
199
200
201
202
			quantsensor = *(sensor.get());
			sensor->setPercentile(0);
			auto topic = sensor->getMqtt();
			sensor->setMqtt(topic + "0");
			break;
		}
203
	}
Carla Guillen Carias's avatar
Carla Guillen Carias committed
204

205
	for(int i = 1; i <= num_quantiles && quantile_found; ++i) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
206
207
		auto outputSensor = std::make_shared<AggregatorSensorBase>(quantsensor);
		outputSensor->setMqtt(outputSensor->getMqtt() + std::to_string(i));
208
209
		outputSensor->setOperation(AggregatorSensorBase::QTL);
		outputSensor->setPercentile(i);
210
		protoGlobalOutputs.push_back(outputSensor);
211
212
	}

213
	return JobOperatorConfiguratorTemplate::readUnits(op, protoInputs, protoOutputs, protoGlobalOutputs, inputMode);
214
}