private async Task ProcessRuleAsync(MonitoringRule rule) { try { var lastNotificationDate = await _notificationsCache.GetLastNotificationDateAsync(rule.Id); if (rule.NeedsNotification(lastNotificationDate)) { var action = new SendNotificationMonitoringAction(); rule.ActionsByTypeName[action.TypeName].CopyTo(action); if (string.IsNullOrWhiteSpace(action.NotificationChannelId)) { await _notificationSender.SendAsync(rule.GetNotificationText()); } else { await _notificationSender.SendAsync(action.NotificationChannelId, rule.GetNotificationText()); } await _notificationsCache.AddOrUpdateAsync(rule.Id, DateTime.UtcNow.Add(MonitoringRuleExtensions.NotificationRemindPeriod)); } } catch (Exception ex) { _logger.LogError(ex, "Failed to ProcessRule. {@Rule}", rule.Name); } }
public static bool NeedsNotification(this MonitoringRule rule, DateTime?lastNotificationDate) { lastNotificationDate ??= DateTime.MinValue; var action = new SendNotificationMonitoringAction(); if (rule.ActionsByTypeName == null || !rule.ActionsByTypeName.TryGetValue(action.TypeName, out _)) { return(false); } if (rule.CurrentState == null) { return(false); } if (rule.CurrentState.IsActive != rule.PrevState?.IsActive) { return(true); } var timeToRemind = DateTime.UtcNow - lastNotificationDate > NotificationRemindPeriod; if (rule.CurrentState.IsActive && timeToRemind) { return(true); } return(false); }
private bool Evaluate(MonitoringRule rule, MonitoringItem loggingItem) { var lambdaParser = new NReco.Linq.LambdaParser(); var varContext = new Dictionary <string, object> { ["LoggingItem"] = loggingItem, ["Warning"] = LogLevel.Warning, ["Error"] = LogLevel.Error, ["Information"] = LogLevel.Information }; return((bool)lambdaParser.Eval(rule.Rule, varContext)); }
public static string GetNotificationText(this MonitoringRule rule) { var ruleChecks = rule.Checks ?? new List <PortfolioCheck>(); var title = $"Rule <b>{rule.Name}</b> is {(rule.CurrentState.IsActive ? "active" : "inactive")}:{Environment.NewLine}{rule.Description}"; var checkDescriptions = ruleChecks.Select(ch => ch.GenerateCheckDescription()); var body = string.Join($"{Environment.NewLine}", checkDescriptions); return($"{title}{Environment.NewLine}" + $"{body}{Environment.NewLine}{Environment.NewLine}" + $"Date: {rule.CurrentState.Date:yyyy-MM-dd hh:mm:ss}"); }