Пример #1
0
        /// <summary>
        /// Saves the resource group cost results to database.
        /// </summary>
        /// <param name="subscription">Subscription name where resources belong.</param>
        /// <param name="results">List of <see cref="ResourceGroupCostResult"/> instances.</param>
        /// <returns>Return <see cref="Task"/>.</returns>
        public async Task SaveResourceGroupCostsResultsAsync(Subscription subscription, IEnumerable <ResourceGroupCostResult> results)
        {
            var now = DateTimeOffset.UtcNow;

            foreach (var result in results)
            {
                var totalSpendLimit = result.TotalSpendLimit.IsNullOrWhiteSpace() ? this._settings.Billing.TotalSpendLimit : Convert.ToDecimal(result.TotalSpendLimit);
                var dailySpendLimit = result.DailySpendLimit.IsNullOrWhiteSpace() ? this._settings.Billing.DailySpendLimit : Convert.ToDecimal(result.DailySpendLimit);
                var overspendAction = result.OverspendAction.IsNullOrWhiteSpace() ? this._settings.Billing.OverspendAction : result.OverspendAction;

                var record = await this._dbContext.ResourceGroupCostHistories
                             .SingleOrDefaultAsync(p => p.Subscription.Equals(subscription.DisplayName, StringComparison.CurrentCultureIgnoreCase) &&
                                                   p.ResourceGroupName.Equals(result.ResourceGroupName, StringComparison.CurrentCultureIgnoreCase) &&
                                                   p.Owners.Equals(result.OwnerEmails, StringComparison.CurrentCultureIgnoreCase) &&
                                                   p.DateStart == result.DateStart &&
                                                   p.DateEnd == result.DateEnd)
                             .ConfigureAwait(false);

                if (record == null)
                {
                    record = new ResourceGroupCostHistory()
                    {
                        ResourceGroupCostHistoryId = Guid.NewGuid(),
                        Subscription      = subscription.DisplayName,
                        SubscriptionId    = Guid.Parse(subscription.SubscriptionId),
                        ResourceGroupName = result.ResourceGroupName,
                        Owners            = result.OwnerEmails,
                        DateStart         = result.DateStart,
                        DateEnd           = result.DateEnd,
                        DateCreated       = now
                    };
                }

                record.Cost            = Convert.ToDecimal(result.Cost);
                record.TotalSpendLimit = totalSpendLimit;
                record.DailySpendLimit = dailySpendLimit;
                record.OverspendAction = overspendAction;
                record.DateUpdated     = now;

                this._dbContext.ResourceGroupCostHistories.AddOrUpdate(record);
            }

            await this._dbContext.SaveChangesAsync().ConfigureAwait(false);
        }
Пример #2
0
        /// <summary>
        /// Gets the list of <see cref="ResourceGroupCost"/> instances.
        /// </summary>
        /// <param name="subscription"><see cref="Subscription"/> instance.</param>
        /// <param name="dateStart">Date starts calculation.</param>
        /// <param name="dateEnd">Date ends calculation.</param>
        /// <param name="authToken">Authentication token value.</param>
        /// <returns>Returns the list of <see cref="Models.ResourceGroupCost"/> instances.</returns>
        public async Task <IEnumerable <ResourceGroupCost> > GetResourceGroupCostsAsync(Subscription subscription, DateTime dateStart, DateTime dateEnd, string authToken)
        {
            var costs = await this._billing.GetResourceCostsAsync(subscription.SubscriptionId, dateStart, dateEnd, authToken).ConfigureAwait(false);

            var grouped = costs.GroupBy(
                p => new
            {
                DateStart         = p.UsageValue.Properties.UsageStartTime,
                DateEnd           = p.UsageValue.Properties.UsageEndTime,
                ResourceGroupName = ResourceGroupDateKey.GetResourceGroupKey(p)
            })
                          .Select(
                p => new ResourceGroupCost()
            {
                ResourceGroupName = p.Key.ResourceGroupName,
                DateStart         = DateTimeOffset.Parse(p.Key.DateStart),
                DateEnd           = DateTimeOffset.Parse(p.Key.DateEnd),
                Cost = p.Sum(q => q.CalculatedCosts)
            })
                          .OrderBy(p => p.ResourceGroupName)
                          .ThenBy(p => p.DateStart)
                          .ThenBy(p => p.DateEnd);

            return(grouped);
        }