dcdbquery.cpp 2.16 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
24
25
26
27
28
29
30
31
32
33
34
35
36
/* 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
            << "dcdbquery [-h <hostname>] <Sensor 1> [<Sensor 2> ...] <Start> <End>" << std::endl
            << "where" << std::endl
            << "    <hostname> - the name of the DB server"
            << "    <Sensor n> - a sensor name alias" << std::endl
            << "    <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
            << "timestamps." << std::endl;
  exit(EXIT_SUCCESS);
}

int main(int argc, char* argv[])
37
{
Axel Auweter's avatar
Axel Auweter committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  /* Check command line arguments */
  if (argc <= 3) {
      usage();
  }

  /* Get the hostname */
  char ret;
  const char *host = "localhost";
  while ((ret=getopt(argc, argv, "+h:"))!=EOF) {
      switch(ret) {
          case 'h':
              host = optarg;
              break;
          default:
              usage();
              exit(EXIT_FAILURE);
      }
  }

  /* Try to create DCDBTimeStamp objects from the arguments */
  DCDBTimeStamp start, end;
  try {
      start = DCDBTimeStamp(argv[argc-2]);
      end   = DCDBTimeStamp(argv[argc-1]);
  }
  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]);
  }

  DCDBQuery::doQuery(host, sensors, start, end);
81
82
83

  return 0;
}