public void ResetExceededThresholds() { foreach (var intervalThreshold in settings.IntervalThresholds) { var interval = (int)intervalThreshold.Key; var threshold = intervalThreshold.Value; var current = UsageByInterval.ContainsKey(interval) ? UsageByInterval[interval] : null; if (current != null && current.Count > threshold) { current.Count = 0; } } ExceededThreshold = null; }
public void Update(DateTime time, int usages) { ExceededThreshold = null; foreach (var intervalThreshold in settings.IntervalThresholds) { var interval = (int)intervalThreshold.Key; var threshold = intervalThreshold.Value; if (!UsageByInterval.ContainsKey(interval)) { UsageByInterval.Add(interval, new UsageBeginAndCount(time)); } var current = UsageByInterval[interval]; var elapsed = time.Subtract(current.Begin).TotalSeconds; if (elapsed < interval) { current.Count += usages; } else { current.Begin = time; current.Count = usages; } if (current.Count > threshold) { ExceededThreshold = new Threshold { UsageInterval = TimeSpan.FromSeconds(interval), Usages = threshold, IsEndingIn = current.Begin + TimeSpan.FromSeconds(interval) - DateTime.Now }; } } }