query.cpp 15.4 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
void DCDBQuery::genOutput(std::list<DCDB::SensorDataStoreReading> &results)
59
{
60
	int64_t prev = 0;
61
    uint64_t prevT = 0;
62
    
63
64
    /* Print Header */
    std::cout << "Sensor,Time";
65
    if(valueFormat.printValue) {
66
        std::cout << ",Value";
67
68
69
70
71
        if(valueFormat.unit != "none")
            std::cout << " (" << valueFormat.unit << ")";
    }
    
    if(deltaFormat.printValue) {
72
        std::cout << ",Delta";
73
74
75
76
        if(deltaFormat.unit != "none")
            std::cout << " (" << deltaFormat.unit << ")";
    }
    if(deltaTFormat.printValue) {
77
        std::cout << ",DeltaT";
78
79
80
81
        if(deltaTFormat.unit != "none")
            std::cout << " (" << deltaTFormat.unit << ")";
    }
    if(derivativeFormat.printValue) {
82
        std::cout << ",Derivative";
83
84
85
86
        if(derivativeFormat.unit != "none")
            std::cout << " (" << derivativeFormat.unit << ")";
    }
    if(integralFormat.printValue) {
87
        std::cout << ",Integral";
88
89
90
        if(integralFormat.unit != "none")
            std::cout << " (" << integralFormat.unit << ")";
    }
91
92
93
94
    
    /*End of header */
    std::cout << std::endl;
    
95
	for (std::list<DCDB::SensorDataStoreReading>::iterator reading = results.begin(); reading != results.end(); reading++) {
96
97
98
99
100

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

		/* Print the time stamp */
101
		if (useLocalTime)
102
			(*reading).timeStamp.convertToLocal();
103
104
        
		if (useRawOutput)
105
			std::cout << (*reading).timeStamp.getRaw();
106
		else
107
			std::cout << (*reading).timeStamp.getString();
108
109

		/* Print the sensor value */
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        if(valueFormat.printValue) {
            
            int64_t result = (*reading).value;
            
            if(valueFormat.scalingFactor != 1.0 || baseScalingFactor != 1.0) {
                if( DCDB::scale(&result, valueFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
            }
            
            /* Convert the unit if requested */
            if (DCDB::UnitConv::fromString(valueFormat.unit) != baseUnit) {
                
                if (!DCDB::UnitConv::convert((*reading).value, baseUnit, DCDB::UnitConv::fromString(valueFormat.unit)))
                    std::cerr << "Warning, cannot convert units ("
                    << DCDB::UnitConv::toString(baseUnit) << " -> "
                    << valueFormat.unit << ")"
                    << std::endl;
127
            }
128
129

            std::cout << "," << result;
130
        }
131

132
		/* Print Delta */
133
134
135
136
137
138
139
140
141
142
143
        if(deltaFormat.printValue && reading != results.begin()) {
            
            int64_t result;
            int64_t current = (*reading).value;
            int64_t previous = prev;
            
            if(deltaFormat.scalingFactor != 1.0) {
                if( DCDB::scale(&current, deltaFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
                if( DCDB::scale(&previous, deltaFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
144
            }
145
146
147
148
149
150
151
152
153
154
155
156
157
            
            if( DCDB::delta(current, previous, &result) == DCDB::DCDB_OP_OVERFLOW )
                notifyOverflow = true;
            
            if (DCDB::UnitConv::fromString(deltaFormat.unit) != baseUnit) {
                if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(deltaFormat.unit)))
                    std::cerr << "Warning, cannot convert units ("
                    << DCDB::UnitConv::toString(baseUnit) << " -> "
                    << deltaFormat.unit << ")"
                    << std::endl;
            }
            
            std::cout << "," << result;
158
159
        }
        
160
161
162
163
164
165
166
167
168
169
170
171
        /* Print Delta T */
        if(deltaTFormat.printValue && reading != results.begin()) {
        
            int64_t result;
            int64_t current = (*reading).timeStamp.getRaw();
            int64_t previous = prevT;
            
            if(deltaTFormat.scalingFactor != 1.0 || baseScalingFactor != 1.0) {
                if( DCDB::scale(&current, deltaTFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
                if( DCDB::scale(&previous, deltaTFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
172
            }
173
174
175
176
177
178
179
180
181
182
183
184
185
            if( DCDB::delta(current, prevT, &result) == DCDB::DCDB_OP_OVERFLOW)
                notifyOverflow = true;
        
            if (DCDB::UnitConv::fromString(deltaTFormat.unit) != baseUnit) {
                if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(deltaTFormat.unit)))
                    std::cerr << "Warning, cannot convert units ("
                    << DCDB::UnitConv::toString(baseUnit) << " -> "
                    << deltaTFormat.unit << ")"
                    << std::endl;
            }
            
            std::cout << "," << result;
        
186
187
        }
        
188
189
190
191
192
193
194
195
196
197
198
199
200
        /* Print Derivative */
        if(derivativeFormat.printValue && reading != results.begin()) {
            
            int64_t result;
            int64_t current = (*reading).value;
            int64_t previous = prev;
            
            /* No need to check overflow for dt as the scaling only applies to dx */
            if(derivativeFormat.scalingFactor != 1.0 || baseScalingFactor != 1.0) {
                if( DCDB::scale(&current, derivativeFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
                if( DCDB::scale(&previous, derivativeFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
201
            }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
            
            if( DCDB::derivative(current, previous, (*reading).timeStamp.getRaw(), prevT, &result) == DCDB::DCDB_OP_OVERFLOW)
                notifyOverflow = true;
            
            if (DCDB::UnitConv::fromString(derivativeFormat.unit) != baseUnit) {
                if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(derivativeFormat.unit)))
                    std::cerr << "Warning, cannot convert units ("
                    << DCDB::UnitConv::toString(baseUnit) << " -> "
                    << derivativeFormat.unit << ")"
                    << std::endl;
            }
            
            std::cout << "," << result;
            
216
217
        }
        
218
219
220
221
222
223
224
225
226
227
228
229
230
        /* Print Integral */
        if(integralFormat.printValue && reading != results.begin()) {
            
            int64_t result;
            int64_t current = (*reading).value;
            int64_t previous = prev;

            /* Very unlikely that dt > dx in case of overflow, so we just scale dx */
            if(integralFormat.scalingFactor != 1.0 || baseScalingFactor != 1.0) {
                if( DCDB::scale(&current, integralFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
                if( DCDB::scale(&previous, integralFormat.scalingFactor, baseScalingFactor) == DCDB::DCDB_OP_OVERFLOW)
                    notifyOverflow = true;
231
            }
232
233
234
235
236
237
238
239
240
241
242
243
244
            
            if( DCDB::integral(current, previous, (*reading).timeStamp.getRaw(), prevT, &result) == DCDB::DCDB_OP_OVERFLOW)
                notifyOverflow = true;
            
            if (DCDB::UnitConv::fromString(integralFormat.unit) != baseUnit) {
                if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(integralFormat.unit)))
                    std::cerr << "Warning, cannot convert units ("
                    << DCDB::UnitConv::toString(baseUnit) << " -> "
                    << integralFormat.unit << ")"
                    << std::endl;
            }
            
            std::cout << "," << result;
245
        }
246
247
		
        std::cout << std::endl;
248
249
250
            
        prev = (*reading).value;
        prevT = (*reading).timeStamp.getRaw();
251
    }
252
253
}

254

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
void DCDBQuery::checkModifier(std::list<std::string>::iterator it, double* scalingFactor, std::string* unit) {
    
    std::string modifierStr = it->substr(it->find('/')+1, it->length());
    
    /* Check what type of modification is requested */
    //boost::regex e("\\.?[0-9]*", boost::regex::extended);
    boost::regex e("[0-9]*\.?[0-9]*", boost::regex::extended);
    if (boost::regex_match(modifierStr, e))
        
        sscanf(modifierStr.c_str(), "%lf", scalingFactor);
   
    else
        unit = &modifierStr;
}

270
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
271
272
{
  /* Create a new connection to the database */
273
  connection = new DCDB::Connection();
Axel Auweter's avatar
Axel Auweter committed
274
275
276
  connection->setHostname(hostname);
  if (!connection->connect()) {
      std::cout << "Cannot connect to database." << std::endl;
277
      exit(EXIT_FAILURE);
Axel Auweter's avatar
Axel Auweter committed
278
279
280
  }

  /* Initialize the SensorConfig interface */
281
  DCDB::SensorConfig sensorConfig(connection);
Axel Auweter's avatar
Axel Auweter committed
282
283
284

  /* Iterate over list of sensors requested by the user */
  for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
285
      
286
      scalingFactor = 1;
287
288
289
290
291
292
293
294
295
      unit = "none";
      
      /* Output format initialization */
      valueFormat = {false,1,"none"};
      deltaFormat = {false,1,"none"};
      deltaTFormat = {false,1,"none"};
      derivativeFormat = {false,1,"none"};
      integralFormat = {false,1,"none"};
      
root's avatar
root committed
296
      std::string modifierStr;
297
      boost::smatch match;
298
      baseUnit = DCDB::Unit_None;
299
300
301
      
      sensorName = *it;
      
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
      DCDB::PublicSensor sen;
      sensorConfig.getPublicSensorByName(sen, it->c_str());
      
      /* Base scaling factor and unit of the public sensor */
      baseUnit = DCDB::UnitConv::fromString(sen.unit);
      baseScalingFactor = sen.scaling_factor;
      
      /* Retrieve sensor object first */
      boost::regex functExp("^delta|^delta_t|^derivative|^integral", boost::regex::extended);
      if(boost::regex_search(*it, match, functExp))
          sensorName = it->substr(it->find('(') + 1, it->size() - it->find('(') - (it->size() - it->find(')')) - 1 );
      else if (it->find('/') != std::string::npos)
          sensorName = it->substr(0, it->find('/'));
      
      DCDB::Sensor sensor(connection, sensorName);
      
      /* Iterate over the list to detect all instances of the sensor with name "sensorName" */
319
320
321
      while(it != sensors.end()) {
          
          if(it->find(sensorName) != std::string::npos) {
322
323
324
              
              /* Check first if the sensor we're looking is part of a function ...*/
              if(boost::regex_search(*it, match, functExp)) {
325
                  
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
                  if(match[0].compare("delta")==0) {
                      
                      deltaFormat.printValue = true;
                      
                      /* ...and for each function, check if we need a different scaling factor or unit... */
                      if(it->find('/') != std::string::npos) {
                          checkModifier(it, &scalingFactor, &unit);
                          deltaFormat.scalingFactor = scalingFactor;
                          deltaFormat.unit = unit;
                      }
                  }
                  else if(match[0].compare("delta_t")==0) {
                      
                      deltaTFormat.printValue = true;
                      
                      if(it->find('/') != std::string::npos) {
                          checkModifier(it, &scalingFactor, &unit);
                          deltaTFormat.scalingFactor = scalingFactor;
                          deltaTFormat.unit = unit;
                      }
                  }
                  else if(match[0].compare("derivative")==0) {
                      
                      derivativeFormat.printValue = true;
                      
                      if(it->find('/') != std::string::npos) {
                          checkModifier(it, &scalingFactor, &unit);
                          derivativeFormat.scalingFactor = scalingFactor;
                          derivativeFormat.unit = unit;
                      }
356
                  }
357
                  else if(match[0].compare("integral")==0) {
358
                      
359
360
361
362
363
364
365
                      integralFormat.printValue = true;
                      
                      if(it->find('/') != std::string::npos) {
                          checkModifier(it, &scalingFactor, &unit);
                          integralFormat.scalingFactor = scalingFactor;
                          integralFormat.unit = unit;
                      }
366
367
368
                  }
              }
              
369
370
              /* ...otherwise, print just the sensor values and check if they are requested in a different scaling factor or unit... */
              else {
371
                  
372
                  valueFormat.printValue = true;
373
                  
374
375
376
377
378
379
                  if(it->find('/') != std::string::npos) {
                      
                      checkModifier(it, &scalingFactor, &unit);
                      valueFormat.scalingFactor = scalingFactor;
                      valueFormat.unit = unit;
                  }
380
              }
381
              /*...remove this instance of the sensor from the list */
382
              it = sensors.erase(it);
383
          }
384
385
          else
              it++;
Axel Auweter's avatar
Axel Auweter committed
386
      }
387
      
388
      std::list<DCDB::SensorDataStoreReading> results;
389
      sensor.query(results, start, end, DCDB::AGGREGATE_NONE);
390
      genOutput(results);
391
392
      if(notifyOverflow)
          std::cout << "There has been overflow on some functions. " << std::endl;
Axel Auweter's avatar
Axel Auweter committed
393
394
395
396
397
398
399
400
401
402
  }

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

}

Axel Auweter's avatar
Axel Auweter committed
403
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
404
{
Axel Auweter's avatar
Axel Auweter committed
405
406
407
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
408
}