query.cpp 9.7 KB
Newer Older
Axel Auweter's avatar
Axel Auweter committed
1
2
//================================================================================
// Name        : query.cpp
3
// Author      : Axel Auweter, Daniele Tafani
Axel Auweter's avatar
Axel Auweter committed
4
5
6
7
8
9
// Copyright   : Leibniz Supercomputing Centre
// Description : Implementation of query class of dcdbquery
//================================================================================

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

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

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

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

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

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

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

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

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

58
59
60
61
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;
62
63
    }
    
64
65
66
67
68
69
70
71
72
    /* 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;
	}
73
    }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	    
    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;
98
	    default:
Alessio Netti's avatar
Alessio Netti committed
99
		break;
100
	}
101
102
	
	std::string unitStr;
103
	if(it->second.unit != DCDB::Unit_None) {
104
	    unitStr = DCDB::UnitConv::toString(it->second.unit);
105
	} else if (baseUnit != DCDB::Unit_None) {
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	    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 << ")";
122
	}
123
    }
124
125
    std::cout << std::endl;
    
126
    int64_t prevReading=0;
127
    DCDB::TimeStamp prevT((uint64_t) 0);
128
129
    for (std::list<DCDB::SensorDataStoreReading>::iterator reading = results.begin(); reading != results.end(); reading++) {
	DCDB::TimeStamp ts = (*reading).timeStamp;
130
	
131
	/* Print the sensor's public name */
132
	std::cout << start->first.name << ",";
133
134
135
136
137
138
139
140
141
	
	/* Print the time stamp */
	if (useLocalTime)
	    ts.convertToLocal();
	
	if (useRawOutput)
	    std::cout << ts.getRaw();
	else
	    std::cout << ts.getString();
142
	
143
144
	/* Print the sensor value */
	for (queryMap_t::iterator it=start; it!=stop; it++) {
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	    DCDB::Unit unit;
	    if (it->second.unit != DCDB::Unit_None) {
		unit = it->second.unit;
	    } else {
		unit = baseUnit;
	    }
	    int64_t value = (*reading).value;
	    int64_t result;
	    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;
165
			}
166
167
168
169
		    }
		    break;
		case DCDB_OP_DELTAT:
		    if (scaleAndConvert(value, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
170
			if ((prevT > (uint64_t) 0) && (DCDB::delta(ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS)) {
171
			    resultOk = true;
172
			}
173
174
175
176
177
178
179
		    }
		    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;
180
			}
181
182
183
184
185
186
187
		    }
		    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;
188
			}
189
190
191
192
193
194
195
		    }
		    break;}
		default:
		    break;
	    }
	    if (resultOk) {
		std::cout << "," << result;
196
197
198
199
	    } else {
		std::cout << ",";
	    }
	}
200
	prevReading = (*reading).value;
201
	prevT = ts;
202
	std::cout << std::endl;
203
    }
204
205
}

206
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
207
{
208
209
210
211
212
213
214
    /* 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);
    }
215
    
216
217
218
    /* Initialize the SensorConfig interface */
    DCDB::SensorConfig sensorConfig(connection);
    
219
220
221
222
223
224
225
    /* 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
226
227
	//double scalingFactor = 1.0;
	//DCDB::Unit unit = DCDB::Unit_None;
228
229
230
231
232
233
234
235
236
	
	/* 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();
	}
237
	boost::regex sensorRegex("([^\\%]+)\\%?([^\\%]*)", boost::regex::extended);
238
239
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
	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);
		}
	    }
268
269
270
271
272
	    std::list<DCDB::PublicSensor> publicSensors;
	    sensorConfig.getPublicSensorsByWildcard(publicSensors, sensorName.c_str());
	    for (auto sen: publicSensors) {
		queries.insert(std::pair<DCDB::PublicSensor,queryConfig_t>(sen, queryCfg));
	    }
273
274
275
276
277
	}
    }
    
    std::string prevSensorName;
    for (auto q: queries) {
278
	if (q.first.name != prevSensorName) {
279
	    std::pair<queryMap_t::iterator, queryMap_t::iterator> range = queries.equal_range(q.first);
280
281
282
283
284
285
286
287

		/* 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);
288
		
289
290
291
		genOutput(results, range.first, range.second);
		if(notifyOverflow)
		    std::cout << "Overflow detected." << std::endl;
292
	    
293
	    prevSensorName = q.first.name;
294
295
296
297
298
299
300
301
	}
    }
    
    /*
     * Clean up
     */
    connection->disconnect();
    delete connection;
Axel Auweter's avatar
Axel Auweter committed
302
303
}

Axel Auweter's avatar
Axel Auweter committed
304
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
305
{
Axel Auweter's avatar
Axel Auweter committed
306
307
308
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
309
}