The expiration time for new job artifacts in CI/CD pipelines is now 30 days (GitLab default). Previously generated artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

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

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

125
126
127
128
129
130
131
132
133
	/* 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;
134
			if ((prevT > (uint64_t) 0) && (DCDB::delta(value, prevValue, &result) == DCDB::DCDB_OP_SUCCESS)) {
135
136
137
138
139
140
141
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
		    case DCDB_OP_DELTAT: {
			int64_t result;
142
			if ((prevT > (uint64_t) 0) && (DCDB::delta(ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS)) {
143
144
145
146
147
148
149
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
		    case DCDB_OP_DERIVATIVE: {
			int64_t result;
150
			if( (prevT > (uint64_t) 0) && DCDB::derivative(value, prevValue, ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS) {
151
152
153
154
155
156
157
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
		    case DCDB_OP_INTEGRAL: {
			int64_t result;
158
			if( (prevT > (uint64_t) 0) && DCDB::integral(value, prevValue, ts.getRaw(), prevT.getRaw(), &result) == DCDB::DCDB_OP_SUCCESS) {
159
160
161
162
163
164
165
166
167
168
169
170
171
			    std::cout << "," << result;
			} else {
			    std::cout << ",";
			}
			break;}
		}
	    } else {
		std::cout << ",";
	    }
	}
	prevValue = (*reading).value;
	prevT = (*reading).timeStamp.getRaw();
	std::cout << std::endl;
172
    }
173
174
}

175
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
176
{
177
178
179
180
181
182
183
    /* 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);
    }
184
    
185
186
187
    /* Initialize the SensorConfig interface */
    DCDB::SensorConfig sensorConfig(connection);
    
188
189
190
191
192
193
194
195
196
197
198
199
200
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
    /* 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;
	double scalingFactor = 1.0;
	DCDB::Unit unit = DCDB::Unit_None;
	
	/* 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);
		}
	    }
237
238
239
240
241
	    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));
	    }
242
243
244
245
246
	}
    }
    
    std::string prevSensorName;
    for (auto q: queries) {
247
	if (q.first.name != prevSensorName) {
248
	    std::pair<queryMap_t::iterator, queryMap_t::iterator> range = queries.equal_range(q.first);
249
250
251
252
253
254
255
256
257
258
259

		/* 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;
260
	    
261
	    prevSensorName = q.first.name;
262
263
264
265
266
267
268
269
	}
    }
    
    /*
     * Clean up
     */
    connection->disconnect();
    delete connection;
Axel Auweter's avatar
Axel Auweter committed
270
271
}

Axel Auweter's avatar
Axel Auweter committed
272
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
273
{
Axel Auweter's avatar
Axel Auweter committed
274
275
276
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
277
}