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 {
private int id;
private String crossId;
private String schemeNo;
private Integer schemeId;
private int dirType;
private String turnType;
private int offset;
......@@ -36,6 +36,9 @@ public class CrossSchemeOptLogPO {
private String optResultDesc;
private int dataBatchTime;
private String restoreResult;
/**
* 目前诊断优化页面存储:场景策略方法参数,展示使用
*/
private String dataExtend;
}
......@@ -5,15 +5,13 @@ import net.wanji.common.enums.CrossStatusEnum;
import net.wanji.common.framework.Constants;
import net.wanji.common.framework.rest.JsonViewObject;
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.feign.pojo.vo.SchemeSendVO;
import net.wanji.feign.pojo.vo.SignalStatusVO;
import net.wanji.feign.service.UtcFeignClients;
import net.wanji.opt.cache.CrossDirTurnPhaseCache;
import net.wanji.opt.cache.CrossRidInfoCache;
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.dto.CrossDirInfoDTO;
import net.wanji.opt.dto.CrossPhaseDTO;
......@@ -51,12 +49,6 @@ import static net.wanji.opt.cache.CrossSchemeInfoCache.geteSchemePOCache;
@Slf4j
public class CrossOptimizeServiceImpl implements CrossOptimizeService {
@Resource
CrossPhaseMapper crossPhaseMapper;
@Resource
CrossSchemeMapper crossSchemeMapper;
@Resource
RidInfoMapper ridInfoMapper;
@Resource
CrossSchemeOptLogMapper crossSchemeOptLogMapper;
@Resource
......@@ -64,6 +56,8 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
@Resource
CrossDirTurnPhaseCache crossDirTurnPhaseCache;
@Resource
CrossRidInfoCache crossRidInfoCache;
@Resource
SceneService sceneService;
@Value("${crossOptParam.maxVehheadDist}")
......@@ -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)) {
continue;
......@@ -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)) {
continue;
......@@ -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 {
// 计算路口各相位调整时长(秒)
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 {
phaseTimeOptResultMap = getPhaseTimeOptMap(timeOffsetPhaseMap);
phaseTimeOptResultMap = getPhaseTimeOptMap(phaseTimeOffset);
}
// 修改原始方案绿灯时间为优化后时间,方案下发
SchemeSendVO schemeSendVO = getSchemeSendVO(crossId, phaseMap, phaseTimeOptResultMap);
......@@ -461,7 +455,7 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
CrossPhaseDTO crossPhaseDTO = item.getValue();
BeanUtils.copyProperties(crossPhaseDTO, crossSchemeOptLogPO);
crossSchemeOptLogPO.setSchemeNo(String.valueOf(crossPhaseDTO.getSchemeId()));
crossSchemeOptLogPO.setSchemeId(crossPhaseDTO.getSchemeId());
crossSchemeOptLogPO.setPhaseOrderId(crossPhaseDTO.getSort());
crossSchemeOptLogPO.setStartTime(new Date());
crossSchemeOptLogPO.setOptType("1");// 自动优化
......@@ -470,7 +464,8 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
crossSchemeOptLogPO.setOptResult("1");
crossSchemeOptLogPO.setOptResultDesc(StatusCodeEnum.STATUS_00200.getDetail());
crossSchemeOptLogPO.setDataBatchTime((int) dataBatchTimeLong);
crossSchemeOptLogPO.setDataExtend("");// todo 需要通过场景策略方法
// todo 测试数据
crossSchemeOptLogPO.setDataExtend("{\"sceneName\": \"测试JSON\",\"strategyName\": \"测试JSON\",\"ideaName\": \"测试JSON\"}");// todo 需要通过场景策略方法
String phaseNo = crossPhaseDTO.getPhaseNo();
for (Map.Entry<String, Integer> entry : phaseTimeOptResultMap.entrySet()) {
......@@ -603,39 +598,34 @@ public class CrossOptimizeServiceImpl implements CrossOptimizeService {
int subTimeSum = subTimeList.stream().mapToInt(Integer::intValue).sum();
int addTimeSum = addTimeList.stream().mapToInt(Integer::intValue).sum();
int countOffsetAdd = Math.abs(subTimeSum) >= addTimeSum ? addTimeSum : Math.abs(subTimeSum);
int countOffsetSub = countOffsetAdd;
int countOffsetSub = -countOffsetAdd;
// 可加可减时间为0,不需要计算相位时间
if (countOffsetAdd == 0) {
return null;
return Collections.EMPTY_MAP;
}
// 对可加相位进行操作
for (Map.Entry<String, Integer> entry : phaseOffsetTimeSetMap.entrySet()) {
Integer offsetTime = entry.getValue();
if (offsetTime <= 0 || countOffsetAdd <= 0) { // 不需要修改相位时间或者没有可加时间
continue;
}
String phaseNo = entry.getKey();
countOffsetAdd -= offsetTime;
if (countOffsetAdd >= 0) {
// 当前相位可加时间就是最大可加时间,可加时间有可减时间
phaseTimeOptResultMap.put(phaseNo, offsetTime);
} else {
phaseTimeOptResultMap.put(phaseNo, countOffsetAdd);
if (countOffsetAdd <= 0 && countOffsetSub >= 0) {
break;
}
}
// 对可减相位进行操作
for (Map.Entry<String, Integer> entry : phaseOffsetTimeSetMap.entrySet()) {
Integer offsetTime = entry.getValue();
if (offsetTime >= 0 || countOffsetSub <= 0) { // 不需要修改相位时间或者没有可加时间
continue;
// 可减时间有可加时间
if (offsetTime < 0 && countOffsetSub < 0) {
countOffsetSub -= offsetTime;
if (countOffsetSub <= 0) {
phaseTimeOptResultMap.put(phaseNo, offsetTime);
} else {
phaseTimeOptResultMap.put(phaseNo, -countOffsetSub);
}
}
String phaseNo = entry.getKey();
countOffsetSub += offsetTime;
if (countOffsetSub >= 0) {
// 可减时间有可加时间
phaseTimeOptResultMap.put(phaseNo, offsetTime);
} else {
phaseTimeOptResultMap.put(phaseNo, countOffsetSub);
// 可加时间有可减时间
if (offsetTime > 0 && countOffsetAdd > 0) {
countOffsetAdd -= offsetTime;
if (countOffsetAdd >= 0) {
phaseTimeOptResultMap.put(phaseNo, offsetTime);
} else {
phaseTimeOptResultMap.put(phaseNo, countOffsetAdd);
}
}
}
......
......@@ -158,7 +158,7 @@ public class DiagnoServiceImpl implements DiagnoService {
CrossSchemeOptLogPO crossSchemeOptLogPO = maxList.get(0);
String optType = crossSchemeOptLogPO.getOptType();
schemeOptVO.setOptType(Integer.parseInt(optType));
String schemeNo = crossSchemeOptLogPO.getSchemeNo();
String schemeNo = String.valueOf(crossSchemeOptLogPO.getSchemeId());
CrossSchemePO crossSchemePO = crossSchemeMapper.selectByCrossIdAndSchemeNo(crossId, schemeNo);
schemeOptVO.setCycle(crossSchemePO.getCycle());
schemeOptVO.setSchemeId(crossSchemePO.getId());
......
......@@ -104,7 +104,7 @@ public class FeignProxyServiceImpl implements FeignProxyService {
CrossSchemeOptLogPO crossSchemeOptLogPO = new CrossSchemeOptLogPO();
crossSchemeOptLogPO.setCrossId(crossId);
crossSchemeOptLogPO.setSchemeNo(patternNo);
crossSchemeOptLogPO.setSchemeId(Integer.valueOf(patternNo));
crossSchemeOptLogPO.setDirType(dirType);
crossSchemeOptLogPO.setTurnType(turnType);
crossSchemeOptLogPO.setOffset(Integer.valueOf(offset));
......
......@@ -6,7 +6,7 @@
<resultMap type="net.wanji.opt.po.base.CrossSchemeOptLogPO" id="BaseResultMap">
<result property="id" column="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="turnType" column="turn_type"/>
<result property="offset" column="offset"/>
......@@ -29,7 +29,7 @@
</resultMap>
<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,
data_batch_time, restore_result, data_extend
</sql>
......@@ -54,25 +54,25 @@
</select>
<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,
start_time,
end_time, is_relation, relation_cross_id, opt_result, opt_result_desc,
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},
#{relationCrossId}, #{optResult}, #{optResultDesc}, #{dataBatchTime}, #{restoreResult}, #{dataExtend})
</insert>
<insert id="insertBatch" 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,
(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,
data_batch_time, restore_result, data_extend)
values
<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.relationCrossId}, #{entity.optResult}, #{entity.optResultDesc}, #{entity.dataBatchTime}, #{entity.restoreResult}, #{entity.dataExtend})
</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