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 <List <ActiveComputeNode> > GetActiveComputeNodes(RenderingEnvironment config)
        {
            var activeNodes = new List <ActiveComputeNode>();
            var query       = string.Format(ActiveProcessQueryFormat, 120);
            var result      = await _queryProvider.ExecuteQuery(
                config.ApplicationInsightsAccount.ApplicationId,
                config.ApplicationInsightsAccount.ApiKey,
                query);

            if (!result.Success)
            {
                return(activeNodes);
            }

            var json = result.Results;

            if (json.ContainsKey("tables"))
            {
                var tables = (JArray)json["tables"];
                if (tables.Count > 0)
                {
                    var rows = (JArray)tables[0]["rows"];
                    foreach (var row in rows)
                    {
                        var acn = new ActiveComputeNode
                        {
                            PoolName        = row[0].Value <string>(),
                            ComputeNodeName = row[1].Value <string>(),
                            TrackedProcess  = !string.IsNullOrEmpty(row[3].Value <string>()),
                            LastActive      = row[4].Value <DateTime>(),
                        };

                        var metric = row[2].Value <string>();
                        if (!string.IsNullOrEmpty(metric))
                        {
                            if (metric == "Cpu usage")
                            {
                                acn.CpuPercent = row[5].Value <long>();
                            }
                            else if (metric == "Gpu usage")
                            {
                                acn.GpuPercent = row[5].Value <long>();
                            }
                        }

                        activeNodes.Add(acn);
                    }
                }
            }

            return(activeNodes);
        }