Пример #1
0
        static async Task <KeyValuePair <string, Usage[]> > GetRegionalUsage(string accessToken, string subscriptionId, string location)
        {
            var usageProvider = new UsageDataProvider();
            var usages        = await usageProvider.GetComputeUsage(accessToken, subscriptionId, location);

            return(KeyValuePair.Create(location, usages.Where(u => u.CurrentValue != 0).ToArray()));
        }
Пример #2
0
        static async Task <SubscriptionUsage[]> GetUsage(string accessToken, string[] specifiedUsages, Regex subscriptionPattern)
        {
            var subscriptionsProvider = new SubscriptionsDataProvider();
            var usageProvider         = new UsageDataProvider();
            var locationsProvider     = new LocationsDataProvider();

            var locations     = (await locationsProvider.GetLocations(accessToken)).Select(l => l.Name).ToArray();
            var subscriptions = await subscriptionsProvider.GetSubscriptions(accessToken);

            var subscriptionUsages = new List <SubscriptionUsage>();

            foreach (var sub in subscriptions)
            {
                if (!subscriptionPattern.IsMatch(sub.DisplayName))
                {
                    continue;
                }

                var subscriptionUsage = new SubscriptionUsage()
                {
                    Subscription = sub, Usages = new Usage[0]
                };
                var tasks = locations.Select(l => Program.GetRegionalUsage(accessToken, sub.SubscriptionId, l)).ToArray();
                await Task.WhenAll(tasks);

                var regionalUsages = tasks.Select(task => task.Result).ToArray();

                foreach (var regionalUsage in regionalUsages)
                {
                    var reportUsages = regionalUsage.Value
                                       .Where(u => (!specifiedUsages.Any() && u.CurrentValue != 0) || specifiedUsages.Any(f => u.Name.Value.Equals(f, StringComparison.OrdinalIgnoreCase)))
                                       .ToArray();

                    Array.ForEach(reportUsages, u => u.Location = regionalUsage.Key);

                    subscriptionUsage.Usages = subscriptionUsage.Usages.Concat(reportUsages).ToArray();
                }
                subscriptionUsages.Add(subscriptionUsage);
            }

            return(subscriptionUsages.ToArray());
        }