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"
40
41
#include "sensordatastore.h"
#include "sensoroperations.h"
Axel Auweter's avatar
Axel Auweter committed
42

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

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

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

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

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

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

		/* Print the time stamp */
105
		if (useLocalTime)
106
			(*reading).timeStamp.convertToLocal();
107
108
        
		if (useRawOutput)
109
			std::cout << (*reading).timeStamp.getRaw();
110
		else
111
			std::cout << (*reading).timeStamp.getString();
112
113

		/* Print the sensor value */
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        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;
131
            }
132
133

            std::cout << "," << result;
134
        }
135

136
		/* Print Delta */
137
138
139
140
141
142
143
144
145
146
147
        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;
148
            }
149
150
151
152
153
154
155
156
157
158
159
160
161
            
            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;
162
163
        }
        
164
165
166
167
168
169
170
171
172
173
174
175
        /* 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;
176
            }
177
178
179
180
181
182
183
184
185
186
187
188
189
            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;
        
190
191
        }
        
192
193
194
195
196
197
198
199
200
201
202
203
204
        /* 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;
205
            }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
            
            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;
            
220
221
        }
        
222
223
224
225
226
227
228
229
230
231
232
233
234
        /* 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;
235
            }
236
237
238
239
240
241
242
243
244
245
246
247
248
            
            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;
249
        }
250
251
		
        std::cout << std::endl;
252
253
254
            
        prev = (*reading).value;
        prevT = (*reading).timeStamp.getRaw();
255
    }
256
257
}

258

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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;
}

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

  /* Initialize the SensorConfig interface */
285
  DCDB::SensorConfig sensorConfig(connection);
Axel Auweter's avatar
Axel Auweter committed
286
287
288

  /* Iterate over list of sensors requested by the user */
  for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
289
      
290
      scalingFactor = 1;
291
292
293
294
295
296
297
298
299
      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
300
      std::string modifierStr;
301
      boost::smatch match;
302
      baseUnit = DCDB::Unit_None;
303
304
305
      
      sensorName = *it;
      
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
      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" */
323
324
325
      while(it != sensors.end()) {
          
          if(it->find(sensorName) != std::string::npos) {
326
327
328
              
              /* Check first if the sensor we're looking is part of a function ...*/
              if(boost::regex_search(*it, match, functExp)) {
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
356
357
358
359
                  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;
                      }
360
                  }
361
                  else if(match[0].compare("integral")==0) {
362
                      
363
364
365
366
367
368
369
                      integralFormat.printValue = true;
                      
                      if(it->find('/') != std::string::npos) {
                          checkModifier(it, &scalingFactor, &unit);
                          integralFormat.scalingFactor = scalingFactor;
                          integralFormat.unit = unit;
                      }
370
371
372
                  }
              }
              
373
374
              /* ...otherwise, print just the sensor values and check if they are requested in a different scaling factor or unit... */
              else {
375
                  
376
                  valueFormat.printValue = true;
377
                  
378
379
380
381
382
383
                  if(it->find('/') != std::string::npos) {
                      
                      checkModifier(it, &scalingFactor, &unit);
                      valueFormat.scalingFactor = scalingFactor;
                      valueFormat.unit = unit;
                  }
384
              }
385
              /*...remove this instance of the sensor from the list */
386
              it = sensors.erase(it);
387
          }
388
389
          else
              it++;
Axel Auweter's avatar
Axel Auweter committed
390
      }
391
      
392
      std::list<DCDB::SensorDataStoreReading> results;
393
      sensor.query(results, start, end, DCDB::AGGREGATE_NONE);
394
      genOutput(results);
395
396
      if(notifyOverflow)
          std::cout << "There has been overflow on some functions. " << std::endl;
Axel Auweter's avatar
Axel Auweter committed
397
398
399
400
401
402
403
404
405
406
  }

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

}

Axel Auweter's avatar
Axel Auweter committed
407
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
408
{
Axel Auweter's avatar
Axel Auweter committed
409
410
411
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
412
}