shellWebsocket.c 8.1 KB
Newer Older
Y
Yang Zhao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#ifdef WEBSOCKET
#include "taosws.h"
#include "shellInt.h"

int shell_conn_ws_server(bool first) {
	shell.ws_conn = ws_connect_with_dsn(shell.args.dsn);
	if (!shell.ws_conn) {
		fprintf(stderr, "failed to connect %s, reason: %s\n",
				shell.args.dsn, ws_errstr(NULL));
		return -1;
	}
	if (first && shell.args.restful) {
		fprintf(stdout, "successfully connect to %s\n\n",
				shell.args.dsn);
	} else if (first && shell.args.cloud) {
		fprintf(stdout, "successfully connect to cloud service\n");
	}
	return 0;
}

36
static int horizontalPrintWebsocket(WS_RES* wres, double* execute_time) {
Y
Yang Zhao 已提交
37 38 39
  const void* data = NULL;
  int rows;
  ws_fetch_block(wres, &data, &rows);
40
  *execute_time += (double)(ws_take_timing(wres)/1E6);
Y
Yang Zhao 已提交
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
  if (!rows) {
	return 0;
  }
  int num_fields = ws_field_count(wres);
  TAOS_FIELD* fields = (TAOS_FIELD*)ws_fetch_fields(wres);
  int precision = ws_result_precision(wres);

  int width[TSDB_MAX_COLUMNS];
  for (int col = 0; col < num_fields; col++) {
    width[col] = shellCalcColWidth(fields + col, precision);
  }

  shellPrintHeader(fields, width, num_fields);

  int numOfRows = 0;
  do {
    uint8_t ty;
    uint32_t len;
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < num_fields; j++) {
        putchar(' ');
        const void *value = ws_get_value_in_block(wres, i, j, &ty, &len);
        shellPrintField((const char*)value, fields+j, width[j], len, precision);
        putchar(' ');
        putchar('|');
      }
	  putchar('\r');
      putchar('\n');
    }
    numOfRows += rows;
    ws_fetch_block(wres, &data, &rows);
  } while (rows && !shell.stop_query);
  return numOfRows;
}

76
static int verticalPrintWebsocket(WS_RES* wres, double* pexecute_time) {
Y
Yang Zhao 已提交
77 78 79
  int rows = 0;
  const void* data = NULL;
  ws_fetch_block(wres, &data, &rows);
80
  *pexecute_time += (double)(ws_take_timing(wres)/1E6);
Y
Yang Zhao 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  if (!rows) {
	return 0;
  }
  int num_fields = ws_field_count(wres);
  TAOS_FIELD* fields = (TAOS_FIELD*)ws_fetch_fields(wres);
  int precision = ws_result_precision(wres);

  int maxColNameLen = 0;
  for (int col = 0; col < num_fields; col++) {
    int len = (int)strlen(fields[col].name);
    if (len > maxColNameLen) {
      maxColNameLen = len;
    }
  }
  int numOfRows = 0;
  do {
    uint8_t ty;
    uint32_t len;
    for (int i = 0; i < rows; i++) {
      printf("*************************** %d.row ***************************\n",
			  numOfRows + 1);
      for (int j = 0; j < num_fields; j++) {
        TAOS_FIELD* field = fields + j;
        int padding = (int)(maxColNameLen - strlen(field->name));
        printf("%*.s%s: ", padding, " ", field->name);
        const void *value = ws_get_value_in_block(wres, i, j, &ty, &len);
        shellPrintField((const char*)value, field, 0, len, precision);
        putchar('\n');
      }
      numOfRows++;
    }
    ws_fetch_block(wres, &data, &rows);
  } while (rows && !shell.stop_query);
  return numOfRows;
}

117
static int dumpWebsocketToFile(const char* fname, WS_RES* wres, double* pexecute_time) {
Y
Yang Zhao 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
  char fullname[PATH_MAX] = {0};
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
    tstrncpy(fullname, fname, PATH_MAX);
  }

  TdFilePtr pFile = taosOpenFile(fullname,
		  TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
  if (pFile == NULL) {
    fprintf(stderr, "failed to open file: %s\r\n", fullname);
    return -1;
  }
  int rows = 0;
  const void* data = NULL;
  ws_fetch_block(wres, &data, &rows);
132
  *pexecute_time += (double)(ws_take_timing(wres)/1E6);
Y
Yang Zhao 已提交
133 134
  if (!rows) {
    taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
135
	  return 0;
Y
Yang Zhao 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  }
  int numOfRows = 0;
  TAOS_FIELD* fields = (TAOS_FIELD*)ws_fetch_fields(wres);
  int num_fields = ws_field_count(wres);
  int precision = ws_result_precision(wres);
  for (int col = 0; col < num_fields; col++) {
    if (col > 0) {
      taosFprintfFile(pFile, ",");
    }
    taosFprintfFile(pFile, "%s", fields[col].name);
  }
  taosFprintfFile(pFile, "\r\n"); 
  do {
    uint8_t ty;
    uint32_t len;
    numOfRows += rows;
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < num_fields; j++) {
        if (j > 0) {
          taosFprintfFile(pFile, ",");
        }
        const void *value = ws_get_value_in_block(wres, i, j, &ty, &len);
wafwerar's avatar
wafwerar 已提交
158
        shellDumpFieldToFile(pFile, (const char*)value, fields + j, len, precision);
Y
Yang Zhao 已提交
159 160 161 162 163 164 165 166 167
      }
      taosFprintfFile(pFile, "\r\n");
    }
    ws_fetch_block(wres, &data, &rows);
  } while (rows && !shell.stop_query);
  taosCloseFile(&pFile);
  return numOfRows;
}

168
static int shellDumpWebsocket(WS_RES *wres, char *fname, int *error_no, bool vertical, double* pexecute_time) {
Y
Yang Zhao 已提交
169 170
  int numOfRows = 0;
  if (fname != NULL) {
171
    numOfRows = dumpWebsocketToFile(fname, wres, pexecute_time);
Y
Yang Zhao 已提交
172
  } else if (vertical) {
173
    numOfRows = verticalPrintWebsocket(wres, pexecute_time);
Y
Yang Zhao 已提交
174
  } else {
175
    numOfRows = horizontalPrintWebsocket(wres, pexecute_time);
Y
Yang Zhao 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  }
  *error_no = ws_errno(wres);
  return numOfRows;
}

void shellRunSingleCommandWebsocketImp(char *command) {
  int64_t st, et;
  char   *sptr = NULL;
  char   *cptr = NULL;
  char   *fname = NULL;
  bool    printMode = false;

  if ((sptr = strstr(command, ">>")) != NULL) {
    cptr = strstr(command, ";");
    if (cptr != NULL) {
      *cptr = '\0';
    }

    fname = sptr + 2;
    while (*fname == ' ') fname++;
    *sptr = '\0';
  }

  if ((sptr = strstr(command, "\\G")) != NULL) {
    cptr = strstr(command, ";");
    if (cptr != NULL) {
      *cptr = '\0';
    }

    *sptr = '\0';
    printMode = true;  // When output to a file, the switch does not work.
  }

  if (!shell.ws_conn && shell_conn_ws_server(0)) {
	return;
  }

  shell.stop_query = false;
  st = taosGetTimestampUs();

  WS_RES* res = ws_query_timeout(shell.ws_conn, command, shell.args.timeout);
  int code = ws_errno(res);
  if (code != 0) {
	et = taosGetTimestampUs();
	fprintf(stderr, "\nDB: error: %s (%.6fs)\n", ws_errstr(res), (et - st)/1E6);
	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");
	} else if (code == TSDB_CODE_WS_INTERNAL_ERRO || code == TSDB_CODE_WS_CLOSED) {
		fprintf(stderr, "TDengine server is down, will try to reconnect\n");
		shell.ws_conn = NULL;
	}
	ws_free_result(res);
	return;
  }

231 232
  double execute_time = ws_take_timing(res)/1E6;

Y
Yang Zhao 已提交
233 234 235
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
    fprintf(stdout, "Database changed.\r\n\r\n");
    fflush(stdout);
wafwerar's avatar
wafwerar 已提交
236
	  ws_free_result(res);
Y
Yang Zhao 已提交
237 238 239 240 241
    return;
  }

  int numOfRows = 0;
  if (ws_is_update_query(res)) {
wafwerar's avatar
wafwerar 已提交
242 243
	  numOfRows = ws_affected_rows(res);
	  et = taosGetTimestampUs();
244 245
    double total_time = (et - st)/1E3;
    double net_time = total_time - (double)execute_time;
wafwerar's avatar
wafwerar 已提交
246
	  printf("Query Ok, %d of %d row(s) in database\n", numOfRows, numOfRows);
247
    printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n", execute_time, net_time, total_time);
Y
Yang Zhao 已提交
248 249
  } else {
	int error_no = 0;
250
	numOfRows  = shellDumpWebsocket(res, fname, &error_no, printMode, &execute_time);
Y
Yang Zhao 已提交
251 252 253 254 255
	if (numOfRows < 0) {
		ws_free_result(res);
		return;
	}
	et = taosGetTimestampUs();
wafwerar's avatar
wafwerar 已提交
256 257
  double total_time = (et - st) / 1E3;
  double net_time = total_time - execute_time;
Y
Yang Zhao 已提交
258
	if (error_no == 0 && !shell.stop_query) {
259
		printf("Query OK, %d row(s) in set\n", numOfRows);
wafwerar's avatar
wafwerar 已提交
260
    printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n", execute_time, net_time, total_time);
Y
Yang Zhao 已提交
261 262 263
	} else {
		printf("Query interrupted, %d row(s) in set (%.6fs)\n", numOfRows,
				(et - st)/1E6);
wafwerar's avatar
wafwerar 已提交
264
    printf("Execute: %.2f ms Network: %.2f ms Total: %.2f ms\n", execute_time, net_time, total_time);
Y
Yang Zhao 已提交
265 266 267 268 269 270
	}
  }
  printf("\n");
  ws_free_result(res);
}
#endif