Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix AvailableAlertDefineInit - query did not return a unique result #1288

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -57,7 +56,7 @@ public interface AlertDefineDao extends JpaRepository<AlertDefine, Long>, JpaSpe
* @param field field
* @return alert define
*/
Optional<AlertDefine> queryAlertDefineByAppAndMetricAndField(String app, String metric, String field);
List<AlertDefine> queryAlertDefineByAppAndMetricAndField(String app, String metric, String field);

/**
* Query the alarm definition list associated with the monitoring ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.dromara.hertzbeat.alert.AlerterWorkerPool;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver;
import org.dromara.hertzbeat.common.entity.manager.NoticeRule;
import org.dromara.hertzbeat.common.entity.manager.NoticeTemplate;
import org.dromara.hertzbeat.common.queue.CommonDataQueue;
import org.dromara.hertzbeat.manager.service.NoticeConfigService;
Expand Down Expand Up @@ -92,23 +93,15 @@ public boolean sendNoticeMsg(NoticeReceiver receiver, NoticeTemplate noticeTempl
return false;
}

private List<Long> matchReceiverIdByNoticeRules(Alert alert) {
return noticeConfigService.getReceiverIdFilterRule(alert);
private NoticeReceiver getOneReceiverById(Long id) {
return noticeConfigService.getOneReceiverById(id);
}

private List<Long> matchTemplateIdByNoticeRules(Alert alert) {
return noticeConfigService.getTemplateIdFilterRule(alert);
private NoticeTemplate getOneTemplateById(Long id) {
return noticeConfigService.getOneTemplateById(id);
}

private NoticeReceiver getOneReciverById(List<Long> ids) {
return noticeConfigService.getOneReciverByIdInFilterRule(ids);
}

private NoticeTemplate getOneTemplateById(List<Long> ids) {
return noticeConfigService.getOneTemplateByIdInFilterRule(ids);
}

private List<NoticeReceiver> matchReceiverByNoticeRules(Alert alert) {
private List<NoticeRule> matchNoticeRulesByAlert(Alert alert) {
return noticeConfigService.getReceiverFilterRule(alert);
}

Expand All @@ -135,25 +128,24 @@ public void run() {
}

private void sendNotify(Alert alert) {
List<Long> receivers = matchReceiverIdByNoticeRules(alert);
List<Long> templates = matchTemplateIdByNoticeRules(alert);
List<NoticeRule> noticeRules = matchNoticeRulesByAlert(alert);
// todo Send notification here temporarily single thread 发送通知这里暂时单线程
for (int i = 0; i < receivers.size(); i++) {
try {
if((templates.subList(i, i + 1)).get(0)==null){
sendNoticeMsg(getOneReciverById(receivers.subList(i, i + 1)),
null, alert);
}
else {
sendNoticeMsg(getOneReciverById(receivers.subList(i, i + 1)),
getOneTemplateById(templates.subList(i, i + 1)), alert);
if (noticeRules != null) {
for (NoticeRule rule : noticeRules) {
try {
if (rule.getTemplateId() == null) {
sendNoticeMsg(getOneReceiverById(rule.getReceiverId()),
null, alert);
} else {
sendNoticeMsg(getOneReceiverById(rule.getReceiverId()),
getOneTemplateById(rule.getTemplateId()), alert);
}
} catch (AlertNoticeException e) {
log.warn("DispatchTask sendNoticeMsg error, message: {}", e.getMessage());
}
} catch (AlertNoticeException e) {
log.warn("DispatchTask sendNoticeMsg error, message: {}", e.getMessage());
}
}

}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

/**
* @author <a href="mailto:gcwm99@gmail.com">gcdd1993</a>
* @version 2.1
Expand All @@ -61,7 +62,7 @@ abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler {
protected String renderContent(NoticeTemplate noticeTemplate, Alert alert) throws TemplateException, IOException {

StringTemplateLoader stringLoader = new StringTemplateLoader();
freemarker.template.Template templateRes=null;
freemarker.template.Template templateRes = null;
Configuration cfg = new Configuration();
Map<String, String> model = new HashMap<>(16);
model.put("title", bundle.getString("alerter.notify.title"));
Expand All @@ -87,17 +88,16 @@ protected String renderContent(NoticeTemplate noticeTemplate, Alert alert) throw
model.put("triggerTime", DTF.format(Instant.ofEpochMilli(alert.getLastAlarmTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()));
model.put("contentLabel", bundle.getString("alerter.notify.content"));
model.put("content", alert.getContent());
if(noticeTemplate==null){
if (noticeTemplate == null) {
String path = this.getClass().getResource("/").getPath();
cfg.setDirectoryForTemplateLoading(new File(path+"templates/"));
cfg.setDirectoryForTemplateLoading(new File(path + "templates/"));
cfg.setDefaultEncoding("utf-8");
templateRes = cfg.getTemplate(templateName()+".txt");
}
else {
templateRes = cfg.getTemplate(templateName() + ".txt");
} else {
String templateName = "freemakerTemplate";
stringLoader.putTemplate(templateName, noticeTemplate.getTemplateContent());
cfg.setTemplateLoader(stringLoader);
templateRes= cfg.getTemplate(templateName, Locale.CHINESE);
templateRes = cfg.getTemplate(templateName, Locale.CHINESE);
}
String template = FreeMarkerTemplateUtils.processTemplateIntoString(templateRes, model);
return template.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -34,8 +34,8 @@ public void run(String... args) throws Exception {
Set<String> apps = appService.getAllAppDefines().keySet();
for (String app : apps) {
try {
Optional<AlertDefine> optional = alertDefineDao.queryAlertDefineByAppAndMetricAndField(app, CommonConstants.AVAILABILITY, null);
if (optional.isEmpty()) {
List<AlertDefine> defines = alertDefineDao.queryAlertDefineByAppAndMetricAndField(app, CommonConstants.AVAILABILITY, null);
if (defines.isEmpty()) {
AlertDefine alertDefine = AlertDefine.builder()
.app(app)
.metric(CommonConstants.AVAILABILITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,43 +117,25 @@ public interface NoticeConfigService {
* @param alert Alarm information 告警信息
* @return Receiver 接收人
*/
List<NoticeReceiver> getReceiverFilterRule(Alert alert);
List<NoticeRule> getReceiverFilterRule(Alert alert);

/**
* According to the alarm information matching all notification policies, filter out the recipients' ID who need to be notified
* 根据告警信息与所有通知策略匹配,过滤出需要通知的接收人ID
* Query the recipient information according to the recipient ID
* 根据接收人ID查询接收人信息
*
* @param alert Alarm information 告警信息
* @return Receiver ID 接收人
*/
List<Long> getReceiverIdFilterRule(Alert alert);

/**
* According to the alarm information matching all notification policies, filter out the templates' ID
* 根据告警信息与所有通知策略匹配,过滤出需要通知模板ID
*
* @param alert Alarm information 告警信息
* @return Template ID 通知模板
*/
List<Long> getTemplateIdFilterRule(Alert alert);

/**
* Query the recipient information according to the recipient ID List
* 根据接收人ID列表查询接收人信息
*
* @param ids Reciver ID List 接收人ID列表
* @param id Receiver ID 接收人ID
* @return Receiver 接收人
*/
NoticeReceiver getOneReciverByIdInFilterRule(List<Long> ids);
NoticeReceiver getOneReceiverById(Long id);

/**
* Query the template information according to the template ID List
* 根据通知模板ID列表查询模板信息
* Query the template information according to the template ID
* 根据通知模板ID查询模板信息
*
* @param ids Template ID List 接收人ID列表
* @param id Template ID List 接收人ID
* @return Template 通知模板
*/
NoticeTemplate getOneTemplateByIdInFilterRule(List<Long> ids);
NoticeTemplate getOneTemplateById(Long id);

/**
* Query recipient information based on recipient ID (primary key Id)
Expand Down
Loading