Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[manager,webapp]feature: enable alerter send test msg button (#117)
Browse files Browse the repository at this point in the history
* [manager]feature: add alert send test msg

* [manager]feature: add alert send test msg

* [monitor]feature: alert test notice

Co-authored-by: xgf <13386772885@163.com>
tomsun28 and a25017012 authored May 8, 2022
1 parent 5d496bf commit 44c7dc1
Showing 14 changed files with 147 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -53,6 +53,25 @@ public void afterPropertiesSet() throws Exception {
}
}

/**
* send alert msg to receiver
* @param receiver receiver
* @param alert alert msg
* @return send success or failed
*/
public boolean sendNoticeMsg(NoticeReceiver receiver, Alert alert){
if(receiver == null || receiver.getType() == null){
log.warn("DispatcherAlarm-sendNoticeMsg params is empty alert:[{}], receiver:[{}]", alert, receiver);
return false;
}
byte type = receiver.getType();
if (alertNotifyHandlerMap.containsKey(type)) {
alertNotifyHandlerMap.get(type).send(receiver, alert);
return true;
}
return false;
}

private List<NoticeReceiver> matchReceiverByNoticeRules(Alert alert) {
// todo use cache 使用缓存
return noticeConfigService.getReceiverFilterRule(alert);
@@ -82,13 +101,9 @@ private void sendNotify(Alert alert) {
List<NoticeReceiver> receivers = matchReceiverByNoticeRules(alert);
// todo Send notification here temporarily single thread 发送通知这里暂时单线程
for (NoticeReceiver receiver : receivers) {
byte type = receiver.getType();
if (alertNotifyHandlerMap.containsKey(type)) {
alertNotifyHandlerMap.get(type).send(receiver, alert);
}
// 暂未支持的通知类型
sendNoticeMsg(receiver, alert);
}
}

}

}
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import com.usthe.manager.service.MailService;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@@ -46,6 +47,7 @@ public void send(NoticeReceiver receiver, Alert alert) {
javaMailSender.send(mimeMessage);
} catch (Exception e) {
log.error("[Email Alert] Exception,Exception information={}", e.getMessage());
throw new AlertNoticeException("[Email Alert] failed: " + e.getMessage());
}
}

Original file line number Diff line number Diff line change
@@ -136,4 +136,12 @@ public ResponseEntity<Message<List<NoticeRule>>> getRules(
return ResponseEntity.ok(message);
}


@PostMapping(path = "/receiver/send-test-msg")
@ApiOperation(value = "Send test msg to receiver", notes = "给指定接收人发送测试消息")
public ResponseEntity<Message<Boolean>> sendTestMsg(@Valid @RequestBody NoticeReceiver noticeReceiver) {
boolean sendFlag = noticeConfigService.sendTestMsg(noticeReceiver);
return ResponseEntity.ok(new Message<>(sendFlag));
}

}
Original file line number Diff line number Diff line change
@@ -108,4 +108,12 @@ public interface NoticeConfigService {
* @return Notification Rule Entity 通知规则实体
*/
NoticeRule getNoticeRulesById(Long ruleId);

/**
* alert Send test message
* 告警 发送测试消息
* @param noticeReceiver recipient information 接收人信息
* @return true send success | false send fail
*/
boolean sendTestMsg(NoticeReceiver noticeReceiver);
}
Original file line number Diff line number Diff line change
@@ -31,8 +31,12 @@ public class MailServiceImpl implements MailService {

@Override
public String buildAlertHtmlTemplate(final Alert alert) {
String monitorId = alert.getTags().get(CommonConstants.TAG_MONITOR_ID);
String monitorName = alert.getTags().get(CommonConstants.TAG_MONITOR_NAME);
String monitorId = null;
String monitorName = null;
if (alert.getTags() != null) {
monitorId = alert.getTags().get(CommonConstants.TAG_MONITOR_ID);
monitorName = alert.getTags().get(CommonConstants.TAG_MONITOR_NAME);
}
monitorId = monitorId == null? "External alarm, no ID" : monitorId;
monitorName = monitorName == null? "External alarm, no Name" : monitorName;
// Introduce thymeleaf context parameters to render pages
Original file line number Diff line number Diff line change
@@ -3,13 +3,16 @@
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.util.CommonConstants;
import com.usthe.manager.component.alerter.DispatcherAlarm;
import com.usthe.manager.dao.NoticeReceiverDao;
import com.usthe.manager.dao.NoticeRuleDao;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.common.entity.manager.NoticeRule;
import com.usthe.manager.service.NoticeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -30,12 +33,20 @@
@Slf4j
public class NoticeConfigServiceImpl implements NoticeConfigService {

private static final String ALERT_TEST_TARGET = "Test Target";

private static final String ALERT_TEST_CONTENT = "test send msg! \n This is the test data. It is proved that it can be received successfully";

@Autowired
private NoticeReceiverDao noticeReceiverDao;

@Autowired
private NoticeRuleDao noticeRuleDao;

@Autowired
@Lazy
private DispatcherAlarm dispatcherAlarm;

@Override
public List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification) {
return noticeReceiverDao.findAll(specification);
@@ -50,6 +61,14 @@ public List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification)
public void addReceiver(NoticeReceiver noticeReceiver) {
noticeReceiverDao.save(noticeReceiver);
}
@Override
public boolean sendTestMsg(NoticeReceiver noticeReceiver) {
Alert alert = new Alert();
alert.setContent(CommonConstants.TEST_SEND_MSG);
alert.setId(0L);
return dispatcherAlarm.sendNoticeMsg(noticeReceiver, alert);
}


@Override
public void editReceiver(NoticeReceiver noticeReceiver) {
@@ -115,4 +134,13 @@ public NoticeReceiver getReceiverById(Long receiverId) {
public NoticeRule getNoticeRulesById(Long ruleId) {
return noticeRuleDao.getOne(ruleId);
}

@Override
public boolean sendTestMsg(NoticeReceiver noticeReceiver) {
Alert alert = new Alert();
alert.setTarget(ALERT_TEST_TARGET);
alert.setContent(ALERT_TEST_CONTENT);
alert.setPriority(CommonConstants.ALERT_PRIORITY_CODE_CRITICAL);
return dispatcherAlarm.sendNoticeMsg(noticeReceiver, alert);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.usthe.manager.support;

import com.usthe.common.entity.dto.Message;
import com.usthe.manager.support.exception.AlertNoticeException;
import com.usthe.manager.support.exception.MonitorDatabaseException;
import com.usthe.manager.support.exception.MonitorDetectException;
import lombok.extern.slf4j.Slf4j;
@@ -18,9 +19,7 @@

import java.lang.reflect.Field;

import static com.usthe.common.util.CommonConstants.DETECT_FAILED_CODE;
import static com.usthe.common.util.CommonConstants.MONITOR_CONFLICT_CODE;
import static com.usthe.common.util.CommonConstants.PARAM_INVALID_CODE;
import static com.usthe.common.util.CommonConstants.*;

/**
* controller exception handler
@@ -82,6 +81,13 @@ ResponseEntity<Message<Void>> handleIllegalArgumentException(IllegalArgumentExce
return ResponseEntity.ok(message);
}

@ExceptionHandler(AlertNoticeException.class)
@ResponseBody
ResponseEntity<Message<Void>> handleAlertNoticeException(AlertNoticeException noticeException) {
Message<Void> message = Message.<Void>builder().msg(noticeException.getMessage()).code(FAIL_CODE).build();
return ResponseEntity.ok(message);
}

/**
* 处理请求参数错误的失败, 请求参数json映射body时出错
* @param exception 参数映射body异常
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.usthe.manager.support.exception;

/**
* alert notice send failed
* 告警通知发送异常
* @author tom
* @date 2022/5/8 17:59
*/
public class AlertNoticeException extends RuntimeException {
public AlertNoticeException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -285,6 +285,14 @@
<input [(ngModel)]="receiver.wechatId" nz-input [required]="receiver.type === 6" name="wechatId" type="text" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-control [nzOffset]="7" [nzSpan]="12">
<button nz-button nzDanger nzType="primary" [nzLoading]="isSendTestButtonLoading" (click)="onSendAlertTestMsg()">
<i nz-icon nzType="send" nzTheme="outline"></i>
{{ 'alert.notice.send-test' | i18n }}
</button>
</nz-form-control>
</nz-form-item>
</form>
</div>
</nz-modal>
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import { NoticeRule } from '../../../pojo/NoticeRule';
import { NoticeReceiverService } from '../../../service/notice-receiver.service';
import { NoticeRuleService } from '../../../service/notice-rule.service';
import { TagService } from '../../../service/tag.service';
import { Tag } from '../../../pojo/Tag';

@Component({
selector: 'app-alert-notice',
@@ -154,6 +153,7 @@ export class AlertNoticeComponent implements OnInit {
isManageReceiverModalVisible: boolean = false;
isManageReceiverModalAdd: boolean = true;
isManageReceiverModalOkLoading: boolean = false;
isSendTestButtonLoading: boolean = false;
receiver!: NoticeReceiver;

onNewNoticeReceiver() {
@@ -167,6 +167,32 @@ export class AlertNoticeComponent implements OnInit {
this.isManageReceiverModalAdd = false;
}

onSendAlertTestMsg() {
this.isSendTestButtonLoading = true;
const sendReq$ = this.noticeReceiverSvc
.sendAlertMsgToReceiver(this.receiver)
.pipe(
finalize(() => {
sendReq$.unsubscribe();
this.isSendTestButtonLoading = false;
})
)
.subscribe(
message => {
if (message.code === 0) {
this.isSendTestButtonLoading = false;
this.notifySvc.success(this.i18nSvc.fanyi('alert.notice.send-test.notify.success'), '');
} else {
this.notifySvc.error(this.i18nSvc.fanyi('alert.notice.send-test.notify.failed'), message.msg);
}
},
error => {
this.isSendTestButtonLoading = false;
this.notifySvc.error(this.i18nSvc.fanyi('alert.notice.send-test.notify.failed'), error.msg);
}
);
}

onManageReceiverModalCancel() {
this.isManageReceiverModalVisible = false;
}
@@ -192,6 +218,7 @@ export class AlertNoticeComponent implements OnInit {
}
},
error => {
this.isManageReceiverModalVisible = false;
this.notifySvc.error(this.i18nSvc.fanyi('common.notify.new-fail'), error.msg);
}
);
@@ -215,6 +242,7 @@ export class AlertNoticeComponent implements OnInit {
}
},
error => {
this.isManageReceiverModalVisible = false;
this.notifySvc.error(this.i18nSvc.fanyi('common.notify.edit-fail'), error.msg);
}
);
6 changes: 6 additions & 0 deletions web-app/src/app/service/notice-receiver.service.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { NoticeReceiver } from '../pojo/NoticeReceiver';

const notice_receiver_uri = '/notice/receiver';
const notice_receivers_uri = '/notice/receivers';
const notice_receiver_send_test_msg_uri = '/notice/receiver/send-test-msg';

@Injectable({
providedIn: 'root'
@@ -21,11 +22,16 @@ export class NoticeReceiverService {
public editReceiver(body: NoticeReceiver): Observable<Message<any>> {
return this.http.put<Message<any>>(notice_receiver_uri, body);
}

public deleteReceiver(receiverId: number): Observable<Message<any>> {
return this.http.delete<Message<any>>(`${notice_receiver_uri}/${receiverId}`);
}

public getReceivers(): Observable<Message<NoticeReceiver[]>> {
return this.http.get<Message<NoticeReceiver[]>>(notice_receivers_uri);
}

public sendAlertMsgToReceiver(body: NoticeReceiver): Observable<Message<any>> {
return this.http.post<Message<any>>(notice_receiver_send_test_msg_uri, body);
}
}
3 changes: 3 additions & 0 deletions web-app/src/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
@@ -176,6 +176,9 @@
"alert.notice.rule.enable": "Enable",
"alert.notice.rule.tag": "Tag Filter",
"alert.notice.rule.priority": "Priority Filter",
"alert.notice.send-test": "Send Alert Test Msg",
"alert.notice.send-test.notify.success": "Send Alert Test Success!",
"alert.notice.send-test.notify.failed": "Send Alert Test Failed!",
"dashboard.alerts.title": "Recently Alerts List",
"dashboard.alerts.title-no": "Recently Pending Alerts",
"dashboard.alerts.no": "No Pending Alerts",
3 changes: 3 additions & 0 deletions web-app/src/assets/i18n/zh-CN.json
Original file line number Diff line number Diff line change
@@ -176,6 +176,9 @@
"alert.notice.rule.enable": "是否启用",
"alert.notice.rule.tag": "标签过滤",
"alert.notice.rule.priority": "级别过滤",
"alert.notice.send-test": "发送告警测试",
"alert.notice.send-test.notify.success": "触发告警测试成功!",
"alert.notice.send-test.notify.failed": "触发告警测试失败!",
"dashboard.alerts.title": "最近告警列表",
"dashboard.alerts.title-no": "最近未处理告警",
"dashboard.alerts.no": "暂无未处理告警",
3 changes: 3 additions & 0 deletions web-app/src/assets/i18n/zh-TW.json
Original file line number Diff line number Diff line change
@@ -176,6 +176,9 @@
"alert.notice.rule.enable": "是否啓用",
"alert.notice.rule.tag": "標簽過濾",
"alert.notice.rule.priority": "級別過濾",
"alert.notice.send-test": "發送告警測試",
"alert.notice.send-test.notify.success": "觸發告警測試成功!",
"alert.notice.send-test.notify.failed": "觸發告警測試失敗!",
"dashboard.alerts.title": "最近告警列表",
"dashboard.alerts.title-no": "最近未處理告警",
"dashboard.alerts.no": "暫無未處理告警",

0 comments on commit 44c7dc1

Please sign in to comment.