query.cpp 9.48 KB
Newer Older
Axel Auweter's avatar
Axel Auweter committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//================================================================================
// Name        : query.cpp
// Author      : Axel Auweter
// Copyright   : Leibniz Supercomputing Centre
// Description : Implementation of query class of dcdbquery
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2016 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.
//================================================================================
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
40
#include "query.h"

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

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

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

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

Axel Auweter's avatar
Axel Auweter committed
57
58
59
60
61
62
63
64
void DCDBQuery::setFloatOutputEnabled(bool enable) {
  useFloatOutput = enable;
}

bool DCDBQuery::getFloatOutputEnabled() {
  return useFloatOutput;
}

65
void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
66
{
67
	int64_t prev = 0;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    uint64_t prevT = 0;
    int64_t dx = 0;
    uint64_t dt = 0;
    
    /* Print Header */
    std::cout << "Sensor,Time";
    if(printValue)
        std::cout << ",Value";
    if(calculateDelta)
        std::cout << ",Delta";
    if(calculateDeltaT)
        std::cout << ",DeltaT";
    if(calculateDerivative)
        std::cout << ",Derivative";
    if(calculateIntegral)
        std::cout << ",Integral";
    
    /*End of header */
    std::cout << std::endl;
    
88
89
90
91
92
93
94
95
96
97
98
	for (std::list<DCDB::SensorDataStoreReading>::iterator reading =
			results.begin(); reading != results.end(); reading++) {
		double fvalue;
		int64_t ivalue;

		/* Assign the reading to local variable */
		if (useFloatOutput) {
			fvalue = (*reading).value;
		} else {
			ivalue = (*reading).value;
		}
99
        
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
		/* Convert the unit if requested */
		if (unitConvert) {
			if (useFloatOutput) {
				if (!DCDB::UnitConv::convert(fvalue, baseUnit, targetUnit)) {
					std::cerr << "Warning, cannot convert units ("
							<< DCDB::UnitConv::toString(baseUnit) << " -> "
							<< DCDB::UnitConv::toString(targetUnit) << ")"
							<< std::endl;
					unitConvert = false;
				}
			} else {
				if (!DCDB::UnitConv::convert(ivalue, baseUnit, targetUnit)) {
					std::cerr << "Warning, cannot convert units ("
							<< DCDB::UnitConv::toString(baseUnit) << " -> "
							<< DCDB::UnitConv::toString(targetUnit) << ")"
							<< std::endl;
					unitConvert = false;
				}
			}
		}

		/* Print the sensor's public name */
		std::cout << sensorName << ",";

		/* Print the time stamp */
		if (useLocalTime) {
			(*reading).timeStamp.convertToLocal();
		}
		if (useRawOutput) {
129
			std::cout << (*reading).timeStamp.getRaw();
130
		} else {
131
			std::cout << (*reading).timeStamp.getString();
132
133
134
		}

		/* Print the sensor value */
135
136
137
138
139
140
141
        if(printValue) {
            if (useFloatOutput) {
                std::cout << "," << fvalue;
            } else {
                std::cout << "," << ivalue;
            }
        }
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
		/* Print Delta */
        if(calculateDelta)
        {
            if (reading != results.begin()) {
                dx = (*reading).value - prev;
                std::cout << "," << dx;
            }
        }
        
        /* Print Delta T*/
        if(calculateDeltaT)
        {
            if (reading != results.begin()) {
                dt = (*reading).timeStamp.getRaw() - prevT;
                std::cout << "," << dt;
            }
        }
        
        /* Print Derivative*/
        if(calculateDerivative)
        {
            if(reading != results.begin()) {
                if(!calculateDelta)
                    dx = (*reading).value - prev;
                if(!calculateDeltaT)
                    dt = (*reading).timeStamp.getRaw() - prevT;
                if(reading != results.begin())
                    std::cout << "," << dx / dt;
            }
        }
        
        /* Print Integral*/
        if(calculateIntegral)
        {
            if(reading != results.begin()) {
                if(!calculateDelta)
                    dx = (*reading).value - prev;
                if(!calculateDeltaT)
                    dt = (*reading).timeStamp.getRaw() - prevT;
                std::cout << "," << dx*dt;
            }
        }
185
		std::cout << std::endl;
186
187
188
            
        prev = (*reading).value;
        prevT = (*reading).timeStamp.getRaw();
189
	}
190
191
}

192

193
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
194
195
{
  /* Create a new connection to the database */
196
  connection = new DCDB::Connection();
Axel Auweter's avatar
Axel Auweter committed
197
198
199
  connection->setHostname(hostname);
  if (!connection->connect()) {
      std::cout << "Cannot connect to database." << std::endl;
200
      exit(EXIT_FAILURE);
Axel Auweter's avatar
Axel Auweter committed
201
202
203
  }

  /* Initialize the SensorConfig interface */
204
  DCDB::SensorConfig sensorConfig(connection);
Axel Auweter's avatar
Axel Auweter committed
205
206

  /* Print the CSV header */
207
    //std::cout << "Sensor,Time,Value,Delta,DeltaT,Derivative,Integral" << std::endl;
Axel Auweter's avatar
Axel Auweter committed
208
209
210

  /* Iterate over list of sensors requested by the user */
  for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
211
      unitConvert = false;
212
213
214
215
216
      printValue = false;
      calculateDelta = false;
      calculateDeltaT = false;
      calculateDerivative = false;
      calculateIntegral = false;
217
218
      scale = false;
      scalingFactor = 1;
root's avatar
root committed
219
      std::string modifierStr;
220
221
      baseUnit = DCDB::Unit_None;
      targetUnit = DCDB::Unit_None;
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
      
      sensorName = *it;
      
      while(it != sensors.end()) {
          
          if(it->find(sensorName) != std::string::npos) {
              /* Check if the sensor was requested in a different unit or with scaling factor */
              if (it->find('/') != std::string::npos) {
                  modifierStr = it->substr(it->find('/')+1, it->length());
                  
                  /* Remove the modifier from the string */
                  *it = it->substr(0, it->find('/'));
                  
                  /* Check what type of modification is requested */
                  boost::regex e("\\.?[0-9]*", boost::regex::extended);
                  if (boost::regex_match(modifierStr, e)) {
                      scale = true;
                      sscanf(modifierStr.c_str(), "%lf", &scalingFactor);
                  }
                  else {
                      unitConvert = true;
                      targetUnit = DCDB::UnitConv::fromString(modifierStr);
                      
                      DCDB::PublicSensor sen;
                      sensorConfig.getPublicSensorByName(sen, it->c_str());
                      baseUnit = DCDB::UnitConv::fromString(sen.unit);
                  }
              }
              
              /* Check if there are functions defined on sensors (e.g., delta, integral, etc.) */
              if (it->find("#delta") != std::string::npos) {
                  
                  /* Remove the modifier from the string */
                  *it = it->substr(0, it->find("#delta"));
                  calculateDelta = true;
                  sensorName = *it;
              }
              else if (it->find("#delta_t") != std::string::npos) {
                  
                  /* Remove the modifier from the string */
                  calculateDeltaT = true;
                  sensorName = *it;
              }
              else if (it->find("#integ") != std::string::npos) {
                  
                  /* Remove the modifier from the string */
                  *it = it->substr(0, it->find("#integ"));
                  calculateIntegral = true;
                  sensorName = *it;
              }
              else if (it->find("#deriv") != std::string::npos) {
                  
                  /* Remove the modifier from the string */
                  *it = it->substr(0, it->find("#deriv"));
                  calculateDerivative = true;
                  sensorName = *it;
              }
              else {
                  printValue = true;
                  sensorName = *it;
              }
          
              it = sensors.erase(it);
285
          }
286
287
          else
              it++;
Axel Auweter's avatar
Axel Auweter committed
288
      }
289
      
290
291
      std::list<DCDB::SensorDataStoreReading> results;
      DCDB::Sensor sensor(connection, sensorName);
292
293
294
      if (scale) {
        sensor.setScalingFactor(scalingFactor);
      }
295
      sensor.query(results, start, end);
296
      genOutput(results);
Axel Auweter's avatar
Axel Auweter committed
297
298
299
300
301
302
303
304
305
306
  }

  /*
   * Clean up
   */
  connection->disconnect();
  delete connection;

}

Axel Auweter's avatar
Axel Auweter committed
307
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
308
{
Axel Auweter's avatar
Axel Auweter committed
309
310
311
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
312
  useFloatOutput = false;
Axel Auweter's avatar
Axel Auweter committed
313
}