public List buildAlertHistoryList()

in ozhera-monitor/ozhera-monitor-service/src/main/java/org/apache/ozhera/monitor/service/helper/AlertHelper.java [115:224]


    public List<AlertHistory> buildAlertHistoryList(JsonElement list) {
        if (list == null || !list.isJsonArray()) {
            return null;
        }
        List<AlertHistory> historyList = new ArrayList<>();
        list.getAsJsonArray().forEach((ele) -> {
            JsonObject data = ele.getAsJsonObject();
            AlertHistory history = new AlertHistory();
            historyList.add(history);
            if (data.has("id")) {
                history.setId(data.get("id").getAsString());
            }
            if (data.has("alert_id")) {
                history.setAlertId(data.get("alert_id").getAsString());
            }
            if (data.has("tree_id")) {
                history.setIamTreeId(data.get("tree_id").getAsInt());
            }
            if (data.has("alert_name")) {
                history.setAlertName(data.get("alert_name").getAsString());
            }
            if (data.has("alert_cname")) {
                history.setAlertCName(data.get("alert_cname").getAsString());
            }
            if (data.has("alert_time")) {
                history.setAlertDate(CommonUtil.toMillis(data.get("alert_time").getAsLong()));
            } else {
                history.setAlertDate(System.currentTimeMillis());
            }
            if (data.has("priority")) {
                history.setAlertLevel(data.get("priority").getAsString());
            }
            if (data.has("start_time")) {
                history.setStartTime(CommonUtil.toMillis(data.get("start_time").getAsLong()));
            }
            if (data.has("end_time")) {
                history.setEndTime(CommonUtil.toMillis(data.get("end_time").getAsLong()));
            }
            //resolved firing
            if (data.has("status")) {
                history.setAlertStat(data.get("status").getAsString());
            }
            history.setDurationTime(buildDurationTime(data));
            // 拼接异常详情
            StringBuilder url = new StringBuilder();
            if (data.has("labels")) {
                JsonObject labels = data.get("labels").getAsJsonObject();
                StringBuilder content = new StringBuilder();
                buildAlertContent(content, labels);
                buildDataCountContent(content, labels);
                buildAlarmSendInterval(content, labels);
                history.setAlertContent(content.toString());
                if (labels.has("project_name")) {
                    history.setAlertApp(labels.get("project_name").getAsString());
                }
                if (labels.has("project_id")) {
                    history.setAlertAppId(labels.get("project_id").getAsString());
                }
                if (labels.has("instance")) {
                    history.setAlertIntance(labels.get("instance").getAsString());
                }
                if (labels.has("methodName")) {
                    history.setMethodName(labels.get("methodName").getAsString());
                }
                if (labels.has("ip")) {
                    history.setAlertIp(labels.get("ip").getAsString());
                }
                if (labels.has("serverIp")) {
                    history.setAlertIp(labels.get("serverIp").getAsString());
                }
                //特殊url处理
                if (labels.has("detailRedirectUrl")) {
                    url.append(labels.get("detailRedirectUrl").getAsString());
                    //时间单位是微秒
                    url.append("&start=").append(history.getAlertDate() * 1000L - FIVE_MINUTES);
                    url.append("&end=").append(history.getAlertDate() * 1000L + FIVE_MINUTES);
                    if (labels.has("group") && labels.has("url")) {
                        url.append("&operation=").append("/mtop/").append(labels.get("group").getAsString()).append("/").append(labels.get("url").getAsString());
                    }
                    history.setDetailedUrl(url.toString());
                    return;
                }
            }
            url.append(heraUrl).append("?id=").append(history.getAlertAppId())
                    .append("&name=").append(history.getAlertApp())
                    .append("&start_time=").append(history.getAlertDate() - TEN_MINUTES)
                    .append("&end_time=").append(history.getAlertDate() + TEN_MINUTES);
            if (StringUtils.isNotBlank(history.getAlertIp())) {
                url.append("&var-instance=").append(history.getAlertIp());
            }
            if (StringUtils.isNotBlank(history.getMethodName())) {
                url.append("&method_name=").append(history.getMethodName());
            }
            ReqErrorMetricsPOJO errMetrics = reqErrorMetricsService.getErrorMetricsByMetrics(history.getAlertName());
            if (errMetrics != null) {
                url.append("&activeTab=exception").append("&metric=").append(errMetrics.getCode());
                history.setDetailedUrl(url.toString());
                return;
            }
            ReqSlowMetricsPOJO slowMetrics = reqSlowMetricsService.getSlowMetricsByMetric(history.getAlertName());
            if (slowMetrics != null) {
                url.append("&activeTab=slowQuery").append("&metric=").append(slowMetrics.getCode());
                history.setDetailedUrl(url.toString());
                return;
            }
            url.append("&activeTab=dashboard");
            history.setDetailedUrl(url.toString());
        });
        return historyList;
    }