dcdbquery.cpp 2.93 KB
Newer Older
1
2
3
4
5
6
7
/*
 * dcdbquery.cpp
 *
 *  Created on: Jan 06, 2015
 *      Author: Axel Auweter
 */

Axel Auweter's avatar
Axel Auweter committed
8
/* C++ standard headers */
9
#include <iostream>
Axel Auweter's avatar
Axel Auweter committed
10
#include <stdexcept>
11

Axel Auweter's avatar
Axel Auweter committed
12
13
14
15
16
17
18
19
20
21
22
23
/* C standard headers */
#include <cstdlib>
#include <unistd.h>

/* Custom headers */
#include "dcdb/timestamp.h"
#include "query.h"

void usage(void)
{
  /*            0---------1---------2---------3---------4---------5---------6---------7--------- */
  std::cout << "Usage:" << std::endl
24
            << "dcdbquery [-h <hostname>] [-r] [-l] <Sensor 1> [<Sensor 2> ...] <Start> <End>" << std::endl
Axel Auweter's avatar
Axel Auweter committed
25
            << "where" << std::endl
26
            << "    <hostname> - the name of the DB server" << std::endl
27
            << "    <Sensor n> - a public sensor name" << std::endl
Axel Auweter's avatar
Axel Auweter committed
28
29
30
31
            << "    <Start>    - start of time series" << std::endl
            << "    <End>      - end of time series" << std::endl
            << "Return the readings for the sensors in the interval from <Start> to <End>." << std::endl
            << "<Start> and <End> times should be supplied as 'yyyy-mm-dd hh:mm:ss' or unix" << std::endl
32
33
            << "timestamps." << std::endl
            << "When the -r option is specified, timestamps will be reported in internal (raw)" << std::endl
34
35
36
            << "format (nanoseconds since epoch) insted of ISO human readable form." << std::endl
            << "When the -l option is specified, times for <Start>, <End>, and in the generated" << std::endl
            << "output will be interpreted / printed in the local time zone." << std::endl;
Axel Auweter's avatar
Axel Auweter committed
37
38
39
40
  exit(EXIT_SUCCESS);
}

int main(int argc, char* argv[])
41
{
Axel Auweter's avatar
Axel Auweter committed
42
43
44
45
46
  /* Check command line arguments */
  if (argc <= 3) {
      usage();
  }

Axel Auweter's avatar
Axel Auweter committed
47
48
49
50
  /* Allocate our lovely helper class */
  DCDBQuery* myQuery;
  myQuery = new DCDBQuery();

51
  /* Get the options */
52
  int ret;
Axel Auweter's avatar
Axel Auweter committed
53
  const char *host = "localhost";
54
  while ((ret=getopt(argc, argv, "+h:rl"))!=-1) {
Axel Auweter's avatar
Axel Auweter committed
55
56
57
58
      switch(ret) {
          case 'h':
              host = optarg;
              break;
59
          case 'r':
Axel Auweter's avatar
Axel Auweter committed
60
              myQuery->setRawOutputEnabled(true);
61
              break;
62
          case 'l':
Axel Auweter's avatar
Axel Auweter committed
63
              myQuery->setLocalTimeEnabled(true);
64
              break;
Axel Auweter's avatar
Axel Auweter committed
65
66
67
68
69
70
71
72
73
          default:
              usage();
              exit(EXIT_FAILURE);
      }
  }

  /* Try to create DCDBTimeStamp objects from the arguments */
  DCDBTimeStamp start, end;
  try {
74
75
76
      bool local = myQuery->getLocalTimeEnabled();
      start = DCDBTimeStamp(argv[argc-2], local);
      end   = DCDBTimeStamp(argv[argc-1], local);
Axel Auweter's avatar
Axel Auweter committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  }
  catch (std::exception& e) {
      std::cout << "Wrong time format." << std::endl;
      exit(EXIT_FAILURE);
  }

  /* Ensure start < end */
  if(start >= end) {
      std::cout << "Start time must be earlier than end time." << std::endl;
      exit(EXIT_FAILURE);
  }

  /* Build a list of sensornames */
  std::list<std::string> sensors;
  for (int arg = optind; arg < argc-2; arg++) {
      sensors.push_back(argv[arg]);
  }

Axel Auweter's avatar
Axel Auweter committed
95
96
97
  myQuery->doQuery(host, sensors, start, end);

  delete myQuery;
98
99
100

  return 0;
}