public async Task <IList <PoolUsageResult> > GetEnvironmentUsage(RenderingEnvironment environment)
        {
            var result = await _queryProvider.ExecuteQuery(
                environment.ApplicationInsightsAccount.ApplicationId,
                environment.ApplicationInsightsAccount.ApiKey,
                EnvironmentUsage);

            var usage = new List <PoolUsageResult>();

            if (result.Success)
            {
                var values    = GetEnvironmentUsageMetrics(result.Results);
                var poolNames = values.Select(p => p.PoolName).Distinct().ToList();
                foreach (var poolName in poolNames)
                {
                    var poolUsage = new PoolUsageResult {
                        PoolName = poolName
                    };
                    poolUsage.Values = values.Where(p => p.PoolName == poolName).OrderBy(p => p.Timestamp).ToList();
                    var lastUsage = poolUsage.Values.LastOrDefault();
                    if (lastUsage != null && lastUsage.Timestamp < DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(10)))
                    {
                        // The pools last metric was > 10 minutes ago, hence the nodes are probably gone, so
                        // let's append a zero to cleanup the chart
                        poolUsage.Values.Add(new PoolUsageMetric
                        {
                            DedicatedCores   = 0,
                            DedicatedNodes   = 0,
                            LowPriorityCores = 0,
                            LowPriorityNodes = 0,
                            TotalCores       = 0,
                            TotalNodes       = 0,
                            PoolName         = lastUsage.PoolName,
                            Timestamp        = lastUsage.Timestamp.AddMinutes(1),
                        });
                    }
                    usage.Add(poolUsage);
                }
            }
            else
            {
                Console.WriteLine($"Error querying environment {environment.Name} usage: {result.Error}");
            }

            return(usage);
        }
        public async Task <PoolUsageResult> GetUsageForPool(RenderingEnvironment environment, string poolName, string vmSize)
        {
            var result = await _queryProvider.ExecuteQuery(
                environment.ApplicationInsightsAccount.ApplicationId,
                environment.ApplicationInsightsAccount.ApiKey,
                GetPoolUsageQuery(poolName));

            var usage = new PoolUsageResult {
                PoolName = poolName
            };

            if (result.Success)
            {
                usage.Values = GetPoolUsageMetrics(result.Results);
                var lastUsage = usage.Values.LastOrDefault();
                if (lastUsage != null && lastUsage.Timestamp < DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(10)))
                {
                    // The pools last metric was > 10 minutes ago, hence the nodes are probably gone, so
                    // let's append a zero to cleanup the chart
                    usage.Values.Add(new PoolUsageMetric
                    {
                        DedicatedCores   = 0,
                        DedicatedNodes   = 0,
                        LowPriorityCores = 0,
                        LowPriorityNodes = 0,
                        TotalCores       = 0,
                        TotalNodes       = 0,
                        PoolName         = lastUsage.PoolName,
                        Timestamp        = lastUsage.Timestamp.AddMinutes(1),
                    });
                }
            }
            else
            {
                Console.WriteLine($"Error querying pool {poolName} usage: {result.Error}");
            }

            return(usage);
        }