Commit d26981e9 authored by duanruiming's avatar duanruiming

[update] 实时监控->代码优化,添加路口关联缓存

parent 6b11008f
package net.wanji.opt.cache;
import lombok.extern.slf4j.Slf4j;
import net.wanji.common.framework.Constants;
import net.wanji.common.utils.tool.StringUtils;
import net.wanji.databus.dao.entity.RidInfoEntity;
import net.wanji.databus.dao.mapper.RidInfoMapper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author duanruiming
* @date 2023/03/07 13:44
*/
@Component
@Slf4j
public class CrossRidInfoCache implements CommandLineRunner {
@Resource
RidInfoMapper ridInfoMapper;
private static List<RidInfoEntity> ridInfoList = new ArrayList<>();
public List<RidInfoEntity> getRidInfoAllList() {
return ridInfoList;
}
public List<RidInfoEntity> getRidInfoListByCrossId(String startCrossId, String endCrossId) {
Stream<RidInfoEntity> stream = ridInfoList.stream();
if (StringUtils.isNotBlank(startCrossId)) {
stream = stream.filter(ridInfoEntity -> StringUtils.equalsIgnoreCase(startCrossId, ridInfoEntity.getStartCrossId()));
}
if (StringUtils.isNotBlank(endCrossId)) {
stream = stream.filter(ridInfoEntity -> StringUtils.equalsIgnoreCase(endCrossId, ridInfoEntity.getEndCrossId()));
}
return stream.collect(Collectors.toList());
}
@Override
public void run(String... args) throws Exception {
init();
}
public void init() {
List<RidInfoEntity> ridInfoEntities = ridInfoMapper.selectOne(Constants.SystemParam.NULL, Constants.SystemParam.NULL);
if (!CollectionUtils.isEmpty(ridInfoEntities)) {
ridInfoList.addAll(ridInfoEntities);
}
}
}
...@@ -13,7 +13,7 @@ public class CrossSchemeOptLogPO { ...@@ -13,7 +13,7 @@ public class CrossSchemeOptLogPO {
private int id; private int id;
private String crossId; private String crossId;
private String schemeNo; private Integer schemeId;
private int dirType; private int dirType;
private String turnType; private String turnType;
private int offset; private int offset;
...@@ -36,6 +36,9 @@ public class CrossSchemeOptLogPO { ...@@ -36,6 +36,9 @@ public class CrossSchemeOptLogPO {
private String optResultDesc; private String optResultDesc;
private int dataBatchTime; private int dataBatchTime;
private String restoreResult; private String restoreResult;
/**
* 目前诊断优化页面存储:场景策略方法参数,展示使用
*/
private String dataExtend; private String dataExtend;
} }
...@@ -5,15 +5,13 @@ import net.wanji.common.enums.CrossStatusEnum; ...@@ -5,15 +5,13 @@ import net.wanji.common.enums.CrossStatusEnum;
import net.wanji.common.framework.Constants; import net.wanji.common.framework.Constants;
import net.wanji.common.framework.rest.JsonViewObject; import net.wanji.common.framework.rest.JsonViewObject;
import net.wanji.databus.dao.entity.RidInfoEntity; import net.wanji.databus.dao.entity.RidInfoEntity;
import net.wanji.databus.dao.mapper.RidInfoMapper;
import net.wanji.databus.entity.develop.servicedevelop.develop.StatusCodeEnum; import net.wanji.databus.entity.develop.servicedevelop.develop.StatusCodeEnum;
import net.wanji.feign.pojo.vo.SchemeSendVO; import net.wanji.feign.pojo.vo.SchemeSendVO;
import net.wanji.feign.pojo.vo.SignalStatusVO; import net.wanji.feign.pojo.vo.SignalStatusVO;
import net.wanji.feign.service.UtcFeignClients; import net.wanji.feign.service.UtcFeignClients;
import net.wanji.opt.cache.CrossDirTurnPhaseCache; import net.wanji.opt.cache.CrossDirTurnPhaseCache;
import net.wanji.opt.cache.CrossRidInfoCache;
import net.wanji.opt.common.exception.OptServiceException; import net.wanji.opt.common.exception.OptServiceException;
import net.wanji.opt.dao.mapper.CrossPhaseMapper;
import net.wanji.opt.dao.mapper.CrossSchemeMapper;
import net.wanji.opt.dao.mapper.CrossSchemeOptLogMapper; import net.wanji.opt.dao.mapper.CrossSchemeOptLogMapper;
import net.wanji.opt.dto.CrossDirInfoDTO; import net.wanji.opt.dto.CrossDirInfoDTO;
import net.wanji.opt.dto.CrossPhaseDTO; import net.wanji.opt.dto.CrossPhaseDTO;
...@@ -51,12 +49,6 @@ import static net.wanji.opt.cache.CrossSchemeInfoCache.geteSchemePOCache; ...@@ -51,12 +49,6 @@ import static net.wanji.opt.cache.CrossSchemeInfoCache.geteSchemePOCache;
@Slf4j @Slf4j
public class CrossOptimizeServiceImpl implements CrossOptimizeService { public class CrossOptimizeServiceImpl implements CrossOptimizeService {
@Resource
CrossPhaseMapper crossPhaseMapper;
@Resource
CrossSchemeMapper crossSchemeMapper;
@Resource
RidInfoMapper ridInfoMapper;
@Resource @Resource
CrossSchemeOptLogMapper crossSchemeOptLogMapper; CrossSchemeOptLogMapper crossSchemeOptLogMapper;
@Resource @Resource
...@@ -64,6 +56,8 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService { ...@@ -64,6 +56,8 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
@Resource @Resource
CrossDirTurnPhaseCache crossDirTurnPhaseCache; CrossDirTurnPhaseCache crossDirTurnPhaseCache;
@Resource @Resource
CrossRidInfoCache crossRidInfoCache;
@Resource
SceneService sceneService; SceneService sceneService;
@Value("${crossOptParam.maxVehheadDist}") @Value("${crossOptParam.maxVehheadDist}")
...@@ -169,7 +163,7 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService { ...@@ -169,7 +163,7 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
// 下游路口通过可排队长度计算可加可减时间优化 // 下游路口通过可排队长度计算可加可减时间优化
// 获取下游路口 // 获取下游路口
List<RidInfoEntity> ridInfoEntities = ridInfoMapper.selectOne(crossId, ""); List<RidInfoEntity> ridInfoEntities = crossRidInfoCache.getRidInfoListByCrossId(crossId, Constants.SystemParam.NULL);
// 不存在下游路口,不做处理 // 不存在下游路口,不做处理
if (CollectionUtils.isEmpty(ridInfoEntities)) { if (CollectionUtils.isEmpty(ridInfoEntities)) {
continue; continue;
...@@ -237,7 +231,7 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService { ...@@ -237,7 +231,7 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
} }
// 判断路口是否是绿波 // 判断路口是否是绿波
// 获取上游路口 // 获取上游路口
List<RidInfoEntity> ridInfoEntities = ridInfoMapper.selectOne("", crossId); List<RidInfoEntity> ridInfoEntities = crossRidInfoCache.getRidInfoListByCrossId(Constants.SystemParam.NULL, crossId);
// 不存在上游路口,不做处理 // 不存在上游路口,不做处理
if (CollectionUtils.isEmpty(ridInfoEntities)) { if (CollectionUtils.isEmpty(ridInfoEntities)) {
continue; continue;
...@@ -424,12 +418,12 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService { ...@@ -424,12 +418,12 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
private void doExecuteCrossOpt(List<CrossTurnDataRealtimeDTO> crossTurnDataRealtimeDTOS, Map<String, CrossPhaseDTO> phaseMap, String crossId, String optType) throws Exception { private void doExecuteCrossOpt(List<CrossTurnDataRealtimeDTO> crossTurnDataRealtimeDTOS, Map<String, CrossPhaseDTO> phaseMap, String crossId, String optType) throws Exception {
// 计算路口各相位调整时长(秒) // 计算路口各相位调整时长(秒)
Map<String, Integer> phaseTimeOptResultMap = new HashMap<>(); Map<String, Integer> phaseTimeOptResultMap = new HashMap<>();
Map<String, Integer> timeOffsetPhaseMap = getTimeOffsetPhaseMap(phaseMap, crossTurnDataRealtimeDTOS); Map<String, Integer> phaseTimeOffset = getTimeOffsetPhaseMap(phaseMap, crossTurnDataRealtimeDTOS);
// 是否需要拆分相位 // 是否需要拆分相位
if (isSplitPhase(timeOffsetPhaseMap)) { if (isSplitPhase(phaseTimeOffset)) {
} else { } else {
phaseTimeOptResultMap = getPhaseTimeOptMap(timeOffsetPhaseMap); phaseTimeOptResultMap = getPhaseTimeOptMap(phaseTimeOffset);
} }
// 修改原始方案绿灯时间为优化后时间,方案下发 // 修改原始方案绿灯时间为优化后时间,方案下发
SchemeSendVO schemeSendVO = getSchemeSendVO(crossId, phaseMap, phaseTimeOptResultMap); SchemeSendVO schemeSendVO = getSchemeSendVO(crossId, phaseMap, phaseTimeOptResultMap);
...@@ -461,7 +455,7 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService { ...@@ -461,7 +455,7 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
CrossPhaseDTO crossPhaseDTO = item.getValue(); CrossPhaseDTO crossPhaseDTO = item.getValue();
BeanUtils.copyProperties(crossPhaseDTO, crossSchemeOptLogPO); BeanUtils.copyProperties(crossPhaseDTO, crossSchemeOptLogPO);
crossSchemeOptLogPO.setSchemeNo(String.valueOf(crossPhaseDTO.getSchemeId())); crossSchemeOptLogPO.setSchemeId(crossPhaseDTO.getSchemeId());
crossSchemeOptLogPO.setPhaseOrderId(crossPhaseDTO.getSort()); crossSchemeOptLogPO.setPhaseOrderId(crossPhaseDTO.getSort());
crossSchemeOptLogPO.setStartTime(new Date()); crossSchemeOptLogPO.setStartTime(new Date());
crossSchemeOptLogPO.setOptType("1");// 自动优化 crossSchemeOptLogPO.setOptType("1");// 自动优化
...@@ -470,7 +464,8 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService { ...@@ -470,7 +464,8 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
crossSchemeOptLogPO.setOptResult("1"); crossSchemeOptLogPO.setOptResult("1");
crossSchemeOptLogPO.setOptResultDesc(StatusCodeEnum.STATUS_00200.getDetail()); crossSchemeOptLogPO.setOptResultDesc(StatusCodeEnum.STATUS_00200.getDetail());
crossSchemeOptLogPO.setDataBatchTime((int) dataBatchTimeLong); crossSchemeOptLogPO.setDataBatchTime((int) dataBatchTimeLong);
crossSchemeOptLogPO.setDataExtend("");// todo 需要通过场景策略方法 // todo 测试数据
crossSchemeOptLogPO.setDataExtend("{\"sceneName\": \"测试JSON\",\"strategyName\": \"测试JSON\",\"ideaName\": \"测试JSON\"}");// todo 需要通过场景策略方法
String phaseNo = crossPhaseDTO.getPhaseNo(); String phaseNo = crossPhaseDTO.getPhaseNo();
for (Map.Entry<String, Integer> entry : phaseTimeOptResultMap.entrySet()) { for (Map.Entry<String, Integer> entry : phaseTimeOptResultMap.entrySet()) {
...@@ -603,39 +598,34 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService { ...@@ -603,39 +598,34 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
int subTimeSum = subTimeList.stream().mapToInt(Integer::intValue).sum(); int subTimeSum = subTimeList.stream().mapToInt(Integer::intValue).sum();
int addTimeSum = addTimeList.stream().mapToInt(Integer::intValue).sum(); int addTimeSum = addTimeList.stream().mapToInt(Integer::intValue).sum();
int countOffsetAdd = Math.abs(subTimeSum) >= addTimeSum ? addTimeSum : Math.abs(subTimeSum); int countOffsetAdd = Math.abs(subTimeSum) >= addTimeSum ? addTimeSum : Math.abs(subTimeSum);
int countOffsetSub = countOffsetAdd; int countOffsetSub = -countOffsetAdd;
// 可加可减时间为0,不需要计算相位时间 // 可加可减时间为0,不需要计算相位时间
if (countOffsetAdd == 0) { if (countOffsetAdd == 0) {
return null; return Collections.EMPTY_MAP;
} }
// 对可加相位进行操作
for (Map.Entry<String, Integer> entry : phaseOffsetTimeSetMap.entrySet()) { for (Map.Entry<String, Integer> entry : phaseOffsetTimeSetMap.entrySet()) {
Integer offsetTime = entry.getValue(); Integer offsetTime = entry.getValue();
if (offsetTime <= 0 || countOffsetAdd <= 0) { // 不需要修改相位时间或者没有可加时间
continue;
}
String phaseNo = entry.getKey(); String phaseNo = entry.getKey();
countOffsetAdd -= offsetTime; if (countOffsetAdd <= 0 && countOffsetSub >= 0) {
if (countOffsetAdd >= 0) { break;
// 当前相位可加时间就是最大可加时间,可加时间有可减时间
phaseTimeOptResultMap.put(phaseNo, offsetTime);
} else {
phaseTimeOptResultMap.put(phaseNo, countOffsetAdd);
} }
} // 可减时间有可加时间
// 对可减相位进行操作 if (offsetTime < 0 && countOffsetSub < 0) {
for (Map.Entry<String, Integer> entry : phaseOffsetTimeSetMap.entrySet()) { countOffsetSub -= offsetTime;
Integer offsetTime = entry.getValue(); if (countOffsetSub <= 0) {
if (offsetTime >= 0 || countOffsetSub <= 0) { // 不需要修改相位时间或者没有可加时间 phaseTimeOptResultMap.put(phaseNo, offsetTime);
continue; } else {
phaseTimeOptResultMap.put(phaseNo, -countOffsetSub);
}
} }
String phaseNo = entry.getKey(); // 可加时间有可减时间
countOffsetSub += offsetTime; if (offsetTime > 0 && countOffsetAdd > 0) {
if (countOffsetSub >= 0) { countOffsetAdd -= offsetTime;
// 可减时间有可加时间 if (countOffsetAdd >= 0) {
phaseTimeOptResultMap.put(phaseNo, offsetTime); phaseTimeOptResultMap.put(phaseNo, offsetTime);
} else { } else {
phaseTimeOptResultMap.put(phaseNo, countOffsetSub); phaseTimeOptResultMap.put(phaseNo, countOffsetAdd);
}
} }
} }
......
...@@ -158,7 +158,7 @@ public class DiagnoServiceImpl implements DiagnoService { ...@@ -158,7 +158,7 @@ public class DiagnoServiceImpl implements DiagnoService {
CrossSchemeOptLogPO crossSchemeOptLogPO = maxList.get(0); CrossSchemeOptLogPO crossSchemeOptLogPO = maxList.get(0);
String optType = crossSchemeOptLogPO.getOptType(); String optType = crossSchemeOptLogPO.getOptType();
schemeOptVO.setOptType(Integer.parseInt(optType)); schemeOptVO.setOptType(Integer.parseInt(optType));
String schemeNo = crossSchemeOptLogPO.getSchemeNo(); String schemeNo = String.valueOf(crossSchemeOptLogPO.getSchemeId());
CrossSchemePO crossSchemePO = crossSchemeMapper.selectByCrossIdAndSchemeNo(crossId, schemeNo); CrossSchemePO crossSchemePO = crossSchemeMapper.selectByCrossIdAndSchemeNo(crossId, schemeNo);
schemeOptVO.setCycle(crossSchemePO.getCycle()); schemeOptVO.setCycle(crossSchemePO.getCycle());
schemeOptVO.setSchemeId(crossSchemePO.getId()); schemeOptVO.setSchemeId(crossSchemePO.getId());
......
...@@ -104,7 +104,7 @@ public class FeignProxyServiceImpl implements FeignProxyService { ...@@ -104,7 +104,7 @@ public class FeignProxyServiceImpl implements FeignProxyService {
CrossSchemeOptLogPO crossSchemeOptLogPO = new CrossSchemeOptLogPO(); CrossSchemeOptLogPO crossSchemeOptLogPO = new CrossSchemeOptLogPO();
crossSchemeOptLogPO.setCrossId(crossId); crossSchemeOptLogPO.setCrossId(crossId);
crossSchemeOptLogPO.setSchemeNo(patternNo); crossSchemeOptLogPO.setSchemeId(Integer.valueOf(patternNo));
crossSchemeOptLogPO.setDirType(dirType); crossSchemeOptLogPO.setDirType(dirType);
crossSchemeOptLogPO.setTurnType(turnType); crossSchemeOptLogPO.setTurnType(turnType);
crossSchemeOptLogPO.setOffset(Integer.valueOf(offset)); crossSchemeOptLogPO.setOffset(Integer.valueOf(offset));
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<resultMap type="net.wanji.opt.po.base.CrossSchemeOptLogPO" id="BaseResultMap"> <resultMap type="net.wanji.opt.po.base.CrossSchemeOptLogPO" id="BaseResultMap">
<result property="id" column="id"/> <result property="id" column="id"/>
<result property="crossId" column="cross_id"/> <result property="crossId" column="cross_id"/>
<result property="schemeNo" column="scheme_no"/> <result property="schemeId" column="scheme_id"/>
<result property="dirType" column="dir_type"/> <result property="dirType" column="dir_type"/>
<result property="turnType" column="turn_type"/> <result property="turnType" column="turn_type"/>
<result property="offset" column="offset"/> <result property="offset" column="offset"/>
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
</resultMap> </resultMap>
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id, cross_id, scheme_no, dir_type, turn_type, offset, ring_no, phase_no, phase_order_id, ori_green_time, id, cross_id, scheme_id, dir_type, turn_type, offset, ring_no, phase_no, phase_order_id, ori_green_time,
opt_time, opt_type, opt_reason, start_time, end_time, is_relation, relation_cross_id, opt_result, opt_result_desc, opt_time, opt_type, opt_reason, start_time, end_time, is_relation, relation_cross_id, opt_result, opt_result_desc,
data_batch_time, restore_result, data_extend data_batch_time, restore_result, data_extend
</sql> </sql>
...@@ -54,25 +54,25 @@ ...@@ -54,25 +54,25 @@
</select> </select>
<insert id="insertOne" parameterType="net.wanji.opt.po.base.CrossSchemeOptLogPO"> <insert id="insertOne" parameterType="net.wanji.opt.po.base.CrossSchemeOptLogPO">
insert into t_base_cross_scheme_opt_log (cross_id, scheme_no, dir_type, turn_type, offset, ring_no, phase_no, insert into t_base_cross_scheme_opt_log (cross_id, scheme_id, dir_type, turn_type, offset, ring_no, phase_no,
phase_order_id, ori_green_time, opt_time, opt_type, opt_reason, phase_order_id, ori_green_time, opt_time, opt_type, opt_reason,
start_time, start_time,
end_time, is_relation, relation_cross_id, opt_result, opt_result_desc, end_time, is_relation, relation_cross_id, opt_result, opt_result_desc,
data_batch_time, restore_result, data_extend) data_batch_time, restore_result, data_extend)
values (#{crossId}, #{schemeNo}, #{dirType}, #{turnType}, #{offset}, #{ringNo}, #{phaseNo}, #{phaseOrderId}, values (#{crossId}, #{schemeId}, #{dirType}, #{turnType}, #{offset}, #{ringNo}, #{phaseNo}, #{phaseOrderId},
#{oriGreenTime}, #{optTime}, #{optType}, #{optReason}, #{startTime}, #{endTime}, #{relationFlag}, #{oriGreenTime}, #{optTime}, #{optType}, #{optReason}, #{startTime}, #{endTime}, #{relationFlag},
#{relationCrossId}, #{optResult}, #{optResultDesc}, #{dataBatchTime}, #{restoreResult}, #{dataExtend}) #{relationCrossId}, #{optResult}, #{optResultDesc}, #{dataBatchTime}, #{restoreResult}, #{dataExtend})
</insert> </insert>
<insert id="insertBatch" parameterType="net.wanji.opt.po.base.CrossSchemeOptLogPO"> <insert id="insertBatch" parameterType="net.wanji.opt.po.base.CrossSchemeOptLogPO">
insert into t_base_cross_scheme_opt_log insert into t_base_cross_scheme_opt_log
(cross_id, scheme_no, dir_type, turn_type, offset, ring_no, phase_no, (cross_id, scheme_id, dir_type, turn_type, offset, ring_no, phase_no,
phase_order_id, ori_green_time, opt_time, opt_type, opt_reason, phase_order_id, ori_green_time, opt_time, opt_type, opt_reason,
start_time, end_time, is_relation, relation_cross_id, opt_result, opt_result_desc, start_time, end_time, is_relation, relation_cross_id, opt_result, opt_result_desc,
data_batch_time, restore_result, data_extend) data_batch_time, restore_result, data_extend)
values values
<foreach collection="list" item="entity" separator=","> <foreach collection="list" item="entity" separator=",">
(#{entity.crossId}, #{entity.schemeNo}, #{entity.dirType}, #{entity.turnType}, #{entity.offset}, #{entity.ringNo}, #{entity.phaseNo}, #{entity.phaseOrderId}, (#{entity.crossId}, #{entity.schemeId}, #{entity.dirType}, #{entity.turnType}, #{entity.offset}, #{entity.ringNo}, #{entity.phaseNo}, #{entity.phaseOrderId},
#{entity.oriGreenTime}, #{entity.optTime}, #{entity.optType}, #{entity.optReason}, #{entity.startTime}, #{entity.endTime}, #{entity.relationFlag}, #{entity.oriGreenTime}, #{entity.optTime}, #{entity.optType}, #{entity.optReason}, #{entity.startTime}, #{entity.endTime}, #{entity.relationFlag},
#{entity.relationCrossId}, #{entity.optResult}, #{entity.optResultDesc}, #{entity.dataBatchTime}, #{entity.restoreResult}, #{entity.dataExtend}) #{entity.relationCrossId}, #{entity.optResult}, #{entity.optResultDesc}, #{entity.dataBatchTime}, #{entity.restoreResult}, #{entity.dataExtend})
</foreach> </foreach>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment