|
@@ -1,5 +1,6 @@
|
|
|
package com.gyee.gaia.dao.taoscz;
|
|
|
|
|
|
+import com.gyee.gaia.common.data.point.PointData;
|
|
|
import com.gyee.gaia.common.data.timeseries.*;
|
|
|
import com.gyee.gaia.common.exception.WisdomException;
|
|
|
import com.gyee.gaia.dao.timeseries.ILatestDao;
|
|
@@ -12,10 +13,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.Statement;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
@@ -51,6 +49,25 @@ public class TaosLatestDao implements ILatestDao {
|
|
|
}
|
|
|
return result2;
|
|
|
}
|
|
|
+ @Override
|
|
|
+ public List<PointData> getTsDataLatest2(List<TsPoint> tsPoints) throws Exception {
|
|
|
+ List<PointData> result = new ArrayList<>();
|
|
|
+ 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(tp -> tp.getId().replaceFirst("\\.", "__")).toArray(String[]::new);
|
|
|
+ if (entry.getKey() == TsDataType.DOUBLE) {
|
|
|
+ result.addAll(getDoubleTsDataSnapshots2(tagNames));
|
|
|
+ } else if (entry.getKey() == TsDataType.BOOLEAN) {
|
|
|
+ result.addAll(getBooleanTsDataSnapshots2(tagNames));
|
|
|
+ } else if (entry.getKey() == TsDataType.LONG) {
|
|
|
+ result.addAll(getLongTsDataSnapshots2(tagNames));
|
|
|
+ } else {
|
|
|
+ throw new WisdomException("Taos不支持数据类型:" + entry.getKey());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<PointData> collect = result.stream().peek(pd -> pd.setTagName(pd.getTagName().replaceFirst("__", "."))).collect(Collectors.toList());
|
|
|
+ return collect;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
@Override
|
|
@@ -74,6 +91,25 @@ public class TaosLatestDao implements ILatestDao {
|
|
|
return i == list.size() ? list.size() : 0;
|
|
|
}
|
|
|
|
|
|
+ public int writeDoubleLatest2(List<PointData> list) throws Exception {
|
|
|
+ Statement st = config.getInstance().createStatement();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("INSERT INTO ");
|
|
|
+ for (PointData obj : list) {
|
|
|
+ long time = obj.getTs();
|
|
|
+ String point = config.getTableName(obj.getTagName());
|
|
|
+ double value = obj.getDoubleValue();
|
|
|
+ sb.append(point).append(" USING ").append(config.getDbName())
|
|
|
+ .append(".").append(config.getAIStableName())
|
|
|
+ .append(" TAGS ('','','','','') VALUES (");
|
|
|
+ sb.append(time).append(",").append(value).append(") ");
|
|
|
+ }
|
|
|
+ sb.append(";");
|
|
|
+ val i = st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ return i == list.size() ? list.size() : 0;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public int writeStringLatest(List<TsPointData> list) throws Exception {
|
|
|
Statement st = config.getInstance().createStatement();
|
|
@@ -110,6 +146,23 @@ public class TaosLatestDao implements ILatestDao {
|
|
|
|
|
|
return i;
|
|
|
}
|
|
|
+ @Override
|
|
|
+ public int writeBooleanLatest2(List<PointData> list) throws Exception {
|
|
|
+ Statement st = config.getInstance().createStatement();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("insert into ");
|
|
|
+ for (PointData obj : list) {
|
|
|
+ long time = obj.getTs();
|
|
|
+ String point = obj.getTagName();
|
|
|
+ boolean value = obj.getBooleanValue();
|
|
|
+ sb.append(point).append(" values (");
|
|
|
+ sb.append(time).append(",").append(value).append(") ");
|
|
|
+ }
|
|
|
+ sb.append(";");
|
|
|
+ int i = st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ return i;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public int writeLongLatest(List<TsPointData> list) throws Exception {
|
|
@@ -175,6 +228,23 @@ public class TaosLatestDao implements ILatestDao {
|
|
|
}
|
|
|
return writeCount > 0 ? true : false;
|
|
|
}
|
|
|
+ @Override
|
|
|
+ public boolean writeLatest2(List<PointData> dataList) throws Exception {
|
|
|
+ int writeCount = 0;
|
|
|
+ dataList = dataList.stream().peek(tpd -> tpd.setTagName(tpd.getTagName().replaceFirst("\\.", "__"))).collect(Collectors.toList());
|
|
|
+ Map<TsDataType, List<PointData>> pointGroup = dataList.stream().collect(Collectors.groupingBy(PointData::getType));
|
|
|
+ for (Map.Entry<TsDataType, List<PointData>> entry : pointGroup.entrySet()) {
|
|
|
+ List<PointData> pointDataList = entry.getValue();
|
|
|
+ if (entry.getKey() == TsDataType.DOUBLE) {
|
|
|
+ int count = writeDoubleLatest2(pointDataList);
|
|
|
+ writeCount = writeCount + count;
|
|
|
+ } else if (entry.getKey() == TsDataType.BOOLEAN) {
|
|
|
+ int count = writeBooleanLatest2(pointDataList);
|
|
|
+ writeCount = writeCount + count;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return writeCount > 0 ? true : false;
|
|
|
+ }
|
|
|
|
|
|
public Map<String, TsData> getDoubleTsDataSnapshots(String... tagNames) throws Exception {
|
|
|
Map<String, TsData> tsDataMap = new HashMap<>();
|
|
@@ -191,6 +261,25 @@ public class TaosLatestDao implements ILatestDao {
|
|
|
return tsDataMap;
|
|
|
}
|
|
|
|
|
|
+ public List<PointData> getDoubleTsDataSnapshots2(String... tagNames) throws Exception {
|
|
|
+ List<PointData> tsDataMap = new ArrayList<>();
|
|
|
+ Statement st = config.getInstance().createStatement();
|
|
|
+
|
|
|
+ String inString = Arrays.stream(tagNames).map(s -> "'" + s.toLowerCase() + "'").collect(Collectors.joining(","));
|
|
|
+ inString = "select last_row(*),tbname from " + config.getDbName() + "." + config.getAIStableName()
|
|
|
+ + " where tbname in (" + inString + ") group by tbname;";
|
|
|
+ ResultSet rs = st.executeQuery(inString);
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ PointData pointData = new PointData();
|
|
|
+ pointData.setTagName(rs.getString(3).toUpperCase());
|
|
|
+ pointData.setTs(rs.getTimestamp(1).getTime());
|
|
|
+ pointData.setDoubleValue(rs.getDouble(2));
|
|
|
+ tsDataMap.add(pointData);
|
|
|
+ }
|
|
|
+ return tsDataMap;
|
|
|
+ }
|
|
|
+
|
|
|
public Map<String, TsData> getLongTsDataSnapshots(String... tagNames) throws Exception {
|
|
|
Map<String, TsData> tsDataMap = new HashMap<>();
|
|
|
Statement st = config.getInstance().createStatement();
|
|
@@ -204,6 +293,23 @@ public class TaosLatestDao implements ILatestDao {
|
|
|
return tsDataMap;
|
|
|
}
|
|
|
|
|
|
+ public List<PointData> getLongTsDataSnapshots2(String... tagNames) throws Exception {
|
|
|
+ List<PointData> tsDataMap = new ArrayList<>();
|
|
|
+ Statement st = config.getInstance().createStatement();
|
|
|
+ for (String tag : tagNames) {
|
|
|
+ String tableName = config.getTableName(tag);
|
|
|
+ ResultSet rs = st.executeQuery("select last_row(*) from " + tableName);
|
|
|
+ while (rs.next()) {
|
|
|
+ PointData pointData = new PointData();
|
|
|
+ pointData.setTagName(tag);
|
|
|
+ pointData.setTs(rs.getTimestamp(1).getTime());
|
|
|
+ pointData.setLongValue(rs.getLong(2));
|
|
|
+ tsDataMap.add(pointData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return tsDataMap;
|
|
|
+ }
|
|
|
+
|
|
|
public Map<String, TsData> getBooleanTsDataSnapshots(String... tagNames) throws Exception {
|
|
|
Map<String, TsData> tsDataMap = new HashMap<>();
|
|
|
Statement st = config.getInstance().createStatement();
|
|
@@ -218,5 +324,23 @@ public class TaosLatestDao implements ILatestDao {
|
|
|
}
|
|
|
return tsDataMap;
|
|
|
}
|
|
|
+ public List<PointData> getBooleanTsDataSnapshots2(String... tagNames) throws Exception {
|
|
|
+ List<PointData> tsDataMap = new ArrayList<>();
|
|
|
+ Statement st = config.getInstance().createStatement();
|
|
|
+
|
|
|
+ String inString = Arrays.stream(tagNames).map(s -> "'" + s.toLowerCase() + "'").collect(Collectors.joining(","));
|
|
|
+ inString = "select last_row(*),tbname from " + config.getDbName() + "." + config.getDIStableName() +
|
|
|
+ " where tbname in (" + inString + ") group by tbname;";
|
|
|
+ ResultSet rs = st.executeQuery(inString);
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ PointData pointData = new PointData();
|
|
|
+ pointData.setTagName(rs.getString(3).toUpperCase());
|
|
|
+ pointData.setTs(rs.getTimestamp(1).getTime());
|
|
|
+ pointData.setBooleanValue(rs.getBoolean(2));
|
|
|
+ tsDataMap.add(pointData);
|
|
|
+ }
|
|
|
+ return tsDataMap;
|
|
|
+ }
|
|
|
|
|
|
}
|