Last active
October 14, 2021 12:17
-
-
Save FrancescoBonizzi/ccc010d7c091b089d55da417488b8ea3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Alert> GenerateAlerts(MonitoringData[] monitoringData) | |
{ | |
var alerts = new List<Alert>(); | |
var boolli = new Evaluator(); | |
foreach (var rule in _alertGenerationRules) | |
{ | |
// I need object for String.Format | |
var lastValues = new Dictionary<Metrics, MonitoringData>(); | |
var namedBooleanFunctions = rule.DataEvaluationFunctionDescription.Select(d => new NamedBooleanFunction( | |
d.Metric.ToString(), | |
() => | |
{ | |
(bool IsCritical, MonitoringData data) = ValueCritical( | |
monitoringData, | |
DateTime.Now.AddMinutes(-rule.TimeFrameMinutes), | |
d.Metric, | |
d.Threshold); | |
if (IsCritical && data != null) | |
{ | |
lastValues[d.Metric] = data; | |
} | |
return IsCritical; | |
})).ToArray(); | |
bool alertShouldBeGenerated = boolli.EvaluateFuncOfBoolExpression( | |
rule.BooleanExpression, | |
namedBooleanFunctions); | |
if (alertShouldBeGenerated) | |
{ | |
var stringFormatParameters = new List<object>(); | |
foreach (var lastValue in lastValues) | |
{ | |
stringFormatParameters.Add((int)lastValue.Value.MetricValue); | |
} | |
if (stringFormatParameters.Count < Enum.GetNames(typeof(Metrics)).Length) | |
{ | |
for (int i = 0; i < Enum.GetNames(typeof(Metrics)).Length; ++i) | |
stringFormatParameters.Add("-"); | |
} | |
stringFormatParameters.Add(lastValues.Values.OrderByDescending(v => v.CollectionTime).FirstOrDefault().CollectionTime); | |
alerts.Add(new Alert() | |
{ | |
GeneratingRuleName = rule.RuleName, | |
AlertGravity = rule.ResultingAlertGravity, | |
GenerationDate = DateTime.Now, | |
Message = string.Format(rule.MessageFormat, stringFormatParameters.ToArray()) | |
}); | |
} | |
} | |
return alerts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment