public void ByDays() { using (var tss = GetStorage()) { var start = new DateTimeOffset(2015, 4, 6, 0, 0, 0, TimeSpan.Zero); using (var writer = tss.CreateWriter()) { for (int i = 10; i < 2000; i++) { var dateTime = start.AddMinutes(2 * i); writer.Append("Simple", "Time", dateTime, i); } writer.Commit(); } using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Days(2), start.AddYears(-1), start.AddYears(2)).ToArray(); Assert.Equal(548, time.Length); for (int i = 0; i < 548; i++) { #if DEBUG Assert.Equal("Time", time[i].DebugKey); #endif Assert.Equal(AggregationDuration.Days(2), time[i].Duration); var daysInMonth = DateTime.DaysInMonth(time[i].StartAt.Year, time[i].StartAt.Month) + DateTime.DaysInMonth(time[i].StartAt.AddMonths(1).Year, time[i].StartAt.AddMonths(1).Month); if (i == 182) { Assert.Equal(710, time[i].Values[0].Volume); Assert.Equal(258795, time[i].Values[0].Sum); Assert.Equal(10, time[i].Values[0].Open); Assert.Equal(10, time[i].Values[0].Low); Assert.Equal(719, time[i].Values[0].Close); Assert.Equal(719, time[i].Values[0].High); } else if (i == 183) { Assert.Equal(1280, time[i].Values[0].Volume); Assert.Equal(1740160, time[i].Values[0].Sum); Assert.Equal(720, time[i].Values[0].Open); Assert.Equal(720, time[i].Values[0].Low); Assert.Equal(1999, time[i].Values[0].Close); Assert.Equal(1999, time[i].Values[0].High); } else { Assert.Equal(0, time[i].Values[0].Volume); Assert.Equal(0, time[i].Values[0].Sum); } } } } }
public void CanQueryDataInSpecificDurationSum() { using (var tss = GetStorage()) { WriteTestData(tss); var start = new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero); using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Hours(6), start.AddDays(-1), start.AddDays(2)).ToArray(); var money = r.GetAggregatedPoints("Simple", "Money", AggregationDuration.Hours(2), start.AddDays(-2), start.AddDays(1)).ToArray(); Assert.Equal(12, time.Length); Assert.Equal(new DateTimeOffset(2015, 3, 31, 0, 0, 0, TimeSpan.Zero), time[0].StartAt); Assert.Equal(new DateTimeOffset(2015, 3, 31, 6, 0, 0, TimeSpan.Zero), time[1].StartAt); Assert.Equal(new DateTimeOffset(2015, 3, 31, 12, 0, 0, TimeSpan.Zero), time[2].StartAt); Assert.Equal(new DateTimeOffset(2015, 3, 31, 18, 0, 0, TimeSpan.Zero), time[3].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero), time[4].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 1, 6, 0, 0, TimeSpan.Zero), time[5].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 1, 12, 0, 0, TimeSpan.Zero), time[6].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 1, 18, 0, 0, TimeSpan.Zero), time[7].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 2, 0, 0, 0, TimeSpan.Zero), time[8].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 2, 6, 0, 0, TimeSpan.Zero), time[9].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 2, 12, 0, 0, TimeSpan.Zero), time[10].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 2, 18, 0, 0, TimeSpan.Zero), time[11].StartAt); Assert.Equal(79, time[4].Values[0].Sum); #if DEBUG Assert.Equal("Time", time[4].DebugKey); #endif Assert.Equal(AggregationDuration.Hours(6), time[4].Duration); Assert.Equal(36, money.Length); for (int i = 0; i < 36; i++) { #if DEBUG Assert.Equal("Money", money[i].DebugKey); #endif Assert.Equal(AggregationDuration.Hours(2), money[0].Duration); if (i == 24 || i == 25) { continue; } Assert.Equal(0, money[i].Values[0].Sum); Assert.Equal(0, money[i].Values[0].Volume); Assert.Equal(0, money[i].Values[0].High); } Assert.Equal(600, money[24].Values[0].Sum); Assert.Equal(2, money[24].Values[0].Volume); Assert.Equal(130, money[25].Values[0].Sum); Assert.Equal(1, money[25].Values[0].Volume); } } }
public void HourlyData_QueryPer3Months_StartedAt4() { var start = new DateTime(2015, 4, 1, 0, 0, 0); using (var tss = GetStorage()) { var r = tss.CreateReader(); Assert.Throws <InvalidOperationException>(() => { r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Months(3), start.AddYears(-1), start.AddYears(2)).ToArray(); }); } }
public void HourlyData_QueryPer2Years_StartedAt2013MiddleYear() { var start = new DateTime(2015, 4, 1, 0, 0, 0); using (var tss = GetStorage()) { var r = tss.CreateReader(); var exception = Assert.Throws <InvalidOperationException>(() => { r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Years(2), start.AddYears(-1), start.AddYears(2)).ToArray(); }); Assert.Equal("When querying a roll up by years, you cannot specify months, days, hours, minutes, seconds or milliseconds", exception.Message); } }
public void HourlyData_QueryPer3Years_StartedAt2017() { var start = new DateTime(2019, 1, 1, 0, 0, 0); using (var tss = GetStorage()) { var r = tss.CreateReader(); var exception = Assert.Throws <InvalidOperationException>(() => { r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Years(3), start.AddYears(-1), start.AddYears(7)).ToArray(); }); Assert.Equal("Cannot create a roll up by 3 Years as it cannot be divided to candles that starts from midnight", exception.Message); } }
public void ByYear() { using (var tss = GetStorage()) { var start = new DateTimeOffset(2014, 1, 1, 0, 0, 0, TimeSpan.Zero); using (var writer = tss.CreateWriter()) { for (int i = 10; i < 5000; i++) { var dateTime = start.AddHours(6 * i); writer.Append("Simple", "Time", dateTime, i); } writer.Commit(); } using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Years(2), start.AddYears(-2), start.AddYears(6)).ToArray(); Assert.Equal(8 / 2, time.Length); Assert.Equal(new DateTimeOffset(2012, 1, 1, 0, 0, 0, TimeSpan.Zero), time[0].StartAt); Assert.Equal(new DateTimeOffset(2014, 1, 1, 0, 0, 0, TimeSpan.Zero), time[1].StartAt); Assert.Equal(new DateTimeOffset(2016, 1, 1, 0, 0, 0, TimeSpan.Zero), time[2].StartAt); Assert.Equal(new DateTimeOffset(2018, 1, 1, 0, 0, 0, TimeSpan.Zero), time[3].StartAt); Assert.Equal(0, time[0].Values[0].Volume); Assert.Equal(0, time[0].Values[0].Sum); Assert.Equal(2910, time[1].Values[0].Volume); Assert.Equal(4261695, time[1].Values[0].Sum); Assert.Equal(2080, time[2].Values[0].Volume); Assert.Equal(8235760, time[2].Values[0].Sum); Assert.Equal(0, time[3].Values[0].Volume); Assert.Equal(0, time[3].Values[0].Sum); } } }
public async Task GetKeys() { var start = new DateTimeOffset(2015, 10, 7, 15, 0, 0, TimeSpan.Zero); using (var store = NewRemoteTimeSeriesStore()) { await store.CreateTypeAsync("Simple", new[] { "Value" }); await store.CreateTypeAsync("FourValues", new[] { "Value 1", "Value Two", "Value Three", "Value 4" }); await store.AppendAsync("FourValues", "Time", start, new[] { 3D, 4D, 5D, 6D }); await store.AppendAsync("Simple", "Is", start, 3D); await store.AppendAsync("Simple", "Money", start, 3D); var cancellationToken = new CancellationToken(); await store.AppendAsync("Simple", "Is", start.AddHours(1), 3456D, cancellationToken); await store.AppendAsync("FourValues", "Time", start.AddHours(1), new[] { 23D, 4D, 5D, 6D }, cancellationToken); await store.AppendAsync("FourValues", "Time", start.AddHours(2), cancellationToken, 33D, 4D, 5D, 6D); await store.AppendAsync("FourValues", "Time", start.AddHours(3), cancellationToken, 33D, 4D, 5D, 6D); var types = await store.Advanced.GetTypes(cancellationToken); Assert.Equal(2, types.Length); var fourValues = types[0]; Assert.Equal("FourValues", fourValues.Type); Assert.Equal(4, fourValues.Fields.Length); Assert.Equal(1, fourValues.KeysCount); var keys = await store.Advanced.GetKeys(fourValues.Type); Assert.Equal(1, keys.Length); var time = keys[0]; Assert.Equal("FourValues", time.Type.Type); Assert.Equal("Time", time.Key); Assert.Equal(4, time.PointsCount); var points = await store.Advanced.GetPoints(fourValues.Type, time.Key); Assert.Equal(points.Length, time.PointsCount); var aggregatedPoints = await store.Advanced.GetAggregatedPoints(fourValues.Type, time.Key, AggregationDuration.Hours(2), start.AddHours(-1), start.AddHours(3)); Assert.Equal(aggregatedPoints.Length, 2); Assert.Equal(aggregatedPoints[0].Values[0].Volume, 1); Assert.Equal(aggregatedPoints[1].Values[0].Volume, 2); var simple = types[1]; Assert.Equal("Simple", simple.Type); Assert.Equal(1, simple.Fields.Length); Assert.Equal(2, simple.KeysCount); keys = await store.Advanced.GetKeys(simple.Type); Assert.Equal(2, keys.Length); var _is = keys[0]; Assert.Equal("Simple", _is.Type.Type); Assert.Equal("Is", _is.Key); Assert.Equal(2, _is.PointsCount); var money = keys[1]; Assert.Equal("Simple", money.Type.Type); Assert.Equal("Money", money.Key); Assert.Equal(1, money.PointsCount); points = await store.Advanced.GetPoints(fourValues.Type, money.Key, start : DateTimeOffset.MinValue, end : DateTimeOffset.MaxValue, skip : 1, take : int.MaxValue, token : cancellationToken); Assert.Equal(points.Length, money.PointsCount - 1); var stats = await store.GetStatsAsync(cancellationToken); Assert.Equal(2, stats.TypesCount); Assert.Equal(3, stats.KeysCount); Assert.Equal(7, stats.PointsCount); } }
public void CanQueryData_With3Values_Rollups() { var start = new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero); using (var tss = GetStorage()) { using (var writer = tss.CreateWriter()) { writer.CreateType("3Val", new[] { "Value 1", "Value Two", "Value 3" }); for (int i = 0; i < 7; i++) { writer.Append("3Val", "Money", start.AddHours(i), 1000 + i, StringToIndex("Money"), start.AddHours(i).Ticks); writer.Append("3Val", "Is", start.AddHours(i), 7000 + i, StringToIndex("Is"), start.AddHours(i).Ticks); writer.Append("3Val", "Time", start.AddHours(i), 19000 + i, StringToIndex("Time"), start.AddHours(i).Ticks); } writer.Commit(); } using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("3Val", "Time", AggregationDuration.Hours(3), start.AddMonths(-1), start.AddDays(1)).ToArray(); var money = r.GetAggregatedPoints("3Val", "Money", AggregationDuration.Hours(2), start.AddDays(-1), start.AddMonths(1)).ToArray(); Assert.Equal(256, time.Length); for (int i = 0; i < 256; i++) { #if DEBUG Assert.Equal("Time", time[i].DebugKey); #endif Assert.Equal(start.AddMonths(-1).AddHours(i * 3), time[i].StartAt); if (i == 248 || i == 249 || i == 250) { Assert.Equal(i == 250 ? 1 : 3, time[i].Values[0].Volume); Assert.Equal(i == 250 ? 1 : 3, time[i].Values[1].Volume); Assert.Equal(i == 250 ? 1 : 3, time[i].Values[1].Sum); Assert.Equal(1, time[i].Values[1].Low); Assert.Equal(1, time[i].Values[1].Open); Assert.Equal(1, time[i].Values[1].Close); Assert.Equal(1, time[i].Values[1].High); Assert.Equal(i == 250 ? 1 : 3, time[i].Values[2].Volume); Assert.Equal(time[i].StartAt.Ticks, time[i].Values[2].Low); Assert.Equal(time[i].StartAt.Ticks, time[i].Values[2].Open); Assert.Equal(time[i].StartAt.AddHours(i == 250 ? 0 : 2).Ticks, time[i].Values[2].Close); Assert.Equal(time[i].StartAt.AddHours(i == 250 ? 0 : 2).Ticks, time[i].Values[2].High); if (i == 250) { Assert.Equal(time[i].StartAt.Ticks, time[i].Values[2].Sum); } else { Assert.Equal(time[i].StartAt.Ticks + time[i].StartAt.AddHours(1).Ticks + time[i].StartAt.AddHours(2).Ticks, time[i].Values[2].Sum); } continue; } for (int j = 0; j < 3; j++) { Assert.Equal(0, time[i].Values[j].Volume); Assert.Equal(0, time[i].Values[j].Sum); Assert.Equal(0, time[i].Values[j].Close); Assert.Equal(0, time[i].Values[j].High); Assert.Equal(0, time[i].Values[j].Low); Assert.Equal(0, time[i].Values[j].Open); } } Assert.Equal(19000, time[248].Values[0].Low); Assert.Equal(19000, time[248].Values[0].Open); Assert.Equal(19002, time[248].Values[0].Close); Assert.Equal(19002, time[248].Values[0].High); Assert.Equal(19000 * 3 + 3, time[248].Values[0].Sum); Assert.Equal(19003, time[249].Values[0].Low); Assert.Equal(19003, time[249].Values[0].Open); Assert.Equal(19005, time[249].Values[0].Close); Assert.Equal(19005, time[249].Values[0].High); Assert.Equal(19003 * 3 + 3, time[249].Values[0].Sum); Assert.Equal(19006, time[250].Values[0].Low); Assert.Equal(19006, time[250].Values[0].Open); Assert.Equal(19006, time[250].Values[0].Close); Assert.Equal(19006, time[250].Values[0].High); Assert.Equal(19006, time[250].Values[0].Sum); Assert.Equal(372, money.Length); for (int i = 0; i < 372; i++) { #if DEBUG Assert.Equal("Money", money[i].DebugKey); #endif Assert.Equal(start.AddDays(-1).AddHours(i * 2), money[i].StartAt); if (i >= 12 && i <= 16) { continue; } for (int j = 0; j < 3; j++) { Assert.Equal(0, money[i].Values[j].Volume); Assert.Equal(0, money[i].Values[j].Sum); Assert.Equal(0, money[i].Values[j].Close); Assert.Equal(0, money[i].Values[j].High); Assert.Equal(0, money[i].Values[j].Low); Assert.Equal(0, money[i].Values[j].Open); } } } var start2 = start.AddMonths(-1).AddDays(5); using (var writer = tss.CreateWriter()) { int value = 6; for (int i = 0; i < 4; i++) { writer.Append("3Val", "Time", start2.AddHours(2 + i), value++, StringToIndex("Time"), start2.AddHours(2 + i).Ticks); writer.Append("3Val", "Is", start2.AddHours(2 + i), value++, StringToIndex("Is"), start2.AddHours(2 + i).Ticks); writer.Append("3Val", "Money", start2.AddHours(2 + i), value++, StringToIndex("Money"), start2.AddHours(2 + i).Ticks); } writer.Commit(); } using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("3Val", "Time", AggregationDuration.Hours(3), start.AddMonths(-1), start.AddDays(1)).ToArray(); var money = r.GetAggregatedPoints("3Val", "Money", AggregationDuration.Hours(2), start.AddMonths(-2).AddDays(-1), start.AddMonths(2)).ToArray(); Assert.Equal(256, time.Length); for (int i = 0; i < 256; i++) { #if DEBUG Assert.Equal("Time", time[i].DebugKey); #endif Assert.Equal(start.AddMonths(-1).AddHours(i * 3), time[i].StartAt); if (i == 40 || i == 41) { Assert.Equal(i == 40 ? 1 : 3, time[i].Values[0].Volume); Assert.Equal(i == 40 ? 1 : 3, time[i].Values[1].Volume); Assert.Equal(i == 40 ? 1 : 3, time[i].Values[1].Sum); Assert.Equal(1, time[i].Values[1].Low); Assert.Equal(1, time[i].Values[1].Open); Assert.Equal(1, time[i].Values[1].Close); Assert.Equal(1, time[i].Values[1].High); Assert.Equal(i == 40 ? 1 : 3, time[i].Values[2].Volume); Assert.Equal(time[i].StartAt.AddHours(i == 40 ? 2 : 0).Ticks, time[i].Values[2].Low); Assert.Equal(time[i].StartAt.AddHours(i == 40 ? 2 : 0).Ticks, time[i].Values[2].Open); Assert.Equal(time[i].StartAt.AddHours(2).Ticks, time[i].Values[2].Close); Assert.Equal(time[i].StartAt.AddHours(2).Ticks, time[i].Values[2].High); if (i == 40) { Assert.Equal(time[i].StartAt.AddHours(2).Ticks, time[i].Values[2].Sum); } else { Assert.Equal(time[i].StartAt.Ticks + time[i].StartAt.AddHours(1).Ticks + time[i].StartAt.AddHours(2).Ticks, time[i].Values[2].Sum); } continue; } if (i == 248 || i == 249 || i == 250) { Assert.Equal(i == 250 ? 1 : 3, time[i].Values[0].Volume); Assert.Equal(i == 250 ? 1 : 3, time[i].Values[1].Volume); Assert.Equal(i == 250 ? 1 : 3, time[i].Values[1].Sum); Assert.Equal(1, time[i].Values[1].Low); Assert.Equal(1, time[i].Values[1].Open); Assert.Equal(1, time[i].Values[1].Close); Assert.Equal(1, time[i].Values[1].High); Assert.Equal(i == 250 ? 1 : 3, time[i].Values[2].Volume); Assert.Equal(time[i].StartAt.Ticks, time[i].Values[2].Low); Assert.Equal(time[i].StartAt.Ticks, time[i].Values[2].Open); Assert.Equal(time[i].StartAt.AddHours(i == 250 ? 0 : 2).Ticks, time[i].Values[2].Close); Assert.Equal(time[i].StartAt.AddHours(i == 250 ? 0 : 2).Ticks, time[i].Values[2].High); if (i == 250) { Assert.Equal(time[i].StartAt.Ticks, time[i].Values[2].Sum); } else { Assert.Equal(time[i].StartAt.Ticks + time[i].StartAt.AddHours(1).Ticks + time[i].StartAt.AddHours(2).Ticks, time[i].Values[2].Sum); } continue; } for (int j = 0; j < 3; j++) { Assert.Equal(0, time[i].Values[j].Volume); Assert.Equal(0, time[i].Values[j].Sum); Assert.Equal(0, time[i].Values[j].Close); Assert.Equal(0, time[i].Values[j].High); Assert.Equal(0, time[i].Values[j].Low); Assert.Equal(0, time[i].Values[j].Open); } } Assert.Equal(19000, time[248].Values[0].Low); Assert.Equal(19000, time[248].Values[0].Open); Assert.Equal(19002, time[248].Values[0].Close); Assert.Equal(19002, time[248].Values[0].High); Assert.Equal(19000 * 3 + 3, time[248].Values[0].Sum); Assert.Equal(19003, time[249].Values[0].Low); Assert.Equal(19003, time[249].Values[0].Open); Assert.Equal(19005, time[249].Values[0].Close); Assert.Equal(19005, time[249].Values[0].High); Assert.Equal(19003 * 3 + 3, time[249].Values[0].Sum); Assert.Equal(19006, time[250].Values[0].Low); Assert.Equal(19006, time[250].Values[0].Open); Assert.Equal(19006, time[250].Values[0].Close); Assert.Equal(19006, time[250].Values[0].High); Assert.Equal(19006, time[250].Values[0].Sum); Assert.Equal(1452, money.Length); for (int i = 0; i < 1452; i++) { #if DEBUG Assert.Equal("Money", money[i].DebugKey); #endif Assert.Equal(AggregationDuration.Hours(2), money[i].Duration); Assert.Equal(start.AddMonths(-2).AddDays(-1).AddHours(2 * i), money[i].StartAt); if ((i >= 409 && i <= 410) || (i >= 720 && i <= 723)) { Assert.Equal(i == 723 ? 1 : 2, money[i].Values[0].Volume); Assert.Equal(i == 723 ? 1 : 2, money[i].Values[1].Volume); Assert.Equal(i == 723 ? 3 : 6, money[i].Values[1].Sum); Assert.Equal(3, money[i].Values[1].Low); Assert.Equal(3, money[i].Values[1].Open); Assert.Equal(3, money[i].Values[1].Close); Assert.Equal(3, money[i].Values[1].High); Assert.Equal(i == 723 ? 1 : 2, money[i].Values[2].Volume); Assert.Equal(money[i].StartAt.Ticks, money[i].Values[2].Low); Assert.Equal(money[i].StartAt.Ticks, money[i].Values[2].Open); Assert.Equal(money[i].StartAt.AddHours(i == 723 ? 0 : 1).Ticks, money[i].Values[2].Close); Assert.Equal(money[i].StartAt.AddHours(i == 723 ? 0 : 1).Ticks, money[i].Values[2].High); if (i == 723) { Assert.Equal(money[i].StartAt.Ticks, money[i].Values[2].Sum); } else { Assert.Equal(money[i].StartAt.Ticks + money[i].StartAt.AddHours(1).Ticks, money[i].Values[2].Sum); } continue; } for (int j = 0; j < 3; j++) { Assert.Equal(0, money[i].Values[j].Volume); Assert.Equal(0, money[i].Values[j].Sum); Assert.Equal(0, money[i].Values[j].Close); Assert.Equal(0, money[i].Values[j].High); Assert.Equal(0, money[i].Values[j].Low); Assert.Equal(0, money[i].Values[j].Open); } } } } }
public async Task <AggregatedPoint[]> GetAggregatedPoints(string type, string key, AggregationDuration duration, DateTimeOffset?start = null, DateTimeOffset?end = null, int skip = 0, int take = 20, CancellationToken token = default(CancellationToken)) { parent.AssertInitialized(); await parent.ReplicationInformer.UpdateReplicationInformationIfNeededAsync().ConfigureAwait(false); return(await parent.ReplicationInformer.ExecuteWithReplicationAsync(parent.Url, HttpMethods.Get, async (url, timeSeriesName) => { var requestUriString = string.Format(CultureInfo.InvariantCulture, "{0}ts/{1}/aggregated-points/{2}?key={3}&durationType={8}&duration={9}&skip={4}&take={5}&start={6}&end={7}", url, timeSeriesName, type, Uri.EscapeDataString(key), skip, take, EscapeDataString(start), EscapeDataString(end), duration.Type, duration.Duration); using (var request = parent.CreateHttpJsonRequest(requestUriString, HttpMethods.Get)) { var result = await request.ReadResponseJsonAsync().WithCancellation(token).ConfigureAwait(false); return result.JsonDeserialization <AggregatedPoint[]>(); } }, token).ConfigureAwait(false)); }
public void ByMonths() { using (var tss = GetStorage()) { var start = new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero); using (var writer = tss.CreateWriter()) { for (int i = 10; i < 5000; i++) { var dateTime = start.AddHours(6 * i); writer.Append("Simple", "Time", dateTime, i); } writer.Commit(); } using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Months(2), start.AddYears(-1), start.AddYears(2)).ToArray(); Assert.Equal(3 * 12 / 2, time.Length); Assert.Equal(new DateTimeOffset(2014, 4, 1, 0, 0, 0, TimeSpan.Zero), time[0].StartAt); Assert.Equal(new DateTimeOffset(2014, 6, 1, 0, 0, 0, TimeSpan.Zero), time[1].StartAt); Assert.Equal(new DateTimeOffset(2014, 8, 1, 0, 0, 0, TimeSpan.Zero), time[2].StartAt); Assert.Equal(new DateTimeOffset(2014, 10, 1, 0, 0, 0, TimeSpan.Zero), time[3].StartAt); Assert.Equal(new DateTimeOffset(2014, 12, 1, 0, 0, 0, TimeSpan.Zero), time[4].StartAt); Assert.Equal(new DateTimeOffset(2015, 2, 1, 0, 0, 0, TimeSpan.Zero), time[5].StartAt); Assert.Equal(new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero), time[6].StartAt); Assert.Equal(new DateTimeOffset(2015, 6, 1, 0, 0, 0, TimeSpan.Zero), time[7].StartAt); Assert.Equal(new DateTimeOffset(2015, 8, 1, 0, 0, 0, TimeSpan.Zero), time[8].StartAt); Assert.Equal(new DateTimeOffset(2015, 10, 1, 0, 0, 0, TimeSpan.Zero), time[9].StartAt); Assert.Equal(new DateTimeOffset(2015, 12, 1, 0, 0, 0, TimeSpan.Zero), time[10].StartAt); Assert.Equal(new DateTimeOffset(2016, 2, 1, 0, 0, 0, TimeSpan.Zero), time[11].StartAt); Assert.Equal(new DateTimeOffset(2016, 4, 1, 0, 0, 0, TimeSpan.Zero), time[12].StartAt); Assert.Equal(new DateTimeOffset(2016, 6, 1, 0, 0, 0, TimeSpan.Zero), time[13].StartAt); Assert.Equal(new DateTimeOffset(2016, 8, 1, 0, 0, 0, TimeSpan.Zero), time[14].StartAt); Assert.Equal(new DateTimeOffset(2016, 10, 1, 0, 0, 0, TimeSpan.Zero), time[15].StartAt); Assert.Equal(new DateTimeOffset(2016, 12, 1, 0, 0, 0, TimeSpan.Zero), time[16].StartAt); Assert.Equal(new DateTimeOffset(2017, 2, 1, 0, 0, 0, TimeSpan.Zero), time[17].StartAt); for (int i = 0; i < 18; i++) { #if DEBUG Assert.Equal("Time", time[i].DebugKey); #endif Assert.Equal(AggregationDuration.Months(2), time[i].Duration); var daysInMonth = DateTime.DaysInMonth(time[i].StartAt.Year, time[i].StartAt.Month) + DateTime.DaysInMonth(time[i].StartAt.AddMonths(1).Year, time[i].StartAt.AddMonths(1).Month); if (i == 6) { Assert.Equal(daysInMonth * 4 - 2.5 * 4, time[i].Values[0].Volume); Assert.NotEqual(0, time[i].Values[0].Sum); } else if (i > 6) { Assert.Equal(daysInMonth * 4, time[i].Values[0].Volume); Assert.NotEqual(0, time[i].Values[0].Sum); Assert.NotEqual(0, time[i].Values[0].High); Assert.NotEqual(0, time[i].Values[0].Low); Assert.NotEqual(0, time[i].Values[0].Open); Assert.NotEqual(0, time[i].Values[0].Close); } else { Assert.Equal(0, time[i].Values[0].Volume); Assert.Equal(0, time[i].Values[0].Sum); } } } } }
public void CanQueryDataInSpecificDuration_LowerDurationThanActualOnDisk() { using (var tss = GetStorage()) { WriteTestData(tss); var start = new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero); using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Seconds(3), start.AddHours(-1), start.AddHours(4)).ToArray(); var money = r.GetAggregatedPoints("Simple", "Money", AggregationDuration.Minutes(3), start.AddHours(-1), start.AddHours(4)).ToArray(); Assert.Equal(5 * 60 * 60 / 3, time.Length); for (int i = 0; i < 6000; i++) { if (i == 1200 || i == 2400 || i == 3600) { continue; } Assert.Equal(AggregationDuration.Seconds(3), time[i].Duration); #if DEBUG Assert.Equal("Time", time[i].DebugKey); #endif Assert.Equal(0, time[i].Values[0].Volume); Assert.Equal(0, time[i].Values[0].Open); Assert.Equal(0, time[i].Values[0].Close); Assert.Equal(0, time[i].Values[0].Low); Assert.Equal(0, time[i].Values[0].High); Assert.Equal(0, time[i].Values[0].Sum); Assert.Equal(0, time[i].Values[0].Volume); } #if DEBUG Assert.Equal("Time", time[1200].DebugKey); #endif Assert.Equal(AggregationDuration.Seconds(3), time[1200].Duration); Assert.Equal(AggregationDuration.Seconds(3), time[2400].Duration); Assert.Equal(AggregationDuration.Seconds(3), time[3600].Duration); Assert.Equal(1, time[1200].Values[0].Volume); Assert.Equal(1, time[2400].Values[0].Volume); Assert.Equal(1, time[3600].Values[0].Volume); Assert.Equal(10, time[1200].Values[0].Open); Assert.Equal(19, time[2400].Values[0].Open); Assert.Equal(50, time[3600].Values[0].Open); Assert.Equal(10, time[1200].Values[0].Close); Assert.Equal(19, time[2400].Values[0].Close); Assert.Equal(50, time[3600].Values[0].Close); Assert.Equal(10, time[1200].Values[0].Low); Assert.Equal(19, time[2400].Values[0].Low); Assert.Equal(50, time[3600].Values[0].Low); Assert.Equal(10, time[1200].Values[0].High); Assert.Equal(19, time[2400].Values[0].High); Assert.Equal(50, time[3600].Values[0].High); Assert.Equal(10, time[1200].Values[0].Sum); Assert.Equal(19, time[2400].Values[0].Sum); Assert.Equal(50, time[3600].Values[0].Sum); Assert.Equal(1, time[1200].Values[0].Volume); Assert.Equal(1, time[2400].Values[0].Volume); Assert.Equal(1, time[3600].Values[0].Volume); Assert.Equal(5 * 60 / 3, money.Length); for (int i = 0; i < 100; i++) { #if DEBUG Assert.Equal("Money", money[i].DebugKey); #endif Assert.Equal(AggregationDuration.Minutes(3), money[i].Duration); if (i == 20 || i == 40 || i == 60) { continue; } Assert.Equal(0, money[i].Values[0].Volume); Assert.Equal(0, money[i].Values[0].Open); Assert.Equal(0, money[i].Values[0].Close); Assert.Equal(0, money[i].Values[0].Low); Assert.Equal(0, money[i].Values[0].High); Assert.Equal(0, money[i].Values[0].Sum); Assert.Equal(0, money[i].Values[0].Volume); } Assert.Equal(1, money[20].Values[0].Volume); Assert.Equal(1, money[40].Values[0].Volume); Assert.Equal(1, money[60].Values[0].Volume); Assert.Equal(54, money[20].Values[0].Open); Assert.Equal(546, money[40].Values[0].Open); Assert.Equal(130, money[60].Values[0].Open); Assert.Equal(54, money[20].Values[0].Close); Assert.Equal(546, money[40].Values[0].Close); Assert.Equal(130, money[60].Values[0].Close); Assert.Equal(54, money[20].Values[0].Low); Assert.Equal(546, money[40].Values[0].Low); Assert.Equal(130, money[60].Values[0].Low); Assert.Equal(54, money[20].Values[0].High); Assert.Equal(546, money[40].Values[0].High); Assert.Equal(130, money[60].Values[0].High); Assert.Equal(54, money[20].Values[0].Sum); Assert.Equal(546, money[40].Values[0].Sum); Assert.Equal(130, money[60].Values[0].Sum); Assert.Equal(1, money[20].Values[0].Volume); Assert.Equal(1, money[40].Values[0].Volume); Assert.Equal(1, money[60].Values[0].Volume); } } }
public void CanQueryDataInSpecificDurationAverageUpdateRollup() { using (var tss = GetStorage()) { WriteTestData(tss); var start = new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero); using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Hours(3), start.AddMonths(-1), start.AddDays(1)).ToArray(); var money = r.GetAggregatedPoints("Simple", "Money", AggregationDuration.Hours(2), start.AddDays(-1), start.AddMonths(1)).ToArray(); Assert.Equal(24 / 3 * 32, time.Length); for (int i = 0; i < 256; i++) { if (i == 248) { continue; } Assert.Equal(0, time[i].Values[0].Volume); } Assert.Equal("26.3333333333333", (time[248].Values[0].Sum / time[248].Values[0].Volume).ToString(CultureInfo.InvariantCulture)); #if DEBUG Assert.Equal("Time", time[248].DebugKey); #endif Assert.Equal(AggregationDuration.Hours(3), time[248].Duration); Assert.Equal(3, time[248].Values[0].Volume); Assert.Equal(10, time[248].Values[0].Open); Assert.Equal(50, time[248].Values[0].Close); Assert.Equal(50, time[248].Values[0].High); Assert.Equal(10, time[248].Values[0].Low); Assert.Equal(24 / 2 * 31, money.Length); for (int i = 0; i < 372; i++) { if (i == 12 || i == 13) { continue; } Assert.Equal(0, money[i].Values[0].Volume); } #if DEBUG Assert.Equal("Money", money[12].DebugKey); #endif Assert.Equal(300, money[12].Values[0].Sum / money[12].Values[0].Volume); Assert.Equal(130, money[13].Values[0].Sum / money[13].Values[0].Volume); Assert.Equal(AggregationDuration.Hours(2), money[12].Duration); Assert.Equal(AggregationDuration.Hours(2), money[13].Duration); Assert.Equal(2, money[12].Values[0].Volume); Assert.Equal(1, money[13].Values[0].Volume); Assert.Equal(54, money[12].Values[0].Open); Assert.Equal(130, money[13].Values[0].Open); Assert.Equal(546, money[12].Values[0].Close); Assert.Equal(130, money[13].Values[0].Close); Assert.Equal(54, money[12].Values[0].Low); Assert.Equal(130, money[13].Values[0].Low); Assert.Equal(546, money[12].Values[0].High); Assert.Equal(130, money[13].Values[0].High); } var start2 = start.AddMonths(-1).AddDays(5); using (var writer = tss.CreateWriter()) { int value = 6; for (int i = 0; i < 4; i++) { writer.Append("Simple", "Time", start2.AddHours(2 + i), value++); writer.Append("Simple", "Is", start2.AddHours(3 + i), value++); writer.Append("Simple", "Money", start2.AddHours(3 + i), value++); } writer.Commit(); } using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Hours(3), start.AddMonths(-1), start.AddDays(1)).ToArray(); var money = r.GetAggregatedPoints("Simple", "Money", AggregationDuration.Hours(2), start.AddMonths(-2).AddDays(-1), start.AddMonths(2)).ToArray(); Assert.Equal(24 / 3 * 32, time.Length); for (int i = 0; i < 256; i++) { #if DEBUG Assert.Equal("Time", time[i].DebugKey); #endif Assert.Equal(AggregationDuration.Hours(3), time[i].Duration); if (i == 40 || i == 41 || i == 248) { continue; } Assert.Equal(0, time[i].Values[0].Volume); } Assert.Equal(1, time[40].Values[0].Volume); Assert.Equal(6, time[40].Values[0].Sum); Assert.Equal(6, time[40].Values[0].Open); Assert.Equal(6, time[40].Values[0].Close); Assert.Equal(6, time[40].Values[0].High); Assert.Equal(6, time[40].Values[0].Low); Assert.Equal(3, time[41].Values[0].Volume); Assert.Equal(36, time[41].Values[0].Sum); Assert.Equal(9, time[41].Values[0].Open); Assert.Equal(15, time[41].Values[0].Close); Assert.Equal(15, time[41].Values[0].High); Assert.Equal(9, time[41].Values[0].Low); Assert.Equal("26.3333333333333", (time[248].Values[0].Sum / time[248].Values[0].Volume).ToString(CultureInfo.InvariantCulture)); Assert.Equal(3, time[248].Values[0].Volume); Assert.Equal(79, time[248].Values[0].Sum); Assert.Equal(10, time[248].Values[0].Open); Assert.Equal(50, time[248].Values[0].Close); Assert.Equal(50, time[248].Values[0].High); Assert.Equal(10, time[248].Values[0].Low); Assert.Equal((60 + 1 + 60) * 24 / 2, money.Length); for (int i = 0; i < 1452; i++) { #if DEBUG Assert.Equal("Money", money[i].DebugKey); #endif Assert.Equal(AggregationDuration.Hours(2), money[i].Duration); if (i == 409 || i == 410 || i == 411 || i == 720 || i == 721) { continue; } Assert.Equal(0, money[i].Values[0].Volume); } Assert.Equal(1, money[409].Values[0].Volume); Assert.Equal(2, money[410].Values[0].Volume); Assert.Equal(1, money[411].Values[0].Volume); Assert.Equal(8, money[409].Values[0].Sum); Assert.Equal(8, money[409].Values[0].Open); Assert.Equal(8, money[409].Values[0].Close); Assert.Equal(8, money[409].Values[0].Low); Assert.Equal(8, money[409].Values[0].High); Assert.Equal(25, money[410].Values[0].Sum); Assert.Equal(11, money[410].Values[0].Open); Assert.Equal(14, money[410].Values[0].Close); Assert.Equal(11, money[410].Values[0].Low); Assert.Equal(14, money[410].Values[0].High); Assert.Equal(17, money[411].Values[0].Sum); Assert.Equal(17, money[411].Values[0].Open); Assert.Equal(17, money[411].Values[0].Close); Assert.Equal(17, money[411].Values[0].Low); Assert.Equal(17, money[411].Values[0].High); Assert.Equal(300, money[720].Values[0].Sum / money[720].Values[0].Volume); Assert.Equal(130, money[721].Values[0].Sum / money[721].Values[0].Volume); Assert.Equal(2, money[720].Values[0].Volume); Assert.Equal(1, money[721].Values[0].Volume); Assert.Equal(54, money[720].Values[0].Open); Assert.Equal(130, money[721].Values[0].Open); Assert.Equal(546, money[720].Values[0].Close); Assert.Equal(130, money[721].Values[0].Close); Assert.Equal(54, money[720].Values[0].Low); Assert.Equal(130, money[721].Values[0].Low); Assert.Equal(546, money[720].Values[0].High); Assert.Equal(130, money[721].Values[0].High); } } }
public void CanQueryDataInSpecificDurationAverage() { using (var tss = GetStorage()) { WriteTestData(tss); var start = new DateTimeOffset(2015, 4, 1, 0, 0, 0, TimeSpan.Zero); using (var r = tss.CreateReader()) { var time = r.GetAggregatedPoints("Simple", "Time", AggregationDuration.Hours(3), start.AddMonths(-1), start.AddDays(1)).ToArray(); var money = r.GetAggregatedPoints("Simple", "Money", AggregationDuration.Hours(2), start.AddDays(-1), start.AddMonths(1)).ToArray(); Assert.Equal(24 / 3 * 32, time.Length); for (int i = 0; i < 256; i++) { #if DEBUG Assert.Equal("Time", time[i].DebugKey); #endif Assert.Equal(AggregationDuration.Hours(3), time[i].Duration); if (i == 248 || i == 249) { continue; } Assert.Equal(0, time[i].Values[0].Volume); } Assert.Equal("26.3333333333333", (time[248].Values[0].Sum / time[248].Values[0].Volume).ToString(CultureInfo.InvariantCulture)); Assert.Equal(3, time[248].Values[0].Volume); Assert.Equal(10, time[248].Values[0].Open); Assert.Equal(50, time[248].Values[0].Close); Assert.Equal(50, time[248].Values[0].High); Assert.Equal(10, time[248].Values[0].Low); Assert.Equal(24 / 2 * 31, money.Length); for (int i = 0; i < 372; i++) { if (i == 12 || i == 13) { continue; } Assert.Equal(0, money[i].Values[0].Volume); } #if DEBUG Assert.Equal("Money", money[12].DebugKey); #endif Assert.Equal(300, money[12].Values[0].Sum / money[12].Values[0].Volume); Assert.Equal(130, money[13].Values[0].Sum / money[13].Values[0].Volume); Assert.Equal(AggregationDuration.Hours(2), money[12].Duration); Assert.Equal(AggregationDuration.Hours(2), money[13].Duration); Assert.Equal(2, money[12].Values[0].Volume); Assert.Equal(1, money[13].Values[0].Volume); Assert.Equal(54, money[12].Values[0].Open); Assert.Equal(130, money[13].Values[0].Open); Assert.Equal(546, money[12].Values[0].Close); Assert.Equal(130, money[13].Values[0].Close); Assert.Equal(54, money[12].Values[0].Low); Assert.Equal(130, money[13].Values[0].Low); Assert.Equal(546, money[12].Values[0].High); Assert.Equal(130, money[13].Values[0].High); } } }