private static void Test(string[] usageData, double expectedResult) { RateCardData rc = GetRateCardTestData(); var ud = new UsageData(); ud.Values = GetUsageValues(usageData); var data = Client.Combine(rc, ud); Assert.AreEqual(expectedResult, data.TotalCosts); }
/// <summary> /// Combines the ratecard data with the usage data. /// </summary> /// <param name="rateCardData">RateCard data</param> /// <param name="usageData">Usage data</param> /// <returns>The costs of the resources (combined data of ratecard and usage api)</returns> public static ResourceCostData Combine(RateCardData rateCardData, UsageData usageData) { ResourceCostData rcd = new ResourceCostData() { Costs = new List <ResourceCosts>(), RateCardData = rateCardData }; // get all used meter ids var meterIds = (from x in usageData.Values select x.Properties.MeterId).Distinct().ToList(); // aggregates all quantity and will be used to calculate costs (e.g. if quantity is included for free) // Dictionary<MeterId, Dictionary<YearAndMonthOfBillingCycle, aggregatedQuantity>> Dictionary <string, Dictionary <string, double> > aggQuant = meterIds.ToDictionary(x => x, x => new Dictionary <string, double>()); foreach (var usageValue in usageData.Values) { string meterId = usageValue.Properties.MeterId; var rateCard = (from x in rateCardData.Meters where x.MeterId.Equals(meterId, StringComparison.OrdinalIgnoreCase) select x).FirstOrDefault(); if (rateCard == null) // e.g. ApplicationInsights: there is no ratecard data for these { continue; } var billingCycleId = GetBillingCycleIdentifier(usageValue.Properties.UsageStartTimeAsDate); if (!aggQuant[meterId].ContainsKey(billingCycleId)) { aggQuant[meterId].Add(billingCycleId, 0.0); } var usedQuantity = aggQuant[meterId][billingCycleId]; var curcosts = GetMeterRate(rateCard.MeterRates, rateCard.IncludedQuantity, usedQuantity, usageValue.Properties.Quantity); aggQuant[meterId][billingCycleId] += usageValue.Properties.Quantity; rcd.Costs.Add(new ResourceCosts() { RateCardMeter = rateCard, UsageValue = usageValue, CalculatedCosts = curcosts.Item1, BillableUnits = curcosts.Item2 }); } return(rcd); }
private static RateCardData GetRateCardTestData() { var rc = new RateCardData(); rc.Currency = "EUR"; rc.Meters = new List <Meter>(); rc.Meters.Add(new Meter() { MeterId = "1", IncludedQuantity = 5, MeterRates = new Dictionary <double, double>() { { 0, 1.0 }, { 5, 2.0 } } }); return(rc); }
public void TestCostCalculation1() { RateCardData rc = GetRateCardTestData(); var ud = new UsageData(); string[] usageData = new string[] { "1,Test,1,2017-01-12,2017-01-13,13", "1,Test,1,2017-01-13,2017-01-14,10", "1,Test,1,2017-01-14,2017-01-15,12" }; ud.Values = GetUsageValues(usageData); // billing cycle 1: 23 units, 5 for free, 5 with 1.0, rest with 2.0 // => 5*0 + 5*1.0 * 13*2.0 = 31.0 // billing cycle 2: 12 units // => 5*0 + 5*1.0 + 2*2.0 ) 9.0 var data = Client.Combine(rc, ud); Assert.AreEqual(40.0, data.TotalCosts); }
public string GetAzurePricingData() { var token = GetOAuthTokenFromAAD(); Console.WriteLine("Token received, read rate card data..."); var data = GetRateCardData(token); if (data != null) { var ratecard = JsonConvert.DeserializeObject <RateCard>(data); List <Meter> meters = ratecard.Meters; using (AzurePricingEntities entities = new AzurePricingEntities()) { foreach (Meter item in meters) { RateCardData rateCardItem = new RateCardData(); rateCardItem.IncludedQuantity = (int)item.IncludedQuantity; rateCardItem.EffectiveDate = item.EffectiveDate; rateCardItem.MeterID = item.MeterId; rateCardItem.MeterName = item.MeterName; rateCardItem.MeterCategory = item.MeterCategory; string meterRates = string.Join(",", item.MeterRates.Select(y => " [ " + y.Key.ToString() + " : " + y.Value.ToString() + " ]")); rateCardItem.MeterRates = meterRates; rateCardItem.MeterRegion = item.MeterRegion; rateCardItem.MeterStatus = item.MeterStatus; rateCardItem.MeterSubCategory = item.MeterSubCategory; string meterTags = string.Join(",", item.MeterTags); rateCardItem.MeterTags = meterTags; rateCardItem.Unit = item.Unit; entities.RateCardDatas.Add(rateCardItem); entities.SaveChanges(); } } } return("success"); }