Skip to content

Commit

Permalink
feat: alert template (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
masaimu authored Jan 24, 2024
1 parent 64da119 commit 7e8a3a8
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public void load() throws Exception {
sc.metaDataDictValueMap = metaDictValueService.getMetaDictValue();
sc.expressionMetricList = metricInfoService.querySpmList();
sc.workspaceTenantMap = new HashMap<>();
List<MonitorInstance> instances = monitorInstanceService.list();
QueryWrapper<MonitorInstance> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("deleted", false);
List<MonitorInstance> instances = monitorInstanceService.list(queryWrapper);
instances.forEach(
instance -> sc.workspaceTenantMap.put(instance.getWorkspace(), instance.getTenant()));
queryMetricInfoByPage(sc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

-- ----------------------------
-- Table structure for alarm_rule
-- ----------------------------
ALTER TABLE `alarm_rule`
ADD COLUMN `alert_notification_template_id` BIGINT(20) NULL DEFAULT NULL comment '告警模板ID' AFTER `env_type`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

-- ----------------------------
-- Table structure for alert_notification_template
-- ----------------------------
CREATE TABLE `alert_notification_template` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT comment '主键',
`gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment '创建时间',
`gmt_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间',
`template_name` varchar(255) NULL comment '模板名称',
`scene_type` varchar(255) NULL comment '场景类型:server、miniapp、iot',
`be_default` tinyint(4) NULL comment '是否为默认模板',
`channel_type` varchar(255) NULL comment '发送渠道:dingtalk、sms',
`template_config` mediumtext NULL comment '模板配置',
`tenant` varchar(255) NULL comment '租户',
`workspace` varchar(255) NULL comment '工作空间',
`creator` varchar(255) NULL comment '创建者',
`modifier` varchar(255) NULL comment '修改者',
PRIMARY KEY(`id`)
) DEFAULT CHARSET = utf8mb4 COMMENT = '告警模板';
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ private Builder(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
}

private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
}
// if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
// }
}

@java.lang.Override
Expand Down Expand Up @@ -1588,8 +1588,8 @@ private Builder(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
}

private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
}
// if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
// }
}

@java.lang.Override
Expand Down Expand Up @@ -5053,8 +5053,8 @@ private Builder(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
}

private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
}
// if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
// }
}

@java.lang.Override
Expand Down Expand Up @@ -6778,8 +6778,8 @@ private Builder(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
}

private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
}
// if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
// }
}

@java.lang.Override
Expand Down Expand Up @@ -7881,8 +7881,8 @@ private Builder(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
}

private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
}
// if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
// }
}

@java.lang.Override
Expand Down Expand Up @@ -9141,8 +9141,8 @@ private Builder(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
}

private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
}
// if (com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders) {
// }
}

@java.lang.Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.dal.converter;

import io.holoinsight.server.common.J;
import io.holoinsight.server.common.dao.transformer.MapJsonMapper;
import io.holoinsight.server.home.dal.model.AlertNotificationTemplate;
import io.holoinsight.server.home.facade.AlertNotificationTemplateDTO;
import io.holoinsight.server.home.facade.NotificationTemplate;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;

import java.util.List;

/**
* @author masaimu
* @version 2024-01-22 17:03:00
*/
@Mapper(componentModel = "spring", uses = {MapJsonMapper.class})
public interface AlertNotificationTemplateConverter {

AlertNotificationTemplateDTO doToDTO(AlertNotificationTemplate template);

AlertNotificationTemplate dtoToDO(AlertNotificationTemplateDTO templateDTO);

List<AlertNotificationTemplateDTO> dosToDTOs(Iterable<AlertNotificationTemplate> templates);

default String map(NotificationTemplate value) {
if (value == null) {
return null;
}
return J.toJson(value);
}

default NotificationTemplate map(String value) {
if (StringUtils.isBlank(value)) {
return null;
}
return J.fromJson(value, NotificationTemplate.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.dal.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.holoinsight.server.home.dal.model.AlertNotificationTemplate;

/**
* @author masaimu
* @version 2024-01-22 16:56:00
*/
public interface AlertNotificationTemplateMapper extends BaseMapper<AlertNotificationTemplate> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,10 @@ public class AlarmRule {
@Column(name = "env_type")
private String envType;

/**
* 告警模板ID
*/
@Column(name = "alert_notification_template_id")
private Long alertNotificationTemplateId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.dal.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Table;
import java.util.Date;

/**
* @author masaimu
* @version 2024-01-22 16:48:00
*/
@Data
@Table(name = "alert_notification_template")
public class AlertNotificationTemplate {

/**
* id
*/
@TableId(type = IdType.AUTO)
public Long id;

/**
* 创建时间
*/
@Column(name = "gmt_create")
public Date gmtCreate;

/**
* 修改时间
*/
@Column(name = "gmt_modified")
public Date gmtModified;

public String templateName;
public String sceneType;
public boolean beDefault;
public String channelType;
public String templateConfig;
public String tenant;
public String workspace;
public String creator;
public String modifier;

}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public class AlarmRuleDTO extends ApiSecurity {
*/
private String pql;

/**
* 告警模板ID
*/
private Long alertNotificationTemplateId;

public static void tryParseLink(AlarmRuleDTO alarmRuleDTO, String domain,
Map<String /* metric */, Map<String /* type */, String /* page */>> systemMetrics,
String parentId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.facade;

import lombok.Data;

import java.util.Date;

/**
* @author masaimu
* @version 2024-01-22 17:02:00
*/
@Data
public class AlertNotificationTemplateDTO {
/**
* id
*/
public Long id;

/**
* 创建时间
*/
public Date gmtCreate;

/**
* 修改时间
*/
public Date gmtModified;
public String templateName;

public String sceneType;
public boolean beDefault;
public String channelType;
public NotificationTemplate templateConfig;
public String tenant;
public String workspace;
public String creator;
public String modifier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class NotificationTemplate {

public LinkedHashMap<String, AlertTemplateField> fieldMap = new LinkedHashMap<>();
public LinkedHashMap<String, String> tagMap = new LinkedHashMap<>();

public static NotificationTemplate defaultWebhookTemplate() {
NotificationTemplate template = new NotificationTemplate();
Expand All @@ -44,16 +45,34 @@ public static NotificationTemplate defaultDingtalkTemplate() {
template.fieldMap = new LinkedHashMap<>();
template.fieldMap.put("告警规则名称", AlertTemplateField.ruleName);
template.fieldMap.put("告警严重度", AlertTemplateField.ALERT_PRIORITY);
// template.fieldMap.put("告警标题", AlertTemplateField.ALERT_TITLE);
template.fieldMap.put("告警内容", AlertTemplateField.EVENT_MSG);
template.fieldMap.put("告警对象", AlertTemplateField.ALERT_SCOPE);
template.fieldMap.put("告警首次触发时间", AlertTemplateField.ALERT_START_TIME);
template.fieldMap.put("此次评估触发时间", AlertTemplateField.alarmTime);
// template.fieldMap.put("告警触发条件", AlertTemplateField.ALERT_TRIGGER_CONDITION);
template.fieldMap.put("告警触发数值", AlertTemplateField.ALERT_VALUE);
template.fieldMap.put("日志内容", AlertTemplateField.LOG_CONTENT);
// template.fieldMap.put("[详情]", AlertTemplateField.LINK);
// template.fieldMap.put("[设置]", AlertTemplateField.ruleUrl);

return template;
}

public static NotificationTemplate defaultMiniappDingtalkTemplate(TemplateValue templateValue) {
NotificationTemplate template = new NotificationTemplate();
template.fieldMap = new LinkedHashMap<>();
template.fieldMap.put("告警规则名称", AlertTemplateField.ruleName);
template.fieldMap.put("PID", AlertTemplateField.PID);
template.fieldMap.put("租户名", AlertTemplateField.TENANT_NAME);
template.fieldMap.put("小程序名", AlertTemplateField.WORKSPACE_NAME);
template.fieldMap.put("告警严重度", AlertTemplateField.ALERT_PRIORITY);
template.fieldMap.put("告警内容", AlertTemplateField.EVENT_MSG);
template.fieldMap.put("告警对象", AlertTemplateField.ALERT_SCOPE);
template.fieldMap.put("告警首次触发时间", AlertTemplateField.ALERT_START_TIME);
template.fieldMap.put("此次评估触发时间", AlertTemplateField.alarmTime);
template.fieldMap.put("告警触发数值", AlertTemplateField.ALERT_VALUE);
template.fieldMap.put("聚合条数", AlertTemplateField.aggregationNum);
template.fieldMap.put("告警来源", AlertTemplateField.SOURCE_TYPE);
if (templateValue != null && StringUtils.isNotEmpty(templateValue.getLogContent())) {
template.fieldMap.put("日志内容", AlertTemplateField.LOG_CONTENT);
}

return template;
}
Expand Down
Loading

0 comments on commit 7e8a3a8

Please sign in to comment.