public async Task <EnvironmentCost> GetCost(RenderingEnvironment env, QueryTimePeriod period)
        {
            var client = await _clientAccessor.GetClient();

            var usageRequest = CreateUsageRequest(env, period);

            IReadOnlyList <Cost> costs =
                await Task.WhenAll(env.ExtractResourceGroupNames().Select(
                                       async rgName =>
            {
                var result = await client.GetUsageForResourceGroup(env.SubscriptionId, rgName, usageRequest);

                if (result.Properties == null)
                {
                    return(null);
                }
                else
                {
                    return(new Cost(usageRequest.TimePeriod, result));
                }
            }));

            costs = costs.Where(x => x != null).ToList();

            if (!costs.Any())
            {
                return(new EnvironmentCost(env.Name, null));
            }

            return(new EnvironmentCost(env.Name, costs.Aggregate(Cost.Aggregate)));
        }
        public Task <EnvironmentCost> GetCost(RenderingEnvironment env, QueryTimePeriod timePeriod)
        {
            var cacheKey = Invariant($"REPORTING:{env.Name}/{timePeriod.From}/{timePeriod.To}");

            return(_memoryCache.GetOrCreateAsync(cacheKey, Fetch));

            Task <EnvironmentCost> Fetch(ICacheEntry ice)
            {
                ice.AbsoluteExpirationRelativeToNow = CacheResultsFor;

                return(_inner.GetCost(env, timePeriod));
            }
        }
        private static UsageRequest CreateUsageRequest(RenderingEnvironment env, QueryTimePeriod period)
        {
            var usageRequest =
                new UsageRequest(
                    Timeframe.Custom,
                    new Dataset(
                        Granularity.Daily,
                        new Dictionary <string, Aggregation>
            {
                { "totalCost", new Aggregation(AggregationFunction.Sum, "PreTaxCost") }
            },
                        new List <Grouping>
            {
                new Grouping("ServiceName", ColumnType.Dimension)
            },
                        FilterExpression.Tag("environment", Operator.In, new[] { env.Id })));

            usageRequest.TimePeriod = period;

            return(usageRequest);
        }