若依微服务1.2.1版本

TeEngine服务完善
dev 1.2.1
xins 1 year ago
parent 1abf79f98f
commit bcccd60a6b

@ -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<Map<String, Map<String, Object>>> getLatestDataByTags(@RequestBody TdSelectDto tdSelectDto);
R<List<Map<String, Object>>> getLatestDataByTags(@Validated @RequestBody TdSuperTableSelectVo tdSuperTableSelectVo);
@PostMapping("/tdengine/getHistoryData")
R<?> getHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto);
R<List<Map<String, Object>>> getHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto);
@PostMapping("/tdengine/getCountOfHistoryData")
R<?> getCountOfHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto);

@ -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;
}

@ -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<RemoteTdEngineService> {
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<RemoteTdEn
}
@Override
public R<Map<String, Map<String, Object>>> getLatestDataByTags(TdSelectDto tdSelectDto) {
public R<List<Map<String, Object>>> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo) {
return R.fail("根据tags获取超级表最新数据失败:" + throwable.getMessage());
}
@Override
public R<?> getHistoryData(TdHistorySelectDto tdHistorySelectDto) {
public R<List<Map<String, Object>>> getHistoryData(TdHistorySelectDto tdHistorySelectDto) {
return R.fail("获取历史数据失败:" + throwable.getMessage());
}

@ -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<Map<String, Map<String, Object>>> getLatestDataByTags(@RequestBody TdSelectDto tdSelectDto) {
public R<List<Map<String, Object>>> 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<DeviceStatus> deviceStatuses = this.deviceStatusService.getOnlineDevicesGroupByDay(queryDeviceStatus);
Map<Long, List<DeviceStatus>> deviceStatusMap = deviceStatuses.stream()

@ -126,7 +126,7 @@ public interface TdEngineMapper {
* @author xins
* @date 2023-08-29 14:42
*/
List<Map<String, Object>> getLatestDataByTags(TdSelectDto tdSelectDto);
List<Map<String, Object>> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo);
/**

@ -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<Map<String, Object>> getLatestData(TdSelectDto tdSelectDto) throws Exception;
/**
* @return Map<Map < Object>>
* @return List<Map<String, Object>>
* @param: tdSelectDto
* @description tagsname
* * select last_row()
* @author xins
* @date 2023-08-29 14:47
*/
public Map<String, Map<String, Object>> getLatestDataByTags(TdSelectDto tdSelectDto);
public List<Map<String, Object>> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo);
/**
* @param: tdHistorySelectDto

@ -161,7 +161,7 @@ public class TdEngineServiceImpl implements ITdEngineService {
}
/**
* @return Map<Map < Object>>
* @return List<Map < String, Object>>
* @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<String, Map<String, Object>> getLatestDataByTags(TdSelectDto tdSelectDto) {
List<Map<String, Object>> latestMaps = this.tdEngineMapper.getLatestDataByTags(tdSelectDto);
Map<String, Map<String, Object>> tagsMap = new HashMap<>();
for (Map<String, Object> latestMap : latestMaps) {
Map<String, Object> filterMap = latestMap.entrySet()
public List<Map<String, Object>> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo) {
List<Map<String, Object>> 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<String, Map<String, Object>> tagsMap = new HashMap<>();
// for (Map<String, Object> latestMap : latestMaps) {
// Map<String, Object> 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;
}

@ -209,7 +209,7 @@
</insert>
<!--适用于设备监测页面(废弃,可以getLatestDataByTags通过tag监控单元ID来获取最新信息先根据监控单元获取设备信息再根据设备来获取最新的数据-->
<!--适用于设备监测页面(可以getLatestDataByTags通过tag监控单元ID来获取最新信息先根据监控单元获取设备信息再根据设备来获取最新的数据-->
<select id="getLatestData" parameterType="com.ruoyi.tdengine.api.domain.TdSelectDto"
resultType="java.util.Map">
select last(*) from #{databaseName}.#{tableName}
@ -217,10 +217,30 @@
<!--适用于设备汇总页面,先分页获取设备信息,再获取所有此物模型下的最新信息(冗余,但每页只获取一次,不需要每个设备都获取一次
如果需要根据时间周期获取最新的数据,则需要用select last_row()-->
也可以用select last_row()-->
<select id="getLatestDataByTags" parameterType="com.ruoyi.tdengine.api.domain.TdSelectDto"
resultType="java.util.Map">
select last(*), last(${tagsName}) from #{databaseName}.#{superTableName} group by ${tagsName}
select * from (select last(*), last(devicename),last(devicecode) from #{databaseName}.#{superTableName}
<where>
<if test="firstFieldName != null and firstFieldName != '' and
startTime!= null ">
and ${firstFieldName} &gt;= #{startTime}
</if>
<if test="firstFieldName != null and firstFieldName != '' and
endTime != 0">
and ${firstFieldName} &lt;= #{endTime}
</if>
<if test="deviceName != null and deviceName != ''">
AND devicename like '%${deviceName}%'
</if>
<if test="deviceCode != null and deviceCode != ''">
AND devicecode like '%${deviceCode}%'
</if>
</where>
group by ${groupByTagsName})
<if test="offset != null and limit != 0 ">
LIMIT #{offset},#{limit}
</if>
</select>
@ -228,21 +248,21 @@
SELECT * FROM #{databaseName}.#{tableName}
<where>
<if test="#{firstFieldName} != null and #{firstFieldName} != '' and
startTime!= 0 and endTime != 0">
${firstFieldName} BETWEEN #{startTime} AND #{endTime}
startTime!= 0 ">
and ${firstFieldName} &gt;= #{startTime}
</if>
<if test="#{firstFieldName} != null and #{firstFieldName} != '' and
endTime != 0">
and ${firstFieldName} &lt;= #{endTime}
</if>
</where>
<!--由于此处orderby是用${orderByFieldName},而不是用#${orderByFieldName}这样就会用sql注入风险所以加上choose和判断条件-->
<choose>
<when test="(orderByFieldName=='ts') and (sort=='desc' or sort=='asc')">
order by ${orderByFieldName} ${sort}
</when>
</choose>
<if test="offset != 0 and limit != 0 ">
<if test="offset != null and limit != 0 ">
LIMIT #{offset},#{limit}
</if>
</select>

Loading…
Cancel
Save