query.cpp 8.68 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-2018 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;
Alessio Netti's avatar
Alessio Netti committed
98
99
		default:
		break;
100
101
102
	}
	if(it->second.unit != DCDB::Unit_None) {
	    std::cout << " (" << DCDB::UnitConv::toString(it->second.unit) << ")";
103
104
	} else if (baseUnit != DCDB::Unit_None) {
	    std::cout << " (" << DCDB::UnitConv::toString(baseUnit) << ")";
105
	}
106
    }
107
108
    std::cout << std::endl;
    
109
    int64_t prevValue;
110
    DCDB::TimeStamp prevT((uint64_t) 0);
111
112
113
    for (std::list<DCDB::SensorDataStoreReading>::iterator reading = results.begin(); reading != results.end(); reading++) {
	int64_t value = (*reading).value;
	DCDB::TimeStamp ts = (*reading).timeStamp;
114

115
	/* Print the sensor's public name */
116
	std::cout << start->first.name << ",";
117
118
119
120
121
122
123
124
125
	
	/* Print the time stamp */
	if (useLocalTime)
	    ts.convertToLocal();
	
	if (useRawOutput)
	    std::cout << ts.getRaw();
	else
	    std::cout << ts.getString();
126

127
128
129
130
131
132
133
134
135
	/* Print the sensor value */
	for (queryMap_t::iterator it=start; it!=stop; it++) {
	    if (scaleAndConvert(value, baseScalingFactor, it->second.scalingFactor, baseUnit, it->second.unit)) {
		switch(it->second.operation) {
		    case DCDB_OP_NONE:
			std::cout << "," << value;
			break;
		    case DCDB_OP_DELTA: {
			int64_t result;
136
			if ((prevT > (uint64_t) 0) && (DCDB::delta(value, prevValue, &result) == DCDB::DCDB_OP_SUCCESS)) {
137
138
139
140
141
142
143
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
		    case DCDB_OP_DELTAT: {
			int64_t result;
144
			if ((prevT > (uint64_t) 0) && (DCDB::delta(ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS)) {
145
146
147
148
149
150
151
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
		    case DCDB_OP_DERIVATIVE: {
			int64_t result;
152
			if( (prevT > (uint64_t) 0) && DCDB::derivative(value, prevValue, ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS) {
153
154
155
156
157
158
159
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
		    case DCDB_OP_INTEGRAL: {
			int64_t result;
160
			if( (prevT > (uint64_t) 0) && DCDB::integral(value, prevValue, ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS) {
161
162
163
164
165
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
Alessio Netti's avatar
Alessio Netti committed
166
167
			default:
			break;
168
169
170
171
172
173
174
175
		}
	    } else {
		std::cout << ",";
	    }
	}
	prevValue = (*reading).value;
	prevT = (*reading).timeStamp.getRaw();
	std::cout << std::endl;
176
    }
177
178
}

179
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
180
{
181
182
183
184
185
186
187
    /* 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);
    }
188
    
189
190
191
    /* Initialize the SensorConfig interface */
    DCDB::SensorConfig sensorConfig(connection);
    
192
193
194
195
196
197
198
    /* 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
199
200
	//double scalingFactor = 1.0;
	//DCDB::Unit unit = DCDB::Unit_None;
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	
	/* 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();
	}
	boost::regex sensorRegex("([^/]+)/?([^/]*)", boost::regex::extended);
	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);
		}
	    }
241
242
243
244
245
	    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));
	    }
246
247
248
249
250
	}
    }
    
    std::string prevSensorName;
    for (auto q: queries) {
251
	if (q.first.name != prevSensorName) {
252
	    std::pair<queryMap_t::iterator, queryMap_t::iterator> range = queries.equal_range(q.first);
253
254
255
256
257
258
259
260
261
262
263

		/* 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);
		genOutput(results, range.first, range.second);
		if(notifyOverflow)
		    std::cout << "Overflow detected." << std::endl;
264
	    
265
	    prevSensorName = q.first.name;
266
267
268
269
270
271
272
273
	}
    }
    
    /*
     * Clean up
     */
    connection->disconnect();
    delete connection;
Axel Auweter's avatar
Axel Auweter committed
274
275
}

Axel Auweter's avatar
Axel Auweter committed
276
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
277
{
Axel Auweter's avatar
Axel Auweter committed
278
279
280
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
281
}