Skip to content

Commit

Permalink
♻ refactor: Alarm notification adds aggregated variables (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 authored Sep 5, 2024
1 parent 11c45cb commit ebf4021
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public async Task HandleEventAsync(TriggerAlarmEvent eto)
{
alarm = new AlarmHistory(eto.AlarmRuleId, eto.AlertSeverity, isNotification, eto.TriggerRuleItems);
alarm.AddAlarmRuleRecord(eto.ExcuteTime, eto.AggregateResult, true, eto.ConsecutiveCount, eto.TriggerRuleItems);
alarm.SetIsNotification(isNotification, isSilence);
alarm.SetIsNotification(isNotification, isSilence, eto.AggregateResult);
await _repository.AddAsync(alarm);
}
else
{
alarm.Update(eto.AlertSeverity, isNotification, eto.TriggerRuleItems);
alarm.SetIsNotification(isNotification, isSilence);
alarm.SetIsNotification(isNotification, isSilence, eto.AggregateResult);
alarm.AddAlarmRuleRecord(eto.ExcuteTime, eto.AggregateResult, true, eto.ConsecutiveCount, eto.TriggerRuleItems);
await _repository.UpdateAsync(alarm);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public async Task HandleEventAsync(SendAlarmNotificationEvent eto)
var notificationConfig = item.AlarmRuleItem.NotificationConfig;

var variables = new Dictionary<string, object>();
var alarmRule = await _alarmRuleRepository.FindAsync(x => x.Id == eto.AlarmRuleId);

AddAggregateVariables(variables, eto.AggregateResult);

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);
Expand All @@ -55,6 +58,14 @@ public async Task HandleEventAsync(SendAlarmNotificationEvent eto)
await _repository.UpdateAsync(alarm);
}

private void AddAggregateVariables(Dictionary<string, object> variables, ConcurrentDictionary<string, long> aggregateResult)
{
foreach (var aggregateItem in aggregateResult)
{
variables.TryAdd(aggregateItem.Key, aggregateItem.Value);
}
}

private async Task AddLogVariablesAsync(AlarmRule alarmRule, Dictionary<string, object> variables)
{
var checkTime = DateTimeOffset.Now;
Expand All @@ -63,6 +74,9 @@ private async Task AddLogVariablesAsync(AlarmRule alarmRule, Dictionary<string,
if (startTime == null)
return;

variables.TryAdd(AlertConsts.CHECK_START_TIME_NOTIFICATION_TEMPLATE_VAR_NAME, startTime.Value.UtcDateTime);
variables.TryAdd(AlertConsts.CHECK_END_TIME_NOTIFICATION_TEMPLATE_VAR_NAME, checkTime.UtcDateTime);

var request = new LogLatestRequest
{
Query = alarmRule.WhereExpression,
Expand Down
2 changes: 2 additions & 0 deletions src/Domain/Masa.Alert.Domain.Shared/Consts/AlertConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public static class AlertConsts
public const string DB_TABLE_PREFIX = null;
public const string DB_SCHEMA = "alert";
public const string ALARM_RULE_NAME_NOTIFICATION_TEMPLATE_VAR_NAME = "Name";
public const string CHECK_START_TIME_NOTIFICATION_TEMPLATE_VAR_NAME = "CheckStartTime";
public const string CHECK_END_TIME_NOTIFICATION_TEMPLATE_VAR_NAME = "CheckEndTime";
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public void Notification()
LastNotificationTime = DateTimeOffset.Now;
}

public void SetIsNotification(bool isNotification, bool isSilence)
public void SetIsNotification(bool isNotification, bool isSilence, ConcurrentDictionary<string, long> aggregateResult)
{
IsNotification = isNotification;

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

Expand Down
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, Guid AlarmRuleId) : DomainEvent
public record SendAlarmNotificationEvent(Guid AlarmHistoryId, ConcurrentDictionary<string, long> AggregateResult) : DomainEvent
{
}

0 comments on commit ebf4021

Please sign in to comment.