/ToMigrate/Raven.Tests.TimeSeries/TimeSeriesOperations.cs

https://github.com/fitzchak/ravendb · C# · 417 lines · 360 code · 52 blank · 5 comment · 6 complexity · 9d644d7aca058a199cab5eea29206e76 MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="TimeSeriesOperations.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Raven.Abstractions.Connection;
  10. using Raven.Abstractions.TimeSeries;
  11. using Raven.Database.TimeSeries;
  12. using Raven.Tests.Common;
  13. using Xunit;
  14. namespace Raven.Tests.TimeSeries
  15. {
  16. public class TimeSeriesOperations : RavenBaseTimeSeriesTest
  17. {
  18. [Fact]
  19. public async Task SimpleAppend()
  20. {
  21. using (var store = NewRemoteTimeSeriesStore())
  22. {
  23. await store.CreateTypeAsync("Simple", new[] { "Value" });
  24. await store.CreateTypeAsync("FourValues", new[] { "Value 1", "Value Two", "Value Three", "Value 4" });
  25. await store.AppendAsync("FourValues", "Time", DateTimeOffset.Now, new[] { 3D, 4D, 5D, 6D });
  26. await store.AppendAsync("Simple", "Is", DateTimeOffset.Now, 3D);
  27. await store.AppendAsync("Simple", "Money", DateTimeOffset.Now, 3D);
  28. var cancellationToken = new CancellationToken();
  29. await store.AppendAsync("Simple", "Is", DateTimeOffset.Now, 3456D, cancellationToken);
  30. await store.AppendAsync("FourValues", "Time", DateTimeOffset.Now, new[] { 23D, 4D, 5D, 6D }, cancellationToken);
  31. await store.AppendAsync("FourValues", "Time", DateTimeOffset.Now, cancellationToken, 33D, 4D, 5D, 6D);
  32. var stats = await store.GetStatsAsync(cancellationToken);
  33. Assert.Equal(2, stats.TypesCount);
  34. Assert.Equal(3, stats.KeysCount);
  35. Assert.Equal(6, stats.PointsCount);
  36. }
  37. }
  38. [Fact]
  39. public async Task ShouldNotAllowOverwriteType_OnlyIfTypeHasKeys()
  40. {
  41. using (var store = NewRemoteTimeSeriesStore())
  42. {
  43. await store.CreateTypeAsync("Simple", new[] { "Value" });
  44. await store.CreateTypeAsync("Simple", new[] {"Value", "Another value"}); // Can overwrite, because we don't have keys yet
  45. await store.AppendAsync("Simple", "Is", DateTimeOffset.Now, CancellationToken.None, 3456D, 8888D);
  46. var exception = await AssertAsync.Throws<ErrorResponseException>(async () => await store.CreateTypeAsync("Simple", new[] { "Value", "Another value", "Different Value" }));
  47. Assert.Contains("System.InvalidOperationException: There an existing type with the same name but with different fields. Since the type has already 1 keys, the replication failed to overwrite this type.", exception.Message);
  48. var stats = await store.GetStatsAsync();
  49. Assert.Equal(1, stats.TypesCount);
  50. Assert.Equal(1, stats.KeysCount);
  51. Assert.Equal(1, stats.PointsCount);
  52. }
  53. }
  54. [Fact]
  55. public async Task SimpleAppend_ShouldFailIfTwoKeysAsDifferentValuesLength()
  56. {
  57. using (var store = NewRemoteTimeSeriesStore())
  58. {
  59. await store.CreateTypeAsync("Simple", new[] { "Value" });
  60. await store.AppendAsync("Simple", "Time", DateTimeOffset.Now, 3D);
  61. var exception = await AssertAsync.Throws<ErrorResponseException>(async () => await store.AppendAsync("Simple", "Time", DateTimeOffset.Now, new[] { 3D, 4D, 5D, 6D }));
  62. Assert.Contains("System.ArgumentOutOfRangeException: Appended values should be the same length the series values length which is 1 and not 4", exception.Message);
  63. var stats = await store.GetStatsAsync();
  64. Assert.Equal(1, stats.TypesCount);
  65. Assert.Equal(1, stats.KeysCount);
  66. Assert.Equal(1, stats.PointsCount);
  67. }
  68. }
  69. [Fact]
  70. public async Task AddAndDeleteType_ShouldThrowIfTypeHasData()
  71. {
  72. using (var store = NewRemoteTimeSeriesStore())
  73. {
  74. await store.CreateTypeAsync("Simple", new[] { "Value" });
  75. await store.AppendAsync("Simple", "Is", DateTimeOffset.Now, 3D);
  76. var exception = await AssertAsync.Throws<ErrorResponseException>(async () => await store.DeleteTypeAsync("Simple"));
  77. Assert.Contains("System.InvalidOperationException: Cannot delete type 'Simple' because it has 1 associated key.", exception.Message);
  78. var stats = await store.GetStatsAsync();
  79. Assert.Equal(1, stats.TypesCount);
  80. Assert.Equal(1, stats.KeysCount);
  81. Assert.Equal(1, stats.PointsCount);
  82. }
  83. }
  84. [Fact]
  85. public async Task AddAndDeleteType()
  86. {
  87. using (var store = NewRemoteTimeSeriesStore())
  88. {
  89. await store.CreateTypeAsync("Simple", new[] { "Value" });
  90. await store.AppendAsync("Simple", "Is", DateTimeOffset.Now, 3D);
  91. await store.DeleteKeyAsync("Simple", "Is");
  92. await store.DeleteTypeAsync("Simple");
  93. var stats = await store.GetStatsAsync();
  94. Assert.Equal(0, stats.TypesCount);
  95. Assert.Equal(0, stats.KeysCount);
  96. Assert.Equal(0, stats.PointsCount);
  97. }
  98. }
  99. [Fact]
  100. public async Task DeletePoints()
  101. {
  102. using (var store = NewRemoteTimeSeriesStore())
  103. {
  104. await store.CreateTypeAsync("Simple", new[] { "Value" });
  105. var start = DateTimeOffset.Now;
  106. for (int i = 0; i < 5; i++)
  107. {
  108. await store.AppendAsync("Simple", "Is", start.AddMinutes(i), 3D);
  109. }
  110. var stats = await store.GetStatsAsync();
  111. Assert.Equal(1, stats.TypesCount);
  112. Assert.Equal(1, stats.KeysCount);
  113. Assert.Equal(5, stats.PointsCount);
  114. await store.DeletePointAsync("Simple", "Is", start.AddMinutes(2));
  115. await store.DeletePointsAsync(new[]
  116. {
  117. new TimeSeriesPointId
  118. {
  119. Type = "Simple", Key = "Is", At = start.AddMinutes(3)
  120. },
  121. new TimeSeriesPointId
  122. {
  123. Type = "Simple", Key = "Is", At = start.AddMinutes(4)
  124. },
  125. });
  126. stats = await store.GetStatsAsync();
  127. Assert.Equal(1, stats.TypesCount);
  128. Assert.Equal(1, stats.KeysCount);
  129. Assert.Equal(2, stats.PointsCount);
  130. }
  131. }
  132. [Fact]
  133. public async Task DeleteRange()
  134. {
  135. using (var store = NewRemoteTimeSeriesStore())
  136. {
  137. await store.CreateTypeAsync("Simple", new[] { "Value" });
  138. var start = new DateTimeOffset(2015, 1, 1, 0, 0, 0, TimeSpan.Zero);
  139. for (int i = 0; i < 12; i++)
  140. {
  141. await store.AppendAsync("Simple", "Time", start.AddHours(i), i + 3D);
  142. }
  143. var stats = await store.GetStatsAsync();
  144. Assert.Equal(1, stats.TypesCount);
  145. Assert.Equal(1, stats.KeysCount);
  146. Assert.Equal(12, stats.PointsCount);
  147. await store.DeleteRangeAsync("Simple", "Time", start.AddHours(3), start.AddHours(7));
  148. stats = await store.GetStatsAsync();
  149. Assert.Equal(1, stats.TypesCount);
  150. Assert.Equal(1, stats.KeysCount);
  151. Assert.Equal(7, stats.PointsCount);
  152. }
  153. }
  154. [Fact]
  155. public async Task DeleteBigRange()
  156. {
  157. using (var store = NewRemoteTimeSeriesStore())
  158. {
  159. await store.CreateTypeAsync("Simple", new[] { "Value" });
  160. var start = new DateTimeOffset(2015, 1, 1, 0, 0, 0, TimeSpan.Zero);
  161. for (int i = 0; i < 1200; i++)
  162. {
  163. await store.AppendAsync("Simple", "Time", start.AddHours(i), i + 3D);
  164. }
  165. var stats = await store.GetStatsAsync();
  166. Assert.Equal(1, stats.TypesCount);
  167. Assert.Equal(1, stats.KeysCount);
  168. Assert.Equal(1200, stats.PointsCount);
  169. await store.DeleteRangeAsync("Simple", "Time", start.AddHours(3), start.AddYears(2));
  170. stats = await store.GetStatsAsync();
  171. Assert.Equal(1, stats.TypesCount);
  172. Assert.Equal(3, stats.PointsCount);
  173. Assert.Equal(1, stats.KeysCount);
  174. }
  175. }
  176. [Fact]
  177. public async Task DeleteBigRange_DeleteAll()
  178. {
  179. using (var store = NewRemoteTimeSeriesStore())
  180. {
  181. await store.CreateTypeAsync("Simple", new[] { "Value" });
  182. var start = new DateTimeOffset(2015, 1, 1, 0, 0, 0, TimeSpan.Zero);
  183. for (int i = 0; i < 1200; i++)
  184. {
  185. await store.AppendAsync("Simple", "Time", start.AddHours(i), i + 3D);
  186. }
  187. var stats = await store.GetStatsAsync();
  188. Assert.Equal(1, stats.TypesCount);
  189. Assert.Equal(1, stats.KeysCount);
  190. Assert.Equal(1200, stats.PointsCount);
  191. await store.DeleteRangeAsync("Simple", "Time", DateTimeOffset.MinValue, DateTimeOffset.MaxValue);
  192. stats = await store.GetStatsAsync();
  193. Assert.Equal(1, stats.TypesCount);
  194. Assert.Equal(0, stats.KeysCount);
  195. Assert.Equal(0, stats.PointsCount);
  196. }
  197. }
  198. [Fact]
  199. public async Task AdvancedAppend()
  200. {
  201. var start = DateTimeOffset.Now;
  202. using (var store = NewRemoteTimeSeriesStore())
  203. {
  204. await store.CreateTypeAsync("Simple", new[] { "Value" });
  205. await store.CreateTypeAsync("FourValues", new[] { "Value 1", "Value Two", "Value Three", "Value 4" });
  206. using (var batch = store.Advanced.NewBatch(new TimeSeriesBatchOptions { }))
  207. {
  208. for (int i = 0; i < 18888; i++)
  209. {
  210. batch.ScheduleAppend("Simple", "Is", start.AddSeconds(i + 1), 3D);
  211. batch.ScheduleAppend("Simple", "Money", start.AddSeconds(i + 18), 13D);
  212. batch.ScheduleAppend("FourValues", "Time", start.AddSeconds(i + 13), 3D, 4D, 5D, 6D);
  213. }
  214. await batch.FlushAsync();
  215. }
  216. var types = await store.Advanced.GetTypes();
  217. Assert.Equal(2, types.Length);
  218. var fourValues = types[0];
  219. Assert.Equal("FourValues", fourValues.Type);
  220. Assert.Equal(4, fourValues.Fields.Length);
  221. Assert.Equal(1, fourValues.KeysCount);
  222. var keys = await store.Advanced.GetKeys(fourValues.Type);
  223. Assert.Equal(1, keys.Length); var time = keys[0];
  224. Assert.Equal("FourValues", time.Type.Type);
  225. Assert.Equal("Time", time.Key);
  226. Assert.Equal(18888, time.PointsCount);
  227. var simple = types[1];
  228. Assert.Equal("Simple", simple.Type);
  229. Assert.Equal(1, simple.Fields.Length);
  230. Assert.Equal(2, simple.KeysCount);
  231. keys = await store.Advanced.GetKeys(simple.Type);
  232. Assert.Equal(2, keys.Length);
  233. var _is = keys[0];
  234. Assert.Equal("Simple", _is.Type.Type);
  235. Assert.Equal("Is", _is.Key);
  236. Assert.Equal(18888, _is.PointsCount);
  237. var money = keys[1];
  238. Assert.Equal("Simple", money.Type.Type);
  239. Assert.Equal("Money", money.Key);
  240. Assert.Equal(18888, money.PointsCount);
  241. var stats = await store.GetStatsAsync();
  242. Assert.Equal(2, stats.TypesCount);
  243. Assert.Equal(3, stats.KeysCount);
  244. Assert.Equal(18888 * 3, stats.PointsCount);
  245. WaitForUserToContinueTheTest(startPage: "/studio/index.html#timeseries/series?type=-Simple&key=Money&timeseries=SeriesName-1");
  246. }
  247. }
  248. [Fact]
  249. public async Task GetKeys()
  250. {
  251. var start = new DateTimeOffset(2015, 10, 7, 15, 0, 0, TimeSpan.Zero);
  252. using (var store = NewRemoteTimeSeriesStore())
  253. {
  254. await store.CreateTypeAsync("Simple", new[] { "Value" });
  255. await store.CreateTypeAsync("FourValues", new[] { "Value 1", "Value Two", "Value Three", "Value 4" });
  256. await store.AppendAsync("FourValues", "Time", start, new[] { 3D, 4D, 5D, 6D });
  257. await store.AppendAsync("Simple", "Is", start, 3D);
  258. await store.AppendAsync("Simple", "Money", start, 3D);
  259. var cancellationToken = new CancellationToken();
  260. await store.AppendAsync("Simple", "Is", start.AddHours(1), 3456D, cancellationToken);
  261. await store.AppendAsync("FourValues", "Time", start.AddHours(1), new[] { 23D, 4D, 5D, 6D }, cancellationToken);
  262. await store.AppendAsync("FourValues", "Time", start.AddHours(2), cancellationToken, 33D, 4D, 5D, 6D);
  263. await store.AppendAsync("FourValues", "Time", start.AddHours(3), cancellationToken, 33D, 4D, 5D, 6D);
  264. var types = await store.Advanced.GetTypes(cancellationToken);
  265. Assert.Equal(2, types.Length);
  266. var fourValues = types[0];
  267. Assert.Equal("FourValues", fourValues.Type);
  268. Assert.Equal(4, fourValues.Fields.Length);
  269. Assert.Equal(1, fourValues.KeysCount);
  270. var keys = await store.Advanced.GetKeys(fourValues.Type);
  271. Assert.Equal(1, keys.Length);
  272. var time = keys[0];
  273. Assert.Equal("FourValues", time.Type.Type);
  274. Assert.Equal("Time", time.Key);
  275. Assert.Equal(4, time.PointsCount);
  276. var points = await store.Advanced.GetPoints(fourValues.Type, time.Key);
  277. Assert.Equal(points.Length, time.PointsCount);
  278. var aggregatedPoints = await store.Advanced.GetAggregatedPoints(fourValues.Type, time.Key, AggregationDuration.Hours(2), start.AddHours(-1), start.AddHours(3));
  279. Assert.Equal(aggregatedPoints.Length, 2);
  280. Assert.Equal(aggregatedPoints[0].Values[0].Volume, 1);
  281. Assert.Equal(aggregatedPoints[1].Values[0].Volume, 2);
  282. var simple = types[1];
  283. Assert.Equal("Simple", simple.Type);
  284. Assert.Equal(1, simple.Fields.Length);
  285. Assert.Equal(2, simple.KeysCount);
  286. keys = await store.Advanced.GetKeys(simple.Type);
  287. Assert.Equal(2, keys.Length);
  288. var _is = keys[0];
  289. Assert.Equal("Simple", _is.Type.Type);
  290. Assert.Equal("Is", _is.Key);
  291. Assert.Equal(2, _is.PointsCount);
  292. var money = keys[1];
  293. Assert.Equal("Simple", money.Type.Type);
  294. Assert.Equal("Money", money.Key);
  295. Assert.Equal(1, money.PointsCount);
  296. points = await store.Advanced.GetPoints(fourValues.Type, money.Key, start: DateTimeOffset.MinValue, end: DateTimeOffset.MaxValue, skip: 1, take: int.MaxValue, token: cancellationToken);
  297. Assert.Equal(points.Length, money.PointsCount - 1);
  298. var stats = await store.GetStatsAsync(cancellationToken);
  299. Assert.Equal(2, stats.TypesCount);
  300. Assert.Equal(3, stats.KeysCount);
  301. Assert.Equal(7, stats.PointsCount);
  302. }
  303. }
  304. [Fact]
  305. public async Task CanOverrideExistingPoints()
  306. {
  307. var start = DateTimeOffset.Now;
  308. using (var store = NewRemoteTimeSeriesStore())
  309. {
  310. await store.CreateTypeAsync("Simple", new[] { "Value" });
  311. await store.CreateTypeAsync("FourValues", new[] { "Value 1", "Value Two", "Value Three", "Value 4" });
  312. await store.AppendAsync("FourValues", "Time", start, new[] { 3D, 4D, 5D, 6D });
  313. await store.AppendAsync("Simple", "Is", start, 3D);
  314. await store.AppendAsync("Simple", "Money", start, 3D);
  315. using (var batch = store.Advanced.NewBatch(new TimeSeriesBatchOptions { }))
  316. {
  317. for (int i = 0; i < 1888; i++)
  318. {
  319. await store.AppendAsync("FourValues", "Time", start, new[] { 3D, 4D, 5D, 6D });
  320. await store.AppendAsync("Simple", "Is", start, 3D);
  321. await store.AppendAsync("Simple", "Money", start, 3D);
  322. batch.ScheduleAppend("Simple", "Is", start, 3D);
  323. batch.ScheduleAppend("Simple", "Money", start, 13D);
  324. batch.ScheduleAppend("FourValues", "Time", start, 3D, 4D, 5D, 6D);
  325. }
  326. await batch.FlushAsync();
  327. }
  328. var cancellationToken = new CancellationToken();
  329. await store.AppendAsync("Simple", "Is", start.AddYears(10), 3456D, cancellationToken);
  330. await store.AppendAsync("FourValues", "Time", start.AddYears(1), new[] { 23D, 4D, 5D, 6D }, cancellationToken);
  331. await store.AppendAsync("FourValues", "Time", start.AddYears(2), cancellationToken, 33D, 4D, 5D, 6D);
  332. await store.AppendAsync("FourValues", "Time", start.AddYears(3), cancellationToken, 33D, 4D, 5D, 6D);
  333. var types = await store.Advanced.GetTypes(cancellationToken);
  334. Assert.Equal(2, types.Length);
  335. var fourValues = types[0];
  336. Assert.Equal("FourValues", fourValues.Type);
  337. Assert.Equal(4, fourValues.Fields.Length);
  338. Assert.Equal(1, fourValues.KeysCount);
  339. var keys = await store.Advanced.GetKeys(fourValues.Type);
  340. Assert.Equal(1, keys.Length); var time = keys[0];
  341. Assert.Equal("FourValues", time.Type.Type);
  342. Assert.Equal("Time", time.Key);
  343. Assert.Equal(4, time.PointsCount);
  344. var simple = types[1];
  345. Assert.Equal("Simple", simple.Type);
  346. Assert.Equal(1, simple.Fields.Length);
  347. Assert.Equal(2, simple.KeysCount);
  348. keys = await store.Advanced.GetKeys(simple.Type, cancellationToken);
  349. Assert.Equal(2, keys.Length);
  350. var _is = keys[0];
  351. Assert.Equal("Simple", _is.Type.Type);
  352. Assert.Equal("Is", _is.Key);
  353. Assert.Equal(2, _is.PointsCount);
  354. var money = keys[1];
  355. Assert.Equal("Simple", money.Type.Type);
  356. Assert.Equal("Money", money.Key);
  357. Assert.Equal(1, money.PointsCount);
  358. var stats = await store.GetStatsAsync(cancellationToken);
  359. Assert.Equal(2, stats.TypesCount);
  360. Assert.Equal(3, stats.KeysCount);
  361. Assert.Equal(7, stats.PointsCount);
  362. }
  363. }
  364. }
  365. }