query.cpp 9.94 KB
Newer Older
Axel Auweter's avatar
Axel Auweter committed
1
2
//================================================================================
// Name        : query.cpp
3
// Author      : Axel Auweter, Daniele Tafani
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
Axel Auweter's avatar
Axel Auweter committed
5
6
7
8
9
10
// Copyright   : Leibniz Supercomputing Centre
// Description : Implementation of query class of dcdbquery
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
11
// Copyright (C) 2011-2019 Leibniz Supercomputing Centre
Axel Auweter's avatar
Axel Auweter committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// 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.
//================================================================================
Axel Auweter's avatar
Axel Auweter committed
27
28
29
30
31
32
33

#include <iostream>
#include <list>
#include <string>
#include <algorithm>

#include <cstdlib>
34
#include <cinttypes>
Axel Auweter's avatar
Axel Auweter committed
35

36
#include <boost/algorithm/string.hpp>
37
#include <boost/regex.hpp>
38
39

#include "dcdbendian.h"
Axel Auweter's avatar
Axel Auweter committed
40
#include "query.h"
Daniele Tafani's avatar
Daniele Tafani committed
41
#include "dcdb/sensoroperations.h"
Axel Auweter's avatar
Axel Auweter committed
42

Axel Auweter's avatar
Axel Auweter committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
void DCDBQuery::setLocalTimeEnabled(bool enable) {
  useLocalTime = enable;
}

bool DCDBQuery::getLocalTimeEnabled() {
  return useLocalTime;
}

void DCDBQuery::setRawOutputEnabled(bool enable) {
  useRawOutput = enable;
}

bool DCDBQuery::getRawOutputEnabled() {
  return useRawOutput;
}

59
60
61
62
bool scaleAndConvert(int64_t &value, double baseScalingFactor, double scalingFactor, DCDB::Unit baseUnit, DCDB::Unit unit) {
    if(scalingFactor != 1.0 || baseScalingFactor != 1.0) {
	if( DCDB::scale(&value, scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
	    return false;
63
64
    }
    
65
66
67
68
69
70
71
72
73
    /* Convert the unit if requested */
    if ((unit != DCDB::Unit_None) && (unit != baseUnit)) {
	if (!DCDB::UnitConv::convert(value, baseUnit, unit)) {
	    std::cerr << "Warning, cannot convert units ("
	    << DCDB::UnitConv::toString(baseUnit) << " -> "
	    << DCDB::UnitConv::toString(unit) << ")"
	    << std::endl;
	    return false;
	}
74
    }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	    
    return true;
}

void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results, queryMap_t::iterator start, queryMap_t::iterator stop) {
    /* Print Header */
    std::cout << "Sensor,Time";
    for (queryMap_t::iterator it=start; it!=stop; it++) {
	switch(it->second.operation) {
	    case DCDB_OP_NONE:
		std::cout << ",Value";
		break;
	    case DCDB_OP_DELTA:
		std::cout << ",Delta";
		break;
	    case DCDB_OP_DELTAT:
		std::cout << ",Delta_t";
		break;
	    case DCDB_OP_DERIVATIVE:
		std::cout << ",Derivative";
		break;
	    case DCDB_OP_INTEGRAL:
		std::cout << ",Integral";
		break;
99
	    default:
Alessio Netti's avatar
Alessio Netti committed
100
		break;
101
	}
102
103
	
	std::string unitStr;
104
	if(it->second.unit != DCDB::Unit_None) {
105
	    unitStr = DCDB::UnitConv::toString(it->second.unit);
106
	} else if (baseUnit != DCDB::Unit_None) {
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	    unitStr = DCDB::UnitConv::toString(baseUnit);
	}
	if (it->second.operation == DCDB_OP_DERIVATIVE) {
	    if ((unitStr.back() == 's') || (unitStr.back() == 'h')) {
		unitStr.pop_back();
	    } else if (unitStr.back() == 'J') {
		unitStr.pop_back();
		unitStr.append("W");
	    }
	} else if (it->second.operation == DCDB_OP_INTEGRAL) {
	    if (unitStr.back() == 'W') {
		unitStr.append("s");
	    }
	}
	if (unitStr.size() > 0) {
	    std::cout << " (" << unitStr << ")";
123
	}
124
    }
125
126
    std::cout << std::endl;
    
127
    int64_t prevReading=0;
128
    DCDB::TimeStamp prevT((uint64_t) 0);
129
130
    for (std::list<DCDB::SensorDataStoreReading>::iterator reading = results.begin(); reading != results.end(); reading++) {
	DCDB::TimeStamp ts = (*reading).timeStamp;
131
	
132
	/* Print the sensor's public name */
133
	std::cout << start->first.name << ",";
134
135
136
137
138
139
140
141
142
	
	/* Print the time stamp */
	if (useLocalTime)
	    ts.convertToLocal();
	
	if (useRawOutput)
	    std::cout << ts.getRaw();
	else
	    std::cout << ts.getString();
143
	
144
	int64_t value, result;
145
146
	/* Print the sensor value */
	for (queryMap_t::iterator it=start; it!=stop; it++) {
147
148
149
150
151
152
	    DCDB::Unit unit;
	    if (it->second.unit != DCDB::Unit_None) {
		unit = it->second.unit;
	    } else {
		unit = baseUnit;
	    }
153
154
	    value = (*reading).value;
	    result = 0;
155
156
157
158
159
160
161
162
163
164
165
166
	    bool resultOk = false;
	    switch(it->second.operation) {
		case DCDB_OP_NONE:
		    if (scaleAndConvert(value, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
			result = value;
			resultOk = true;
		    }
		    break;
		case DCDB_OP_DELTA:
		    if (scaleAndConvert(value, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
			if ((prevT > (uint64_t) 0) && (DCDB::delta(value, prevReading, &result) == DCDB::DCDB_OP_SUCCESS)) {
			    resultOk = true;
167
			}
168
169
170
171
		    }
		    break;
		case DCDB_OP_DELTAT:
		    if (scaleAndConvert(value, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
172
			if ((prevT > (uint64_t) 0) && (DCDB::delta(ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS)) {
173
			    resultOk = true;
174
			}
175
176
177
178
179
180
181
		    }
		    break;
		case DCDB_OP_DERIVATIVE: {
		    int64_t prevValue = prevReading;
		    if (scaleAndConvert(value, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit) && scaleAndConvert(prevValue, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
			if( (prevT > (uint64_t) 0) && DCDB::derivative(value, prevValue, ts.getRaw(), prevT.getRaw(), &result, unit) == DCDB::DCDB_OP_SUCCESS) {
			    resultOk = true;
182
			}
183
184
185
186
187
188
189
		    }
		    break;}
		case DCDB_OP_INTEGRAL: {
		    int64_t prevValue = prevReading;
		    if (scaleAndConvert(prevValue, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
			if( (prevT > (uint64_t) 0) && DCDB::integral(prevValue, ts.getRaw(), prevT.getRaw(), &result, unit) == DCDB::DCDB_OP_SUCCESS) {
			    resultOk = true;
190
			}
191
192
193
194
195
196
197
		    }
		    break;}
		default:
		    break;
	    }
	    if (resultOk) {
		std::cout << "," << result;
198
199
200
201
	    } else {
		std::cout << ",";
	    }
	}
202
	prevReading = value;
203
	prevT = ts;
204
	std::cout << std::endl;
205
    }
206
207
}

Michael Ott's avatar
Michael Ott committed
208
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
209
{
210
211
212
213
214
215
216
    /* Create a new connection to the database */
    connection = new DCDB::Connection();
    connection->setHostname(hostname);
    if (!connection->connect()) {
	std::cout << "Cannot connect to database." << std::endl;
	exit(EXIT_FAILURE);
    }
217
    
218
219
220
    /* Initialize the SensorConfig interface */
    DCDB::SensorConfig sensorConfig(connection);
    
221
222
223
224
225
226
227
    /* Iterate over list of sensors requested by the user */
    for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
	notifyOverflow = false;
	
	std::string sensorName;
	std::string functName;
	std::string modifierStr;
Alessio Netti's avatar
Alessio Netti committed
228
229
	//double scalingFactor = 1.0;
	//DCDB::Unit unit = DCDB::Unit_None;
230
231
232
233
234
235
236
237
238
	
	/* Retrieve sensor object first */
	std::string str = *it;
	boost::regex functRegex("^([^\\(\\)]+)\\(([^\\(\\)]+)\\)$", boost::regex::extended);
	boost::smatch match;
	if(boost::regex_search(str, match, functRegex)) {
	    functName = match[1].str();
	    str = match[2].str();
	}
Alessio Netti's avatar
Alessio Netti committed
239
	boost::regex sensorRegex("([^\\@]+)\\@?([^\\@]*)", boost::regex::extended);
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	if(boost::regex_search(str, match, sensorRegex)) {
	    sensorName = match[1].str();
	    modifierStr = match[2].str();
	}
	
	queryConfig_t queryCfg = { 1.0, DCDB::Unit_None, DCDB_OP_NONE};
	if (functName.length() == 0) {
	    queryCfg.operation = DCDB_OP_NONE;
	} else if (boost::iequals(functName, "delta")) {
	    queryCfg.operation = DCDB_OP_DELTA;
	} else if (boost::iequals(functName, "delta_t")) {
	    queryCfg.operation = DCDB_OP_DELTAT;
	} else if (boost::iequals(functName, "derivative")) {
	    queryCfg.operation = DCDB_OP_DERIVATIVE;
	} else if (boost::iequals(functName, "integral")) {
	    queryCfg.operation = DCDB_OP_INTEGRAL;
	} else {
	    queryCfg.operation = DCDB_OP_UNKNOWN;
	    std::cerr << "Unknown sensor operation: " << functName << std::endl;
	}
	
	if (queryCfg.operation != DCDB_OP_UNKNOWN) {
	    if (modifierStr.length() > 0) {
		boost::regex e("[0-9]*\\.?[0-9]*", boost::regex::extended);
		if (boost::regex_match(modifierStr, e)) {
		    queryCfg.scalingFactor = atof(modifierStr.c_str());
		} else {
		    queryCfg.unit = DCDB::UnitConv::fromString(modifierStr);
		}
	    }
Michael Ott's avatar
Michael Ott committed
270
271
272
273
274
275
276
277
278
279
280
281
	    
	    std::list <DCDB::PublicSensor> publicSensors;
	    sensorConfig.getPublicSensorsByWildcard(publicSensors, sensorName.c_str());
	    if (publicSensors.size() > 0) {
		for (auto sen: publicSensors) {
		    queries.insert(std::pair<DCDB::PublicSensor, queryConfig_t>(sen, queryCfg));
		}
	    } else {
		DCDB::PublicSensor pS;
		pS.name = sensorName;
		pS.pattern = sensorName;
		queries.insert(std::pair<DCDB::PublicSensor, queryConfig_t>(pS, queryCfg));
282
	    }
283
284
285
286
287
	}
    }
    
    std::string prevSensorName;
    for (auto q: queries) {
288
	if (q.first.name != prevSensorName) {
289
	    std::pair<queryMap_t::iterator, queryMap_t::iterator> range = queries.equal_range(q.first);
290
291
292
293
294
295
296
297

		/* Base scaling factor and unit of the public sensor */
		baseUnit = DCDB::UnitConv::fromString(q.first.unit);
		baseScalingFactor = q.first.scaling_factor;
		
		std::list<DCDB::SensorDataStoreReading> results;
		DCDB::Sensor sensor(connection, q.first);
		sensor.query(results, start, end, DCDB::AGGREGATE_NONE);
298
		
299
300
301
		genOutput(results, range.first, range.second);
		if(notifyOverflow)
		    std::cout << "Overflow detected." << std::endl;
302
	    
303
	    prevSensorName = q.first.name;
304
305
306
307
308
309
310
311
	}
    }
    
    /*
     * Clean up
     */
    connection->disconnect();
    delete connection;
Axel Auweter's avatar
Axel Auweter committed
312
313
}

Axel Auweter's avatar
Axel Auweter committed
314
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
315
{
Axel Auweter's avatar
Axel Auweter committed
316
317
318
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
319
}