Skip to content

Commit

Permalink
Refactor alert template (DataLinkDC#2312)
Browse files Browse the repository at this point in the history
* change metrics transform

* Refactor Job Alert

* replace mvel to spel

* formate code

* add alert ui

* add alert ui

* add alert ui

* remove getOnlineTask and optim some bug

* formate ui code

* fix jdk8 type

* formate  code

* add alert rule ddl sql

* add alert rule dml sql

* fix alertRuleschedule logic bug

* refesh on alertRule update

* Remove logic that is not needed

* move FreeMarkerHolder.java to common package

* fix some code style

* refactor alert base

* refactor alert Dingtalk

* refactor alert FeiShu

* refactor alert WeChat

* refactor alert base

* fix alert rule ui bug

* fix some alert template

* fix some alert template

* fix code style

* fix code style

* refactor email templator

* refactor email template

* formate code

* Desensitization email

* delete some comment

* delete some comment

---------

Co-authored-by: zhu-mingye <[email protected]>
  • Loading branch information
gaoyan1998 and Zzm0809 authored Sep 14, 2023
1 parent ccf3e33 commit a377b70
Show file tree
Hide file tree
Showing 45 changed files with 763 additions and 1,563 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
import org.dinky.data.model.AlertInstance;
import org.dinky.data.model.JobInfoDetail;
import org.dinky.data.model.JobInstance;
import org.dinky.data.model.SystemConfiguration;
import org.dinky.data.model.Task;
import org.dinky.job.FlinkJobTaskPool;
import org.dinky.service.impl.AlertGroupServiceImpl;
import org.dinky.service.impl.AlertHistoryServiceImpl;
import org.dinky.service.impl.AlertRuleServiceImpl;
import org.dinky.service.impl.TaskServiceImpl;
import org.dinky.utils.TimeUtil;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -140,11 +142,17 @@ protected void check() {
for (Map.Entry<String, JobInfoDetail> job : taskPool.entrySet()) {
JobInfoDetail jobInfoDetail = job.getValue();
String key = job.getKey();
ruleFacts.put("", jobInfoDetail);
ruleFacts.put("time", TimeUtil.nowStr());
ruleFacts.put("jobDetail", jobInfoDetail);
ruleFacts.put("job", jobInfoDetail.getHistory());
ruleFacts.put("job", jobInfoDetail.getJobHistory().getJob());
ruleFacts.put("key", key);
ruleFacts.put("jobInstance", jobInfoDetail.getInstance());
ruleFacts.put(
"startTime",
TimeUtil.convertTimeToString(jobInfoDetail.getHistory().getStartTime()));
ruleFacts.put(
"endTime",
TimeUtil.convertTimeToString(jobInfoDetail.getHistory().getEndTime()));
ruleFacts.put("checkPoints", jobInfoDetail.getJobHistory().getCheckpoints());
ruleFacts.put("cluster", jobInfoDetail.getCluster());
ruleFacts.put("exceptions", jobInfoDetail.getJobHistory().getExceptions());
Expand All @@ -156,7 +164,6 @@ protected void check() {
* Refreshes the alert rules and related data.
*/
public void refeshRulesData() {

ruleFacts.put("exceptionRule", new ExceptionRule());
ruleFacts.put("checkpointRule", new CheckpointsRule());

Expand Down Expand Up @@ -209,8 +216,13 @@ private void executeAlertAction(Facts facts, AlertRuleDTO alertRuleDTO) {
JobInstance jobInstance = jobInfoDetail.getInstance();
Task task = taskService.getById(jobInfoDetail.getInstance().getTaskId());

String taskUrl = StrFormatter.format(
"{}/#/devops/job-detail?id={}",
SystemConfiguration.getInstances().getDinkyAddr(),
task.getId());
Map<String, Object> dataModel = new HashMap<>(facts.asMap());
dataModel.put("task", task);
dataModel.put("taskUrl", taskUrl);
dataModel.put("rule", alertRuleDTO);

String alertContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@

import org.dinky.alert.Alert;
import org.dinky.alert.AlertConfig;
import org.dinky.alert.AlertMsg;
import org.dinky.alert.AlertResult;
import org.dinky.alert.ShowType;
import org.dinky.data.constant.BaseConstant;
import org.dinky.data.enums.Status;
import org.dinky.data.model.AlertGroup;
import org.dinky.data.model.AlertInstance;
import org.dinky.data.result.Result;
Expand All @@ -40,14 +36,12 @@
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -77,52 +71,15 @@ public AlertResult testAlert(AlertInstance alertInstance) {
alertInstance.getName(), alertInstance.getType(), JSONUtil.toMap(alertInstance.getParams()));
Alert alert = Alert.buildTest(alertConfig);

AlertMsg alertMsg = getAlertMsg(alertInstance);
String title = Status.TEST_MSG_JOB_NAME_TITLE.getMessage()
+ "【"
+ alertMsg.getJobName()
+ "】:"
+ alertMsg.getJobStatus()
+ "!";
return alert.send(title, alertMsg.toString());
}
String msg = "\n- **Job Name :** <font color='gray'>Test Job</font>\n"
+ "- **Job Status :** <font color='red'>FAILED</font>\n"
+ "- **Alert Time :** 2023-01-01 12:00:00\n"
+ "- **Start Time :** 2023-01-01 12:00:00\n"
+ "- **End Time :** 2023-01-01 12:00:00\n"
+ "- **<font color='red'>The test exception, your job exception will pass here</font>**\n"
+ "[Go to Task Web](https://github.com/DataLinkDC/dinky)";

private static AlertMsg getAlertMsg(AlertInstance alertInstance) {
String currentDateTime =
LocalDateTime.now().format(DateTimeFormatter.ofPattern(BaseConstant.YYYY_MM_DD_HH_MM_SS));

String uuid = UUID.randomUUID().toString();

AlertMsg.AlertMsgBuilder alertMsgBuilder = AlertMsg.builder()
.alertType(Status.TEST_MSG_TITLE.getMessage())
.alertTime(currentDateTime)
.jobID(uuid)
.jobName(Status.TEST_MSG_JOB_NAME.getMessage())
.jobType("SQL")
.jobStatus("FAILED")
.jobStartTime(currentDateTime)
.jobEndTime(currentDateTime)
.jobDuration("1 Seconds");

String linkUrl = "http://cdh1:8081/#/job/" + uuid + "/overview";
String exceptionUrl = "http://cdh1:8081/#/job/" + uuid + "/exceptions";

Map<String, String> map = JSONUtil.toMap(alertInstance.getParams());
if (!alertInstance.getType().equals("Sms")) {
if (map.get("msgtype").equals(ShowType.MARKDOWN.getValue())) {
alertMsgBuilder.linkUrl("[" + Status.TEST_MSG_JOB_URL.getMessage() + " FlinkWeb](" + linkUrl + ")");
alertMsgBuilder.exceptionUrl(
"[" + Status.TEST_MSG_JOB_LOG_URL.getMessage() + "](" + exceptionUrl + ")");
} else {
alertMsgBuilder.linkUrl(linkUrl);
alertMsgBuilder.exceptionUrl(exceptionUrl);
}
} else {
alertMsgBuilder.linkUrl(linkUrl);
alertMsgBuilder.exceptionUrl(exceptionUrl);
}
AlertMsg alertMsg = alertMsgBuilder.build();
return alertMsg;
return alert.send("Fei Shu Alert Test", msg);
}

/**
Expand Down
48 changes: 0 additions & 48 deletions dinky-admin/src/main/java/org/dinky/utils/HttpUtils.java

This file was deleted.

2 changes: 1 addition & 1 deletion dinky-admin/src/main/resources/mapper/AlertRulesMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
dat.name as template_name,
dat.template_content
from dinky_alert_rules dar
left join dinky.dinky_alert_template dat on dar.template_id = dat.id
left join dinky_alert_template dat on dar.template_id = dat.id
where dar.enabled = 1
</select>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,40 @@

package org.dinky.alert;

import org.dinky.context.FreeMarkerHolder;

import java.io.IOException;
import java.util.Map;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.StrFormatter;
import freemarker.template.TemplateException;
import lombok.Getter;

/**
* AbstractAlert
*
* @since 2022/2/23 19:22
*/
public abstract class AbstractAlert implements Alert {

@Getter
private AlertConfig config;

public AlertConfig getConfig() {
return config;
}
private final FreeMarkerHolder freeMarkerHolder = new FreeMarkerHolder();

public Alert setConfig(AlertConfig config) {
this.config = config;
freeMarkerHolder.putTemplate(config.getType(), getTemplate());
return this;
}

public String getTemplate() {
return ResourceUtil.readUtf8Str(StrFormatter.format("{}.ftl", config.getType()))
.replace("\n", "\n\n");
}

protected String buildContent(Map<String, Object> params) throws TemplateException, IOException {
return freeMarkerHolder.buildWithData(getType(), params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@
public class AlertBaseConstant {

/** base constant */
public static final String MARKDOWN_QUOTE_MIDDLE_LINE = "- ";

public static final String TAB = "\t";
public static final String ENTER_LINE = "\n";
public static final String MARKDOWN_QUOTE_RIGHT_TAG = "> ";
public static final String MARKDOWN_QUOTE_RIGHT_TAG_WITH_SPACE = ">";
public static final String MARKDOWN_ENTER_BACK_SLASH = "/n";

public static final String CHARSET = "UTF-8";
public static final String PROXY_ENABLE = "isEnableProxy";
public static final String WEB_HOOK = "webhook";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CheckpointsRule() {
*/
private boolean isExpire(JsonNode latest, String key, String ckKey) {
JsonNode his = checkpointsCache.getIfPresent(key);
if (latest.get(ckKey) == null) {
if (latest.get(ckKey) == null || !latest.get(ckKey).has("trigger_timestamp")) {
return true;
}
long latestTime = latest.get(ckKey).get("trigger_timestamp").asLong(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import org.dinky.alert.AbstractAlert;
import org.dinky.alert.AlertResult;

import java.io.IOException;

import freemarker.template.TemplateException;

/**
* DingTalkAlert
*
Expand All @@ -37,6 +41,11 @@ public String getType() {
@Override
public AlertResult send(String title, String content) {
DingTalkSender sender = new DingTalkSender(getConfig().getParam());
return sender.send(title, content);
try {
String built = buildContent(sender.buildTemplateParams(title, content));
return sender.send(built);
} catch (TemplateException | IOException e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit a377b70

Please sign in to comment.