|
@@ -1,44 +1,162 @@
|
|
|
package com.gyee.gaia.dao.simulator;
|
|
|
|
|
|
import com.gyee.gaia.common.data.timeseries.*;
|
|
|
+import com.gyee.gaia.common.exception.WisdomException;
|
|
|
import com.gyee.gaia.dao.timeseries.IHistoryDao;
|
|
|
import com.gyee.gaia.dao.timeseries.SimulatorDao;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.sql.Blob;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.Future;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
@SimulatorDao
|
|
|
public class SimulatorHistoryDao implements IHistoryDao {
|
|
|
|
|
|
-
|
|
|
+ private final Random random = new Random();
|
|
|
+ @Override
|
|
|
public List<TsData> getTsDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
+ if (tsQuery.getTsPoint().getTsDataType() == TsDataType.DOUBLE) {
|
|
|
+ return getDoubleTsDataHistory(tsQuery);
|
|
|
+ } else if (tsQuery.getTsPoint().getTsDataType() == TsDataType.BOOLEAN) {
|
|
|
+ return getBooleanTsDataHistory(tsQuery);
|
|
|
+ } else if (tsQuery.getTsPoint().getTsDataType() == TsDataType.LONG) {
|
|
|
+ return getLongTsDataHistory(tsQuery);
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public List<TsData> getDoubleTsDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public List<DoubleStatData> getDoubleStatDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
+ if (tsQuery.getTsPoint().getTsDataType() != TsDataType.DOUBLE) {
|
|
|
+ throw new WisdomException("无效的数据类型:" + tsQuery.getTsPoint().getTsDataType());
|
|
|
+ }
|
|
|
|
|
|
- public List<TsData> getBooleanTsDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ List<DoubleStatData> result = new ArrayList<>();
|
|
|
|
|
|
+ double v1 = generateRandomDouble(0.0, 1000.0);
|
|
|
+ double v2 = generateRandomDouble(0.0, 1000.0);
|
|
|
|
|
|
- @Override
|
|
|
- public List<DoubleStatData> getDoubleStatDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
- return null;
|
|
|
+ DoubleTsData maxData;
|
|
|
+ DoubleTsData minData;
|
|
|
+ if(v1>=v2){
|
|
|
+ maxData = new DoubleTsData(tsQuery.getStartTs(), (short) 1, v1);
|
|
|
+ minData = new DoubleTsData(tsQuery.getStartTs(), (short) 1, v2);
|
|
|
+ }else {
|
|
|
+ maxData = new DoubleTsData(tsQuery.getStartTs(), (short) 1, v2);
|
|
|
+ minData = new DoubleTsData(tsQuery.getStartTs(), (short) 1, v1);
|
|
|
+ }
|
|
|
+ DoubleTsData avgData = new DoubleTsData(tsQuery.getStartTs(), (short) 1, (v1+v2)/2);
|
|
|
+ DoubleStatData statData = new DoubleStatData(avgData, maxData, minData);
|
|
|
+ result.add(statData);
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean writeHistoryValue(List<TsPointData> dataList) throws Exception {
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Map<String, TsData> getHistorySection(List<TsPoint> tsPoints, Long ts) throws Exception {
|
|
|
- return null;
|
|
|
+ Map<String, TsData> results = new LinkedHashMap<>();
|
|
|
+
|
|
|
+ Map<TsDataType, List<TsPoint>> pointGroup = tsPoints.stream().collect(Collectors.groupingBy(TsPoint::getTsDataType));
|
|
|
+ for (Map.Entry<TsDataType, List<TsPoint>> entry : pointGroup.entrySet()) {
|
|
|
+ String[] tagNames = entry.getValue().stream().map(TsPoint::getId).toArray(String[]::new);
|
|
|
+ if (entry.getKey() == TsDataType.DOUBLE)
|
|
|
+ for (String tag : tagNames) {
|
|
|
+ results.put(tag,new DoubleTsData(ts, (short) 0, generateRandomDouble(0.0, 1000.0)));
|
|
|
+ }
|
|
|
+ if (entry.getKey() == TsDataType.BOOLEAN) {
|
|
|
+ for (String tag : tagNames) {
|
|
|
+ results.put(tag,new BooleanTsData(ts, (short) 0, random.nextBoolean()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return results;
|
|
|
}
|
|
|
|
|
|
+ public List<TsData> getDoubleTsDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
+ List<TsData> tsDataList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<Long> timestampList = generateTimestampList(tsQuery.getStartTs(), tsQuery.getEndTs(), tsQuery.getInterval()*1000L);
|
|
|
+
|
|
|
+ for (Long aLong : timestampList) {
|
|
|
+ tsDataList.add(new DoubleTsData(aLong, (short) 0, generateRandomDouble(0.0, 1000.0)));
|
|
|
+ }
|
|
|
+
|
|
|
+ return tsDataList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public List<TsData> getLongTsDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
+ List<TsData> tsDataList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<Long> timestampList = generateTimestampList(tsQuery.getStartTs(), tsQuery.getEndTs(), tsQuery.getInterval()*1000L);
|
|
|
+
|
|
|
+ for (Long aLong : timestampList) {
|
|
|
+ tsDataList.add(new LongTsData(aLong, (short) 0, generateRandomInt(0, 100000)));
|
|
|
+ }
|
|
|
+
|
|
|
+ return tsDataList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<TsData> getBooleanTsDataHistory(TsQuery tsQuery) throws Exception {
|
|
|
+ List<TsData> tsDataList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<Long> timestampList = generateTimestampList(tsQuery.getStartTs(), tsQuery.getEndTs(), tsQuery.getInterval()*1000L);
|
|
|
+
|
|
|
+ for (Long aLong : timestampList) {
|
|
|
+ tsDataList.add(new BooleanTsData(aLong, (short) 0, random.nextBoolean()));
|
|
|
+ }
|
|
|
+
|
|
|
+ return tsDataList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据指定的开始时间戳、结束时间戳和数量生成随机的时间戳列表
|
|
|
+ *
|
|
|
+ * @param startTs 开始时间戳
|
|
|
+ * @param endTs 结束时间戳
|
|
|
+ * @param num 时间戳列表数量
|
|
|
+ * @return 随机的时间戳列表
|
|
|
+ */
|
|
|
+ public static List<Long> generateTimestampList(long startTs, long endTs, long num) {
|
|
|
+ List<Long> list = new ArrayList<>();
|
|
|
+ for (long timestamp = startTs; timestamp <= endTs; timestamp += num) {
|
|
|
+ list.add(timestamp);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成指定范围内的随机数
|
|
|
+ *
|
|
|
+ * @param min 随机数的最小值
|
|
|
+ * @param max 随机数的最大值
|
|
|
+ * @return 在指定范围内的随机数
|
|
|
+ */
|
|
|
+ public double generateRandomDouble(double min, double max) {
|
|
|
+ return min + (max - min) * random.nextDouble();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 生成指定范围内的随机整数
|
|
|
+ *
|
|
|
+ * @param min 随机数的最小值
|
|
|
+ * @param max 随机数的最大值
|
|
|
+ * @return 在指定范围内的随机整数
|
|
|
+ */
|
|
|
+ public int generateRandomInt(int min, int max) {
|
|
|
+ return random.nextInt(max - min + 1) + min;
|
|
|
+ }
|
|
|
}
|