【代码评审】IoT:首页统计

This commit is contained in:
YunaiV 2025-02-27 13:30:39 +08:00
parent 2a65e3bd2e
commit 6e1ec8b3eb
17 changed files with 90 additions and 75 deletions

View File

@ -19,7 +19,6 @@ import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@Tag(name = "管理后台 - IoT 数据统计")
@RestController
@RequestMapping("/iot/statistics")
@ -27,48 +26,47 @@ import java.time.LocalDateTime;
public class IotStatisticsController {
@Resource
private IotDeviceService iotDeviceService;
private IotDeviceService deviceService;
@Resource
private IotProductCategoryService iotProductCategoryService;
private IotProductCategoryService productCategoryService;
@Resource
private IotProductService iotProductService;
private IotProductService productService;
@Resource
private IotDeviceLogService iotDeviceLogService;
private IotDeviceLogService deviceLogService;
// TODO @superdescription 非必要可以不写哈
@GetMapping("/main")
@Operation(summary = "获取IOT首页的数据统计", description = "用于IOT首页的数据统计")
@Operation(summary = "获取首页的数据统计", description = "用于IOT首页的数据统计")
public CommonResult<IotStatisticsRespVO> getIotMainStats(@Valid IotStatisticsReqVO reqVO){
// TODO @super新增 get-summary 接口返回总数今日新增数量状态
IotStatisticsRespVO iotStatisticsRespVO = new IotStatisticsRespVO();
// 获取总数
iotStatisticsRespVO.setCategoryTotal(iotProductCategoryService.getProductCategoryCount(null));
iotStatisticsRespVO.setProductTotal(iotProductService.getProductCount(null));
iotStatisticsRespVO.setDeviceTotal(iotDeviceService.getDeviceCount(null));
iotStatisticsRespVO.setReportTotal(iotDeviceLogService.getDeviceLogCount(null));
iotStatisticsRespVO.setCategoryTotal(productCategoryService.getProductCategoryCount(null));
iotStatisticsRespVO.setProductTotal(productService.getProductCount(null));
iotStatisticsRespVO.setDeviceTotal(deviceService.getDeviceCount(null));
iotStatisticsRespVO.setReportTotal(deviceLogService.getDeviceLogCount(null));
// 获取今日新增数量
LocalDateTime todayStart = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
iotStatisticsRespVO.setCategoryTodayTotal(iotProductCategoryService.getProductCategoryCount(todayStart));
iotStatisticsRespVO.setProductTodayTotal(iotProductService.getProductCount(todayStart));
iotStatisticsRespVO.setDeviceTodayTotal(iotDeviceService.getDeviceCount(todayStart));
iotStatisticsRespVO.setReportTodayTotal(iotDeviceLogService.getDeviceLogCount(todayStart));
iotStatisticsRespVO.setCategoryTodayTotal(productCategoryService.getProductCategoryCount(todayStart));
iotStatisticsRespVO.setProductTodayTotal(productService.getProductCount(todayStart));
iotStatisticsRespVO.setDeviceTodayTotal(deviceService.getDeviceCount(todayStart));
iotStatisticsRespVO.setReportTodayTotal(deviceLogService.getDeviceLogCount(todayStart));
// 获取各个品类下设备数量统计
iotStatisticsRespVO.setDeviceStatsOfCategory(
iotProductCategoryService.getDeviceCountsOfProductCategory()
productCategoryService.getDeviceCountsOfProductCategory()
);
// 获取设备状态数量统计
iotStatisticsRespVO.setOnlineTotal(iotDeviceService.getDeviceCountByState(IotDeviceStateEnum.ONLINE.getState()));
iotStatisticsRespVO.setOfflineTotal(iotDeviceService.getDeviceCountByState(IotDeviceStateEnum.OFFLINE.getState()));
iotStatisticsRespVO.setNeverOnlineTotal(iotDeviceService.getDeviceCountByState(IotDeviceStateEnum.INACTIVE.getState()));
iotStatisticsRespVO.setOnlineTotal(deviceService.getDeviceCountByState(IotDeviceStateEnum.ONLINE.getState()));
iotStatisticsRespVO.setOfflineTotal(deviceService.getDeviceCountByState(IotDeviceStateEnum.OFFLINE.getState()));
iotStatisticsRespVO.setNeverOnlineTotal(deviceService.getDeviceCountByState(IotDeviceStateEnum.INACTIVE.getState()));
// TODO @super新增 get-log-summary 接口返回
// 根据传入时间范围获取设备上下行消息数量统计
iotStatisticsRespVO.setDeviceUpMessageStats(iotDeviceLogService.getDeviceLogUpCountByHour(null, reqVO.getStartTime(), reqVO.getEndTime()));
iotStatisticsRespVO.setDeviceDownMessageStats(iotDeviceLogService.getDeviceLogDownCountByHour(null, reqVO.getStartTime(), reqVO.getEndTime()));
iotStatisticsRespVO.setDeviceUpMessageStats(deviceLogService.getDeviceLogUpCountByHour(null, reqVO.getStartTime(), reqVO.getEndTime()));
iotStatisticsRespVO.setDeviceDownMessageStats(deviceLogService.getDeviceLogDownCountByHour(null, reqVO.getStartTime(), reqVO.getEndTime()));
return CommonResult.success(iotStatisticsRespVO);
}

View File

@ -7,10 +7,16 @@ import lombok.Data;
@Schema(description = "管理后台 - Iot统计 Request VO")
@Data
public class IotStatisticsReqVO {
// TODO @suppertimes 直接传递哈
// TODO 2superprivate 不要丢了
@Schema(description = "查询起始时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "177")
@NotNull(message = "查询起始时间不能为空")
Long startTime;
@Schema(description = "查询结束时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "177")
@NotNull(message = "查询结束时间不能为空")
Long endTime;
}

View File

@ -5,10 +5,16 @@ import lombok.Data;
import java.util.List;
@Schema(description = "管理后台 - Iot统计 Response VO")
// TODO @superTotal 全部改成 Count
// TODO @superIotStatisticsSummaryRespVO
/**
* 管理后台 - Iot 统计 Response VO
*/
@Schema(description = "管理后台 - Iot 统计 Response VO")
@Data
public class IotStatisticsRespVO {
// TODO @superproductCategory
@Schema(description = "品类数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private long categoryTotal;
@ -18,9 +24,11 @@ public class IotStatisticsRespVO {
@Schema(description = "设备数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
private long deviceTotal;
// TODO @superdeviceMessageCount设备消息数量
@Schema(description = "上报数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
private long reportTotal;
// TODO @superproductCategory
@Schema(description = "今日新增品类数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private long categoryTodayTotal;
@ -30,30 +38,42 @@ public class IotStatisticsRespVO {
@Schema(description = "今日新增设备数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
private long deviceTodayTotal;
// TODO @superdeviceMessageCount今日设备消息数量
@Schema(description = "今日新增上报数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
private long reportTodayTotal;
// TODO @superdeviceOnlineCount
@Schema(description = "在线数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "80")
private long onlineTotal;
// TODO @superdeviceOfflineCount
@Schema(description = "离线数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "15")
private long offlineTotal;
// TODO @superdeviceInactivECount
@Schema(description = "待激活设备数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "5")
private long neverOnlineTotal;
// TODO @super1类型改成 Mapkey 分类名value 设备数量2deviceStatsOfCategory => productCategoryDeviceCounts
@Schema(description = "按品类统计的设备数量")
private List<DataItem> deviceStatsOfCategory;
// TODO @super貌似界面里用不到这个字段
@Schema(description = "上报数据数量统计")
private List<TimeData> reportDataStats;
// TODO @superdeviceUpMessageStatsdeviceDownMessageStats 单独抽到 IotStatisticsDeviceMessageSummaryRespVO然后里面属性就是 upstreamCountsdownstreamCounts
@Schema(description = "上行数据数量统计")
private List<TimeData> deviceUpMessageStats;
@Schema(description = "下行数据数量统计")
private List<TimeData> deviceDownMessageStats;
@Schema(description = "按品类统计的设备数量")
private List<DataItem> deviceStatsOfCategory;
// TODO @super如果只有这两个字段使用 KeyValue 这个键值对
@Schema(description = "时间数据")
@Data
public static class TimeData {
@ -63,6 +83,7 @@ public class IotStatisticsRespVO {
@Schema(description = "数据值", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
private Object data;
}
@Schema(description = "数据项")
@ -74,5 +95,7 @@ public class IotStatisticsRespVO {
@Schema(description = "数据项值", requiredMode = Schema.RequiredMode.REQUIRED, example = "50")
private Object value;
}
}

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
@ -70,24 +71,12 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> {
.apply("FIND_IN_SET(" + groupId + ",group_ids) > 0"));
}
/**
* 统计设备数量
*
* @param createTime 创建时间如果为空则统计所有设备数量
* @return 设备数量
*/
default Long selectCountByCreateTime(LocalDateTime createTime) {
default Long selectCountByCreateTime(@Nullable LocalDateTime createTime) {
return selectCount(new LambdaQueryWrapperX<IotDeviceDO>()
.geIfPresent(IotDeviceDO::getCreateTime, createTime));
}
/**
* 统计指定状态的设备数量
*
* @param state 状态
* @return 设备数量
*/
default Long selectCountByState(Integer state) {
default Long selectCountByState(@Nullable Integer state) {
return selectCount(new LambdaQueryWrapperX<IotDeviceDO>()
.eqIfPresent(IotDeviceDO::getState, state));
}

View File

@ -6,8 +6,8 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProductCategoryPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
@ -30,13 +30,7 @@ public interface IotProductCategoryMapper extends BaseMapperX<IotProductCategory
return selectList(IotProductCategoryDO::getStatus, status);
}
/**
* 统计产品分类数量
*
* @param createTime 创建时间如果为空则统计所有分类数量
* @return 产品分类数量
*/
default Long selectCountByCreateTime(LocalDateTime createTime) {
default Long selectCountByCreateTime(@Nullable LocalDateTime createTime) {
return selectCount(new LambdaQueryWrapperX<IotProductCategoryDO>()
.geIfPresent(IotProductCategoryDO::getCreateTime, createTime));
}

View File

@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
@ -31,23 +32,11 @@ public interface IotProductMapper extends BaseMapperX<IotProductDO> {
.apply("LOWER(product_key) = {0}", productKey.toLowerCase()));
}
/**
* 统计产品数量
*
* @param createTime 创建时间如果为空则统计所有产品数量
* @return 产品数量
*/
default Long selectCountByCreateTime(LocalDateTime createTime) {
default Long selectCountByCreateTime(@Nullable LocalDateTime createTime) {
return selectCount(new LambdaQueryWrapperX<IotProductDO>()
.geIfPresent(IotProductDO::getCreateTime, createTime));
}
/**
* 获得产品列表基于分类编号
*
* @param categoryId 分类编号
* @return 产品列表
*/
default List<IotProductDO> selectListByCategoryId(Long categoryId) {
return selectList(new LambdaQueryWrapperX<IotProductDO>()
.eq(IotProductDO::getCategoryId, categoryId)

View File

@ -73,6 +73,7 @@ public interface IotThingModelMapper extends BaseMapperX<IotThingModelDO> {
IotThingModelDO::getName, name);
}
// TODO @super用不到删除下
/**
* 统计物模型数量
*

View File

@ -9,7 +9,6 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.List;
/**
@ -58,6 +57,8 @@ public interface IotDeviceLogMapper {
*/
Long selectCountByCreateTime(@Param("createTime") Long createTime);
// TODO @super1上行下行不写在 mapper 而是通过参数传递这样selectDeviceLogUpCountByHourselectDeviceLogDownCountByHour 可以合并
// TODO @super2不能只基于 identifier 来计算而是要 type + identifier 成对
/**
* 获得每个小时设备上行消息数量统计
*

View File

@ -192,7 +192,7 @@ public interface IotDeviceService {
* @param createTime 创建时间如果为空则统计所有设备数量
* @return 设备数量
*/
Long getDeviceCount(LocalDateTime createTime);
Long getDeviceCount(@Nullable LocalDateTime createTime);
/**
* 获得设备数量基于状态

View File

@ -429,6 +429,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
return deviceMapper.selectCountByCreateTime(createTime);
}
// TODO @super是不是 groupby 查询更高效不过 controller还是要考虑 null 的情况不过可以直接枚举 foreach 处理下
@Override
public Long getDeviceCountByState(Integer state) {
return deviceMapper.selectCountByState(state);

View File

@ -5,8 +5,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogP
import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsRespVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
import org.apache.ibatis.annotations.Param;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
@ -45,7 +45,7 @@ public interface IotDeviceLogService {
* @param createTime 创建时间如果为空则统计所有日志数量
* @return 日志数量
*/
Long getDeviceLogCount(LocalDateTime createTime);
Long getDeviceLogCount(@Nullable LocalDateTime createTime);
/**
* 获得每个小时设备上行消息数量统计
@ -55,7 +55,9 @@ public interface IotDeviceLogService {
* @param endTime 结束时间如果为空则不限制结束时间
* @return 每小时消息数量统计列表
*/
List<IotStatisticsRespVO.TimeData> getDeviceLogUpCountByHour(String deviceKey, Long startTime, Long endTime);
List<IotStatisticsRespVO.TimeData> getDeviceLogUpCountByHour(@Nullable String deviceKey,
@Nullable Long startTime,
@Nullable Long endTime);
/**
* 获得每个小时设备下行消息数量统计
@ -65,6 +67,8 @@ public interface IotDeviceLogService {
* @param endTime 结束时间如果为空则不限制结束时间
* @return 每小时消息数量统计列表
*/
List<IotStatisticsRespVO.TimeData> getDeviceLogDownCountByHour( String deviceKey, Long startTime, Long endTime);
List<IotStatisticsRespVO.TimeData> getDeviceLogDownCountByHour(@Nullable String deviceKey,
@Nullable Long startTime,
@Nullable Long endTime);
}

View File

@ -73,11 +73,13 @@ public class IotDeviceLogServiceImpl implements IotDeviceLogService {
public Long getDeviceLogCount(LocalDateTime createTime) {
Long time = null;
if (createTime != null) {
// todo @super1LocalDateTimeUtil.toEpochMilli(createTime);2直接表达式更简洁 time != null ? createTime.toInstant(ZoneOffset.UTC).toEpochMilli() : null;
time = createTime.toInstant(ZoneOffset.UTC).toEpochMilli();
}
return deviceLogMapper.selectCountByCreateTime(time);
}
// TODO @super加一个参数Boolean upstreamtrue 上行false 下行null 不过滤
@Override
public List<IotStatisticsRespVO.TimeData> getDeviceLogUpCountByHour(String deviceKey, Long startTime, Long endTime) {
try {

View File

@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.iot.controller.admin.statistics.vo.IotStatisticsR
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
import jakarta.validation.Valid;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
@ -91,8 +92,9 @@ public interface IotProductCategoryService {
* @param createTime 创建时间如果为空则统计所有分类数量
* @return 产品分类数量
*/
Long getProductCategoryCount(LocalDateTime createTime);
Long getProductCategoryCount(@Nullable LocalDateTime createTime);
// TODO @super1Map<ProductCategory, Count> 虽然有点怪哈然后 Controller 按需转换成 Map<String, Integer> 2名字 getProductCategoryDeviceCountMap 方法
/**
* 获得各个品类下设备数量统计
*

View File

@ -11,7 +11,6 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductCategoryMapper;
import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
import cn.iocoder.yudao.module.iot.service.product.IotProductCategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -108,6 +107,7 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
// 1. 获取所有数据
List<IotProductCategoryDO> categoryList = productCategoryMapper.selectList();
List<IotProductDO> productList = productService.getProductList();
// TODO @super不要 list 查询返回内存而是查询一个 Map<productId, count>
List<IotDeviceDO> deviceList = deviceService.getDeviceList();
// 2. 统计每个分类下的设备数量
@ -116,6 +116,7 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
// 2.1 初始化所有分类的计数为0
for (IotProductCategoryDO category : categoryList) {
categoryDeviceCountMap.put(category.getName(), 0);
// TODO @super直接这里面计算不用多个循环产品本身也不多不用构建 Map直接 filter 就好了
}
// 2.2 构建产品ID到分类的映射

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.iot.controller.admin.product.vo.product.IotProduc
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import jakarta.validation.Valid;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
@ -99,8 +100,9 @@ public interface IotProductService {
* @param createTime 创建时间如果为空则统计所有产品数量
* @return 产品数量
*/
Long getProductCount(LocalDateTime createTime);
Long getProductCount(@Nullable LocalDateTime createTime);
// TODO @super用不到的删除下哈
/**
* 获得产品列表基于分类编号
*

View File

@ -81,6 +81,7 @@ public interface IotThingModelService {
*/
List<IotThingModelDO> getThingModelList(IotThingModelListReqVO reqVO);
// TODO @super用不到删除下哈
/**
* 获得物模型数量
*

View File

@ -364,6 +364,7 @@ public class IotThingModelServiceImpl implements IotThingModelService {
return SpringUtil.getBean(getClass());
}
// TODO @super用不到删除下
@Override
public Long getThingModelCount(LocalDateTime createTime) {
return thingModelMapper.selectCountByCreateTime(createTime);