Commit 8daa2131ba475a938e7be01b51a791662a69dc75

Authored by alwayssuper
1 parent 9f8c6a94

[fix]:iot home count

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/statistics/IotStatisticsController.java
1 1 package cn.iocoder.yudao.module.iot.controller.admin.statistics;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
4   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.product.IotProductRespVO;
5 4 import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO;
  5 +import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStateEnum;
6 6 import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
  7 +import cn.iocoder.yudao.module.iot.service.device.data.IotDeviceLogService;
7 8 import cn.iocoder.yudao.module.iot.service.product.IotProductCategoryService;
8 9 import cn.iocoder.yudao.module.iot.service.product.IotProductService;
9   -import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
10 10 import io.swagger.v3.oas.annotations.Operation;
11 11 import io.swagger.v3.oas.annotations.tags.Tag;
12 12 import jakarta.annotation.Resource;
... ... @@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
16 16 import org.springframework.web.bind.annotation.RestController;
17 17  
18 18 import java.time.LocalDateTime;
19   -import java.util.List;
20 19  
21 20  
22 21 @Tag(name = "管理后台 - IoT 数据统计")
... ... @@ -34,21 +33,40 @@ public class IotStatisticsController {
34 33 @Resource
35 34 private IotProductService iotProductService;
36 35  
  36 + @Resource
  37 + private IotDeviceLogService iotDeviceLogService;
  38 +
37 39  
38 40 @GetMapping("/count")
39   - @Operation(summary = "获取IOT首页的数据统计", description = "主要用于IOT首页的数据统计")
40   - public CommonResult<IotStatisticsRespVO> getSimpleProductList(){
  41 + @Operation(summary = "获取IOT首页的数据统计", description = "用于IOT首页的数据统计")
  42 + public CommonResult<IotStatisticsRespVO> getIotCount(){
41 43 IotStatisticsRespVO iotStatisticsRespVO = new IotStatisticsRespVO();
42 44 // 获取总数
43 45 iotStatisticsRespVO.setCategoryTotal(iotProductCategoryService.getProductCategoryCount(null));
44 46 iotStatisticsRespVO.setProductTotal(iotProductService.getProductCount(null));
45 47 iotStatisticsRespVO.setDeviceTotal(iotDeviceService.getDeviceCount(null));
  48 + iotStatisticsRespVO.setReportTotal(iotDeviceLogService.getDeviceLogCount(null));
46 49  
47 50 // 获取今日新增数量
48 51 LocalDateTime todayStart = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
49 52 iotStatisticsRespVO.setCategoryTodayTotal(iotProductCategoryService.getProductCategoryCount(todayStart));
50 53 iotStatisticsRespVO.setProductTodayTotal(iotProductService.getProductCount(todayStart));
51 54 iotStatisticsRespVO.setDeviceTodayTotal(iotDeviceService.getDeviceCount(todayStart));
  55 + iotStatisticsRespVO.setReportTodayTotal(iotDeviceLogService.getDeviceLogCount(todayStart));
  56 +
  57 + // 获取各个品类下设备数量统计
  58 + iotStatisticsRespVO.setDeviceStatsOfCategory(
  59 + iotProductCategoryService.getDeviceCountsOfProductCategory()
  60 + );
  61 +
  62 + // 获取设备状态数量统计
  63 + iotStatisticsRespVO.setOnlineTotal(iotDeviceService.getDeviceCountByState(IotDeviceStateEnum.ONLINE.getState()));
  64 + iotStatisticsRespVO.setOfflineTotal(iotDeviceService.getDeviceCountByState(IotDeviceStateEnum.OFFLINE.getState()));
  65 + iotStatisticsRespVO.setNeverOnlineTotal(iotDeviceService.getDeviceCountByState(IotDeviceStateEnum.INACTIVE.getState()));
  66 +
  67 + // 获取设备上下行消息数量统计
  68 + iotStatisticsRespVO.setDeviceUpMessageStats(iotDeviceLogService.getDeviceLogUpCountByHour(null,null,null));
  69 + iotStatisticsRespVO.setDeviceDownMessageStats(iotDeviceLogService.getDeviceLogDownCountByHour(null,null,null));
52 70  
53 71 return CommonResult.success(iotStatisticsRespVO);
54 72 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDeviceLogMapper.java
1 1 package cn.iocoder.yudao.module.iot.dal.tdengine;
2 2  
3 3 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogPageReqVO;
  4 +import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO;
4 5 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
5 6 import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
6 7 import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
... ... @@ -8,6 +9,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
8 9 import org.apache.ibatis.annotations.Mapper;
9 10 import org.apache.ibatis.annotations.Param;
10 11  
  12 +import java.time.LocalDateTime;
  13 +import java.util.List;
  14 +
11 15 /**
12 16 * 设备日志 {@link IotDeviceLogDO} Mapper 接口
13 17 */
... ... @@ -46,4 +50,36 @@ public interface IotDeviceLogMapper {
46 50 IPage<IotDeviceLogDO> selectPage(IPage<IotDeviceLogDO> page,
47 51 @Param("reqVO") IotDeviceLogPageReqVO reqVO);
48 52  
  53 + /**
  54 + * 统计设备日志数量
  55 + *
  56 + * @param createTime 创建时间,如果为空,则统计所有日志数量
  57 + * @return 日志数量
  58 + */
  59 + Long selectCountByCreateTime(@Param("createTime") Long createTime);
  60 +
  61 + /**
  62 + * 获得每个小时设备上行消息数量统计
  63 + *
  64 + * @param deviceKey 设备标识
  65 + * @param startTime 开始时间
  66 + * @param endTime 结束时间
  67 + * @return 每小时消息数量统计
  68 + */
  69 + List<IotStatisticsRespVO.TimeData> selectDeviceLogUpCountByHour(@Param("deviceKey") String deviceKey,
  70 + @Param("startTime") Long startTime,
  71 + @Param("endTime") Long endTime);
  72 +
  73 + /**
  74 + * 获得每个小时设备下行消息数量统计
  75 + *
  76 + * @param deviceKey 设备标识
  77 + * @param startTime 开始时间
  78 + * @param endTime 结束时间
  79 + * @return 每小时消息数量统计
  80 + */
  81 + List<IotStatisticsRespVO.TimeData> selectDeviceLogDownCountByHour(@Param("deviceKey") String deviceKey,
  82 + @Param("startTime") Long startTime,
  83 + @Param("endTime") Long endTime);
  84 +
49 85 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java
... ... @@ -135,6 +135,22 @@ public interface IotDeviceService {
135 135 List<IotDeviceDO> getDeviceListByState(Integer state);
136 136  
137 137 /**
  138 + * 根据产品ID获取设备列表
  139 + *
  140 + * @param productId 产品ID,用于查询特定产品的设备列表
  141 + * @return 返回与指定产品ID关联的设备列表,列表中的每个元素为IotDeviceDO对象
  142 + */
  143 + List<IotDeviceDO> getDeviceListByProductId(Long productId);
  144 +
  145 + /**
  146 + * 根据设备ID列表获取设备信息列表
  147 + *
  148 + * @param deviceIdList 设备ID列表,包含需要查询的设备ID
  149 + * @return 返回与设备ID列表对应的设备信息列表,列表中的每个元素为IotDeviceDO对象
  150 + */
  151 + List<IotDeviceDO> getDeviceListByIdList(List<Long> deviceIdList);
  152 +
  153 + /**
138 154 * 基于产品编号,获得设备数量
139 155 *
140 156 * @param productId 产品编号
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDeviceLogService.java
... ... @@ -2,8 +2,13 @@ package cn.iocoder.yudao.module.iot.service.device.data;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 4 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogPageReqVO;
  5 +import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO;
5 6 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
6 7 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
  8 +import org.apache.ibatis.annotations.Param;
  9 +
  10 +import java.time.LocalDateTime;
  11 +import java.util.List;
7 12  
8 13 /**
9 14 * IoT 设备日志数据 Service 接口
... ... @@ -34,4 +39,32 @@ public interface IotDeviceLogService {
34 39 */
35 40 PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO);
36 41  
  42 + /**
  43 + * 获得设备日志数量
  44 + *
  45 + * @param createTime 创建时间,如果为空,则统计所有日志数量
  46 + * @return 日志数量
  47 + */
  48 + Long getDeviceLogCount(LocalDateTime createTime);
  49 +
  50 + /**
  51 + * 获得每个小时设备上行消息数量统计
  52 + *
  53 + * @param deviceKey 设备标识,如果为空,则统计所有设备
  54 + * @param startTime 开始时间,如果为空,则不限制开始时间
  55 + * @param endTime 结束时间,如果为空,则不限制结束时间
  56 + * @return 每小时消息数量统计列表
  57 + */
  58 + List<IotStatisticsRespVO.TimeData> getDeviceLogUpCountByHour(String deviceKey, Long startTime, Long endTime);
  59 +
  60 + /**
  61 + * 获得每个小时设备下行消息数量统计
  62 + *
  63 + * @param deviceKey 设备标识,如果为空,则统计所有设备
  64 + * @param startTime 开始时间,如果为空,则不限制开始时间
  65 + * @param endTime 结束时间,如果为空,则不限制结束时间
  66 + * @return 每小时消息数量统计列表
  67 + */
  68 + List<IotStatisticsRespVO.TimeData> getDeviceLogDownCountByHour( String deviceKey, Long startTime, Long endTime);
  69 +
37 70 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDeviceLogServiceImpl.java
... ... @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 6 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
7 7 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
8 8 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogPageReqVO;
  9 +import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO;
9 10 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
10 11 import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogMapper;
11 12 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
... ... @@ -16,6 +17,11 @@ import lombok.extern.slf4j.Slf4j;
16 17 import org.springframework.stereotype.Service;
17 18 import org.springframework.validation.annotation.Validated;
18 19  
  20 +import java.time.LocalDateTime;
  21 +import java.time.ZoneOffset;
  22 +import java.util.ArrayList;
  23 +import java.util.List;
  24 +
19 25 /**
20 26 * IoT 设备日志数据 Service 实现类
21 27 *
... ... @@ -63,4 +69,37 @@ public class IotDeviceLogServiceImpl implements IotDeviceLogService {
63 69 }
64 70 }
65 71  
  72 + @Override
  73 + public Long getDeviceLogCount(LocalDateTime createTime) {
  74 + Long time = null;
  75 + if (createTime != null) {
  76 + time = createTime.toInstant(ZoneOffset.UTC).toEpochMilli();
  77 + }
  78 + return deviceLogMapper.selectCountByCreateTime(time);
  79 + }
  80 +
  81 + @Override
  82 + public List<IotStatisticsRespVO.TimeData> getDeviceLogUpCountByHour(String deviceKey, Long startTime, Long endTime) {
  83 + try {
  84 + return deviceLogMapper.selectDeviceLogUpCountByHour(deviceKey, startTime, endTime);
  85 + } catch (Exception exception) {
  86 + if (exception.getMessage().contains("Table does not exist")) {
  87 + return new ArrayList<>();
  88 + }
  89 + throw exception;
  90 + }
  91 + }
  92 +
  93 + @Override
  94 + public List<IotStatisticsRespVO.TimeData> getDeviceLogDownCountByHour(String deviceKey, Long startTime, Long endTime) {
  95 + try {
  96 + return deviceLogMapper.selectDeviceLogDownCountByHour(deviceKey, startTime, endTime);
  97 + } catch (Exception exception) {
  98 + if (exception.getMessage().contains("Table does not exist")) {
  99 + return new ArrayList<>();
  100 + }
  101 + throw exception;
  102 + }
  103 + }
  104 +
66 105 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryService.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.service.product;
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 4 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProductCategoryPageReqVO;
5 5 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProductCategorySaveReqVO;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO;
6 7 import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
7 8 import jakarta.validation.Valid;
8 9  
... ... @@ -92,12 +93,11 @@ public interface IotProductCategoryService {
92 93 */
93 94 Long getProductCategoryCount(LocalDateTime createTime);
94 95  
95   -
96 96 /**
97   - * 获得各产品分类下属的设备总数
  97 + * 获得各个品类下设备数量统计
98 98 *
99   - * @return 产品分类名称和各产品分类下属的设备总数
  99 + * @return 品类设备统计列表
100 100 */
101   - Map<String,Integer> getDeviceCountsOfProductCategoryMap();
  101 + List<IotStatisticsRespVO.DataItem> getDeviceCountsOfProductCategory();
102 102  
103 103 }
104 104 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryServiceImpl.java
... ... @@ -5,11 +5,13 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 5 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
6 6 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProductCategoryPageReqVO;
7 7 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProductCategorySaveReqVO;
  8 +import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO;
8 9 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
9 10 import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
10 11 import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
11 12 import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductCategoryMapper;
12 13 import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
  14 +import cn.iocoder.yudao.module.iot.service.product.IotProductCategoryService;
13 15 import jakarta.annotation.Resource;
14 16 import org.springframework.stereotype.Service;
15 17 import org.springframework.validation.annotation.Validated;
... ... @@ -19,6 +21,7 @@ import java.util.Collection;
19 21 import java.util.HashMap;
20 22 import java.util.List;
21 23 import java.util.Map;
  24 +import java.util.stream.Collectors;
22 25  
23 26 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
24 27 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_CATEGORY_NOT_EXISTS;
... ... @@ -101,7 +104,7 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
101 104 }
102 105  
103 106 @Override
104   - public Map<String, Integer> getDeviceCountsOfProductCategoryMap() {
  107 + public List<IotStatisticsRespVO.DataItem> getDeviceCountsOfProductCategory() {
105 108 // 1. 获取所有数据
106 109 List<IotProductCategoryDO> categoryList = productCategoryMapper.selectList();
107 110 List<IotProductDO> productList = productService.getProductList();
... ... @@ -137,8 +140,16 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
137 140 categoryDeviceCountMap.merge(categoryName, 1, Integer::sum);
138 141 }
139 142 }
140   -
141   - return categoryDeviceCountMap;
  143 +
  144 + // 3. 转换为 DataItem 列表
  145 + return categoryDeviceCountMap.entrySet().stream()
  146 + .map(entry -> {
  147 + IotStatisticsRespVO.DataItem dataItem = new IotStatisticsRespVO.DataItem();
  148 + dataItem.setName(entry.getKey());
  149 + dataItem.setValue(entry.getValue());
  150 + return dataItem;
  151 + })
  152 + .collect(Collectors.toList());
142 153 }
143 154  
144 155 }
145 156 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceLogMapper.xml
... ... @@ -55,4 +55,68 @@
55 55 ORDER BY ts DESC
56 56 </select>
57 57  
  58 + <select id="selectCountByCreateTime" resultType="Long">
  59 + SELECT COUNT(*)
  60 + FROM device_log
  61 + <where>
  62 + <if test="createTime != null">
  63 + AND ts >= #{createTime}
  64 + </if>
  65 + </where>
  66 + </select>
  67 +
  68 + <select id="selectDeviceLogUpCountByHour" resultType="cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO$TimeData">
  69 + SELECT
  70 + TIMETRUNCATE(ts, 1h) as time,
  71 + COUNT(*) as data
  72 + FROM
  73 + <choose>
  74 + <when test="deviceKey != null and deviceKey != ''">
  75 + device_log_${deviceKey}
  76 + </when>
  77 + <otherwise>
  78 + device_log
  79 + </otherwise>
  80 + </choose>
  81 + <where>
  82 + <if test="startTime != null">
  83 + AND ts >= #{startTime}
  84 + </if>
  85 + <if test="endTime != null">
  86 + AND ts &lt;= #{endTime}
  87 + </if>
  88 + AND (
  89 + identifier IN ('online', 'offline', 'pull', 'progress', 'report', 'register', 'register_sub')
  90 + )
  91 + </where>
  92 + GROUP BY TIMETRUNCATE(ts, 1h)
  93 + ORDER BY time ASC
  94 + </select>
  95 +
  96 + <select id="selectDeviceLogDownCountByHour" resultType="cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO$TimeData">
  97 + SELECT
  98 + TIMETRUNCATE(ts, 1h) as time,
  99 + COUNT(*) as data
  100 + FROM
  101 + <choose>
  102 + <when test="deviceKey != null and deviceKey != ''">
  103 + device_log_${deviceKey}
  104 + </when>
  105 + <otherwise>
  106 + device_log
  107 + </otherwise>
  108 + </choose>
  109 + <where>
  110 + <if test="startTime != null">
  111 + AND ts >= #{startTime}
  112 + </if>
  113 + <if test="endTime != null">
  114 + AND ts &lt;= #{endTime}
  115 + </if>
  116 + AND identifier IN ('set', 'get', 'upgrade', 'unregister_sub', 'topology_add')
  117 + </where>
  118 + GROUP BY TIMETRUNCATE(ts, 1h)
  119 + ORDER BY time ASC
  120 + </select>
  121 +
58 122 </mapper>
59 123 \ No newline at end of file
... ...