Commit 4207e326 authored by duanruiming's avatar duanruiming

Merge remote-tracking branch 'origin/master'

parents 245c3eb4 1a5fd980
......@@ -5,20 +5,11 @@ import net.wanji.opt.common.enums.TimeGranularityEnum;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Months;
import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
import java.util.stream.Collectors;
public class EsDateIndexUtil {
......@@ -60,6 +51,8 @@ public class EsDateIndexUtil {
sortedSet.addAll(EsDateIndexUtil.getTimeScopeList(start, end, TimeGranularityEnum.ONE_HOUR, EsDateIndexUtil.H_FORMATTER));
} else if (Objects.equals("4", groupType)) {
sortedSet.addAll(EsDateIndexUtil.getTimeScopeList(start, end, TimeGranularityEnum.ONE_DAY, EsDateIndexUtil.YMD_FORMATTER));
} else if (Objects.equals("5", groupType)) {
sortedSet.addAll(EsDateIndexUtil.getTimeScopeList(start, end, TimeGranularityEnum.TEN_MINUTE, EsDateIndexUtil.HM_FORMATTER));
}
return sortedSet;
}
......@@ -89,6 +82,8 @@ public class EsDateIndexUtil {
unitTime = 60;
}else if (Objects.equals(TimeGranularityEnum.ONE_DAY, timeGranularityEnum)) {
unitTime = 24*60;
}else if (Objects.equals(TimeGranularityEnum.TEN_MINUTE, timeGranularityEnum)) {
unitTime = 10;
}
for (int i = 0; i <= diffNum; i=i+unitTime) {
......
......@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
import net.wanji.common.enums.BaseEnum;
import net.wanji.common.framework.exception.DubboProviderException;
import net.wanji.common.utils.tool.DateUtil;
import net.wanji.common.utils.tool.LocalDateTimeUtil;
import net.wanji.databus.dao.entity.GreenwaveHistPO;
import net.wanji.databus.dao.mapper.BaseCrossInfoMapper;
import net.wanji.databus.dao.mapper.GreenwaveHistMapper;
......@@ -30,6 +31,10 @@ import org.joda.time.format.DateTimeFormat;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
......@@ -209,6 +214,7 @@ public class EventServiceImpl implements EventService {
EventInfoTrafficStatusVO eventInfoTrafficStatusVO = new EventInfoTrafficStatusVO();
String eventType = eventInfoTrafficStatusDTO.getEventType();
Integer queryType = eventInfoTrafficStatusDTO.getQueryType();
String timeType = eventInfoTrafficStatusDTO.getTimeType();
List<EventInfoTrafficStatusPO> eventInfoTrafficStatusPOS = null;
if (ObjectUtil.equals(1, queryType)) {
//路口
......@@ -229,16 +235,62 @@ public class EventServiceImpl implements EventService {
}
List<GreenwaveHistPO> greenwaveHistPOS = greenwaveHistMapper.selectByIdAndType(Integer.valueOf(eventInfoTrafficStatusDTO.getId()), statusList);
if (ObjectUtil.isNotEmpty(greenwaveHistPOS)) {
eventInfoTrafficStatusPOS = new ArrayList<EventInfoTrafficStatusPO>();
eventInfoTrafficStatusPOS = greenwaveHistHandler(timeType,greenwaveHistPOS);
/*eventInfoTrafficStatusPOS = new ArrayList<EventInfoTrafficStatusPO>();
if (ObjectUtil.equals(timeType,3)) {
//按小时统计,还按照之前逻辑处理
Map<Integer, List<GreenwaveHistPO>> collect = greenwaveHistPOS.stream().collect(Collectors.groupingBy(x -> x.getStartTime().getHours()));
for (Map.Entry<Integer, List<GreenwaveHistPO>> entry : collect.entrySet()) {
eventInfoTrafficStatusPOS.add(handerEntry(entry));
}
}else {
//其它十分钟,15分钟,30分钟数据有一次算一次
Map<String, List<GreenwaveHistPO>> groupedEvents = greenwaveHistPOS.stream()
.collect(Collectors.groupingBy(event -> {
GreenwaveHistPO greenwaveHistPO = (GreenwaveHistPO)event;
Date date = greenwaveHistPO.getStartTime();
LocalDateTime startTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
// 将时间向下取整到最近的15分钟(按时间聚合)
LocalDateTime roundedTime = null;
if (ObjectUtil.equals(timeType,"5")){
//十分钟
roundedTime = startTime.truncatedTo(ChronoUnit.HOURS)
.plusMinutes(10 * (startTime.getMinute() / 10));
}else if (ObjectUtil.equals(timeType,"1")){
//15分钟
roundedTime = startTime.truncatedTo(ChronoUnit.HOURS)
.plusMinutes(15 * (startTime.getMinute() / 15));
}else if (ObjectUtil.equals(timeType,"2")){
//30分钟
roundedTime = startTime.truncatedTo(ChronoUnit.HOURS)
.plusMinutes(30 * (startTime.getMinute() / 30));
}
// 格式化为"小时:分钟"字符串
return roundedTime.format(DateTimeFormatter.ofPattern("HH:mm"));
}));
Set<String> strings = groupedEvents.keySet();
for (String string : strings) {
List<GreenwaveHistPO> value = groupedEvents.get(string);
EventInfoTrafficStatusPO eventInfoTrafficStatusPO = new EventInfoTrafficStatusPO();
int size = value.size();
int sum = value.stream().mapToInt(GreenwaveHistPO::getStrategyDuration).sum();
eventInfoTrafficStatusPO.setDuration(sum);
eventInfoTrafficStatusPO.setCount(size);
eventInfoTrafficStatusPO.setTimeAxisStart(string);
eventInfoTrafficStatusPOS.add(eventInfoTrafficStatusPO);
}
}*/
}
}
//获取当前时间的时间轴
Set<String> timeScopeList = getTimeScopeList(DateTime.now().withTimeAtStartOfDay(), new DateTime(), EsDateIndexUtil.H_FORMATTER);
LocalDateTime localDateTime = LocalDateTimeUtil.convertDateToLDT(new Date());
LocalDateTime dayStart = LocalDateTimeUtil.getDayStart(localDateTime);
String now = LocalDateTimeUtil.formatTime(localDateTime, LocalDateTimeUtil.TIMEFORMATTER);
String startTime = LocalDateTimeUtil.formatTime(dayStart, LocalDateTimeUtil.TIMEFORMATTER);
Set<String> timeScopeList = EsDateIndexUtil.getTimeGranularityAxis(eventInfoTrafficStatusDTO.getTimeType(), startTime, now);
if (ObjectUtil.isNotEmpty(timeScopeList)) {
ArrayList<String> arrayList = timeScopeList.stream().collect(Collectors.toCollection(ArrayList::new));
List<String> collect = arrayList.stream().sorted(Comparator.comparing(time -> Integer.valueOf(time.split(":")[0]))).collect(Collectors.toList());
......@@ -271,6 +323,76 @@ public class EventServiceImpl implements EventService {
return eventInfoTrafficStatusVO;
}
/**
* @Description 干线数据处理
* @Param [greenwaveHistPOS]
* @return void
**/
private List<EventInfoTrafficStatusPO> greenwaveHistHandler(String timeType ,List<GreenwaveHistPO> greenwaveHistPOS){
List<EventInfoTrafficStatusPO> eventInfoTrafficStatusPOS = new ArrayList<>();
//其它十分钟,15分钟,30分钟数据有一次算一次
Map<String, List<GreenwaveHistPO>> groupedEvents = greenwaveHistPOS.stream()
.collect(Collectors.groupingBy(event -> {
GreenwaveHistPO greenwaveHistPO = (GreenwaveHistPO)event;
Date date = greenwaveHistPO.getStartTime();
LocalDateTime startTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
// 将时间向下取整到最近的15分钟(按时间聚合)
LocalDateTime roundedTime = null;
if (ObjectUtil.equals(timeType,"5")){
//十分钟
roundedTime = startTime.truncatedTo(ChronoUnit.HOURS)
.plusMinutes(10 * (startTime.getMinute() / 10));
}else if (ObjectUtil.equals(timeType,"1")){
//15分钟
roundedTime = startTime.truncatedTo(ChronoUnit.HOURS)
.plusMinutes(15 * (startTime.getMinute() / 15));
}else if (ObjectUtil.equals(timeType,"2")){
//30分钟
roundedTime = startTime.truncatedTo(ChronoUnit.HOURS)
.plusMinutes(30 * (startTime.getMinute() / 30));
}else if (ObjectUtil.equals(timeType,"3")){
//1小时
roundedTime = startTime.truncatedTo(ChronoUnit.HOURS)
.plusMinutes(60 * (startTime.getMinute() / 60));
}
// 格式化为"小时:分钟"字符串
return roundedTime.format(DateTimeFormatter.ofPattern("HH:mm"));
}));
Set<String> strings = groupedEvents.keySet();
for (String string : strings) {
EventInfoTrafficStatusPO eventInfoTrafficStatusPO = new EventInfoTrafficStatusPO();
List<GreenwaveHistPO> value = groupedEvents.get(string);
int count = 0;
int duration = 0;
for (int i = 0; i < value.size(); i++) {
if (i == 0){
count++;
//五分钟数据,则一次拥堵为300秒
duration+=300;
}else {
if (value.get(i).getStartTime().getTime() - value.get(i - 1).getStartTime().getTime() <= 305) {
//视为连续拥堵,则不加次数,拥堵时长加300秒
//五分钟数据,则一次拥堵为300秒
duration+=300;
}else {
count++;
//五分钟数据,则一次拥堵为300秒
duration+=300;
}
}
}
eventInfoTrafficStatusPO.setDuration(duration);
eventInfoTrafficStatusPO.setCount(count);
eventInfoTrafficStatusPO.setTimeAxisStart(string);
eventInfoTrafficStatusPOS.add(eventInfoTrafficStatusPO);
}
return eventInfoTrafficStatusPOS;
}
/**
* @return net.wanji.databus.po.EventInfoTrafficStatusPO
* @Description 干线数据处理
......
......@@ -210,7 +210,12 @@
SELECT cross_id, dt,type AS event_type,start_time,ifnull(end_time,now()) end_time,
sum( TIMESTAMPDIFF( SECOND, start_time, ifnull( end_time, now())) ) duration,
count(*) as count,
DATE_FORMAT( start_time, '%H:00' ) time_axis_start
case
when #{timeType}=5 then DATE_FORMAT(concat( date( start_time ), ' ', HOUR ( start_time ), ':', floor( MINUTE ( start_time ) / 10 ) * 10 ),'%H:%i' )
when #{timeType}=1 then DATE_FORMAT(concat( date( start_time ), ' ', HOUR ( start_time ), ':', floor( MINUTE ( start_time ) / 15 ) * 15 ),'%H:%i' )
when #{timeType}=2 then DATE_FORMAT(concat( date( start_time ), ' ', HOUR ( start_time ), ':', floor( MINUTE ( start_time ) / 30 ) * 30 ),'%H:%i' )
when #{timeType}=3 then DATE_FORMAT( start_time, '%H:00' )
end as time_axis_start
FROM
t_event_info
WHERE
......
......@@ -29,4 +29,9 @@ public class EventInfoTrafficStatusDTO {
@Range(min =1,max = 7,message = "eventType 区间1-7")
@NotNull(message = "eventType 不能为空")
private String eventType ;
@ApiModelProperty(value = "时间粒度类型:5-10分钟,1-15分钟,2-30分钟,3-1小时",notes = "")
@Range(min =1,max = 5,message = "timeType 区间1-5")
@NotNull(message = "timeType 不能为空")
private String timeType ;
}
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