PerSystSqlConfigurator.cpp 8.95 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
81
82
83
84
85
86
		} else if (boost::iequals(val.first, "batch_domain")){
			try {
				int batch_domain = std::stoi(val.second.data());
				op.setBatchDomain(batch_domain);
			} catch (const std::exception &e) {
				LOG(error) << "  Error parsing batch_domain \"" << val.second.data() << "\": " << e.what();
			}
Carla Guillen's avatar
Carla Guillen committed
87
		} else if (boost::iequals(val.first, "go_back_ms")) {
88
89
90
91
92
93
			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
94
		} else if (boost::iequals(val.first, "severity_threshold")){
95
96
97
98
99
100
			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();
			}
101
		} else if (boost::iequals(val.first, "severity_exponent")){
102
103
104
105
106
107
			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();
			}
108
109
		} else if (boost::iequals(val.first, "severity_formula")){
			std::string formula = val.second.data();
110
			if(formula == "formula1" || formula == "FORMULA1"){
111
				op.setSeverityFormula(PerSystSqlOperator::FORMULA1);
112
			} else if (formula == "formula2" || formula == "FORMULA2") {
113
				op.setSeverityFormula(PerSystSqlOperator::FORMULA2);
114
			} else if (formula == "formula3" || formula == "FORMULA3") {
115
				op.setSeverityFormula(PerSystSqlOperator::FORMULA3);
116
			} else if (formula == "memory_formula" || formula == "MEMORY_FORMULA"){
117
				op.setSeverityFormula(PerSystSqlOperator::MEMORY_FORMULA);
118
119
			} else {
				LOG(error) << "Unrecognized/unsupported severity formula: " << formula;
120
121
			}
		} else if (boost::iequals(val.first, "severity_max_memory")){
122
123
124
125
126
127
			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();
			}
128
129
130
131
132
		} 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);
133
134
			} else {
				LOG(error) << "  Unrecognized/unsupported backend \"" << val.second.data();
135
			}
136
137
138
139
140
141
142
	    } 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();
	    	}
143
		} else if (boost::iequals(val.first, "mariadb_host")) {
144
			op.setMariaDBHost(val.second.data());
145
		} else if (boost::iequals(val.first, "mariadb_user")){
146
			op.setMariaDBUser(val.second.data());
147
		} else if (boost::iequals(val.first, "mariadb_password")){
148
			op.setMariaDBPassword(val.second.data());
149
		} else if (boost::iequals(val.first, "mariadb_database_name")){
150
			op.setMariaDBDatabaseName(val.second.data());
151
		} else if (boost::iequals(val.first, "mariadb_port")){
152
153
154
155
156
157
			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();
	    	}
158
159
160
		} else if (boost::iequals(val.first, "mariadb_rotation")){
			auto found = _rotation_map.find(val.second.data());
			if (found != _rotation_map.end()){
161
				op.setMariaDBRotation(found->second);
162
163
164
165
			} else {
				LOG(error) << " Rotation strategy (" << val.second.data() << ") not found.";
			}
		} else if (boost::iequals(val.first, "mariadb_every_x_days")){
166
167
168
169
170
171
			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
172
		}
173
	}
174
175
}

176
bool PerSystSqlConfigurator::unit(UnitTemplate<AggregatorSensorBase>& u) {
177
	if(!u.isTopUnit()) {
178
		LOG(error) << "    " << _operatorName << ": This operator type only supports hierarchical units!";
179
180
181
		return false;
	}
	if(u.getOutputs().empty()) {
182
		LOG(error) << "    " << _operatorName << ": At least one output sensor per unit must be defined!";
183
184
		return false;
	}
185
186
187
	return true;
}

188
bool PerSystSqlConfigurator::readUnits(PerSystSqlOperator& op,
189
		std::vector<shared_ptr<AggregatorSensorBase>>& protoInputs,
190
191
192
		std::vector<shared_ptr<AggregatorSensorBase>>& protoOutputs,
	    std::vector<shared_ptr<AggregatorSensorBase>>& protoGlobalOutputs,
	    inputMode_t inputMode) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
193

194
195

	int num_quantiles = op.getNumberOfEvenQuantiles();
196
	if(num_quantiles == 0){
Carla Guillen Carias's avatar
Carla Guillen Carias committed
197
198
		return false;
	}
199
	bool quantile_found = false;
Carla Guillen's avatar
Carla Guillen committed
200
	AggregatorSensorBase quantsensor("");
201
	for(auto &sensor: protoGlobalOutputs) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
202
		if(sensor->getOperation() == AggregatorSensorBase::QTL){
203
			quantile_found = true;
Carla Guillen Carias's avatar
Carla Guillen Carias committed
204
205
206
207
208
209
			quantsensor = *(sensor.get());
			sensor->setPercentile(0);
			auto topic = sensor->getMqtt();
			sensor->setMqtt(topic + "0");
			break;
		}
210
	}
Carla Guillen Carias's avatar
Carla Guillen Carias committed
211

212
	for(int i = 1; i <= num_quantiles && quantile_found; ++i) {
Carla Guillen Carias's avatar
Carla Guillen Carias committed
213
214
		auto outputSensor = std::make_shared<AggregatorSensorBase>(quantsensor);
		outputSensor->setMqtt(outputSensor->getMqtt() + std::to_string(i));
215
216
		outputSensor->setOperation(AggregatorSensorBase::QTL);
		outputSensor->setPercentile(i);
217
		protoGlobalOutputs.push_back(outputSensor);
218
219
	}

220
	return JobOperatorConfiguratorTemplate::readUnits(op, protoInputs, protoOutputs, protoGlobalOutputs, inputMode);
221
}