若依微服务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.RequestBody;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map; import java.util.Map;
@FeignClient(contextId = "remoteTdEngineService", value = ServiceNameConstants.TDENGINE_SERVICE, fallbackFactory = RemoteTdEngineFallbackFactory.class) @FeignClient(contextId = "remoteTdEngineService", value = ServiceNameConstants.TDENGINE_SERVICE, fallbackFactory = RemoteTdEngineFallbackFactory.class)
@ -43,10 +44,10 @@ public interface RemoteTdEngineService {
R<?> getLatestData(@Validated @RequestBody TdSelectDto tdSelectDto); R<?> getLatestData(@Validated @RequestBody TdSelectDto tdSelectDto);
@PostMapping("/tdengine/getLatestDataByTags") @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") @PostMapping("/tdengine/getHistoryData")
R<?> getHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto); R<List<Map<String, Object>>> getHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto);
@PostMapping("/tdengine/getCountOfHistoryData") @PostMapping("/tdengine/getCountOfHistoryData")
R<?> getCountOfHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto); 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,6 +8,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -21,10 +22,10 @@ import java.util.Map;
public class RemoteTdEngineFallbackFactory implements FallbackFactory<RemoteTdEngineService> { public class RemoteTdEngineFallbackFactory implements FallbackFactory<RemoteTdEngineService> {
private static final Logger log = LoggerFactory.getLogger(RemoteTdEngineFallbackFactory.class); private static final Logger log = LoggerFactory.getLogger(RemoteTdEngineFallbackFactory.class);
@Override @Override
public RemoteTdEngineService create(Throwable throwable) { public RemoteTdEngineService create(Throwable throwable) {
return new RemoteTdEngineService() return new RemoteTdEngineService() {
{
@Override @Override
public R<?> createDataBase(String databaseName) { public R<?> createDataBase(String databaseName) {
return R.fail("创建数据库失败:" + throwable.getMessage()); return R.fail("创建数据库失败:" + throwable.getMessage());
@ -66,12 +67,12 @@ public class RemoteTdEngineFallbackFactory implements FallbackFactory<RemoteTdEn
} }
@Override @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()); return R.fail("根据tags获取超级表最新数据失败:" + throwable.getMessage());
} }
@Override @Override
public R<?> getHistoryData(TdHistorySelectDto tdHistorySelectDto) { public R<List<Map<String, Object>>> getHistoryData(TdHistorySelectDto tdHistorySelectDto) {
return R.fail("获取历史数据失败:" + throwable.getMessage()); return R.fail("获取历史数据失败:" + throwable.getMessage());
} }

@ -200,11 +200,11 @@ public class TdEngineController {
} }
/** /**
* @return R<?>
* @param: alterTagVo * @param: alterTagVo
* @description tag * @description tag
* @author xins * @author xins
* @date 2023-08-30 11:17 * @date 2023-08-30 11:17
* @return R<?>
*/ */
@PostMapping("/alterTableTag") @PostMapping("/alterTableTag")
public R<?> alterTableTag(@Validated @RequestBody AlterTagVo alterTagVo) { public R<?> alterTableTag(@Validated @RequestBody AlterTagVo alterTagVo) {
@ -214,9 +214,9 @@ public class TdEngineController {
Object tagValue = alterTagVo.getTagValue(); Object tagValue = alterTagVo.getTagValue();
try { try {
this.tdEngineService.alterTableTag(databaseName,tableName,tagName,tagValue); this.tdEngineService.alterTableTag(databaseName, tableName, tagName, tagValue);
log.info("successfully altered tag "+tagName+" value to "+tagValue+" of tableName"); log.info("successfully altered tag " + tagName + " value to " + tagValue + " of tableName");
return R.ok("successfully altered tag "+tagName+" value to "+tagValue+" of tableName"); return R.ok("successfully altered tag " + tagName + " value to " + tagValue + " of tableName");
} catch (UncategorizedSQLException e) { } catch (UncategorizedSQLException e) {
String message = e.getCause().getMessage(); String message = e.getCause().getMessage();
try { try {
@ -295,7 +295,7 @@ public class TdEngineController {
* @date 2023-08-29 14:51 * @date 2023-08-29 14:51
*/ */
@PostMapping("/getLatestDataByTags") @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 { try {
return R.ok(this.tdEngineService.getLatestDataByTags(tdSelectDto)); return R.ok(this.tdEngineService.getLatestDataByTags(tdSelectDto));
} catch (UncategorizedSQLException e) { } catch (UncategorizedSQLException e) {
@ -348,11 +348,11 @@ public class TdEngineController {
} }
/** /**
* @return R<?>
* @param: tdHistorySelectDto * @param: tdHistorySelectDto
* @description * @description
* @author xins * @author xins
* @date 2023-08-29 17:29 * @date 2023-08-29 17:29
* @return R<?>
*/ */
@PostMapping("/getCountOfHistoryData") @PostMapping("/getCountOfHistoryData")
public R<?> getCountOfHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto) { public R<?> getCountOfHistoryData(@Validated @RequestBody TdHistorySelectDto tdHistorySelectDto) {

@ -126,7 +126,7 @@ public interface TdEngineMapper {
* @author xins * @author xins
* @date 2023-08-29 14:42 * @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; package com.ruoyi.tdengine.service;
import com.ruoyi.tdengine.api.domain.TdField; import com.ruoyi.tdengine.api.domain.*;
import com.ruoyi.tdengine.api.domain.TdFieldVo;
import com.ruoyi.tdengine.api.domain.TdHistorySelectDto;
import com.ruoyi.tdengine.api.domain.TdSelectDto;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -99,14 +96,14 @@ public interface ITdEngineService {
public List<Map<String, Object>> getLatestData(TdSelectDto tdSelectDto) throws Exception; public List<Map<String, Object>> getLatestData(TdSelectDto tdSelectDto) throws Exception;
/** /**
* @return Map<Map < Object>> * @return List<Map<String, Object>>
* @param: tdSelectDto * @param: tdSelectDto
* @description tagsname * @description tagsname
* * select last_row() * * select last_row()
* @author xins * @author xins
* @date 2023-08-29 14:47 * @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 * @param: tdHistorySelectDto

@ -161,7 +161,7 @@ public class TdEngineServiceImpl implements ITdEngineService {
} }
/** /**
* @return Map<Map < Object>> * @return List<Map < String, Object>>
* @param: tdSelectDto * @param: tdSelectDto
* @description tagsname * @description tagsname
* * select last_row() * * select last_row()
@ -169,19 +169,27 @@ public class TdEngineServiceImpl implements ITdEngineService {
* @date 2023-08-29 14:47 * @date 2023-08-29 14:47
*/ */
@Override @Override
public Map<String, Map<String, Object>> getLatestDataByTags(TdSelectDto tdSelectDto) { public List<Map<String, Object>> getLatestDataByTags(TdSuperTableSelectVo tdSuperTableSelectVo) {
List<Map<String, Object>> latestMaps = this.tdEngineMapper.getLatestDataByTags(tdSelectDto); List<Map<String, Object>> latestMaps = this.tdEngineMapper.getLatestDataByTags(tdSuperTableSelectVo);
Map<String, Map<String, Object>> tagsMap = new HashMap<>(); latestMaps.replaceAll(latestMap -> {
for (Map<String, Object> latestMap : latestMaps) { return latestMap.entrySet()
Map<String, Object> filterMap = latestMap.entrySet()
.stream() .stream()
.filter(entry -> entry.getValue() != null) .filter(entry -> entry.getValue() != null)
.collect(HashMap::new, (m, v) -> .collect(HashMap::new, (m, v) ->
m.put(v.getKey().substring(v.getKey().indexOf("(") + 1, m.put(v.getKey().substring(v.getKey().indexOf("(") + 1,
v.getKey().indexOf(")")), v.getValue()), HashMap::putAll); v.getKey().indexOf(")")), v.getValue()), HashMap::putAll);
tagsMap.put(filterMap.get(tdSelectDto.getTagsName()).toString(), filterMap); });
} // Map<String, Map<String, Object>> tagsMap = new HashMap<>();
return tagsMap; // 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> </insert>
<!--适用于设备监测页面(废弃,可以getLatestDataByTags通过tag监控单元ID来获取最新信息先根据监控单元获取设备信息再根据设备来获取最新的数据--> <!--适用于设备监测页面(可以getLatestDataByTags通过tag监控单元ID来获取最新信息先根据监控单元获取设备信息再根据设备来获取最新的数据-->
<select id="getLatestData" parameterType="com.ruoyi.tdengine.api.domain.TdSelectDto" <select id="getLatestData" parameterType="com.ruoyi.tdengine.api.domain.TdSelectDto"
resultType="java.util.Map"> resultType="java.util.Map">
select last(*) from #{databaseName}.#{tableName} 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" <select id="getLatestDataByTags" parameterType="com.ruoyi.tdengine.api.domain.TdSelectDto"
resultType="java.util.Map"> 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> </select>
@ -228,21 +248,21 @@
SELECT * FROM #{databaseName}.#{tableName} SELECT * FROM #{databaseName}.#{tableName}
<where> <where>
<if test="#{firstFieldName} != null and #{firstFieldName} != '' and <if test="#{firstFieldName} != null and #{firstFieldName} != '' and
startTime!= 0 and endTime != 0"> startTime!= 0 ">
${firstFieldName} BETWEEN #{startTime} AND #{endTime} and ${firstFieldName} &gt;= #{startTime}
</if>
<if test="#{firstFieldName} != null and #{firstFieldName} != '' and
endTime != 0">
and ${firstFieldName} &lt;= #{endTime}
</if> </if>
</where> </where>
<!--由于此处orderby是用${orderByFieldName},而不是用#${orderByFieldName}这样就会用sql注入风险所以加上choose和判断条件--> <!--由于此处orderby是用${orderByFieldName},而不是用#${orderByFieldName}这样就会用sql注入风险所以加上choose和判断条件-->
<choose> <choose>
<when test="(orderByFieldName=='ts') and (sort=='desc' or sort=='asc')"> <when test="(orderByFieldName=='ts') and (sort=='desc' or sort=='asc')">
order by ${orderByFieldName} ${sort} order by ${orderByFieldName} ${sort}
</when> </when>
</choose> </choose>
<if test="offset != null and limit != 0 ">
<if test="offset != 0 and limit != 0 ">
LIMIT #{offset},#{limit} LIMIT #{offset},#{limit}
</if> </if>
</select> </select>

Loading…
Cancel
Save