private static void FixCounterSerieData(IList <SeriesValue> inSeriesValue, SeriesResolution inResolution, DateTime inFromDate) { // it can be the case that there is not data yet at the actualFrom date, this results in null values which should be 0 if (inSeriesValue.Any(sv => !sv.Value.HasValue)) { for (var i = 0; i < inSeriesValue.Count(v => v.End <= DateTime.UtcNow); i++) { if (inSeriesValue[i].Value.HasValue) { break; } inSeriesValue[i].Value = 0; } } while (inSeriesValue[0].Begin > inFromDate) { var value = new SeriesValue(); if (inResolution == SeriesResolution.Month) { value.Begin = inSeriesValue[0].Begin.AddMonths(-1); value.End = value.Begin.AddMonths(1); } else { value.Begin = inSeriesValue[0].Begin.AddMinutes(-1 * (int)inResolution); value.End = value.Begin.AddMinutes((int)inResolution); } value.Value = 0; inSeriesValue.Insert(0, value); } }
public void BuildSeriesShouldCreateWeekValueIntervalTest() { // arrange var period = new Period(DateTime.Now, DateTime.Now); // should create one week const SeriesResolution resolution = SeriesResolution.Week; // act var actual = SeriesValueListBuilder.BuildSeries(period.From, period.To, resolution); // assert Assert.AreEqual(1, actual.Count); Data2Console(actual); }
public void BuildSeriesShouldCreateMonthlyValueFewMonthsIntervalsTest() { // arrange const SeriesResolution resolution = SeriesResolution.Month; // act var actual = SeriesValueListBuilder.BuildSeries(DateTime.Today.AddDays(-100), DateTime.Today, resolution); // assert Assert.AreEqual(DateTime.Today.AddDays(-100).Day, actual.First().Begin.Day); Assert.AreEqual(DateTime.Today.Day, actual.Last().End.Day); Data2Console(actual); }
public void BuildSeriesShouldCreateMonthlyValueOneYearIntervalsTest() { // arrange var period = PeriodBuilder.ThisYear(); const SeriesResolution resolution = SeriesResolution.Month; // act var actual = SeriesValueListBuilder.BuildSeries(period.From, period.To, resolution); // assert Assert.AreEqual(DateTime.Now.Month, actual.Count); Assert.AreEqual(1, actual.First().Begin.Day); Assert.AreEqual(DateTime.Now.Day, actual.Last().End.Day); Data2Console(actual); }
public void BuildSeriesShouldCreateDailyValueIntervalsTest() { // arrange var period = PeriodBuilder.ThisWeek(); const SeriesResolution resolution = SeriesResolution.Day; // act var actual = SeriesValueListBuilder.BuildSeries(period.From, period.To, resolution); // assert Assert.AreEqual(Math.Round(period.Span.TotalDays + 0.5), actual.Count); Assert.AreEqual(0, actual[0].Begin.Minute); Assert.AreEqual(0, actual.Last().End.Minute); Data2Console(actual); }
public void BuildSeriesShouldCreate5MinutesValueIntervalsTest() { // arrange var from = new DateTime(2012, 9, 11, 11, 2, 34); var to = new DateTime(2012, 9, 11, 11, 37, 34); const SeriesResolution resolution = SeriesResolution.FiveMinutes; // act var actual = SeriesValueListBuilder.BuildSeries(@from, to, resolution); // assert Assert.AreEqual(8, actual.Count); Assert.AreEqual(0, actual[0].Begin.Minute); Assert.AreEqual(35, actual.Last().Begin.Minute); Assert.AreEqual(40, actual.Last().End.Minute); }
/// <summary> /// Build the C# result that can be used to generate the Json result for GetSeries. /// </summary> public IEnumerable <ValueSerie> RetrieveSerieValuesForAccount(Mini mini, DateTime inFromUtc, DateTime inToUtc, SeriesResolution inResolution) { var parameters = new RetrieveSeriesParameters { Mini = mini, FromUtc = inFromUtc, ToUtc = inToUtc, Resolution = inResolution }; List <ValueSerie> resultSeries = RetrieveQboxSeries(parameters); // If there is generation, it can be that there are odd negative consumption numbers, these will be set to 0. if (resultSeries.Any(s => s.EnergyType == DeviceEnergyType.Generation) && resultSeries.Any(s => s.EnergyType == DeviceEnergyType.Consumption)) { foreach (var datum in resultSeries.First(s => s.EnergyType == DeviceEnergyType.Consumption).Data.Where(d => d.Value.HasValue && d.Value < 0)) { datum.Value = 0; } } return(resultSeries); }
public IList <SeriesValue> GetSeries(Unit eenheid, DateTime from, DateTime to, SeriesResolution resolution, bool negate, bool smoothing) { Logger.LogTrace("Enter, counter = {0}, unit = {1}, from = {2}, to = {3}, resolution = {4}, negate = {5}, smoothing = {6}", CounterId, eenheid, from, to, resolution, negate, smoothing); Guard.IsBefore(from, to, "From cannot be later than to"); if (resolution == SeriesResolution.FiveMinutes && CounterId == 2421) { resolution = SeriesResolution.Hour; } var values = SeriesValueListBuilder.BuildSeries(from, to, resolution); if (CounterId == 2421) { //todo: refactor hack, this can be properly handled when we have Energy and Power as units. eenheid = Unit.M3; } var result = StorageProvider.GetRecords(from, to, eenheid, values, negate); if (smoothing) { var data = values.OrderByDescending(o => o.Begin).ToList(); var currentIndex = 0; foreach (var rec in data) { if (rec != null && rec.Value != null) { if (rec.Value == 0m) { var distance = 1; while (currentIndex + distance < data.Count && data[currentIndex + distance].Value == 0m) { distance++; } rec.Value = Math.Round(1m / FormulaForTime(rec.Begin) * 60m * 1000m) / (distance + 1m); } else if (currentIndex + 1 < data.Count && data[currentIndex + 1].Value == 0m) { var distance = 1; while (currentIndex + distance < data.Count && data[currentIndex + distance].Value == 0m) { distance++; } rec.Value = rec.Value / distance; } } currentIndex++; } } Logger.LogTrace("Return"); return(values); }
/// <summary> /// Build the C# result that can be used to generate the Json result for GetSeries. /// </summary> public IList <Serie> RetrieveForAccount(string inQboxSerial, DateTime inFromUtc, DateTime inToUtc, SeriesResolution inResolution) { var valueSeries = RetrieveSerieValuesForAccount(inQboxSerial, inFromUtc, inToUtc, inResolution); return(Mapper.Map <IEnumerable <ValueSerie>, IList <Serie> >(valueSeries)); }
public void TestCalculate(DateTime from, DateTime to, SeriesResolution expectedResolution) { Assert.That(_calculator.Calculate(from, to), Is.EqualTo(expectedResolution)); }
/// <summary> /// Builds the series without values. /// </summary> /// <param name="to">To.</param> /// <param name="from">From.</param> /// <param name="resolution">The resolution.</param> public static List <SeriesValue> BuildSeries(DateTime from, DateTime to, SeriesResolution resolution) { var serie = new List <SeriesValue>(); // naar beneden afronden op hele minuten @from = @from.Truncate(TimeSpan.FromMinutes(1)); to = @to.Truncate(TimeSpan.FromMinutes(1)); DateTime calculatedFrom; DateTime calculatedTo; switch (resolution) { case SeriesResolution.OneMinute: calculatedFrom = @from; calculatedTo = to; break; case SeriesResolution.FiveMinutes: calculatedFrom = @from.AddMinutes((@from.Minute % 5) * -1); calculatedTo = to.AddMinutes((to.Minute % 5 > 0 ? 5 - to.Minute % 5 : 0)); break; case SeriesResolution.Hour: calculatedFrom = @from.AddMinutes((@from.Minute % 60) * -1); calculatedTo = to.AddMinutes((to.Minute % 60 > 0 ? 60 - to.Minute % 60 : 0)); break; case SeriesResolution.Day: calculatedFrom = @from.Date; calculatedTo = to.TimeOfDay.TotalMinutes > 0 ? to.AddDays(1).Date : to; break; case SeriesResolution.Week: var daysToWeekBegin = (int)@from.DayOfWeek; var daysToWeekEnd = 6 - (int)to.DayOfWeek; calculatedFrom = @from.Date.AddDays(daysToWeekBegin * -1d); calculatedTo = to.Date.AddDays(daysToWeekEnd + 1).Date; break; case SeriesResolution.Month: var refDate = @from.Date; while (refDate < to) { var endDate = refDate.AddMonths(1).FirstDayOfMonth(); serie.Add(new SeriesValue(refDate, endDate < to ? endDate : to)); refDate = endDate; } return(serie); default: throw new ArgumentOutOfRangeException("resolution"); } var span = calculatedTo - calculatedFrom; var nrOfValues = (int)span.TotalMinutes / (int)resolution; for (var i = 0; i < nrOfValues; i++) { var time = calculatedFrom.AddMinutes(i * (int)resolution); serie.Add(new SeriesValue(time, time.AddMinutes((int)resolution))); } return(serie); }
/// <summary> /// Build the C# result that can be used to generate the Json result for GetSeries. /// </summary> private IList <Serie> RetrieveForAccount(Mini mini, DateTime inFromUtc, DateTime inToUtc, SeriesResolution inResolution) { var valueSeries = _seriesRetriever.RetrieveSerieValuesForAccount(mini, inFromUtc, inToUtc, inResolution); return(Mapper.Map <IEnumerable <ValueSerie>, IList <Serie> >(valueSeries)); }