public static void main(String[] arg){
System.out.println("qi yun");
Map<Long, Double> map = new HashMap<>();
Date now = new Date();
List<MonitorChartInfVo> result = packageForMonitorChartInfVo(map,1656297644000L,1656319244000L,60 * 1000);
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String dateStr = fmt.format(new Date());
result.forEach(s -> {
System.out.println( fmt.format(new Date(s.getSamplingTime())));
});
}
//时间聚合
private static List<MonitorChartInfVo> packageForMonitorChartInfVo(Map<Long, Double> map, Long beginTime, Long endTime, int timeInterval){
List<MonitorChartInfVo> voList = new LinkedList<>();
int periodSecond = timeInterval;
int aggregationTimes = 0;
long timeIndex = beginTime;
//一段时间里数据的总和
//Double aggregationSumBytes = 0.0;
Double dataForEveryInterval = null;
while (timeIndex <= endTime) {
Double sumBytes = map.getOrDefault((timeIndex), 0.0);
aggregationTimes += 1;
//用于筛选
Double everyTimeData = map.get(timeIndex);
if(everyTimeData != null ){
dataForEveryInterval = everyTimeData;
}
if (aggregationTimes == periodSecond / (60000)) {
if( null == dataForEveryInterval){
voList.add(new MonitorChartInfVo(sumBytes, (timeIndex)));
}else{
voList.add(new MonitorChartInfVo(dataForEveryInterval, (timeIndex)));
}
aggregationTimes = 0;
dataForEveryInterval = null;
}
timeIndex += 60000;
}
return voList;
}
public static class MonitorChartInfVo {
//采样时间
private Long samplingTime;
//数据
private Double traffic;
public MonitorChartInfVo(Double traffic, Long samplingTime) {
this.traffic = traffic;
this.samplingTime = samplingTime;
}
public Long getSamplingTime() {
return samplingTime;
}
public void setSamplingTime(Long samplingTime) {
this.samplingTime = samplingTime;
}
public Double getTraffic() {
return traffic;
}
public void setTraffic(Double traffic) {
this.traffic = traffic;
}
}
版权声明:本文为limingcai168原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。