Selaa lähdekoodia

优化查询性能

menglu 4 vuotta sitten
vanhempi
commit
409f51accc

+ 55 - 15
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/RestUtil.java

@@ -649,27 +649,27 @@ public class RestUtil {
 		}
 		Map<String, JSONArray> Point2datas = new HashMap<String, JSONArray>();
 		Map<String, IntWrapper> Point2index = new HashMap<String, IntWrapper>();
+		JSONArray points = new JSONArray();
 		for (String objInfo : ObjInfo2Point.keySet()) {
 			String point = ObjInfo2Point.get(objInfo);
 			int index_ = point.lastIndexOf('-');
 			String meter = point.substring(0, index_);
 			int funcid = Integer.parseInt(point.substring(index_ + 1));
-			JSONArray datas = null;
-			try {
-				datas = fjd_query_inner(meter, funcid, time_from, time_to);
-			} catch (Exception e) {
-				// e.printStackTrace();
-				try {
-					datas = fjd_query_inner(meter, funcid, time_from, time_to);
-				} catch (Exception e1) {
-					// e.printStackTrace();
-				}
+			JSONObject pointObject = new JSONObject();
+			pointObject.put("meter", meter);
+			pointObject.put("funcid", funcid);
+			pointObject.put("time_period", "15min");
+			pointObject.put("time_from", time_from);
+			pointObject.put("time_to", time_to);
+			points.add(pointObject);
+			if (points.size() > 10) {
+				fjd_query_result_process(points, Point2datas, Point2index);
+				points = new JSONArray();
 			}
-			if (datas == null) {
-				datas = new JSONArray();
-			}
-			Point2datas.put(point, datas);
-			Point2index.put(point, new IntWrapper());
+		}
+		if (points.size() > 0) {
+			fjd_query_result_process(points, Point2datas, Point2index);
+			points = new JSONArray();
 		}
 		JSONArray result = new JSONArray();
 		try {
@@ -699,6 +699,46 @@ public class RestUtil {
 		return result.toJSONString();
 	}
 
+	private static void fjd_query_result_process(JSONArray points, Map<String, JSONArray> Point2datas, Map<String, IntWrapper> Point2index) {
+		JSONArray pointResult = null;
+		try {
+			pointResult = fjd_query_inner(points);
+		} catch (Exception e) {
+			// e.printStackTrace();
+			try {
+				pointResult = fjd_query_inner(points);
+			} catch (Exception e1) {
+				// e.printStackTrace();
+			}
+		}
+		if (pointResult == null) {
+			pointResult = new JSONArray();
+		}
+		for (int i = 0; i < pointResult.size(); i++) {
+			JSONObject point = pointResult.getJSONObject(i);
+			String meter = point.getString("meter");
+			int funcid = point.getIntValue("funcid");
+			JSONArray datas = point.getJSONArray("datas");
+			if (datas == null) {
+				datas = new JSONArray();
+			}
+			String pointString = meter + "-" + funcid;
+			Point2datas.put(pointString, datas);
+			Point2index.put(pointString, new IntWrapper());
+		}
+	}
+
+	private static JSONArray fjd_query_inner(JSONArray points) throws Exception {
+		JSONObject postJSON = new JSONObject();
+		postJSON.put("building", RepositoryBase.projectId.substring(2));
+		postJSON.put("points", points);
+		String post_url = Constant.iot_collect_url + "fjd_query_batch_post";
+		String post_result = HttpClientUtil.post(post_url, postJSON.toJSONString());
+		JSONObject resultJSON = JSON.parseObject(post_result);
+		JSONArray result_points = (JSONArray) resultJSON.get("points");
+		return result_points;
+	}
+
 	private static JSONArray fjd_query_inner(String meter, int funcid, String time_from, String time_to) throws Exception {
 		JSONObject pointObject = new JSONObject();
 		pointObject.put("meter", meter);