Skip to content

Commit

Permalink
feat : Alarm notification delivery rule name variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 committed Jul 29, 2023
1 parent f69da49 commit e5cd537
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ namespace Masa.Alert.Application.AlarmHistories.EventHandler;
public class NoticeAlarmHandleEventHandler
{
private readonly INotificationSender _notificationSender;
private readonly IAlarmRuleRepository _alarmRuleRepository;

public NoticeAlarmHandleEventHandler(INotificationSender notificationSender)
public NoticeAlarmHandleEventHandler(INotificationSender notificationSender, IAlarmRuleRepository alarmRuleRepository)
{
_notificationSender = notificationSender;
_alarmRuleRepository = alarmRuleRepository;
}

[EventHandler]
public async Task HandleEventAsync(NoticeAlarmHandleEvent eto)
{
var notificationConfig = eto.AlarmHandle.NotificationConfig;

await _notificationSender.SendAsync(notificationConfig);
var variables = new Dictionary<string, object>();
var alarmRule = await _alarmRuleRepository.FindAsync(x => x.Id == eto.AlarmRuleId);
if (alarmRule != null)
{
variables.TryAdd(AlertConsts.ALARM_RULE_NAME_NOTIFICATION_TEMPLATE_VAR_NAME, alarmRule.DisplayName);
}

await _notificationSender.SendAsync(notificationConfig, variables);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ public class SendAlarmNotificationEventHandler
{
private readonly INotificationSender _notificationSender;
private readonly IAlarmHistoryRepository _repository;
private readonly IAlarmRuleRepository _alarmRuleRepository;

public SendAlarmNotificationEventHandler(INotificationSender notificationSender
, IAlarmHistoryRepository repository)
, IAlarmHistoryRepository repository
, IAlarmRuleRepository alarmRuleRepository)
{
_notificationSender = notificationSender;
_repository = repository;
_alarmRuleRepository = alarmRuleRepository;
}

[EventHandler]
Expand All @@ -27,7 +30,14 @@ public async Task HandleEventAsync(SendAlarmNotificationEvent eto)

var notificationConfig = item.AlarmRuleItem.NotificationConfig;

await _notificationSender.SendAsync(notificationConfig);
var variables = new Dictionary<string, object>();
var alarmRule = await _alarmRuleRepository.FindAsync(x => x.Id == eto.AlarmRuleId);
if (alarmRule != null)
{
variables.TryAdd(AlertConsts.ALARM_RULE_NAME_NOTIFICATION_TEMPLATE_VAR_NAME, alarmRule.DisplayName);
}

await _notificationSender.SendAsync(notificationConfig, variables);
}

alarm.Notification();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ public class SendAlarmRecoveryNotificationEventHandler
{
private readonly INotificationSender _notificationSender;
private readonly IAlarmHistoryRepository _repository;
private readonly IAlarmRuleRepository _alarmRuleRepository;

public SendAlarmRecoveryNotificationEventHandler(INotificationSender notificationSender
, IAlarmHistoryRepository repository)
, IAlarmHistoryRepository repository
, IAlarmRuleRepository alarmRuleRepository)
{
_notificationSender = notificationSender;
_repository = repository;
_alarmRuleRepository = alarmRuleRepository;
}

[EventHandler]
Expand All @@ -27,7 +30,13 @@ public async Task HandleEventAsync(SendAlarmRecoveryNotificationEvent eto)

var notificationConfig = item.AlarmRuleItem.RecoveryNotificationConfig;

await _notificationSender.SendAsync(notificationConfig);
var variables = new Dictionary<string, object>();
var alarmRule = await _alarmRuleRepository.FindAsync(x => x.Id == alarm.AlarmRuleId);
if (alarmRule != null)
{
variables.TryAdd(AlertConsts.ALARM_RULE_NAME_NOTIFICATION_TEMPLATE_VAR_NAME, alarmRule.DisplayName);
}
await _notificationSender.SendAsync(notificationConfig, variables);
}
}
}
1 change: 1 addition & 0 deletions src/Domain/Masa.Alert.Domain.Shared/Consts/AlertConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public static class AlertConsts
{
public const string DbTablePrefix = null;
public const string DbSchema = "alert";
public const string ALARM_RULE_NAME_NOTIFICATION_TEMPLATE_VAR_NAME = "Name";
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void SetIsNotification(bool isNotification, bool isSilence)

if (IsNotification && !isSilence)
{
AddDomainEvent(new SendAlarmNotificationEvent(Id));
AddDomainEvent(new SendAlarmNotificationEvent(Id, AlarmRuleId));
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public void Completed(Guid operatorId, string remark)

if (Handle.IsHandleNotice)
{
AddDomainEvent(new NoticeAlarmHandleEvent(Handle));
AddDomainEvent(new NoticeAlarmHandleEvent(Handle, AlarmRuleId));

_handleStatusCommits.Add(new AlarmHandleStatusCommit(AlarmHistoryHandleStatuses.Notified, operatorId, Handle.NotificationConfig.TemplateName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace Masa.Alert.Domain.AlarmHistories.Events;

public record NoticeAlarmHandleEvent(AlarmHandle AlarmHandle) : DomainEvent
public record NoticeAlarmHandleEvent(AlarmHandle AlarmHandle, Guid AlarmRuleId) : DomainEvent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace Masa.Alert.Domain.AlarmRules.Events;

public record SendAlarmNotificationEvent(Guid AlarmHistoryId) : DomainEvent
public record SendAlarmNotificationEvent(Guid AlarmHistoryId, Guid AlarmRuleId) : DomainEvent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Masa.Alert.Domain.NotificationService;

public interface INotificationSender
{
Task SendAsync(NotificationConfig notificationConfig);
Task SendAsync(NotificationConfig notificationConfig, Dictionary<string, object> variables);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ public McNotificationSender(IMcClient mcClient)
{
_mcClient = mcClient;
}
public async Task SendAsync(NotificationConfig notificationConfig)
public async Task SendAsync(NotificationConfig notificationConfig, Dictionary<string, object> variables)
{
if (!notificationConfig.Receivers.Any())
{
return;
}

var request = new BuildingBlocks.StackSdks.Mc.Model.SendTemplateMessageByInternalModel
var request = new SendTemplateMessageByInternalModel
{
ChannelCode = notificationConfig.ChannelCode,
ChannelType = (ChannelTypes)notificationConfig.ChannelType,
TemplateCode = notificationConfig.TemplateCode,
ReceiverType = SendTargets.Assign,
Receivers = notificationConfig.Receivers.Select(x=>new InternalReceiverModel {
Receivers = notificationConfig.Receivers.Select(x => new InternalReceiverModel {
Type = MessageTaskReceiverTypes.User,
SubjectId = x
}).ToList()
}).ToList(),
Variables = new System.Collections.Concurrent.ExtraPropertyDictionary(variables)
};

await _mcClient.MessageTaskService.SendTemplateMessageByInternalAsync(request);
Expand Down

0 comments on commit e5cd537

Please sign in to comment.