对周/月 Object进行分组计算
- 先对Object进行大条件(一些前置条件)进行分组归类
- 对每组的 Object进行 每周/月的分组
- 计算每周/月 组内的的特殊数据占总数据的比例
public Map<String, Map<String, Map<String, Double>>> computeAutomaticRatio(List<CaseInfo> caseInfoList){
Map<String, Map<String, Map<String, Double>>> resultMap = new HashMap<>();
Map<String, List<CaseInfo>> cs = caseInfoList.stream().collect(Collectors.groupingBy(CaseInfo::getBusiness_line));
HashMap<String, Map<String, Double>> mouthRatioMap = new HashMap<>();
cs.forEach((k,v)->{
HashMap<String, Double> mm = new HashMap<>();
Map<String, List<CaseInfo>> mouthCaseInfoMap = v.stream()
.filter(caseInfo -> caseInfo.getCreate_time() != null)
.collect(Collectors.groupingBy(caseInfo -> DateUtils.getMonth(caseInfo.getCreate_time())));
mouthCaseInfoMap.forEach((month, caseInfos) -> {
long count = caseInfos.stream()
.filter(caseInfo -> "xray".equals(caseInfo.getCreator()))
.count();
if (!caseInfos.isEmpty()){
double ratio = (double) count / caseInfos.size();
mm.put(month, ratio);
}else {
mm.put(month, 0.0);
}
});
mouthRatioMap.put(k, mm);
});
HashMap<String, Map<String, Double>> weekRatioMap = new HashMap<>();
cs.forEach((k,v)->{
HashMap<String, Double> vm = new HashMap<>();
Map<String, List<CaseInfo>> mouthCaseInfoMap = v.stream()
.filter(caseInfo -> caseInfo.getCreate_time() != null)
.collect(Collectors.groupingBy(caseInfo -> DateUtils.getWeek(caseInfo.getCreate_time())));
mouthCaseInfoMap.forEach((week, caseInfos) -> {
long count = caseInfos.stream()
.filter(caseInfo -> "xray".equals(caseInfo.getCreator()))
.count();
if (!caseInfos.isEmpty()){
double ratio = (double) count / caseInfos.size();
vm.put(week, ratio);
}else {
vm.put(week, 0.0);
}
});
weekRatioMap.put(k, vm);
});
resultMap.put("mouthRatioMap", mouthRatioMap);
resultMap.put("weekRatioMap", weekRatioMap);
return resultMap;
}
相关事件计算方法
public static List<String> getAllMonths(long startTime, long endTime) {
List<String> months = new ArrayList<>();
LocalDateTime start = LocalDateTime.ofInstant(Instant.ofEpochSecond(startTime), ZoneId.systemDefault());
LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochSecond(endTime), ZoneId.systemDefault());
while (!start.isAfter(end)) {
months.add(start.getYear() + "-" + String.format("%02d", start.getMonthValue()));
start = start.plusMonths(1);
}
return months;
}
public static List<String> getAllWeeks(long startTime, long endTime) {
List<String> weeks = new ArrayList<>();
LocalDateTime start = LocalDateTime.ofInstant(Instant.ofEpochSecond(startTime), ZoneId.systemDefault());
LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochSecond(endTime), ZoneId.systemDefault());
while (!start.isAfter(end)) {
int weekOfYear = start.get(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear());
weeks.add(start.getYear() + "年-第" + weekOfYear + "周");
start = start.plusWeeks(1);
}
return weeks;
}
public static String getMonth(Date createTime) {
LocalDate localDate = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return localDate.getYear() + "-" + String.format("%02d", localDate.getMonthValue());
}
public static String getWeek(Date createTime) {
LocalDate localDate = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int weekOfYear = localDate.get(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear());
return localDate.getYear() + "年-第" + weekOfYear + "周";
}
输出结果示例
{
"success": true,
"data": {
"weekRatioMap": {
"广告": {
"2023年-第49周": 1,
"2024年-第10周": 1,
"2023年-第48周": 0
},
"搜索": {
"2024年-第12周": 1,
"2023年-第47周": 1
},
"其他": {
"2024年-第19周": 0
}
},
"mouthRatioMap": {
"广告": {
"2023-12": 0.5,
"2024-03": 1
},
"搜索": {
"2023-11": 1,
"2024-03": 1
},
"其他": {
"2024-05": 0
}
}
}
}