<commit_before>﻿namespace Microsoft.ApplicationInsights.Extensibility.AggregateMetrics.AzureWebApp
{
    using System;
    using Microsoft.ApplicationInsights.DataContracts;
    using Microsoft.ApplicationInsights.Extensibility.AggregateMetrics.Two;

    /// <summary>
    /// Struct for metrics dependant on time.
    /// </summary>
    public class RateCounterGauge : ICounterValue
    {
        private string name;

        private double? lastValue;

        private ICacheHelper cacheHelper;

        /// <summary>
        /// DateTime object to keep track of the last time this metric was retrieved.
        /// </summary>
        private DateTimeOffset dateTime;

        /// <summary>
        /// Initializes a new instance of <see cref="RateCounterGauge"/> class. 
        /// This constructor is intended for Unit Tests.
        /// </summary>
        /// <param name="name"> Name of the counter variable.</param>
        /// <param name="cache"> Cache object.</param>
        internal RateCounterGauge(string name, ICacheHelper cache) 
        {
            this.name = name;
            this.cacheHelper = cache;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RateCounterGauge"/> class.
        /// </summary>
        /// <param name="name"> Name of counter variable.</param>
        public RateCounterGauge(string name)
            : this(name, CacheHelper.Instance)
        {
        }

        /// <summary>
        /// Returns the current value of the rate counter if enough information exists.
        /// </summary>
        /// <returns> MetricTelemetry object.</returns>
        public MetricTelemetry GetValueAndReset()
        {
            MetricTelemetry metric = new MetricTelemetry();

            metric.Name = this.name;
            DateTimeOffset currentTime = System.DateTimeOffset.Now;

            if (this.lastValue == null)
            {
                this.lastValue = cacheHelper.GetCounterValue(this.name);
                this.dateTime = currentTime;

                return metric;
            }

            metric.Value = (currentTime.Second - this.dateTime.Second != 0) ? (cacheHelper.GetCounterValue(this.name) - (double)this.lastValue) / (currentTime.Subtract(this.dateTime).Seconds) : 0;
            this.lastValue = cacheHelper.GetCounterValue(this.name);
            this.dateTime = currentTime;

            return metric;
        }
    }
}<commit_msg>Calculate the time difference for RateCounters once.<commit_after>﻿namespace Microsoft.ApplicationInsights.Extensibility.AggregateMetrics.AzureWebApp
{
    using System;
    using Microsoft.ApplicationInsights.DataContracts;
    using Microsoft.ApplicationInsights.Extensibility.AggregateMetrics.Two;

    /// <summary>
    /// Struct for metrics dependant on time.
    /// </summary>
    public class RateCounterGauge : ICounterValue
    {
        private string name;

        private double? lastValue;

        private ICacheHelper cacheHelper;

        /// <summary>
        /// DateTime object to keep track of the last time this metric was retrieved.
        /// </summary>
        private DateTimeOffset dateTime;

        /// <summary>
        /// Initializes a new instance of <see cref="RateCounterGauge"/> class. 
        /// This constructor is intended for Unit Tests.
        /// </summary>
        /// <param name="name"> Name of the counter variable.</param>
        /// <param name="cache"> Cache object.</param>
        internal RateCounterGauge(string name, ICacheHelper cache) 
        {
            this.name = name;
            this.cacheHelper = cache;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RateCounterGauge"/> class.
        /// </summary>
        /// <param name="name"> Name of counter variable.</param>
        public RateCounterGauge(string name)
            : this(name, CacheHelper.Instance)
        {
        }

        /// <summary>
        /// Returns the current value of the rate counter if enough information exists.
        /// </summary>
        /// <returns> MetricTelemetry object.</returns>
        public MetricTelemetry GetValueAndReset()
        {
            MetricTelemetry metric = new MetricTelemetry();

            metric.Name = this.name;
            DateTimeOffset currentTime = System.DateTimeOffset.Now;

            if (this.lastValue == null)
            {
                this.lastValue = cacheHelper.GetCounterValue(this.name);
                this.dateTime = currentTime;

                return metric;
            }

            var timeDifferenceInSeconds = currentTime.Subtract(this.dateTime).Seconds;

            metric.Value = (timeDifferenceInSeconds != 0) ? (cacheHelper.GetCounterValue(this.name) - (double)this.lastValue) / timeDifferenceInSeconds : 0;
            this.lastValue = cacheHelper.GetCounterValue(this.name);
            this.dateTime = currentTime;

            return metric;
        }
    }
}
{'fraction_non_alphanumeric': 0.1035175879396985, 'fraction_numerical': 0.0008040201005025126, 'mean_word_length': 1.9921828021647625, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6887183', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1220, 'n_words': 324}