| | |
| | | package com.ycl.calculate; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.ycl.platform.domain.entity.CheckIndexCar; |
| | | import com.ycl.platform.domain.entity.TMonitor; |
| | | import com.ycl.platform.domain.result.HK.SnapshotDataMonitorResult; |
| | | import com.ycl.platform.mapper.CheckIndexCarMapper; |
| | | import com.ycl.platform.service.ICheckIndexCarService; |
| | | import com.ycl.platform.service.ITMonitorService; |
| | | import com.ycl.system.mapper.SysConfigMapper; |
| | | import com.ycl.system.entity.SysDictData; |
| | | import com.ycl.system.mapper.SysDictDataMapper; |
| | | import com.ycl.utils.DateUtils; |
| | | import constant.ApiConstants; |
| | | import constant.CheckConstants; |
| | | import constant.CheckSnapCountConstants; |
| | | import constant.CheckThreadConstants; |
| | | import constant.RedisConstant; |
| | | import enumeration.general.AreaDeptEnum; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | import utils.DateUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.time.LocalDate; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 计算车辆点位在线率、视图库对接稳定性 |
| | | * 计算车辆视图库对接稳定性 |
| | | * 抓拍数据量监测结果接口数据 |
| | | * 获取分省厅、区域的map<k,v> k为deptId或者Province_deptId |
| | | * 循环map计算点位在线率 |
| | | * 更新或新增 |
| | | */ |
| | | @Component |
| | | public class CarSnapshopDataCalculation extends IndexCalculationServe implements CalculationStrategy<SnapshotDataMonitorResult> { |
| | | @Slf4j |
| | | public class CarSnapshopDataCalculation extends IndexCalculationServe<SnapshotDataMonitorResult, CarSnapshopDataCalculation.AreaStats> implements CalculationStrategy<SnapshotDataMonitorResult> { |
| | | @Autowired |
| | | private CheckIndexCarMapper checkIndexCarMapper; |
| | | @Autowired |
| | | private SysConfigMapper sysConfigMapper; |
| | | @Autowired |
| | | private ITMonitorService monitorService; |
| | | @Autowired |
| | | private ICheckIndexCarService checkIndexCarService; |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | @Autowired |
| | | private SysDictDataMapper dictDataMapper; |
| | | |
| | | //区域车辆点位在线指标的内部类 |
| | | private static class AreaStats { |
| | | //区域车辆视图库对接稳定性的内部类 |
| | | protected static class AreaStats { |
| | | int totalSites = 0; |
| | | int offlineSites = 0; |
| | | int totalDataSum = 0; |
| | | //加权后的同期抓拍量 |
| | | int lastSnapCount = 0; |
| | | } |
| | | |
| | | @Override |
| | | public void calculate(List<SnapshotDataMonitorResult> list) { |
| | | if (CollectionUtils.isEmpty(list)) { |
| | | return; |
| | | //获取分区域的指标数量 |
| | | Map<String, CarSnapshopDataCalculation.AreaStats> areaStatsMap = getAreaStatsMap(list, Boolean.FALSE); |
| | | if (areaStatsMap == null) return; |
| | | |
| | | // 获取当前日期 |
| | | LocalDate today = LocalDate.now(); |
| | | // 获取本月的第一天 |
| | | LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth()); |
| | | // 判断今天是否是本月的第一天 |
| | | if (today.equals(firstDayOfMonth)) { |
| | | // 如果是,则清除Redis中记录中断次数的数据 |
| | | redisTemplate.delete(RedisConstant.Check_Car_ViewConnect); |
| | | } |
| | | |
| | | //获得国标码为key的设备map |
| | | Map<String, TMonitor> monitorMap = monitorService.list(new QueryWrapper<TMonitor>() |
| | | .in("serial_number", list.stream().map(SnapshotDataMonitorResult::getExternalIndexCode).collect(Collectors.toList()))) |
| | | .stream().collect(Collectors.toMap(TMonitor::getSerialNumber, Function.identity())); |
| | | //获取省厅国标码集合 |
| | | List<String> provinceIds = getProvince(); |
| | | |
| | | Map<String, AreaStats> areaStatsMap = new HashMap<>(); |
| | | for (SnapshotDataMonitorResult result : list) { |
| | | TMonitor monitor = monitorMap.get(result.getExternalIndexCode()); |
| | | if (monitor == null) continue; |
| | | |
| | | String deptId = monitor.getDeptId().toString(); |
| | | updateAreaStats(areaStatsMap, deptId, result); |
| | | |
| | | // 处理省厅数据 |
| | | if (!CollectionUtils.isEmpty(provinceIds) && provinceIds.contains(monitor.getSerialNumber())) { |
| | | String provinceKey = "Province_" + deptId; |
| | | updateAreaStats(areaStatsMap, provinceKey, result); |
| | | } |
| | | } |
| | | |
| | | //获取2022同期抓拍平均值 省厅、市局 |
| | | BigDecimal cityCountAvg = getAverageCount(CheckSnapCountConstants.City); |
| | | BigDecimal countyCountAvg = getAverageCount(CheckSnapCountConstants.County); |
| | | |
| | | //从字典获取同期抓拍量 |
| | | List<SysDictData> lastSnap = dictDataMapper.selectDictDataByType(CheckConstants.DICT_LAST_SNAP); |
| | | //根据各个区县点位数据对同期抓拍量做加权处理 |
| | | fillLastSnap(areaStatsMap, lastSnap); |
| | | // 查询是否index表已经存在今日数据 |
| | | List<CheckIndexCar> checkIndexCarList = checkIndexCarMapper.selectToday(DateUtils.getDate()); |
| | | List<CheckIndexCar> checkIndexCars = new ArrayList<>(); |
| | | areaStatsMap.forEach((deptId, stats) -> { |
| | | areaStatsMap.forEach((key, stats) -> { |
| | | if (stats.totalSites > 0) { |
| | | CheckIndexCar checkIndexCar = createOrUpdateCheckIndexCar(deptId, stats, cityCountAvg, countyCountAvg, checkIndexCarList); |
| | | CheckIndexCar checkIndexCar = createOrUpdateCheckIndexCar(key, stats, checkIndexCarList); |
| | | if (checkIndexCar != null) { |
| | | checkIndexCars.add(checkIndexCar); |
| | | } |
| | |
| | | checkIndexCarService.saveOrUpdateBatch(checkIndexCars); |
| | | } |
| | | |
| | | /** |
| | | * 累计总点位数、离线数、总抓拍量 |
| | | */ |
| | | private void updateAreaStats(Map<String, AreaStats> areaStatsMap, String key, SnapshotDataMonitorResult result) { |
| | | //返回对象的引用,如果不存在会放入新的key,value |
| | | AreaStats stats = areaStatsMap.computeIfAbsent(key, k -> new AreaStats()); |
| | | stats.totalSites++; |
| | | if (ApiConstants.HK_SnapCount_ResultType_Null == result.getResultType()) { |
| | | stats.offlineSites++; |
| | | } else { |
| | | stats.totalDataSum += result.getDataCount(); |
| | | //根据点位数量对同期抓拍量作加权处理 |
| | | private void fillLastSnap(Map<String, AreaStats> areaStatsMap, List<SysDictData> lastSnap) { |
| | | //省厅总点位数 |
| | | int provinceTotalSites = 0; |
| | | //区县总点位数 |
| | | int countyTotalSites = 0; |
| | | //同期省厅抓拍量 |
| | | int lastProvinceSnap = 0; |
| | | //同期区县抓拍量 |
| | | int lastCountySnap = 0; |
| | | //从字典里筛选省厅值 |
| | | Optional<SysDictData> first = lastSnap.stream().filter(sysDictData -> CheckConstants.SNAP_PROVINCE_CAR.equals(sysDictData.getDictLabel())).findFirst(); |
| | | if (first.isPresent()) { |
| | | lastProvinceSnap = Integer.parseInt(first.get().getDictValue()); |
| | | } |
| | | //从字典里筛选区县值 |
| | | Optional<SysDictData> first2 = lastSnap.stream().filter(sysDictData -> CheckConstants.SNAP_COUNTY_CAR.equals(sysDictData.getDictLabel())).findFirst(); |
| | | if (first2.isPresent()) { |
| | | lastCountySnap = Integer.parseInt(first2.get().getDictValue()); |
| | | } |
| | | log.info("省厅同期:{},区县同期:{}",lastProvinceSnap,lastCountySnap); |
| | | //计算总数 |
| | | for (Map.Entry<String, AreaStats> entry : areaStatsMap.entrySet()) { |
| | | String key = entry.getKey(); |
| | | AreaStats value = entry.getValue(); |
| | | if (key.startsWith(ApiConstants.Province)) { |
| | | provinceTotalSites += value.totalSites; |
| | | } else { |
| | | countyTotalSites += value.totalSites; |
| | | } |
| | | } |
| | | log.info("省厅总数:{},区县总数:{}",provinceTotalSites,countyTotalSites); |
| | | //加权操作,补充同期抓拍量 |
| | | for (Map.Entry<String, AreaStats> entry : areaStatsMap.entrySet()) { |
| | | String key = entry.getKey(); |
| | | AreaStats value = entry.getValue(); |
| | | //省厅 |
| | | if (key.startsWith(ApiConstants.Province)) { |
| | | if (value.totalSites == 0) { |
| | | value.lastSnapCount = 0; |
| | | } else { |
| | | BigDecimal result = new BigDecimal(value.totalSites) |
| | | .divide(new BigDecimal(provinceTotalSites), 2, RoundingMode.HALF_UP) |
| | | .multiply(new BigDecimal(lastProvinceSnap)); |
| | | value.lastSnapCount = result.setScale(0, RoundingMode.HALF_UP).intValue(); |
| | | } |
| | | } else { |
| | | //区县 |
| | | if (value.totalSites == 0) { |
| | | value.lastSnapCount = 0; |
| | | } else { |
| | | BigDecimal result = new BigDecimal(value.totalSites) |
| | | .divide(new BigDecimal(countyTotalSites), 2, RoundingMode.HALF_UP) |
| | | .multiply(new BigDecimal(lastCountySnap)); |
| | | value.lastSnapCount = result.setScale(0, RoundingMode.HALF_UP).intValue(); |
| | | } |
| | | } |
| | | log.info("计算的数据:总点位数:{},总抓拍量:{},同期抓拍量:{}",value.totalSites,value.totalDataSum,value.lastSnapCount); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取2022同期抓拍平均值 |
| | | * 累计总点位数、离线数、总抓拍量 |
| | | */ |
| | | private BigDecimal getAverageCount(String configKey) { |
| | | String count = sysConfigMapper.checkConfigKeyUnique(configKey).getConfigValue(); |
| | | return new BigDecimal(count) |
| | | .multiply(new BigDecimal(CheckSnapCountConstants.Multiply)) |
| | | .divide(new BigDecimal(CheckSnapCountConstants.CountyNum), 10, RoundingMode.HALF_UP) |
| | | .divide(new BigDecimal(LocalDate.now().getDayOfMonth()), 0, RoundingMode.HALF_UP); |
| | | @Override |
| | | public void updateAreaStats(Map<String, AreaStats> areaStatsMap, String key, SnapshotDataMonitorResult result) { |
| | | //返回对象的引用,如果不存在会放入新的key,value |
| | | AreaStats stats = areaStatsMap.computeIfAbsent(key, k -> new AreaStats()); |
| | | stats.totalSites++; |
| | | |
| | | if (ApiConstants.HK_SnapCount_ResultType_Null != result.getResultType()) { |
| | | stats.totalDataSum += result.getDataCount(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 车辆点位在线率和视图库对接稳定性 |
| | | * 车辆视图库对接稳定性 |
| | | */ |
| | | private CheckIndexCar createOrUpdateCheckIndexCar(String key, AreaStats stats, BigDecimal cityCountAvg, BigDecimal countyCountAvg, List<CheckIndexCar> checkIndexCarList) { |
| | | private CheckIndexCar createOrUpdateCheckIndexCar(String key, AreaStats stats, List<CheckIndexCar> checkIndexCarList) { |
| | | CheckIndexCar checkIndexCar = getCheckIndex(key, checkIndexCarList, CheckIndexCar.class); |
| | | if (checkIndexCar == null) { |
| | | return null; |
| | | } |
| | | //调用点位在线计算方法 |
| | | Map<String, Object> siteOnlineParam = new HashMap<>(); |
| | | siteOnlineParam.put("totalSites", stats.totalSites); |
| | | siteOnlineParam.put("offlineSites", stats.offlineSites); |
| | | BigDecimal siteOnline = siteOnline(siteOnlineParam); |
| | | checkIndexCar.setSiteOnline(siteOnline); |
| | | |
| | | //视图库对接稳定性 |
| | | BigDecimal avgCount = key.startsWith("Province_") ? cityCountAvg : countyCountAvg; |
| | | Map<String, Object> viewConnectParam = new HashMap<>(); |
| | | viewConnectParam.put("totalDataSum", stats.totalDataSum); |
| | | viewConnectParam.put("avgCount", avgCount); |
| | | BigDecimal viewConnectStability = viewConnectStability(viewConnectParam); |
| | | checkIndexCar.setViewConnectStability(viewConnectStability); |
| | | //Redis记录该区县当月无数据上传次数 |
| | | Integer noDateCount = (Integer) redisTemplate.opsForHash().get(RedisConstant.Check_Car_ViewConnect, key); |
| | | // 如果值为null,则初始化为0 |
| | | if (noDateCount == null) { |
| | | noDateCount = 0; |
| | | } |
| | | Double deductScore = 0.1 * noDateCount; |
| | | |
| | | if (stats.totalDataSum != 0) { |
| | | Map<String, Object> viewConnectParam = new HashMap<>(); |
| | | viewConnectParam.put("totalDataSum", stats.totalDataSum); |
| | | viewConnectParam.put("lastCount", new BigDecimal(stats.lastSnapCount).multiply(new BigDecimal(CheckConstants.MultiplyNum)).divide(new BigDecimal(LocalDate.now().getDayOfMonth()), 0, RoundingMode.HALF_UP)); |
| | | BigDecimal viewConnectStability = viewConnectStability(viewConnectParam); |
| | | viewConnectStability = viewConnectStability.subtract(new BigDecimal(deductScore)).max(BigDecimal.ZERO).min(BigDecimal.ONE); |
| | | checkIndexCar.setViewConnectStability(viewConnectStability); |
| | | } else { |
| | | noDateCount++; |
| | | } |
| | | // 将新的值放回Hash中 |
| | | redisTemplate.opsForHash().put(RedisConstant.Check_Car_ViewConnect, key, noDateCount); |
| | | return checkIndexCar; |
| | | } |
| | | } |