周/月维度故障自动发现率看板

对周/月 Object进行分组计算

  1. 先对Object进行大条件(一些前置条件)进行分组归类
  2. 对每组的 Object进行 每周/月的分组
  3. 计算每周/月 组内的的特殊数据占总数据的比例
    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
      }
    }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值