query.cpp 14.9 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
        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 */
120
121
	    if ((DCDB::UnitConv::fromString(valueFormat.unit) != DCDB::Unit_None) && (DCDB::UnitConv::fromString(valueFormat.unit) != baseUnit)) {
                if (!DCDB::UnitConv::convert(result, baseUnit, DCDB::UnitConv::fromString(valueFormat.unit)))
122
123
124
125
                    std::cerr << "Warning, cannot convert units ("
                    << DCDB::UnitConv::toString(baseUnit) << " -> "
                    << valueFormat.unit << ")"
                    << std::endl;
126
            }
127
128

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

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

253

254
void DCDBQuery::checkModifier(std::list<std::string>::iterator it, struct outputFormat *format) {
255
256
257
258
259
260
261
262
    
    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))
        
263
        sscanf(modifierStr.c_str(), "%lf", &(format->scalingFactor));
264
265
   
    else
266
        format->unit = modifierStr;
267
268
}

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

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

  /* Iterate over list of sensors requested by the user */
  for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
284
      scalingFactor = 1;
285
      unit = "none";
286
      notifyOverflow = false;
287
288
289
290
291
292
293
294
      
      /* 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
295
      std::string modifierStr;
296
      boost::smatch match;
297
      baseUnit = DCDB::Unit_None;
298
      
299
300
      /* Retrieve sensor object first */
      boost::regex functExp("^delta|^delta_t|^derivative|^integral", boost::regex::extended);
301

302
303
304
305
      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('/'));
306
307
308
309
310
      else
	  sensorName = *it;
    
      DCDB::PublicSensor sen;
      sensorConfig.getPublicSensorByName(sen, sensorName.c_str());
311
      
312
313
314
315
316
      /* Base scaling factor and unit of the public sensor */
      baseUnit = DCDB::UnitConv::fromString(sen.unit);
      baseScalingFactor = sen.scaling_factor;

      DCDB::Sensor sensor(connection, sen);
317
318
      
      /* Iterate over the list to detect all instances of the sensor with name "sensorName" */
319
320
      while(it != sensors.end()) {
          if(it->find(sensorName) != std::string::npos) {
321
322
323
324
325
326
327
              /* Check first if the sensor we're looking is part of a function ...*/
              if(boost::regex_search(*it, match, functExp)) {
                  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) {
328
                          checkModifier(it, &deltaFormat);
329
330
331
332
333
334
                      }
                  }
                  else if(match[0].compare("delta_t")==0) {
                      deltaTFormat.printValue = true;
                      
                      if(it->find('/') != std::string::npos) {
335
                          checkModifier(it, &deltaTFormat);
336
337
338
339
340
341
                      }
                  }
                  else if(match[0].compare("derivative")==0) {
                      derivativeFormat.printValue = true;
                      
                      if(it->find('/') != std::string::npos) {
342
                          checkModifier(it, &derivativeFormat);
343
                      }
344
                  }
345
346
347
348
                  else if(match[0].compare("integral")==0) {
                      integralFormat.printValue = true;
                      
                      if(it->find('/') != std::string::npos) {
349
                          checkModifier(it, &integralFormat);
350
                      }
351
352
353
                  }
              }
              
354
355
356
              /* ...otherwise, print just the sensor values and check if they are requested in a different scaling factor or unit... */
              else {
                  valueFormat.printValue = true;
357
                  
358
                  if(it->find('/') != std::string::npos) {
359
                      checkModifier(it, &valueFormat);
360
                  }
361
              }
362
              /*...remove this instance of the sensor from the list */
363
              it = sensors.erase(it);
364
          }
365
366
          else
              it++;
Axel Auweter's avatar
Axel Auweter committed
367
      }
368
      
369
      std::list<DCDB::SensorDataStoreReading> results;
370
      sensor.query(results, start, end, DCDB::AGGREGATE_NONE);
371
      genOutput(results);
372
      if(notifyOverflow)
373
          std::cout << "Overflow detected." << std::endl;
Axel Auweter's avatar
Axel Auweter committed
374
375
376
377
378
379
380
381
382
383
  }

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

}

Axel Auweter's avatar
Axel Auweter committed
384
DCDBQuery::DCDBQuery()
Axel Auweter's avatar
Axel Auweter committed
385
{
Axel Auweter's avatar
Axel Auweter committed
386
387
388
  connection = nullptr;
  useLocalTime = false;
  useRawOutput = false;
Axel Auweter's avatar
Axel Auweter committed
389
}