You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ge...@apache.org on 2020/02/06 15:37:23 UTC
[incubator-iotdb] 05/05: fix test.
This is an automated email from the ASF dual-hosted git repository.
geniuspig pushed a commit to branch http
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git
commit e4994088728ed5e8133f2cf819d22f7382df4df9
Author: zhutianci <zh...@gmail.com>
AuthorDate: Thu Feb 6 23:36:55 2020 +0800
fix test.
---
.../iotdb/db/rest/controller/RestController.java | 4 +-
.../apache/iotdb/db/rest/service/RestService.java | 64 ++++++++++++++++------
.../java/org/apache/iotdb/db/rest/RestTest.java | 20 +++++--
3 files changed, 64 insertions(+), 24 deletions(-)
diff --git a/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java b/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java
index 80b7577..fec0822 100644
--- a/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java
+++ b/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java
@@ -21,9 +21,7 @@ package org.apache.iotdb.db.rest.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@@ -46,7 +44,7 @@ import org.slf4j.LoggerFactory;
public class RestController {
private static final Logger logger = LoggerFactory.getLogger(RestController.class);
- private RestService restService = new RestService();
+ private RestService restService = RestService.getInstance();
/**
* http request to login IoTDB
diff --git a/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java b/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java
index 7cec82c..9d4329b 100644
--- a/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java
+++ b/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java
@@ -29,6 +29,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.lang3.math.NumberUtils;
import org.apache.iotdb.db.auth.AuthException;
import org.apache.iotdb.db.auth.AuthorityChecker;
import org.apache.iotdb.db.conf.IoTDBConstant;
@@ -75,7 +76,7 @@ public class RestService {
String to = timeRange.right;
String suffixPath = s.substring(s.lastIndexOf('.') + 1);
String prefixPath = s.substring(0, s.lastIndexOf('.'));
- String sql = "SELECT " + suffixPath + " FROM root."
+ String sql = "SELECT " + suffixPath + " FROM"
+ prefixPath + " WHERE time > " + from + " and time < " + to;
logger.info(sql);
QueryOperator queryOperator = generateOperator(suffixPath, prefixPath, timeRange);
@@ -110,8 +111,8 @@ public class RestService {
while(queryDataSet.hasNext()) {
TimeValues timeValues = new TimeValues();
args = queryDataSet.next().toString().split("\t");
- timeValues.setTime(Long.parseLong(args[1]));
- timeValues.setValue(args[0]);
+ timeValues.setTime(Long.parseLong(args[0]));
+ timeValues.setValue(args[1]);
list.add(timeValues);
}
return list;
@@ -135,18 +136,41 @@ public class RestService {
*/
private QueryOperator generateOperator(String suffixPath, String prefixPath, Pair<String, String> timeRange) {
FilterOperator binaryOp = new FilterOperator(SQLConstant.KW_AND);
- binaryOp.addChildOperator(
- new BasicFunctionOperator(SQLConstant.GREATERTHAN,
- new Path(SQLConstant.RESERVED_TIME),
- String.valueOf(parseTimeFormat(timeRange.left))
- )
- );
- binaryOp.addChildOperator(
- new BasicFunctionOperator(SQLConstant.LESSTHAN,
- new Path(SQLConstant.RESERVED_TIME),
- String.valueOf(parseTimeFormat(timeRange.right))
- )
- );
+ long timeLeft;
+ long timeRight;
+ if(!NumberUtils.isDigits(timeRange.left)) {
+ timeLeft = parseTimeFormat(timeRange.left);
+ binaryOp.addChildOperator(
+ new BasicFunctionOperator(SQLConstant.GREATERTHAN,
+ new Path(SQLConstant.RESERVED_TIME),
+ String.valueOf(timeLeft)
+ )
+ );
+ } else {
+ binaryOp.addChildOperator(
+ new BasicFunctionOperator(SQLConstant.GREATERTHAN,
+ new Path(SQLConstant.RESERVED_TIME),
+ timeRange.left
+ )
+ );
+ }
+
+ if(!NumberUtils.isDigits(timeRange.right)) {
+ timeRight = parseTimeFormat(timeRange.right);
+ binaryOp.addChildOperator(
+ new BasicFunctionOperator(SQLConstant.LESSTHAN,
+ new Path(SQLConstant.RESERVED_TIME),
+ String.valueOf(timeRight)
+ )
+ );
+ } else {
+ binaryOp.addChildOperator(
+ new BasicFunctionOperator(SQLConstant.LESSTHAN,
+ new Path(SQLConstant.RESERVED_TIME),
+ timeRange.right
+ )
+ );
+ }
QueryOperator queryOp = new QueryOperator(SQLConstant.TOK_QUERY);
SelectOperator selectOp = new SelectOperator(SQLConstant.TOK_SELECT);
selectOp.addSelectPath(new Path(suffixPath));
@@ -253,10 +277,18 @@ public class RestService {
long time = tv.getTime();
String value = tv.getValue();
JSONArray jsonArray = new JSONArray();
- jsonArray.add(value);
jsonArray.add(time);
+ jsonArray.add(value);
dataPoints.add(jsonArray);
}
obj.put("datapoints", dataPoints);
}
+
+ public static RestService getInstance() {
+ return RestServiceHolder.INSTANCE;
+ }
+
+ private static class RestServiceHolder {
+ private static final RestService INSTANCE = new RestService();
+ }
}
diff --git a/server/src/test/java/org/apache/iotdb/db/rest/RestTest.java b/server/src/test/java/org/apache/iotdb/db/rest/RestTest.java
index 4976364..013af85 100644
--- a/server/src/test/java/org/apache/iotdb/db/rest/RestTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/rest/RestTest.java
@@ -13,6 +13,7 @@ import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.utils.EnvironmentUtils;
import org.apache.iotdb.jdbc.Config;
import org.junit.After;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -20,6 +21,9 @@ public class RestTest {
private static final String REST_URI
= "http://localhost:8181/rest/query";
+ private static final String LOGIN
+ = "http://localhost:8181/rest/login";
+
private static String[] creationSqls = new String[]{
"SET STORAGE GROUP TO root.vehicle.d0",
"SET STORAGE GROUP TO root.vehicle.d1",
@@ -120,20 +124,26 @@ public class RestTest {
@Test
public void testQuery() {
Client client = ClientBuilder.newClient();
- String json = "{\n"
+ String json1 = "{\n"
+ " \"range\": {\n"
- + " \"from\": \"1\",\n"
+ + " \"from\": \"0\",\n"
+ " \"to\": \"300\",\n"
+ " },\n"
+ " \n"
+ " \"targets\": [\n"
- + " { \"target\": \"root.ln.wf01.wt01\", \"type\": \"timeserie\" },\n"
+ + " { \"target\": \"root.ln.wf01.wt01.temperature\", \"type\": \"timeserie\" },\n"
+ " ]\n"
+ "}";
+
+ String json2 = "{username : \"root\", password : \"root\"}";
+ client.target(LOGIN)
+ .request(MediaType.TEXT_PLAIN)
+ .post(Entity.entity(json2, MediaType.TEXT_PLAIN));
Response response = client.target(REST_URI)
.request(MediaType.TEXT_PLAIN)
- .post(Entity.entity(json, MediaType.TEXT_PLAIN));
+ .post(Entity.entity(json1, MediaType.TEXT_PLAIN));
String result = response.readEntity(String.class);
- System.out.println(result);
+ Assert.assertEquals("[{\"datapoints\":[[1,\"1.1\"],[2,\"2.2\"],[3,\"3.3\"],[4,\"4.4\"],[5,\"5.5\"]],\"target\":\"root.ln.wf01.wt01.temperature\"}]"
+ , result);
}
}