Skip to content

Commit

Permalink
fix: taosdump start-time end-time cmdline argument parsing (#593)
Browse files Browse the repository at this point in the history
* fix: taosdump start-time end-time cmdline argument parsing

* test: add test case to test start-time and end-time

* fix: time argument parse logic

* fix: refactor time convert func
  • Loading branch information
sangshuduo committed Feb 28, 2023
1 parent ca187f3 commit 0111c66
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/taosdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,11 @@ static void parse_args(
}

static void copyHumanTimeToArg(char *timeStr, bool isStartTime) {
if (0 == strncmp(timeStr, "--start-time=", strlen("--start-time="))) {
timeStr += strlen("--start-time=");
} else if (0 == strncmp(timeStr, "--end-time=", strlen("--end-time="))) {
timeStr += strlen("--end-time=");
}
if (isStartTime) {
tstrncpy(g_args.humanStartTime, timeStr, HUMAN_TIME_LEN);
} else {
Expand All @@ -1099,34 +1104,49 @@ static void copyTimestampToArg(char *timeStr, bool isStartTime) {
}
}

static void parseTimestampConvert(char *input, bool isStartTime) {
if (NULL == input) {
errorPrint("%s", "input timestamp need a valid value!\n");
exit(-1);
}

char *tmp = strdup(input);
if (strchr(tmp, ':') && strchr(tmp, '-')) {
copyHumanTimeToArg(tmp, isStartTime);
} else {
copyTimestampToArg(tmp, isStartTime);
}
free(tmp);
}

static void parse_timestamp(
int argc, char *argv[], SArguments *arguments) {
for (int i = 1; i < argc; i++) {
char *tmp;
bool isStartTime = false;
bool isEndTime = false;

if (strcmp(argv[i], "-S") == 0) {
isStartTime = true;
} else if (strcmp(argv[i], "-E") == 0) {
isEndTime = true;
}

if (isStartTime || isEndTime) {
if (NULL == argv[i+1]) {
errorPrint("%s need a valid value following!\n", argv[i]);
exit(-1);
}
tmp = strdup(argv[i+1]);

if (strchr(tmp, ':') && strchr(tmp, '-')) {
copyHumanTimeToArg(tmp, isStartTime);
if ((0 == strcmp(argv[i], "-S")
|| (0 == strncmp(
argv[i], "--start-time=",
strlen("--start-time="))))) {
if (0 == strcmp(argv[i], "-S")) {
tmp = argv[i+1];
} else {
tmp = argv[i];
}
parseTimestampConvert(tmp, true);
} else if ((0 == strcmp(argv[i], "-E")
|| (0 == strncmp(
argv[i],
"--end-time=",
strlen("--end-time="))))) {
if (0 == strcmp(argv[i], "-E")) {
tmp = argv[i+1];
} else {
copyTimestampToArg(tmp, isStartTime);
tmp = argv[i];
}

free(tmp);
parseTimestampConvert(tmp, false);
}

}
}

Expand Down Expand Up @@ -12773,7 +12793,8 @@ int inspectAvroFile(char *filename) {

debugPrint("array_size is %zu\n", array_size);
uint32_t array_u32 = 0;
for (size_t item = 0; item < array_size; item++) {
for (size_t item = 0; item < array_size;
item++) {
avro_value_t item_value;
avro_value_get_by_index(&field_value, item,
&item_value, NULL);
Expand Down
145 changes: 145 additions & 0 deletions tests/taosdump/native/taosdumpStartEndTime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
###################################################################
# Copyright (c) 2016 by TAOS Technologies, Inc.
# All rights reserved.
#
# This file is proprietary and confidential to TAOS Technologies.
# No part of this file may be reproduced, stored, transmitted,
# disclosed or used in any form or by any means other than as
# expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import os
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *


class TDTestCase:
def caseDescription(self):
"""
case1<sdsang>: [TS-2769] taosdump start-time end-time test
"""

def init(self, conn, logSql):
tdLog.debug("start to execute %s" % __file__)
tdSql.init(conn.cursor(), logSql)
self.tmpdir = "tmp"

def getPath(self, tool="taosdump"):
selfPath = os.path.dirname(os.path.realpath(__file__))

if "community" in selfPath:
projPath = selfPath[: selfPath.find("community")]
elif "src" in selfPath:
projPath = selfPath[: selfPath.find("src")]
elif "/tools/" in selfPath:
projPath = selfPath[: selfPath.find("/tools/")]
elif "/tests/" in selfPath:
projPath = selfPath[: selfPath.find("/tests/")]
else:
tdLog.info("cannot found %s in path: %s, use system's" % (tool, selfPath))
projPath = "/usr/local/taos/bin/"

paths = []
for root, dummy, files in os.walk(projPath):
if (tool) in files:
rootRealPath = os.path.dirname(os.path.realpath(root))
if "packaging" not in rootRealPath:
paths.append(os.path.join(root, tool))
break
if len(paths) == 0:
return ""
return paths[0]

def run(self):
binPath = self.getPath()
if binPath == "":
tdLog.exit("taosdump not found!")
else:
tdLog.info("taosdump found in %s" % binPath)

tdSql.prepare()

tdSql.execute("drop database if exists db")
tdSql.execute("create database db keep 3649 ")

tdSql.execute("use db")
tdSql.execute(
"create table t1 (ts timestamp, n int)"
)
tdSql.execute(
"create table t2 (ts timestamp, n int)"
)
tdSql.execute(
"create table t3 (ts timestamp, n int)"
)
tdSql.execute(
"insert into t1 values('2023-02-28 12:00:00.997',11)('2023-02-28 12:00:01.997',12)('2023-02-28 12:00:02.997',15)('2023-02-28 12:00:03.997',16)('2023-02-28 12:00:04.997',17)"
)
tdSql.execute(
"insert into t2 values('2023-02-28 12:00:00.997',11)('2023-02-28 12:00:01.997',12)('2023-02-28 12:00:02.997',15)('2023-02-28 12:00:03.997',16)('2023-02-28 12:00:04.997',17)"
)
tdSql.execute(
"insert into t3 values('2023-02-28 12:00:00.997',11)('2023-02-28 12:00:01.997',12)('2023-02-28 12:00:02.997',15)('2023-02-28 12:00:03.997',16)('2023-02-28 12:00:04.997',17)"
)

# sys.exit(1)

if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
else:
print("directory exists")
os.system("rm -rf %s" % self.tmpdir)
os.makedirs(self.tmpdir)

os.system("%s db t1 -o %s -T 1 -S 2023-02-28T12:00:01.997+0800 -E 2023-02-28T12:00:03.997+0800 " % (binPath, self.tmpdir))

tdSql.execute("drop table t1")

os.system("%s -i %s -T 1" % (binPath, self.tmpdir))

tdSql.query("select count(*) from db.t1")
tdSql.checkData(0, 0, 3)

if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
else:
print("directory exists")
os.system("rm -rf %s" % self.tmpdir)
os.makedirs(self.tmpdir)

os.system("%s db t2 -o %s -T 1 -S 2023-02-28T12:00:01.997+0800 " % (binPath, self.tmpdir))

tdSql.execute("drop table t2")
os.system("%s -i %s -T 1" % (binPath, self.tmpdir))

tdSql.query("select count(*) from db.t2")
tdSql.checkData(0, 0, 4)

if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
else:
print("directory exists")
os.system("rm -rf %s" % self.tmpdir)
os.makedirs(self.tmpdir)

os.system("%s db t3 -o %s -T 1 -E 2023-02-28T12:00:03.997+0800 " % (binPath, self.tmpdir))

tdSql.execute("drop table t3")

os.system("%s -i %s -T 1" % (binPath, self.tmpdir))

tdSql.query("select count(*) from db.t3")
tdSql.checkData(0, 0, 4)

def stop(self):
tdSql.close()
tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())
145 changes: 145 additions & 0 deletions tests/taosdump/native/taosdumpStartEndTimeLong.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
###################################################################
# Copyright (c) 2016 by TAOS Technologies, Inc.
# All rights reserved.
#
# This file is proprietary and confidential to TAOS Technologies.
# No part of this file may be reproduced, stored, transmitted,
# disclosed or used in any form or by any means other than as
# expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import os
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *


class TDTestCase:
def caseDescription(self):
"""
case1<sdsang>: [TS-2769] taosdump start-time end-time test
"""

def init(self, conn, logSql):
tdLog.debug("start to execute %s" % __file__)
tdSql.init(conn.cursor(), logSql)
self.tmpdir = "tmp"

def getPath(self, tool="taosdump"):
selfPath = os.path.dirname(os.path.realpath(__file__))

if "community" in selfPath:
projPath = selfPath[: selfPath.find("community")]
elif "src" in selfPath:
projPath = selfPath[: selfPath.find("src")]
elif "/tools/" in selfPath:
projPath = selfPath[: selfPath.find("/tools/")]
elif "/tests/" in selfPath:
projPath = selfPath[: selfPath.find("/tests/")]
else:
tdLog.info("cannot found %s in path: %s, use system's" % (tool, selfPath))
projPath = "/usr/local/taos/bin/"

paths = []
for root, dummy, files in os.walk(projPath):
if (tool) in files:
rootRealPath = os.path.dirname(os.path.realpath(root))
if "packaging" not in rootRealPath:
paths.append(os.path.join(root, tool))
break
if len(paths) == 0:
return ""
return paths[0]

def run(self):
binPath = self.getPath()
if binPath == "":
tdLog.exit("taosdump not found!")
else:
tdLog.info("taosdump found in %s" % binPath)

tdSql.prepare()

tdSql.execute("drop database if exists db")
tdSql.execute("create database db keep 3649 ")

tdSql.execute("use db")
tdSql.execute(
"create table t1 (ts timestamp, n int)"
)
tdSql.execute(
"create table t2 (ts timestamp, n int)"
)
tdSql.execute(
"create table t3 (ts timestamp, n int)"
)
tdSql.execute(
"insert into t1 values('2023-02-28 12:00:00.997',11)('2023-02-28 12:00:01.997',12)('2023-02-28 12:00:02.997',15)('2023-02-28 12:00:03.997',16)('2023-02-28 12:00:04.997',17)"
)
tdSql.execute(
"insert into t2 values('2023-02-28 12:00:00.997',11)('2023-02-28 12:00:01.997',12)('2023-02-28 12:00:02.997',15)('2023-02-28 12:00:03.997',16)('2023-02-28 12:00:04.997',17)"
)
tdSql.execute(
"insert into t3 values('2023-02-28 12:00:00.997',11)('2023-02-28 12:00:01.997',12)('2023-02-28 12:00:02.997',15)('2023-02-28 12:00:03.997',16)('2023-02-28 12:00:04.997',17)"
)

# sys.exit(1)

if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
else:
print("directory exists")
os.system("rm -rf %s" % self.tmpdir)
os.makedirs(self.tmpdir)

os.system("%s db t1 -o %s -T 1 --start-time=2023-02-28T12:00:01.997+0800 --end-time=2023-02-28T12:00:03.997+0800 " % (binPath, self.tmpdir))

tdSql.execute("drop table t1")

os.system("%s -i %s -T 1" % (binPath, self.tmpdir))

tdSql.query("select count(*) from db.t1")
tdSql.checkData(0, 0, 3)

if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
else:
print("directory exists")
os.system("rm -rf %s" % self.tmpdir)
os.makedirs(self.tmpdir)

os.system("%s db t2 -o %s -T 1 --start-time=2023-02-28T12:00:01.997+0800 " % (binPath, self.tmpdir))

tdSql.execute("drop table t2")
os.system("%s -i %s -T 1" % (binPath, self.tmpdir))

tdSql.query("select count(*) from db.t2")
tdSql.checkData(0, 0, 4)

if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
else:
print("directory exists")
os.system("rm -rf %s" % self.tmpdir)
os.makedirs(self.tmpdir)

os.system("%s db t3 -o %s -T 1 --end-time=2023-02-28T12:00:03.997+0800 " % (binPath, self.tmpdir))

tdSql.execute("drop table t3")

os.system("%s -i %s -T 1" % (binPath, self.tmpdir))

tdSql.query("select count(*) from db.t3")
tdSql.checkData(0, 0, 4)

def stop(self):
tdSql.close()
tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())

0 comments on commit 0111c66

Please sign in to comment.