Skip to content

Commit

Permalink
refactor alert instance struct && support aliyun sms alert (#2586)
Browse files Browse the repository at this point in the history
* Spotless Apply

* refactor alert struct

* format and added i18n

* Spotless Apply

* modify some code

* fix database sql upgrade bug

---------

Co-authored-by: Zzm0809 <[email protected]>
  • Loading branch information
Zzm0809 and Zzm0809 authored Dec 3, 2023
1 parent fbaa3dc commit 5bd3dcb
Show file tree
Hide file tree
Showing 72 changed files with 2,123 additions and 1,487 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.dinky.mybatis.annotation.Save;

import java.util.Map;

import javax.validation.constraints.NotNull;

import io.swagger.annotations.ApiModel;
Expand Down Expand Up @@ -68,5 +70,5 @@ public class AlertInstanceDTO {
required = true,
dataType = "String",
example = "{\"webhook\":\"https://oapi.dingtalk.com/robot/send?access_token=xxxxxx\"}")
private String params;
private Map<String, Object> params;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@

package org.dinky.data.model.alert;

import org.dinky.data.typehandler.JSONObjectHandler;
import org.dinky.mybatis.model.SuperEntity;

import org.apache.ibatis.type.JdbcType;

import java.util.Map;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

import io.swagger.annotations.ApiModelProperty;
Expand Down Expand Up @@ -61,5 +67,6 @@ public class AlertInstance extends SuperEntity<AlertInstance> {
required = true,
dataType = "String",
example = "{\"webhook\":\"https://oapi.dingtalk.com/robot/send?access_token=xxxxxx\"}")
private String params;
@TableField(jdbcType = JdbcType.VARCHAR, typeHandler = JSONObjectHandler.class)
private Map<String, Object> params;
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ private void executeAlertAction(Facts facts, AlertRuleDTO alertRuleDTO) {
*/
private void sendAlert(
AlertInstance alertInstance, int jobInstanceId, int alertGid, String title, String alertMsg) {
Map<String, String> params = JsonUtils.toMap(alertInstance.getParams());
AlertConfig alertConfig = AlertConfig.build(alertInstance.getName(), alertInstance.getType(), params);
AlertConfig alertConfig =
AlertConfig.build(alertInstance.getName(), alertInstance.getType(), alertInstance.getParams());
Alert alert = Alert.build(alertConfig);
AlertResult alertResult = alert.send(title, alertMsg);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.service.impl;

import org.dinky.alert.Alert;
import org.dinky.alert.AlertBaseConstant;
import org.dinky.alert.AlertConfig;
import org.dinky.alert.AlertResult;
import org.dinky.data.dto.AlertInstanceDTO;
Expand All @@ -30,7 +31,6 @@
import org.dinky.mybatis.service.impl.SuperServiceImpl;
import org.dinky.service.AlertGroupService;
import org.dinky.service.AlertInstanceService;
import org.dinky.utils.JsonUtils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
Expand Down Expand Up @@ -68,19 +68,10 @@ public List<AlertInstance> listEnabledAll() {

@Override
public AlertResult testAlert(AlertInstanceDTO alertInstanceDTO) {
AlertConfig alertConfig = AlertConfig.build(
alertInstanceDTO.getName(), alertInstanceDTO.getType(), JsonUtils.toMap(alertInstanceDTO.getParams()));
AlertConfig alertConfig =
AlertConfig.build(alertInstanceDTO.getName(), alertInstanceDTO.getType(), alertInstanceDTO.getParams());
Alert alert = Alert.buildTest(alertConfig);

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)";

return alert.send("Fei Shu Alert Test", msg);
return alert.send(AlertBaseConstant.ALERT_TEMPLATE_TITLE, AlertBaseConstant.ALERT_TEMPLATE_MSG);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dinky-admin/src/main/resources/db/db-h2-ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ CREATE TABLE `dinky_alert_instance` (
`name` varchar(50) NOT null COMMENT 'alert instance name',
`tenant_id` int(11) NOT null DEFAULT 1 COMMENT 'tenant id',
`type` varchar(50) null DEFAULT null COMMENT 'alert instance type such as: DingTalk,Wechat(Webhook,app) Feishu ,email',
`params` text null COMMENT 'configuration',
`params` json null COMMENT 'configuration',
`enabled` tinyint(4) null DEFAULT 1 COMMENT 'is enable',
`create_time` datetime(0) null DEFAULT null COMMENT 'create time',
`update_time` datetime(0) null DEFAULT null COMMENT 'update time',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ public class AlertBaseConstant {
public static final String SECRET = "secret";
public static final String MSG_TYPE = "msgtype";
public static final String AT_MOBILES = "atMobiles";
public static final String AT_USERIDS = "atUserIds";
public static final String AT_ALL = "isAtAll";
public static final String PROXY = "proxy";
public static final String PORT = "port";
public static final String USER = "user";
public static final String AT_USERS = "users";
public static final String AT_USERS = "atUsers";
public static final String PASSWORD = "password";

public static final String ALERT_TEMPLATE_TITLE = "Job Alert Test Title";
public static final String ALERT_TEMPLATE_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)";

public AlertBaseConstant() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class AlertConfig {

private String name;
private String type;
private Map<String, String> param;
private Map<String, Object> param;

public AlertConfig() {}

public AlertConfig(String name, String type, Map<String, String> param) {
public AlertConfig(String name, String type, Map<String, Object> param) {
this.name = name;
this.type = type;
this.param = param;
}

public static AlertConfig build(String name, String type, Map<String, String> param) {
public static AlertConfig build(String name, String type, Map<String, Object> param) {
return new AlertConfig(name, type, param);
}

Expand All @@ -60,11 +60,11 @@ public void setType(String type) {
this.type = type;
}

public Map<String, String> getParam() {
public Map<String, Object> getParam() {
return param;
}

public void setParam(Map<String, String> param) {
public void setParam(Map<String, Object> param) {
this.param = param;
}
}

This file was deleted.

Loading

0 comments on commit 5bd3dcb

Please sign in to comment.