Skip to content

Commit

Permalink
feat : Alarm notification delivery rule name variable name (#331)
Browse files Browse the repository at this point in the history
* feat : Alarm notification delivery rule name variable name

* fix : OnChange =》 OnBlur

* reactor : Unified constant naming convention
  • Loading branch information
wzh425 authored Jul 31, 2023
1 parent f69da49 commit 82ef358
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 34 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);
}
}
}
5 changes: 3 additions & 2 deletions src/Domain/Masa.Alert.Domain.Shared/Consts/AlertConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Masa.Alert.Domain.Shared.Consts;

public static class AlertConsts
{
public const string DbTablePrefix = null;
public const string DbSchema = "alert";
public const string DB_TABLE_PREFIX = null;
public const string DB_SCHEMA = "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 @@ -21,7 +21,7 @@ protected override void OnModelCreatingExecuting(ModelBuilder builder)
{
builder.Entity<AlarmRuleQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "AlarmRules", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "AlarmRules", AlertConsts.DB_SCHEMA);
b.HasMany(x => x.LogMonitorItems).WithOne().HasForeignKey(x => x.AlarmRuleId).IsRequired();
b.HasMany(x => x.MetricMonitorItems).WithOne().HasForeignKey(x => x.AlarmRuleId).IsRequired();
b.HasMany(x => x.Items).WithOne().HasForeignKey(x => x.AlarmRuleId).IsRequired();
Expand Down Expand Up @@ -50,14 +50,14 @@ protected override void OnModelCreatingExecuting(ModelBuilder builder)

builder.Entity<AlarmRuleRecordQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "AlarmRuleRecords", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleRecords", AlertConsts.DB_SCHEMA);
b.Property(x => x.AggregateResult).HasConversion(new JsonValueConverter<ConcurrentDictionary<string, long>>());
b.Property(x => x.RuleResultItems).HasConversion(new JsonValueConverter<List<RuleResultItemQueryModel>>());
});

builder.Entity<AlarmHistoryQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "AlarmHistorys", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "AlarmHistorys", AlertConsts.DB_SCHEMA);
b.HasOne(x => x.AlarmRule);
b.Property(x => x.HandleNotificationConfig).HasConversion(new JsonValueConverter<NotificationConfigQueryModel>());
b.Property(x => x.RuleResultItems).HasConversion(new JsonValueConverter<List<RuleResultItemQueryModel>>());
Expand All @@ -66,12 +66,12 @@ protected override void OnModelCreatingExecuting(ModelBuilder builder)

builder.Entity<LogMonitorItemQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "AlarmRuleLogMonitors", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleLogMonitors", AlertConsts.DB_SCHEMA);
});

builder.Entity<MetricMonitorItemQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "AlarmRuleMetricMonitors", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleMetricMonitors", AlertConsts.DB_SCHEMA);
b.OwnsOne(x => x.Aggregation, b =>
{
b.Property(x => x.Name).HasColumnName("Name");
Expand All @@ -84,19 +84,19 @@ protected override void OnModelCreatingExecuting(ModelBuilder builder)

builder.Entity<AlarmRuleItemQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "AlarmRuleItems", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleItems", AlertConsts.DB_SCHEMA);
b.Property(x => x.RecoveryNotificationConfig).HasConversion(new JsonValueConverter<NotificationConfigQueryModel>());
b.Property(x => x.NotificationConfig).HasConversion(new JsonValueConverter<NotificationConfigQueryModel>());
});

builder.Entity<AlarmHandleStatusCommitQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "AlarmHandleStatusCommits", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "AlarmHandleStatusCommits", AlertConsts.DB_SCHEMA);
});

builder.Entity<WebHookQueryModel>(b =>
{
b.ToView(AlertConsts.DbTablePrefix + "WebHooks", AlertConsts.DbSchema);
b.ToView(AlertConsts.DB_TABLE_PREFIX + "WebHooks", AlertConsts.DB_SCHEMA);
});

base.OnModelCreatingExecuting(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public class AlarmHistoryConfiguration : IEntityTypeConfiguration<AlarmHistory>
{
public void Configure(EntityTypeBuilder<AlarmHistory> builder)
{
builder.ToTable(AlertConsts.DbTablePrefix + "AlarmHistorys", AlertConsts.DbSchema);
builder.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmHistorys", AlertConsts.DB_SCHEMA);
builder.Property(x => x.RuleResultItems).HasConversion(new JsonValueConverter<List<RuleResultItem>>());
builder.OwnsMany(x => x.HandleStatusCommits, b =>
{
b.ToTable(AlertConsts.DbTablePrefix + "AlarmHandleStatusCommits", AlertConsts.DbSchema);
b.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmHandleStatusCommits", AlertConsts.DB_SCHEMA);
b.Property<Guid>("Id").ValueGeneratedOnAdd();
b.HasKey("Id");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ public class AlarmRuleConfiguration : IEntityTypeConfiguration<AlarmRule>
{
public void Configure(EntityTypeBuilder<AlarmRule> builder)
{
builder.ToTable(AlertConsts.DbTablePrefix + "AlarmRules", AlertConsts.DbSchema);
builder.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmRules", AlertConsts.DB_SCHEMA);
builder.Property(x => x.DisplayName).IsRequired().HasMaxLength(128);
builder.Property(x => x.ProjectIdentity).HasMaxLength(128);
builder.Property(x => x.AppIdentity).HasMaxLength(128);
builder.Property(x => x.ChartYAxisUnit).HasMaxLength(128);
builder.Property(x => x.TotalVariable).HasMaxLength(64);
builder.OwnsMany(x => x.LogMonitorItems, b =>
{
b.ToTable(AlertConsts.DbTablePrefix + "AlarmRuleLogMonitors", AlertConsts.DbSchema);
b.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleLogMonitors", AlertConsts.DB_SCHEMA);
b.Property<Guid>("Id").ValueGeneratedOnAdd();
b.HasKey("Id");
b.Property(x => x.AggregationType).HasConversion(v => v.Id, v => Enumeration.FromValue<Domain.AlarmRules.LogAggregationType>(v));
});
builder.OwnsMany(x => x.MetricMonitorItems, b =>
{
b.ToTable(AlertConsts.DbTablePrefix + "AlarmRuleMetricMonitors", AlertConsts.DbSchema);
b.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleMetricMonitors", AlertConsts.DB_SCHEMA);
b.Property<Guid>("Id").ValueGeneratedOnAdd();
b.HasKey("Id");
b.OwnsOne(x => x.Aggregation, b =>
Expand All @@ -36,7 +36,7 @@ public void Configure(EntityTypeBuilder<AlarmRule> builder)
});
builder.OwnsMany(x => x.Items, b =>
{
b.ToTable(AlertConsts.DbTablePrefix + "AlarmRuleItems", AlertConsts.DbSchema);
b.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleItems", AlertConsts.DB_SCHEMA);
b.Property<Guid>("Id").ValueGeneratedOnAdd();
b.HasKey("Id");
b.Property(x => x.RecoveryNotificationConfig).HasConversion(new JsonValueConverter<NotificationConfig>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AlarmRuleRecordConfiguration : IEntityTypeConfiguration<AlarmRuleRe
{
public void Configure(EntityTypeBuilder<AlarmRuleRecord> builder)
{
builder.ToTable(AlertConsts.DbTablePrefix + "AlarmRuleRecords", AlertConsts.DbSchema);
builder.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleRecords", AlertConsts.DB_SCHEMA);
builder.Property(x => x.AggregateResult).HasConversion(new JsonValueConverter<ConcurrentDictionary<string, long>>());
builder.Property(x => x.RuleResultItems).HasConversion(new JsonValueConverter<List<RuleResultItem>>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class WebHookConfiguration : IEntityTypeConfiguration<WebHook>
{
public void Configure(EntityTypeBuilder<WebHook> builder)
{
builder.ToTable(AlertConsts.DbTablePrefix + "WebHooks", AlertConsts.DbSchema);
builder.ToTable(AlertConsts.DB_TABLE_PREFIX + "WebHooks", AlertConsts.DB_SCHEMA);
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
@if (_model.CheckFrequency.Type == AlarmCheckFrequencyTypes.Cron)
{
<MCol Md="8">
<STextField Class="ml-6" @bind-Value="_model.CheckFrequency.CronExpression" Label="@T(nameof(_model.CheckFrequency.CronExpression))" AppendIcon="mdi-clock" OnAppendClick="OpenCronModal" OnChange="(string v)=>GetNextRunTime()" />
<STextField Class="ml-6" @bind-Value="_model.CheckFrequency.CronExpression" Label="@T(nameof(_model.CheckFrequency.CronExpression))" AppendIcon="mdi-clock" OnAppendClick="OpenCronModal" OnBlur="()=>GetNextRunTime()" />
</MCol>
}
</MRow>
Expand Down

0 comments on commit 82ef358

Please sign in to comment.