Пример #1
0
        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;
        }
Пример #2
0
        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
                    };
                }
            }
        }