<commit_before>﻿namespace Microsoft.ApplicationInsights.Extensibility.AggregateMetrics.AzureWebApp
{
    using System;
    using System.Net.Http;
    using System.Runtime.Caching;
    using System.Threading.Tasks;

    /// <summary>
    /// Class to contain the one cache for all Gauges 
    /// </summary>
    internal class CacheHelper
    {
        /// <summary>
        /// Checks if a key is in the cache and if not
        /// Retrieves raw counter data from Environment Variables
        /// Cleans raw JSON for only requested counter
        /// Creates value for caching
        /// </summary>
        /// <param name="name">cache key and name of the counter to be selected from JSON</param>
        /// <returns>value from cache</returns>
        public static int GetCountervalue(string name)
        {
            const string jsonKey = "json";
            if (!CacheHelper.IsInCache(jsonKey))
            {
               
                PerformanceCounterImplementation client = new PerformanceCounterImplementation();
                string uncachedJson= client.GetAzureWebAppEnvironmentVariables(AzureWebApEnvironmentVariables.All);

                /* http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_ASPNET/
                http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_APP/
                http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_CLR/
               http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_ALL/ */

                if (uncachedJson == null)
                {
                    return 0;
                }

                CacheHelper.SaveToCache(jsonKey, uncachedJson, DateTimeOffset.Now.AddSeconds(5.0));
            }

            string json = GetFromCache(jsonKey).ToString();
            string prefix = "\\\"" + name + "\\\":";
            string postfix = ",\\";

            int idx = json.IndexOf(prefix) + prefix.Length;
            int endIdx = json.IndexOf(postfix, idx);

            string value = json.Substring(idx, endIdx - idx);
            
            return Convert.ToInt32(value);
        }

        /// <summary>
        /// Method saves an object to the cache
        /// </summary>
        /// <param name="cacheKey"> string name of the counter value to be saved to cache</param>
        /// /<param name="toCache">Object to be cached</param>
        /// <param name="absoluteExpiration"> DateTimeOffset until item expires from cache</param>
        public static void SaveToCache(string cacheKey, object toCache,  DateTimeOffset absoluteExpiration)
        {  
            MemoryCache.Default.Add(cacheKey, toCache, absoluteExpiration);
        }

        /// <summary>
        /// Retrieves requested item from cache
        /// </summary>
        /// <param name="cacheKey">Key for the retrieved object</param>
        /// <returns> The requested item, as object type T</returns><commit_msg>Get integer value from json<commit_after>﻿namespace Microsoft.ApplicationInsights.Extensibility.AggregateMetrics.AzureWebApp
{
    using System;
    using System.Net.Http;
    using System.Runtime.Caching;
    using System.Threading.Tasks;

    /// <summary>
    /// Class to contain the one cache for all Gauges 
    /// </summary>
    internal class CacheHelper
    {
        /// <summary>
        /// Checks if a key is in the cache and if not
        /// Retrieves raw counter data from Environment Variables
        /// Cleans raw JSON for only requested counter
        /// Creates value for caching
        /// </summary>
        /// <param name="name">cache key and name of the counter to be selected from JSON</param>
        /// <returns>value from cache</returns>
        public static int GetCountervalue(string name)
        {
            const string jsonKey = "json";
            if (!CacheHelper.IsInCache(jsonKey))
            {
               
                PerformanceCounterImplementation client = new PerformanceCounterImplementation();
                string uncachedJson= client.GetAzureWebAppEnvironmentVariables(AzureWebApEnvironmentVariables.All);

                /* http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_ASPNET/
                http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_APP/
                http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_CLR/
               http://remoteenvironmentvariables.azurewebsites.net/api/EnvironmentVariables/WEBSITE_COUNTERS_ALL/ */

                if (uncachedJson == null)
                {
                    return 0;
                }

                CacheHelper.SaveToCache(jsonKey, uncachedJson, DateTimeOffset.Now.AddSeconds(5.0));
            }

            string json = GetFromCache(jsonKey).ToString();
            string jsonSubstring = json.Substring(json.IndexOf(name), json.Length - json.IndexOf(name));

            int startingIdx = jsonSubstring.IndexOf(" ");
            int endingIdx = jsonSubstring.IndexOf(",");

            string value = jsonSubstring.Substring(startingIdx + 1, endingIdx - startingIdx - 1);

            return Convert.ToInt32(value);
        }

        /// <summary>
        /// Method saves an object to the cache
        /// </summary>
        /// <param name="cacheKey"> string name of the counter value to be saved to cache</param>
        /// /<param name="toCache">Object to be cached</param>
        /// <param name="absoluteExpiration"> DateTimeOffset until item expires from cache</param>
        public static void SaveToCache(string cacheKey, object toCache,  DateTimeOffset absoluteExpiration)
        {  
            MemoryCache.Default.Add(cacheKey, toCache, absoluteExpiration);
        }

        /// <summary>
        /// Retrieves requested item from cache
        /// </summary>
        /// <param name="cacheKey">Key for the retrieved object</param>
        /// <returns> The requested item, as object type T</returns>
{'fraction_non_alphanumeric': 0.10001628929793126, 'fraction_numerical': 0.0019547157517510994, 'mean_word_length': 2.1471040492055358, 'pattern_counts': {'":': 1, '<': 47, '<?xml version=': 0, '>': 47, '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': '2650843', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1502, 'n_words': 387}