From bcccd60a6bdc9307ffabc7620a30899df027209e Mon Sep 17 00:00:00 2001 From: xins Date: Sat, 16 Sep 2023 18:17:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=A5=E4=BE=9D=E5=BE=AE=E6=9C=8D=E5=8A=A11.?= =?UTF-8?q?2.1=E7=89=88=E6=9C=AC=20TeEngine=E6=9C=8D=E5=8A=A1=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tdengine/api/RemoteTdEngineService.java | 5 +- .../api/domain/TdSuperTableSelectVo.java | 49 +++++++++++++++++++ .../RemoteTdEngineFallbackFactory.java | 13 ++--- .../controller/TdEngineController.java | 14 +++--- .../ruoyi/tdengine/mapper/TdEngineMapper.java | 2 +- .../tdengine/service/ITdEngineService.java | 9 ++-- .../service/impl/TdEngineServiceImpl.java | 26 ++++++---- .../mapper/tdengine/TdEngineMapper.xml | 40 +++++++++++---- 8 files changed, 117 insertions(+), 41 deletions(-) create mode 100644 ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/TdSuperTableSelectVo.java diff --git a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java index 9b83e0e..8cfe757 100644 --- a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java +++ b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/RemoteTdEngineService.java @@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; +import java.util.List; import java.util.Map; @FeignClient(contextId = "remoteTdEngineService", value = ServiceNameConstants.TDENGINE_SERVICE, fallbackFactory = RemoteTdEngineFallbackFactory.class) @@ -43,10 +44,10 @@ public interface RemoteTdEngineService { R getLatestData(@Validated @RequestBody TdSelectDto tdSelectDto); @PostMapping("/tdengine/getLatestDataByTags") - R>> getLatestDataByTags(@RequestBody TdSelectDto tdSelectDto); + R>> getLatestDataByTags(@Validated @RequestBody TdSuperTableSelectVo tdSuperTableSelectVo); @PostMapping("/tdengine/getHistoryData") - R getHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto); + R>> getHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto); @PostMapping("/tdengine/getCountOfHistoryData") R getCountOfHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto); diff --git a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/TdSuperTableSelectVo.java b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/TdSuperTableSelectVo.java new file mode 100644 index 0000000..16ad660 --- /dev/null +++ b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/domain/TdSuperTableSelectVo.java @@ -0,0 +1,49 @@ +package com.ruoyi.tdengine.api.domain; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * @Description: 用于查询超级表最新数据 + * @ClassName: TdSuperTableSelectVo + * @Author : xins + * @Date :2023-09-16 13:48 + * @Version :1.0 + */ +@Data +public class TdSuperTableSelectVo { + //数据库名称 + @NotBlank(message="databaseName cannot be empty") + private String databaseName; + + //超级表名称 + @NotBlank(message="superTableName cannot be empty") + private String superTableName; + + //group by tags名称 + @NotBlank(message="groupByTagsName cannot be empty") + private String groupByTagsName; + + //控制输出条数 + private int limit; + + //指定从第几条之后输出(例如:limit 2,5,输出第3行到第7行的数据) + private int offset; + + /** + * 第一个字段,数据类型必须是timestamp + */ + private String firstFieldName; + + //过滤条件:开始时间 + private long startTime; + + //过滤条件:结束时间 + private long endTime; + + private String deviceCode; + + private String deviceName; + +} diff --git a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java index 64c1a65..d8a386c 100644 --- a/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java +++ b/ruoyi-api/hw-api-tdengine/src/main/java/com/ruoyi/tdengine/api/factory/RemoteTdEngineFallbackFactory.java @@ -8,23 +8,24 @@ import org.slf4j.LoggerFactory; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; +import java.util.List; import java.util.Map; /** * @Description: TDengine服务降级处理 * @ClassName: RemoteTdEngineFallbackFactory - * @Author : xins - * @Date :2023-09-01 14:14 + * @Author : xins + * @Date :2023-09-01 14:14 * @Version :1.0 */ @Component public class RemoteTdEngineFallbackFactory implements FallbackFactory { private static final Logger log = LoggerFactory.getLogger(RemoteTdEngineFallbackFactory.class); + @Override public RemoteTdEngineService create(Throwable throwable) { - return new RemoteTdEngineService() - { + return new RemoteTdEngineService() { @Override public R createDataBase(String databaseName) { return R.fail("创建数据库失败:" + throwable.getMessage()); @@ -66,12 +67,12 @@ public class RemoteTdEngineFallbackFactory implements FallbackFactory>> getLatestDataByTags(TdSelectDto tdSelectDto) { + public R>> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo) { return R.fail("根据tags获取超级表最新数据失败:" + throwable.getMessage()); } @Override - public R getHistoryData(TdHistorySelectDto tdHistorySelectDto) { + public R>> getHistoryData(TdHistorySelectDto tdHistorySelectDto) { return R.fail("获取历史数据失败:" + throwable.getMessage()); } diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java index 6a33bea..86ce794 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/controller/TdEngineController.java @@ -200,11 +200,11 @@ public class TdEngineController { } /** + * @return R * @param: alterTagVo * @description 修改子表tag值 * @author xins * @date 2023-08-30 11:17 - * @return R */ @PostMapping("/alterTableTag") public R alterTableTag(@Validated @RequestBody AlterTagVo alterTagVo) { @@ -214,9 +214,9 @@ public class TdEngineController { Object tagValue = alterTagVo.getTagValue(); try { - this.tdEngineService.alterTableTag(databaseName,tableName,tagName,tagValue); - log.info("successfully altered tag "+tagName+" value to "+tagValue+" of tableName"); - return R.ok("successfully altered tag "+tagName+" value to "+tagValue+" of tableName"); + this.tdEngineService.alterTableTag(databaseName, tableName, tagName, tagValue); + log.info("successfully altered tag " + tagName + " value to " + tagValue + " of tableName"); + return R.ok("successfully altered tag " + tagName + " value to " + tagValue + " of tableName"); } catch (UncategorizedSQLException e) { String message = e.getCause().getMessage(); try { @@ -295,7 +295,7 @@ public class TdEngineController { * @date 2023-08-29 14:51 */ @PostMapping("/getLatestDataByTags") - public R>> getLatestDataByTags(@RequestBody TdSelectDto tdSelectDto) { + public R>> getLatestDataByTags(@Validated @RequestBody TdSuperTableSelectVo tdSelectDto) { try { return R.ok(this.tdEngineService.getLatestDataByTags(tdSelectDto)); } catch (UncategorizedSQLException e) { @@ -348,11 +348,11 @@ public class TdEngineController { } /** + * @return R * @param: tdHistorySelectDto * @description 获取历史数据数量 * @author xins * @date 2023-08-29 17:29 - * @return R */ @PostMapping("/getCountOfHistoryData") public R getCountOfHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto) { @@ -381,7 +381,7 @@ public class TdEngineController { * @date 2023-08-29 11:26 */ @PostMapping("/getOnlineDevicesGroupByDay") - public R getOnlineDevicesGroupByDay(@RequestBody DeviceStatus queryDeviceStatus) { + public R getOnlineDevicesGroupByDay(@RequestBody DeviceStatus queryDeviceStatus) { try { List deviceStatuses = this.deviceStatusService.getOnlineDevicesGroupByDay(queryDeviceStatus); Map> deviceStatusMap = deviceStatuses.stream() diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/TdEngineMapper.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/TdEngineMapper.java index 1ba469c..0b24b91 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/TdEngineMapper.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/mapper/TdEngineMapper.java @@ -126,7 +126,7 @@ public interface TdEngineMapper { * @author xins * @date 2023-08-29 14:42 */ - List> getLatestDataByTags(TdSelectDto tdSelectDto); + List> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo); /** diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/ITdEngineService.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/ITdEngineService.java index 81644f9..8bba645 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/ITdEngineService.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/ITdEngineService.java @@ -1,9 +1,6 @@ package com.ruoyi.tdengine.service; -import com.ruoyi.tdengine.api.domain.TdField; -import com.ruoyi.tdengine.api.domain.TdFieldVo; -import com.ruoyi.tdengine.api.domain.TdHistorySelectDto; -import com.ruoyi.tdengine.api.domain.TdSelectDto; +import com.ruoyi.tdengine.api.domain.*; import java.util.List; import java.util.Map; @@ -99,14 +96,14 @@ public interface ITdEngineService { public List> getLatestData(TdSelectDto tdSelectDto) throws Exception; /** - * @return Map> + * @return List> * @param: tdSelectDto * @description 根据tagsname获取超级表最新信息,适用于设备汇总页面,先分页获取设备信息,再获取所有此物模型下的最新信息(冗余,但每页只获取一次,不需要每个设备都获取一次 * * 如果需要根据时间周期获取最新的数据,则需要用select last_row() * @author xins * @date 2023-08-29 14:47 */ - public Map> getLatestDataByTags(TdSelectDto tdSelectDto); + public List> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo); /** * @param: tdHistorySelectDto diff --git a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/TdEngineServiceImpl.java b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/TdEngineServiceImpl.java index 8340b54..857faec 100644 --- a/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/TdEngineServiceImpl.java +++ b/ruoyi-modules/hw-tdengine/src/main/java/com/ruoyi/tdengine/service/impl/TdEngineServiceImpl.java @@ -161,7 +161,7 @@ public class TdEngineServiceImpl implements ITdEngineService { } /** - * @return Map> + * @return List> * @param: tdSelectDto * @description 根据tagsname获取超级表最新信息,适用于设备汇总页面,先分页获取设备信息,再获取所有此物模型下的最新信息(冗余,但每页只获取一次,不需要每个设备都获取一次 * * 如果需要根据时间周期获取最新的数据,则需要用select last_row() @@ -169,19 +169,27 @@ public class TdEngineServiceImpl implements ITdEngineService { * @date 2023-08-29 14:47 */ @Override - public Map> getLatestDataByTags(TdSelectDto tdSelectDto) { - List> latestMaps = this.tdEngineMapper.getLatestDataByTags(tdSelectDto); - Map> tagsMap = new HashMap<>(); - for (Map latestMap : latestMaps) { - Map filterMap = latestMap.entrySet() + public List> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo) { + List> latestMaps = this.tdEngineMapper.getLatestDataByTags(tdSuperTableSelectVo); + latestMaps.replaceAll(latestMap -> { + return latestMap.entrySet() .stream() .filter(entry -> entry.getValue() != null) .collect(HashMap::new, (m, v) -> m.put(v.getKey().substring(v.getKey().indexOf("(") + 1, v.getKey().indexOf(")")), v.getValue()), HashMap::putAll); - tagsMap.put(filterMap.get(tdSelectDto.getTagsName()).toString(), filterMap); - } - return tagsMap; + }); + // Map> tagsMap = new HashMap<>(); +// for (Map latestMap : latestMaps) { +// Map filterMap = latestMap.entrySet() +// .stream() +// .filter(entry -> entry.getValue() != null) +// .collect(HashMap::new, (m, v) -> +// m.put(v.getKey().substring(v.getKey().indexOf("(") + 1, +// v.getKey().indexOf(")")), v.getValue()), HashMap::putAll); +// tagsMap.put(filterMap.get(tdSuperTableSelectVo.getTagsName()).toString(), filterMap); +// } + return latestMaps; } diff --git a/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/TdEngineMapper.xml b/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/TdEngineMapper.xml index 685f21d..95afd7b 100644 --- a/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/TdEngineMapper.xml +++ b/ruoyi-modules/hw-tdengine/src/main/resources/mapper/tdengine/TdEngineMapper.xml @@ -209,7 +209,7 @@ - + - select last(*), last(${tagsName}) from #{databaseName}.#{superTableName} group by ${tagsName} + select * from (select last(*), last(devicename),last(devicecode) from #{databaseName}.#{superTableName} + + + and ${firstFieldName} >= #{startTime} + + + and ${firstFieldName} <= #{endTime} + + + AND devicename like '%${deviceName}%' + + + AND devicecode like '%${deviceCode}%' + + + group by ${groupByTagsName}) + + LIMIT #{offset},#{limit} + @@ -228,21 +248,21 @@ SELECT * FROM #{databaseName}.#{tableName} - ${firstFieldName} BETWEEN #{startTime} AND #{endTime} + startTime!= 0 "> + and ${firstFieldName} >= #{startTime} + + + and ${firstFieldName} <= #{endTime} - order by ${orderByFieldName} ${sort} - - - - + LIMIT #{offset},#{limit}