Commit 234adb03 authored by Axel Auweter's avatar Axel Auweter
Browse files

Add feature for "now"-style time formats in dcdbquery.

parent 74af8523
......@@ -5,7 +5,7 @@ OBJS = collectagent.o \
simplemqttserver.o \
simplemqttserverthread.o \
simplemqttservermessage.o
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lpthread -lcassandra -luv -lboost_system -lboost_random -lboost_thread -lboost_date_time -lssl -lcrypto
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lpthread -lcassandra -luv -lboost_system -lboost_random -lboost_thread -lboost_date_time -lboost_regex -lssl -lcrypto
TARGET = collectagent
.PHONY : clean install
......
......@@ -21,7 +21,7 @@ PUBHEADERS = $(shell find include -type f -iname "*.h")
PRIVHEADERS = $(shell find include_internal -type f -iname "*.h")
# External libraries to link against
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -lcassandra -lboost_random -lboost_system -lboost_date_time -luv
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -lcassandra -lboost_random -lboost_system -lboost_date_time -lboost_regex -luv
# Dynamic library building differs between Linux/BSD and MacOS
OS = $(shell uname)
......
......@@ -38,8 +38,9 @@ protected:
* @brief Parses a string and tries to derive the time from it by
* guessing the format. Throws DCDBTimeStampException on failure.
* @param timestr A string containing a representation of time
* @param localTime Denotes if the timestr contains local time instead of UTC
*/
void guessFromString(std::string timestr);
void guessFromString(std::string timestr, bool localTime = false);
public:
......@@ -56,7 +57,7 @@ public:
/**
* @brief String constructor. Initializes the object by best guess from a time string.
*/
DCDBTimeStamp(std::string ts);
DCDBTimeStamp(std::string ts, bool localTime = false);
/**
* @brief Time_t constructor. Initializes the object from a time_t struct.
......
......@@ -11,6 +11,7 @@
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/local_time/local_time.hpp"
#include "boost/date_time/c_local_time_adjustor.hpp"
#include "boost/regex.hpp"
#include "timestamp.h"
......@@ -22,9 +23,10 @@ typedef boost::date_time::c_local_adjustor<boost::posix_time::ptime> local_adj;
* time information. Currently, it detects strings in the format "yyyy-mm-dd hh:mm:ss.000"
* and posix time.
*/
void DCDBTimeStamp::guessFromString(std::string timestr)
void DCDBTimeStamp::guessFromString(std::string timestr, bool localTime)
{
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
uint64_t tmp;
/* First try to match it against a time string */
try {
......@@ -32,6 +34,9 @@ void DCDBTimeStamp::guessFromString(std::string timestr)
if (ts != boost::posix_time::not_a_date_time) {
boost::posix_time::time_duration diff = ts - epoch;
raw = diff.total_nanoseconds();
if (localTime) {
convertFromLocal();
}
return;
}
}
......@@ -39,10 +44,60 @@ void DCDBTimeStamp::guessFromString(std::string timestr)
/* Ignore on error */
}
/*
* Try to match it against a string containing "now".
* Note that we ignore the localTime flag in this case since
* we already get "now" in UTC.
*/
if (timestr.find("now") != std::string::npos) {
/* If the string is just "now"... */
if ((timestr.compare("now") == 0) && (timestr.length() == 3)) {
setNow();
return;
}
/* If the string is of the form "now-X" */
boost::regex d("now-[0-9]*d", boost::regex::extended);
boost::regex h("now-[0-9]*h", boost::regex::extended);
boost::regex m("now-[0-9]*m", boost::regex::extended);
boost::regex s("now-[0-9]*s", boost::regex::extended);
if (boost::regex_match(timestr, d)) {
sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
setNow();
raw -= tmp * 24 * 60 * 60 * 1000 * 1000 * 1000;
return;
}
if (boost::regex_match(timestr, h)) {
sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
setNow();
raw -= tmp * 60 * 60 * 1000 * 1000 * 1000;
return;
}
if (boost::regex_match(timestr, m)) {
sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
setNow();
raw -= tmp * 1000 * 1000 * 1000;
return;
}
if (boost::regex_match(timestr, s)) {
sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
setNow();
raw -= tmp * 1000 * 1000 * 1000;
return;
}
/* "now" keyword is in the timestamp but does not match one of the predefined formats */
throw DCDBTimeStampConversionException();
}
/* Try to match it against a POSIX time */
uint64_t tmp;
if (sscanf(timestr.c_str(), "%" PRIu64, &tmp) == 1) {
raw = tmp * 1000 * 1000 * 1000;
if (localTime) {
convertFromLocal();
}
return;
}
......@@ -69,9 +124,9 @@ DCDBTimeStamp::DCDBTimeStamp(uint64_t ts)
/**
* This constructor sets the time using the magic implemented in guessFromString.
*/
DCDBTimeStamp::DCDBTimeStamp(std::string ts)
DCDBTimeStamp::DCDBTimeStamp(std::string ts, bool localTime)
{
guessFromString(ts);
guessFromString(ts, localTime);
}
/**
......
......@@ -2,7 +2,7 @@ include ../../config.mk
CXXFLAGS = -O2 -ggdb --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/
OBJS = dcdbconfig.o sensoraction.o useraction.o
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lcassandra -luv -lboost_random -lboost_system -lboost_date_time -lssl -lcrypto
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lcassandra -luv -lboost_random -lboost_system -lboost_date_time -lboost_regex -lssl -lcrypto
# GCC 4.8 is broken
ifeq ($(findstring 4.8, $(shell $(CXX) --version)), 4.8)
SLIBS = $(DCDBDEPLOYPATH)/lib/libboost_random.a $(DCDBDEPLOYPATH)/lib/libboost_system.a
......
......@@ -2,7 +2,7 @@ include ../../config.mk
CXXFLAGS = -O2 -ggdb --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
OBJS = dcdbquery.o query.o
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lcassandra -luv -lboost_random -lboost_system -lboost_date_time -lssl -lcrypto
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lcassandra -luv -lboost_random -lboost_system -lboost_date_time -lboost_regex -lssl -lcrypto
TARGET = dcdbquery
.PHONY : clean install
......
......@@ -71,8 +71,9 @@ int main(int argc, char* argv[])
/* Try to create DCDBTimeStamp objects from the arguments */
DCDBTimeStamp start, end;
try {
start = DCDBTimeStamp(argv[argc-2]);
end = DCDBTimeStamp(argv[argc-1]);
bool local = myQuery->getLocalTimeEnabled();
start = DCDBTimeStamp(argv[argc-2], local);
end = DCDBTimeStamp(argv[argc-1], local);
}
catch (std::exception& e) {
std::cout << "Wrong time format." << std::endl;
......
......@@ -36,12 +36,6 @@ bool DCDBQuery::getRawOutputEnabled() {
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDBTimeStamp start, DCDBTimeStamp end)
{
/* Convert start and end to UTC if they're specified in local time. */
if (useLocalTime) {
start.convertFromLocal();
end.convertFromLocal();
}
/* Create a new connection to the database */
DCDBConnection* connection;
connection = new DCDBConnection();
......
......@@ -2,7 +2,7 @@ include ../../config.mk
CXXFLAGS = -O2 -ggdb --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
OBJS = dcdbunitconv.o
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lcassandra -luv -lboost_random -lboost_system -lboost_date_time -lssl -lcrypto
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -ldcdb -lcassandra -luv -lboost_random -lboost_system -lboost_date_time -lboost_regex -lssl -lcrypto
TARGET = dcdbunitconv
.PHONY : clean install
......
......@@ -215,7 +215,7 @@ $(DCDBDEPSPATH)/.prerequesites: $(DCDBDEPSPATH)/.extract-distfiles
echo " using gcc : arm : $(CROSS_COMPILE)g++ ; " > $(DCDBDEPSPATH)/$(B)/tools/build/src/user-config.jam; \
fi; \
cd $(DCDBDEPSPATH)/$(B) && ./bootstrap.sh --prefix=$(DCDBDEPLOYPATH) \
--with-libraries=atomic,chrono,date_time,exception,filesystem,program_options,random,system,thread,timer; \
--with-libraries=atomic,chrono,date_time,exception,filesystem,program_options,random,regex,system,thread,timer; \
cd $(DCDBDEPSPATH)/$(B) && \
./b2 -j $(MAKETHREADS) cxxflags="$(CXX11FLAGS)" install && touch $(DCDBDEPSPATH)/$(B)/.installed; \
else \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment