TDengine测试和优化

This commit is contained in:
kerwincui
2022-06-18 23:14:43 +08:00
parent 452c5a5903
commit 381ff00ea4
12 changed files with 159 additions and 167 deletions

View File

@@ -84,11 +84,10 @@ public interface DeviceLogMapper
/** /**
* 根据设备Ids批量删除设备日志 * 根据设备Ids批量删除设备日志
* *
* @param deviceId 需要删除的数据设备Id * @param deviceNumber 需要删除的数据设备Id
* @return 结果 * @return 结果
*/ */
public int deleteDeviceLogByDeviceId(Long deviceId); public int deleteDeviceLogByDeviceNumber(String deviceNumber);
// List<DeviceLog> selectLogList(Long deviceId,String serialNumber,Long isMonitor,Long logType, Date beginDate, Date endDate);
} }

View File

@@ -18,25 +18,25 @@ public class DeviceStatistic
private int productCount; private int productCount;
/** 告警 **/ /** 告警 **/
private int alertCount; private long alertCount;
/** 属性上报 **/ /** 属性上报 **/
private int propertyCount; private long propertyCount;
/** 功能上报 **/ /** 功能上报 **/
private int functionCount; private long functionCount;
/** 事件上报 **/ /** 事件上报 **/
private int eventCount; private long eventCount;
/** 监测数据上报 **/ /** 监测数据上报 **/
private int monitorCount; private long monitorCount;
public int getMonitorCount() { public long getMonitorCount() {
return monitorCount; return monitorCount;
} }
public void setMonitorCount(int monitorCount) { public void setMonitorCount(long monitorCount) {
this.monitorCount = monitorCount; this.monitorCount = monitorCount;
} }
@@ -56,35 +56,35 @@ public class DeviceStatistic
this.productCount = productCount; this.productCount = productCount;
} }
public int getAlertCount() { public long getAlertCount() {
return alertCount; return alertCount;
} }
public void setAlertCount(int alertCount) { public void setAlertCount(long alertCount) {
this.alertCount = alertCount; this.alertCount = alertCount;
} }
public int getPropertyCount() { public long getPropertyCount() {
return propertyCount; return propertyCount;
} }
public void setPropertyCount(int propertyCount) { public void setPropertyCount(long propertyCount) {
this.propertyCount = propertyCount; this.propertyCount = propertyCount;
} }
public int getFunctionCount() { public long getFunctionCount() {
return functionCount; return functionCount;
} }
public void setFunctionCount(int functionCount) { public void setFunctionCount(long functionCount) {
this.functionCount = functionCount; this.functionCount = functionCount;
} }
public int getEventCount() { public long getEventCount() {
return eventCount; return eventCount;
} }
public void setEventCount(int eventCount) { public void setEventCount(long eventCount) {
this.eventCount = eventCount; this.eventCount = eventCount;
} }
} }

View File

@@ -70,10 +70,10 @@ public interface IDeviceLogService
public int deleteDeviceLogByLogId(Long logId); public int deleteDeviceLogByLogId(Long logId);
/** /**
* 根据设备ID批量删除设备日志 * 根据设备编号批量删除设备日志
* *
* @param deviceId 需要删除的设备日志ID * @param deviceNumber 需要删除的设备日志ID
* @return 结果 * @return 结果
*/ */
public int deleteDeviceLogByDeviceId(Long deviceId); public int deleteDeviceLogByDeviceNumber(String deviceNumber);
} }

View File

@@ -101,13 +101,13 @@ public class DeviceLogServiceImpl implements IDeviceLogService
/** /**
* 根据设备Ids批量删除设备日志 * 根据设备Ids批量删除设备日志
* *
* @param deviceId 需要删除数据的设备Ids * @param deviceNumber 需要删除数据的设备Ids
* @return 结果 * @return 结果
*/ */
@Override @Override
public int deleteDeviceLogByDeviceId(Long deviceId) public int deleteDeviceLogByDeviceNumber(String deviceNumber)
{ {
return deviceLogMapper.deleteDeviceLogByDeviceId(deviceId); return deviceLogMapper.deleteDeviceLogByDeviceNumber(deviceNumber);
} }
/** /**

View File

@@ -234,7 +234,7 @@ public class DeviceServiceImpl implements IDeviceService {
deviceLog.setLogValue(input.getThingsModelValueRemarkItem().get(i).getValue()); deviceLog.setLogValue(input.getThingsModelValueRemarkItem().get(i).getValue());
deviceLog.setRemark(input.getThingsModelValueRemarkItem().get(i).getRemark()); deviceLog.setRemark(input.getThingsModelValueRemarkItem().get(i).getRemark());
deviceLog.setIdentity(input.getThingsModelValueRemarkItem().get(i).getId()); deviceLog.setIdentity(input.getThingsModelValueRemarkItem().get(i).getId());
deviceLog.setIsMonitor(valueList.get(k).getIsMonitor()); deviceLog.setIsMonitor(valueList.get(k).getIsMonitor()==null ? 0:valueList.get(k).getIsMonitor());
deviceLog.setLogType(type); deviceLog.setLogType(type);
deviceLog.setUserId(deviceThings.getUserId()); deviceLog.setUserId(deviceThings.getUserId());
deviceLog.setUserName(deviceThings.getUserName()); deviceLog.setUserName(deviceThings.getUserName());
@@ -244,7 +244,6 @@ public class DeviceServiceImpl implements IDeviceService {
// 1=影子模式2=在线模式3=其他 // 1=影子模式2=在线模式3=其他
deviceLog.setMode(isShadow?1:2); deviceLog.setMode(isShadow?1:2);
logService.saveDeviceLog(deviceLog); logService.saveDeviceLog(deviceLog);
break; break;
} }
} }
@@ -885,7 +884,7 @@ public class DeviceServiceImpl implements IDeviceService {
// 删除定时任务 // 删除定时任务
deviceJobService.deleteJobByDeviceIds(new Long[]{deviceId}); deviceJobService.deleteJobByDeviceIds(new Long[]{deviceId});
// 批量删除设备日志 // 批量删除设备日志
logService.deleteDeviceLogByDeviceId(deviceId); logService.deleteDeviceLogByDeviceNumber(device.getSerialNumber());
// TODO 删除设备告警记录 // TODO 删除设备告警记录
// 删除设备 // 删除设备

View File

@@ -19,28 +19,55 @@ import java.util.List;
@Repository @Repository
public interface TDDeviceLogDAO { public interface TDDeviceLogDAO {
/***
* 创建数据库
*/
int createDB( String database); int createDB( String database);
/***
* 创建超级表
*/
int createSTable(String database); int createSTable(String database);
int createTable(String database,String deviceId);
/***
* 新增设备日志
*/
int save(@Param("database") String database,@Param("device") DeviceLog deviceLog); int save(@Param("database") String database,@Param("device") DeviceLog deviceLog);
DeviceStatistic selectCategoryLogCount(@Param("database") String database, Device device); /***
* 设备属性数据总数
*/
Long selectPropertyLogCount(@Param("database") String database,@Param("device") Device device);
List<DeviceLog> selectSTable(String database,DeviceLog deviceLog); /***
* 设备功能数据总数
*/
Long selectFunctionLogCount(@Param("database") String database,@Param("device") Device device);
int delete(String dbName, DeviceLog deviceLog); /***
* 设备事件数据总数
*/
Long selectEventLogCount(@Param("database") String database,@Param("device") Device device);
int deleteDeviceLogByDeviceId(String dbName,Long deviceId); /***
* 设备监测数据总数
// List<DeviceLog> selectLogListByPage(String dbName, Integer pageSize, Integer pageNum, String deviceNum, Date beginDate, Date endDate); */
Long selectMonitorLogCount(@Param("database") String database,@Param("device") Device device);
List<DeviceLog> selectLogList(String dbName, Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate);
/***
* 监测数据列表
*/
List<MonitorModel> selectMonitorList(@Param("database") String database, @Param("device") DeviceLog deviceLog); List<MonitorModel> selectMonitorList(@Param("database") String database, @Param("device") DeviceLog deviceLog);
/***
* 日志列表
*/
List<DeviceLog> selectDeviceLogList(@Param("database") String database,@Param("device") DeviceLog deviceLog); List<DeviceLog> selectDeviceLogList(@Param("database") String database,@Param("device") DeviceLog deviceLog);
/***
* 根据设备ID删除设备日志
*/
int deleteDeviceLogByDeviceNumber(@Param("database")String dbName,@Param("serialNumber") String serialNumber);
} }

View File

@@ -70,10 +70,9 @@ public class ApplicationStarted implements ApplicationRunner {
createDatabase(); createDatabase();
//创建数据库表 //创建数据库表
deviceLogMapper.createSTable(dbName); deviceLogMapper.createSTable(dbName);
System.out.println("完成超级表的创建");
LOGGER.info("完成超级表的创建"); LOGGER.info("完成超级表的创建");
} catch (Exception e) { } catch (Exception e) {
LOGGER.info("错误",e.getMessage()); LOGGER.error("错误",e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -22,8 +22,8 @@ public interface ILogService {
/** 保存设备日志 **/ /** 保存设备日志 **/
int saveDeviceLog(DeviceLog deviceLog); int saveDeviceLog(DeviceLog deviceLog);
/** 根据设备ID删除设备日志 **/ /** 根据设备编号删除设备日志 **/
int deleteDeviceLogByDeviceId(Long deviceId); int deleteDeviceLogByDeviceNumber(String deviceNumber);
/** 设备属性、功能、事件总数 **/ /** 设备属性、功能、事件总数 **/
DeviceStatistic selectCategoryLogCount(Device device); DeviceStatistic selectCategoryLogCount(Device device);

View File

@@ -38,8 +38,8 @@ public class MySqlLogServiceImpl implements ILogService {
* @return * @return
*/ */
@Override @Override
public int deleteDeviceLogByDeviceId(Long deviceId) { public int deleteDeviceLogByDeviceNumber(String deviceNumber) {
return deviceLogMapper.deleteDeviceLogByDeviceId(deviceId); return deviceLogMapper.deleteDeviceLogByDeviceNumber(deviceNumber);
} }
/*** /***

View File

@@ -48,22 +48,22 @@ public class TdengineLogServiceImpl implements ILogService {
return tdDeviceLogDAO.save(dbName,deviceLog); return tdDeviceLogDAO.save(dbName,deviceLog);
} }
/***
* 根据设备ID删除设备日志
* @return
*/
@Override
public int deleteDeviceLogByDeviceId(Long deviceId) {
return tdDeviceLogDAO.deleteDeviceLogByDeviceId(dbName,deviceId);
}
/*** /***
* 设备属性、功能、事件和监测数据总数 * 设备属性、功能、事件和监测数据总数
* @return * @return
*/ */
@Override @Override
public DeviceStatistic selectCategoryLogCount(Device device){ public DeviceStatistic selectCategoryLogCount(Device device){
return tdDeviceLogDAO.selectCategoryLogCount(dbName,device); DeviceStatistic statistic=new DeviceStatistic();
Long property=tdDeviceLogDAO.selectPropertyLogCount(dbName,device);
Long function=tdDeviceLogDAO.selectFunctionLogCount(dbName,device);
Long event=tdDeviceLogDAO.selectEventLogCount(dbName,device);
Long monitor=tdDeviceLogDAO.selectMonitorLogCount(dbName,device);
statistic.setPropertyCount(property==null?0:property);
statistic.setFunctionCount(function==null?0:function);
statistic.setEventCount(event==null?0:event);
statistic.setMonitorCount(monitor==null?0:monitor);
return statistic;
} }
/*** /***
@@ -86,4 +86,13 @@ public class TdengineLogServiceImpl implements ILogService {
} }
return tdDeviceLogDAO.selectMonitorList(dbName,deviceLog); return tdDeviceLogDAO.selectMonitorList(dbName,deviceLog);
} }
/***
* 根据设备ID删除设备日志
* @return
*/
@Override
public int deleteDeviceLogByDeviceNumber(String deviceNumber) {
return tdDeviceLogDAO.deleteDeviceLogByDeviceNumber(dbName,deviceNumber);
}
} }

View File

@@ -167,7 +167,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</foreach> </foreach>
</delete> </delete>
<delete id="deleteDeviceLogByDeviceId" parameterType="Long"> <delete id="deleteDeviceLogByDeviceNumber" parameterType="String">
delete from iot_device_log where device_id = #{deviceId} delete from iot_device_log where serial_number = #{deviceNumber}
</delete> </delete>
</mapper> </mapper>

View File

@@ -4,36 +4,26 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO"> <mapper namespace="com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO">
<resultMap id="BaseResultMap" type="com.ruoyi.iot.domain.DeviceLog">
<id column="ts" property="ts"/>
<result column="log_id" property="logId"/>
<result column="device_name" property="deviceName"/>
<result column="serial_number" property="serialNumber"/>
<result column="log_type" property="logType"/>
<result column="log_value" property="logValue"/>
<result column="identity" property="identity"/>
<result column="is_monitor" property="isMonitor"/>
<result column="create_by" property="createBy"/>
<result column="create_time" property="createTime"/>
<result column="remark" property="remark"/>
</resultMap>
<resultMap type="com.ruoyi.iot.model.MonitorModel" id="MonitorResult"> <resultMap type="com.ruoyi.iot.model.MonitorModel" id="MonitorResult">
<result property="value" column="log_value" /> <result property="value" column="log_value" />
<result property="time" column="create_time" /> <result property="time" column="ts" />
</resultMap> </resultMap>
<resultMap type="com.ruoyi.iot.domain.DeviceLog" id="DeviceLogResult"> <resultMap type="com.ruoyi.iot.domain.DeviceLog" id="DeviceLogResult">
<result property="logId" column="log_id" />
<result property="logType" column="log_type" /> <result property="logType" column="log_type" />
<result property="logValue" column="log_value" /> <result property="logValue" column="log_value" />
<result property="mode" column="mode" />
<result property="deviceId" column="device_id" /> <result property="deviceId" column="device_id" />
<result property="deviceName" column="device_name" /> <result property="deviceName" column="device_name" />
<result property="serialNumber" column="serial_number" /> <result property="serialNumber" column="serial_number" />
<result property="identity" column="identity" /> <result property="identity" column="identity" />
<result property="createBy" column="create_by" /> <result property="createBy" column="create_by" />
<result property="isMonitor" column="is_monitor" /> <result property="isMonitor" column="is_monitor" />
<result property="createTime" column="create_time" /> <result property="createTime" column="ts" />
<result property="userId" column="user_id" />
<result property="userName" column="user_name" />
<result property="tenantId" column="tenant_id" />
<result property="tenantName" column="tenant_name" />
<result property="remark" column="remark" /> <result property="remark" column="remark" />
</resultMap> </resultMap>
@@ -43,124 +33,93 @@
<update id="createSTable"> <update id="createSTable">
create STABLE if not exists ${database}.device_log create STABLE if not exists ${database}.device_log
( (ts timestamp,
ts timestamp,
log_id BIGINT,
log_type TINYINT,
`identity` BINARY(100),
`log_value` BINARY(100), `log_value` BINARY(100),
is_monitor TINYINT, is_monitor TINYINT,
create_by BINARY(100), log_type TINYINT,
create_time timestamp, `identity` BINARY(100),
remark BINARY(500) mode TINYINT,
remark BINARY(500))
)
TAGS( TAGS(
device_id BIGINT, device_id BIGINT,
device_name BINARY(100), serial_number BINARY(50),
serial_number BINARY(50)); user_id BIGINT,
tenant_Id BIGINT,
user_name BINARY(100),
tenant_name BINARY(100),
device_name BINARY(100));
</update> </update>
<update id="createTable">
create TABLE if not exists ${database}.${tableName}
USING ${database}.device_${deviceId} TAGS(log_type,is_monitor,create_by,create_time,remark)
(ts2 timestamp,
log_type BINARY(100)
);
</update>
<select id="selectSTable" resultMap="BaseResultMap">
select
*
from ${database}.device_log
order by ts desc
</select>
<select id="selectCategoryLogCount" parameterType="Long" resultType="com.ruoyi.iot.model.DeviceStatistic">
SELECT
(select count(log_id)
from iot_device_log
<where>
<if test="1==1"> and log_type=1</if>
<if test="userId != null and userId != 0"> and user_id = #{userId}</if>
<if test="tenantId != null and tenantId != 0"> and tenant_id = #{tenantId}</if>
</where>
) as propertyCount,
(select count(log_id)
from iot_device_log
<where>
<if test="1==1"> and log_type=2</if>
<if test="userId != null and userId != 0"> and user_id = #{userId}</if>
<if test="tenantId != null and tenantId != 0"> and tenant_id = #{tenantId}</if>
</where>
) as functionCount,
(select count(log_id)
from iot_device_log
<where>
<if test="1==1"> and log_type=3</if>
<if test="userId != null and userId != 0"> and user_id = #{userId}</if>
<if test="tenantId != null and tenantId != 0"> and tenant_id = #{tenantId}</if>
</where>
) as eventCount,
(select count(log_id)
from iot_device_log
<where>
<if test="1==1"> and log_type=1 and is_monitor=1</if>
<if test="userId != null and userId != 0"> and user_id = #{userId}</if>
<if test="tenantId != null and tenantId != 0"> and tenant_id = #{tenantId}</if>
</where>
) as monitorCount
from ${database}.device_log
limit 1
</select>
<insert id="save" parameterType="com.ruoyi.iot.domain.DeviceLog"> <insert id="save" parameterType="com.ruoyi.iot.domain.DeviceLog">
INSERT INTO ${database}.device_${device.serialNumber} USING device_log INSERT INTO ${database}.${device.serialNumber} USING device_log
TAGS (#{device.deviceId},#{device.deviceName},#{device.serialNumber}) TAGS (#{device.deviceId},
VALUES (now, #{device.logId}, #{device.logType},#{device.identity},#{device.logValue},#{device.isMonitor},#{device.createBy},now,#{device.remark} ); #{device.serialNumber},
#{device.userId},
#{device.tenantId},
#{device.userName},
#{device.tenantName},
#{device.deviceName})
VALUES (now,
#{device.logValue},
#{device.isMonitor},
#{device.logType},
#{device.identity},
#{device.mode},
#{device.remark});
</insert> </insert>
<delete id="delete" parameterType="com.ruoyi.iot.domain.DeviceLog"> <delete id="deleteDeviceLogByDeviceNumber" parameterType="com.ruoyi.iot.domain.DeviceLog">
delete from ${database}.device_${device.serialNumber} DROP TABLE IF EXISTS ${database}.${serialNumber};
<where>
<if test="logId !=null"> log_id = #{logId} </if>
<if test="deviceId !=null and deviceId!=''"> device_id = #{deviceId} </if>
<if test="serialNumber !=null and serialNumber!=''"> serial_number = #{serialNumber} </if>
</where>
</delete> </delete>
<delete id="deleteDeviceLogByDeviceId" parameterType="com.ruoyi.iot.domain.DeviceLog"> <select id="selectPropertyLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
delete from ${database}.device_${device.serialNumber} select count(mode) as propertyCount
from ${database}.device_log
<where> <where>
<if test="logId !=null"> log_id = #{logId} </if> <if test="1==1"> and log_type=1</if>
<if test="deviceId !=null and deviceId!=''"> device_id = #{deviceId} </if> <if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
<if test="serialNumber !=null and serialNumber!=''"> serial_number = #{serialNumber} </if> <if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
</where> </where>
</delete> </select>
<select id="selectFunctionLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
<select id="selectLogList" resultMap="BaseResultMap"> select count(mode) as functionCount
select * from ${database}.device_log from ${database}.device_log
<where> <where>
<if test="deviceId != null "> and device_id = #{deviceId}</if> <if test="1==1"> and log_type=2</if>
<if test="serialNumber != null and serialNumber!=''"> and serial_number = #{serialNumber}</if> <if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
<if test="isMonitor != null "> and is_monitor = #{isMonitor}</if> <if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
<if test="logType != null "> and log_type = #{logType}</if> </where>
</select>
<if test="beginDate != null "> and create_time &lt;= #{beginDate}</if>
<if test="endDate != null "> and create_time &gt;= #{endDate}</if> <select id="selectEventLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
select count(mode) as eventCount
from ${database}.device_log
<where>
<if test="1==1"> and log_type=3</if>
<if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
<if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
</where>
</select>
<select id="selectMonitorLogCount" parameterType="com.ruoyi.iot.domain.Device" resultType="Long">
select count(mode) as monitorCount
from ${database}.device_log
<where>
<if test="1==1"> and log_type=1 and is_monitor=1</if>
<if test="device.userId != null and device.userId != 0"> and user_id = #{device.userId}</if>
<if test="device.tenantId != null and device.tenantId != 0"> and tenant_id = #{device.tenantId}</if>
</where> </where>
order by ts desc
</select> </select>
<select id="selectMonitorList" parameterType="com.ruoyi.iot.domain.DeviceLog" resultMap="MonitorResult"> <select id="selectMonitorList" parameterType="com.ruoyi.iot.domain.DeviceLog" resultMap="MonitorResult">
select log_value, create_time from ${database}.device_log select log_value, ts from ${database}.device_log
<where> <where>
is_monitor=1 is_monitor=1
<if test="device.deviceId!=null"> and device_id = #{device.deviceId} </if> <if test="device.deviceId!=null"> and device_id = #{device.deviceId} </if>
<if test="device.identity != null and device.identity != ''"> and identity like #{device.identity}</if> <if test="device.identity != null and device.identity != ''"> and identity like #{device.identity}</if>
<if test="device.params.beginTime != null and device.params.beginTime != '' and device.params.endTime != null and device.params.endTime != ''"> and ts between #{device.params.beginTime} and #{device.params.endTime}</if>
order by ts desc order by ts desc
limit 2000 limit 2000
</where> </where>