Ejemplo n.º 1
0
        private static void PullUsageAndBillingData(string baseUrl)
        {
            string   runId        = "";
            DateTime jobStartTime = DateTime.UtcNow;
            DateTime jobEndTime   = DateTime.UtcNow;

            var missingSubscriptionIds = new List <string>();

            // pull subscription data
            EntityRepo <UserSubscription> subscriptionRepo = new EntityRepo <UserSubscription>();
            var subscriptionList = subscriptionRepo.Get(USER_SUBSCRIPTION_TABLE_PARTITION_KEY, null, "").ToList();

            foreach (var item in subscriptionList)
            {
                jobStartTime = DateTime.UtcNow;
                string subscriptionId   = item.SubscriptionId;
                string organizationId   = item.OrganizationId;
                string offerId          = item.OfferId;
                string currency         = item.Currency;
                string regionInfo       = item.RegionInfo;
                string subscriptionName = item.DisplayName;

                // pulling the month to date data
                // including the last month last date as well
                var    today                   = DateTime.UtcNow;
                var    currentMonth            = new DateTime(today.Year, today.Month, 1);
                var    lastDateOfPreviousMonth = currentMonth.AddDays(-1);
                string endTime                 = GetDateString(today);
                string startTime               = GetDateString(lastDateOfPreviousMonth);

                // if subscription data is incomplete, skip the whole data load process
                if (!IsValidData(subscriptionId, organizationId, offerId, currency, regionInfo))
                {
                    missingSubscriptionIds.Add(subscriptionId);
                    continue;
                }

                runId = Guid.NewGuid().ToString();
                string language = ConfigurationManager.AppSettings["Language"];
                // get rate card data
                Dictionary <string, MeterData> meterRateDictionary = GetMeterData(subscriptionId, organizationId, offerId, currency, language, regionInfo, baseUrl);

                // get usage data
                GetUsageData(meterRateDictionary, subscriptionName, subscriptionId, organizationId, startTime, endTime, runId, baseUrl);
                jobEndTime = DateTime.UtcNow;
            }

            // log message for yet to be configured subscriptions
            if (missingSubscriptionIds.Count > 0)
            {
                string message = "USER ACTION REQUIRED FOR Subscription(s): " + string.Join(",", missingSubscriptionIds.ToArray()) + " Visit Dashboard, go to 'My Subscription' and fill up details.";
                LogWebJobRunInfo(message, jobStartTime, jobEndTime);
            }

            // log the run info
            if (!string.IsNullOrEmpty(runId))
            {
                LogWebJobRunInfo(runId, jobStartTime, jobEndTime);
            }
        }
Ejemplo n.º 2
0
        private UserTokenCache GetCacheFromStorage(string user)
        {
            EntityRepo <UserTokenCache> repo = new EntityRepo <UserTokenCache>();
            var list  = repo.Get("UserTokenCache", null, user);
            var first = list.FirstOrDefault();

            return(first);
        }
Ejemplo n.º 3
0
        public ActionResult Refresh()
        {
            try
            {
                // get all organizations and their respective subscription for a given tenant
                var orgs = AzureResourceManagerUtil.GetUserOrganizations();
                Dictionary <Organization, List <Subscription> > dictionary = new Dictionary <Organization, List <Subscription> >();
                foreach (var item in orgs)
                {
                    if (!dictionary.ContainsKey(item))
                    {
                        var subscriptions = AzureResourceManagerUtil.GetUserSubscriptions(item.Id);
                        dictionary.Add(item, subscriptions);
                    }
                }

                // check if these subscriptions are already added in the storage
                var repo = new EntityRepo <UserSubscription>();
                var list = repo.Get(USER_SUBSCRIPTION_TABLE_PARTITION_KEY, null, "");
                var existingSubscriptions = new Dictionary <string, string>();
                foreach (var item in list)
                {
                    existingSubscriptions.Add(item.SubscriptionId, item.SubscriptionId);
                }

                // list of new subscription to add
                var listOfUserSubscription = new List <UserSubscription>();
                foreach (var subscriptions in dictionary.Values)
                {
                    foreach (var subscription in subscriptions)
                    {
                        UserSubscription userSubscription = new UserSubscription(subscription.Id, subscription.OrganizationId);
                        userSubscription.DisplayName = subscription.DisplayName;

                        // if the subscription is not already in the storage add them
                        // otherwise the one in the storage should have latest info
                        if (!existingSubscriptions.ContainsKey(userSubscription.SubscriptionId))
                        {
                            listOfUserSubscription.Add(userSubscription);
                        }
                    }
                }

                // if one or more subscriptions are discovered, add them
                if (listOfUserSubscription.Count > 0)
                {
                    repo.Insert(listOfUserSubscription);
                }
                return(Json(dictionary.ToList(), JsonRequestBehavior.AllowGet));
            }
            catch (Exception exp)
            {
                Logger.Log("Subscription-Web-API", "Error", exp.Message, exp.ToString());
                return(new HttpStatusCodeResult(500, exp.Message));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get Azure expense by account (summary)
        /// </summary>
        /// <param name="monthId">month in YYYY-mm format</param>
        /// <returns>expenses for a given month in JSON format</returns>
        public JsonResult SpendingByAccount(string monthId = "")
        {
            if (string.IsNullOrEmpty(monthId))
            {
                monthId = GetMonthId();
            }
            var repo  = new EntityRepo <EAUsageAccountSummaryEntity>();
            var data  = repo.Get(monthId, new List <Tuple <string, string> > {
            });
            var array = data.Select(p => new { name = p.AccountName, y = p.Amount });

            return(Json(array.ToList(), JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get Azure expense by subscription (summary)
        /// </summary>
        /// <param name="monthId">month in YYYY-mm format</param>
        /// <returns>expenses for a given month in JSON format</returns>
        public JsonResult SpendingBySubscription(string monthId = "")
        {
            if (string.IsNullOrEmpty(monthId))
            {
                monthId = GetMonthId();
            }

            var repo  = new EntityRepo <AzureUsageDetailsMeterAggregate>();
            var data  = repo.Get(monthId, new List <Tuple <string, string> > {
            });
            var array = data.GroupBy(x => x.SubscriptionName).Select(a => new { name = a.Key, y = Math.Max(a.Sum(w => w.Amount), 0.0) });

            return(Json(array.ToList(), JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Get Azure expense by account (daily)
        /// </summary>
        /// <param name="monthId">month in YYYY-mm format</param>
        /// <returns>expenses for a given month in JSON format</returns>
        public JsonResult SpendingByAccountDaily(string monthId = "")
        {
            if (string.IsNullOrEmpty(monthId))
            {
                monthId = GetMonthId();
            }
            var repo = new EntityRepo <EAUsageAccountDailySummaryEntity>();
            var data = repo.Get(monthId, new List <Tuple <string, string> > {
            });

            var aggregateUsage = data.OrderBy(x => x.Day).Select(p => new DailyBillInfo {
                Amount = p.Amount, Name = p.AccountName, Date = p.Day
            });

            return(GetDailyBillSeries(aggregateUsage));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get Azure expense by subscription (daily)
        /// </summary>
        /// <param name="monthId">month in YYYY-mm format</param>
        /// <returns>expenses for a given month in JSON format</returns>
        public JsonResult SpendingBySubscriptionDaily(string monthId = "")
        {
            if (string.IsNullOrEmpty(monthId))
            {
                monthId = GetMonthId();
            }
            var repo = new EntityRepo <AzureUsageDetailsDaily>();
            var data = repo.Get(monthId, new List <Tuple <string, string> > {
            });

            var aggregateUsage = data.OrderBy(x => x.UsageEndTime).GroupBy(x => new { x.SubscriptionName, x.UsageEndTime }).Select(p => new DailyBillInfo {
                Amount = Math.Max(p.Sum(y => y.Amount), 0.0), Name = p.Key.SubscriptionName, Date = p.Key.UsageEndTime.ToString("yyyy-MM-dd")
            });

            return(GetDailyBillSeries(aggregateUsage));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Get Azure expense by azure services (daily)
        /// </summary>
        /// <param name="monthId">month in YYYY-mm format</param>
        /// <returns>expenses for a given month in JSON format</returns>
        public JsonResult SpendingByServiceDaily(string monthId = "")
        {
            var dictionary  = new Dictionary <string, Dictionary <string, double> >();
            var uniqueDates = new Dictionary <string, string>();

            if (string.IsNullOrEmpty(monthId))
            {
                monthId = GetMonthId();
            }
            var repo = new EntityRepo <EAUsageMeterDailySummaryEntity>();
            var data = repo.Get(monthId, new List <Tuple <string, string> > {
            });

            var aggregateUsage = data.OrderBy(x => x.Day).Select(p => new DailyBillInfo {
                Amount = p.Amount, Name = p.MeterCategory, Date = p.Day
            });

            return(GetDailyBillSeries(aggregateUsage));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Get Azure expense by azure services (daily)
        /// </summary>
        /// <param name="monthId">month in YYYY-mm format</param>
        /// <returns>expenses for a given month in JSON format</returns>
        public JsonResult SpendingByServiceDaily(string monthId = "")
        {
            var dictionary  = new Dictionary <string, Dictionary <string, double> >();
            var uniqueDates = new Dictionary <string, string>();

            if (string.IsNullOrEmpty(monthId))
            {
                monthId = GetMonthId();
            }
            var repo = new EntityRepo <AzureUsageDetailsDaily>();
            var data = repo.Get(monthId, new List <Tuple <string, string> > {
            });

            var aggregateUsage = data.OrderBy(x => x.UsageEndTime).Select(p => new DailyBillInfo {
                Amount = Math.Max(p.Amount, 0.0), Name = p.MeterCategory, Date = p.UsageEndTime.ToString("yyyy-MM-dd")
            });

            return(GetDailyBillSeries(aggregateUsage));
        }
Ejemplo n.º 10
0
        public ActionResult GetSubscriptionDetails()
        {
            try
            {
                EntityRepo <UserSubscription> subscriptionRepo = new EntityRepo <UserSubscription>();
                var list = subscriptionRepo.Get(USER_SUBSCRIPTION_TABLE_PARTITION_KEY, null, "").Select(p => new {
                    SubscriptionId = p.SubscriptionId,
                    OrganizationId = p.OrganizationId,
                    DisplayName    = p.DisplayName,
                    OfferId        = p.OfferId,
                    Currency       = p.Currency,
                    RegionInfo     = p.RegionInfo
                });;

                return(Json(list, JsonRequestBehavior.AllowGet));
            }
            catch (Exception exp)
            {
                Logger.Log("Subscription-Web-API", "Error", exp.Message, exp.ToString());
                return(new HttpStatusCodeResult(500, exp.Message));
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Get Azure expense by azure services (summary)
        /// </summary>
        /// <param name="monthId">month in YYYY-mm format</param>
        /// <returns>expenses for a given month in JSON format</returns>
        public JsonResult SpendingByService(string monthId = "")
        {
            if (string.IsNullOrEmpty(monthId))
            {
                monthId = GetMonthId();
            }
            var repo = new EntityRepo <EAUsageMeterSummaryEntity>();
            var data = repo.Get(monthId, new List <Tuple <string, string> > {
            });

            var aggregateUsage = from us in data
                                 group us by new
            {
                MeterCategory = us.MeterCategory,
            }
            into fus
                select new
            {
                y    = fus.Sum(x => x.Amount),
                name = fus.Key.MeterCategory,
            };

            return(Json(aggregateUsage.ToList(), JsonRequestBehavior.AllowGet));
        }