未验证 提交 528dcf86 编写于 作者: sangshuduo's avatar sangshuduo 提交者: GitHub

fix: improve taos shell user experience for connecting cloud (#20528)

上级 9affd11e
...@@ -28,6 +28,10 @@ ...@@ -28,6 +28,10 @@
#ifdef WEBSOCKET #ifdef WEBSOCKET
#include "taosws.h" #include "taosws.h"
#define SHELL_WS_TIMEOUT 30
#define SHELL_WS_DSN_BUFF 256
#define SHELL_WS_DSN_MASK 10
#endif #endif
#define SHELL_MAX_HISTORY_SIZE 1000 #define SHELL_MAX_HISTORY_SIZE 1000
......
...@@ -60,7 +60,7 @@ ...@@ -60,7 +60,7 @@
#ifdef WEBSOCKET #ifdef WEBSOCKET
#define SHELL_DSN "The dsn to use when connecting to cloud server." #define SHELL_DSN "The dsn to use when connecting to cloud server."
#define SHELL_REST "Use restful mode when connecting." #define SHELL_REST "Use restful mode when connecting."
#define SHELL_TIMEOUT "Set the timeout for websocket query in seconds, default is 10." #define SHELL_TIMEOUT "Set the timeout for websocket query in seconds, default is 30."
#endif #endif
static int32_t shellParseSingleOpt(int32_t key, char *arg); static int32_t shellParseSingleOpt(int32_t key, char *arg);
...@@ -246,7 +246,8 @@ int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) { ...@@ -246,7 +246,8 @@ int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) {
SShellArgs *pArgs = &shell.args; SShellArgs *pArgs = &shell.args;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 || strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "/?") == 0) { if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0
|| strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "/?") == 0) {
shellParseSingleOpt('?', NULL); shellParseSingleOpt('?', NULL);
return 0; return 0;
} }
...@@ -262,8 +263,10 @@ int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) { ...@@ -262,8 +263,10 @@ int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) {
return -1; return -1;
} }
if (key[1] == 'h' || key[1] == 'P' || key[1] == 'u' || key[1] == 'a' || key[1] == 'c' || key[1] == 's' || if (key[1] == 'h' || key[1] == 'P' || key[1] == 'u'
key[1] == 'f' || key[1] == 'd' || key[1] == 'w' || key[1] == 'n' || key[1] == 'l' || key[1] == 'N' || key[1] == 'a' || key[1] == 'c' || key[1] == 's'
|| key[1] == 'f' || key[1] == 'd' || key[1] == 'w'
|| key[1] == 'n' || key[1] == 'l' || key[1] == 'N'
#ifdef WEBSOCKET #ifdef WEBSOCKET
|| key[1] == 'E' || key[1] == 'T' || key[1] == 'E' || key[1] == 'T'
#endif #endif
...@@ -279,8 +282,10 @@ int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) { ...@@ -279,8 +282,10 @@ int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) {
} }
shellParseSingleOpt(key[1], val); shellParseSingleOpt(key[1], val);
i++; i++;
} else if (key[1] == 'p' || key[1] == 'A' || key[1] == 'C' || key[1] == 'r' || key[1] == 'k' || } else if (key[1] == 'p' || key[1] == 'A' || key[1] == 'C'
key[1] == 't' || key[1] == 'V' || key[1] == '?' || key[1] == 1 || key[1] == 'r' || key[1] == 'k'
|| key[1] == 't' || key[1] == 'V'
|| key[1] == '?' || key[1] == 1
#ifdef WEBSOCKET #ifdef WEBSOCKET
||key[1] == 'R' ||key[1] == 'R'
#endif #endif
......
...@@ -45,7 +45,7 @@ void shellCrashHandler(int signum, void *sigInfo, void *context) { ...@@ -45,7 +45,7 @@ void shellCrashHandler(int signum, void *sigInfo, void *context) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
shell.exit = false; shell.exit = false;
#ifdef WEBSOCKET #ifdef WEBSOCKET
shell.args.timeout = 10; shell.args.timeout = SHELL_WS_TIMEOUT;
shell.args.cloud = true; shell.args.cloud = true;
#endif #endif
......
...@@ -14,22 +14,51 @@ ...@@ -14,22 +14,51 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifdef WEBSOCKET #ifdef WEBSOCKET
#include "taosws.h" #include <taosws.h>
#include "shellInt.h" #include <shellInt.h>
int shell_conn_ws_server(bool first) { int shell_conn_ws_server(bool first) {
char cuttedDsn[SHELL_WS_DSN_BUFF] = {0};
int dsnLen = strlen(shell.args.dsn);
snprintf(cuttedDsn,
((dsnLen-SHELL_WS_DSN_MASK) > SHELL_WS_DSN_BUFF)?
SHELL_WS_DSN_BUFF:(dsnLen-SHELL_WS_DSN_MASK),
"%s", shell.args.dsn);
fprintf(stdout, "trying to connect %s*** ", cuttedDsn);
fflush(stdout);
for (int i = 0; i < shell.args.timeout; i++) {
shell.ws_conn = ws_connect_with_dsn(shell.args.dsn); shell.ws_conn = ws_connect_with_dsn(shell.args.dsn);
if (!shell.ws_conn) { if (NULL == shell.ws_conn) {
fprintf(stderr, "failed to connect %s, reason: %s\n", int errNo = ws_errno(NULL);
shell.args.dsn, ws_errstr(NULL)); if (0xE001 == errNo) {
fprintf(stdout, ".");
fflush(stdout);
taosMsleep(1000); // sleep 1 second then try again
continue;
} else {
fprintf(stderr, "\nfailed to connect %s***, reason: %s\n",
cuttedDsn, ws_errstr(NULL));
return -1;
}
} else {
break;
}
}
if (NULL == shell.ws_conn) {
fprintf(stdout, "\n timeout\n");
fprintf(stderr, "\nfailed to connect %s***, reason: %s\n",
cuttedDsn, ws_errstr(NULL));
return -1; return -1;
} else {
fprintf(stdout, "\n");
} }
if (first && shell.args.restful) { if (first && shell.args.restful) {
fprintf(stdout, "successfully connect to %s\n\n", fprintf(stdout, "successfully connected to %s\n\n",
shell.args.dsn); shell.args.dsn);
} else if (first && shell.args.cloud) { } else if (first && shell.args.cloud) {
fprintf(stdout, "successfully connect to cloud service\n"); fprintf(stdout, "successfully connected to cloud service\n");
} }
fflush(stdout);
return 0; return 0;
} }
...@@ -118,7 +147,8 @@ static int verticalPrintWebsocket(WS_RES* wres, double* pexecute_time) { ...@@ -118,7 +147,8 @@ static int verticalPrintWebsocket(WS_RES* wres, double* pexecute_time) {
return numOfRows; return numOfRows;
} }
static int dumpWebsocketToFile(const char* fname, WS_RES* wres, double* pexecute_time) { static int dumpWebsocketToFile(const char* fname, WS_RES* wres,
double* pexecute_time) {
char fullname[PATH_MAX] = {0}; char fullname[PATH_MAX] = {0};
if (taosExpandDir(fname, fullname, PATH_MAX) != 0) { if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
tstrncpy(fullname, fname, PATH_MAX); tstrncpy(fullname, fname, PATH_MAX);
...@@ -161,7 +191,8 @@ static int dumpWebsocketToFile(const char* fname, WS_RES* wres, double* pexecute ...@@ -161,7 +191,8 @@ static int dumpWebsocketToFile(const char* fname, WS_RES* wres, double* pexecute
taosFprintfFile(pFile, ","); taosFprintfFile(pFile, ",");
} }
const void *value = ws_get_value_in_block(wres, i, j, &ty, &len); const void *value = ws_get_value_in_block(wres, i, j, &ty, &len);
shellDumpFieldToFile(pFile, (const char*)value, fields + j, len, precision); shellDumpFieldToFile(pFile, (const char*)value,
fields + j, len, precision);
} }
taosFprintfFile(pFile, "\r\n"); taosFprintfFile(pFile, "\r\n");
} }
...@@ -171,7 +202,9 @@ static int dumpWebsocketToFile(const char* fname, WS_RES* wres, double* pexecute ...@@ -171,7 +202,9 @@ static int dumpWebsocketToFile(const char* fname, WS_RES* wres, double* pexecute
return numOfRows; return numOfRows;
} }
static int shellDumpWebsocket(WS_RES *wres, char *fname, int *error_no, bool vertical, double* pexecute_time) { static int shellDumpWebsocket(WS_RES *wres, char *fname,
int *error_no, bool vertical,
double* pexecute_time) {
int numOfRows = 0; int numOfRows = 0;
if (fname != NULL) { if (fname != NULL) {
numOfRows = dumpWebsocketToFile(fname, wres, pexecute_time); numOfRows = dumpWebsocketToFile(fname, wres, pexecute_time);
...@@ -227,13 +260,16 @@ void shellRunSingleCommandWebsocketImp(char *command) { ...@@ -227,13 +260,16 @@ void shellRunSingleCommandWebsocketImp(char *command) {
// if it's not a ws connection error // if it's not a ws connection error
if (TSDB_CODE_WS_DSN_ERROR != (code&TSDB_CODE_WS_DSN_ERROR)) { if (TSDB_CODE_WS_DSN_ERROR != (code&TSDB_CODE_WS_DSN_ERROR)) {
et = taosGetTimestampUs(); et = taosGetTimestampUs();
fprintf(stderr, "\nDB: error: %s (%.6fs)\n", ws_errstr(res), (et - st)/1E6); fprintf(stderr, "\nDB: error: %s (%.6fs)\n",
ws_errstr(res), (et - st)/1E6);
ws_free_result(res); ws_free_result(res);
return; return;
} }
if (code == TSDB_CODE_WS_SEND_TIMEOUT || code == TSDB_CODE_WS_RECV_TIMEOUT) { if (code == TSDB_CODE_WS_SEND_TIMEOUT
|| code == TSDB_CODE_WS_RECV_TIMEOUT) {
fprintf(stderr, "Hint: use -t to increase the timeout in seconds\n"); fprintf(stderr, "Hint: use -t to increase the timeout in seconds\n");
} else if (code == TSDB_CODE_WS_INTERNAL_ERRO || code == TSDB_CODE_WS_CLOSED) { } else if (code == TSDB_CODE_WS_INTERNAL_ERRO
|| code == TSDB_CODE_WS_CLOSED) {
shell.ws_conn = NULL; shell.ws_conn = NULL;
} }
ws_free_result(res); ws_free_result(res);
...@@ -252,7 +288,8 @@ void shellRunSingleCommandWebsocketImp(char *command) { ...@@ -252,7 +288,8 @@ void shellRunSingleCommandWebsocketImp(char *command) {
execute_time = ws_take_timing(res)/1E6; execute_time = ws_take_timing(res)/1E6;
} }
if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) { if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$",
REG_EXTENDED | REG_ICASE)) {
fprintf(stdout, "Database changed.\r\n\r\n"); fprintf(stdout, "Database changed.\r\n\r\n");
fflush(stdout); fflush(stdout);
ws_free_result(res); ws_free_result(res);
...@@ -266,10 +303,12 @@ void shellRunSingleCommandWebsocketImp(char *command) { ...@@ -266,10 +303,12 @@ void shellRunSingleCommandWebsocketImp(char *command) {
double total_time = (et - st)/1E3; double total_time = (et - st)/1E3;
double net_time = total_time - (double)execute_time; double net_time = total_time - (double)execute_time;
printf("Query Ok, %d of %d row(s) in database\n", numOfRows, numOfRows); printf("Query Ok, %d of %d row(s) in database\n", numOfRows, numOfRows);
printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n", execute_time, net_time, total_time); printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n",
execute_time, net_time, total_time);
} else { } else {
int error_no = 0; int error_no = 0;
numOfRows = shellDumpWebsocket(res, fname, &error_no, printMode, &execute_time); numOfRows = shellDumpWebsocket(res, fname, &error_no,
printMode, &execute_time);
if (numOfRows < 0) { if (numOfRows < 0) {
ws_free_result(res); ws_free_result(res);
return; return;
...@@ -279,11 +318,13 @@ void shellRunSingleCommandWebsocketImp(char *command) { ...@@ -279,11 +318,13 @@ void shellRunSingleCommandWebsocketImp(char *command) {
double net_time = total_time - execute_time; double net_time = total_time - execute_time;
if (error_no == 0 && !shell.stop_query) { if (error_no == 0 && !shell.stop_query) {
printf("Query OK, %d row(s) in set\n", numOfRows); printf("Query OK, %d row(s) in set\n", numOfRows);
printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n", execute_time, net_time, total_time); printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n",
execute_time, net_time, total_time);
} else { } else {
printf("Query interrupted, %d row(s) in set (%.6fs)\n", numOfRows, printf("Query interrupted, %d row(s) in set (%.6fs)\n", numOfRows,
(et - st)/1E6); (et - st)/1E6);
printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n", execute_time, net_time, total_time); printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n",
execute_time, net_time, total_time);
} }
} }
printf("\n"); printf("\n");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册