Beispiel #1
0
        public async Task ValidateCorrectRetentionAndGet(int minutesOffset)
        {
            using (var store = GetDocumentStore())
            {
                var raw = new RawTimeSeriesPolicy(TimeSpan.FromHours(24));

                var p1 = new TimeSeriesPolicy("By6Hours", TimeSpan.FromHours(6), raw.RetentionTime * 4);
                var p2 = new TimeSeriesPolicy("By1Day", TimeSpan.FromDays(1), raw.RetentionTime * 5);
                var p3 = new TimeSeriesPolicy("By30Minutes", TimeSpan.FromMinutes(30), raw.RetentionTime * 2);
                var p4 = new TimeSeriesPolicy("By1Hour", TimeSpan.FromMinutes(60), raw.RetentionTime * 3);

                var config = new TimeSeriesConfiguration
                {
                    Collections = new Dictionary <string, TimeSeriesCollectionConfiguration>
                    {
                        ["Users"] = new TimeSeriesCollectionConfiguration
                        {
                            RawPolicy = raw,
                            Policies  = new List <TimeSeriesPolicy>
                            {
                                p1, p2, p3, p4
                            }
                        },
                    },
                    PolicyCheckFrequency = TimeSpan.FromSeconds(1)
                };
                await store.Maintenance.SendAsync(new ConfigureTimeSeriesOperation(config));

                var database = await GetDocumentDatabaseInstanceFor(store);

                var now = new DateTime(2020, 4, 2).AddMinutes(minutesOffset);
                database.Time.UtcDateTime = () => now.AddMilliseconds(1);

                var baseline = now.AddDays(-12);
                var total    = TimeSpan.FromDays(12).TotalMinutes;

                using (var session = store.OpenSession())
                {
                    session.Store(new Core.Utils.Entities.User {
                        Name = "Karmel"
                    }, "users/karmel");
                    for (int i = 0; i <= total; i++)
                    {
                        session.TimeSeriesFor("users/karmel", "Heartrate")
                        .Append(baseline.AddMinutes(i), i, "watches/fitbit");
                    }
                    session.SaveChanges();
                }

                await database.TimeSeriesPolicyRunner.RunRollups();

                await database.TimeSeriesPolicyRunner.DoRetention();

                await QueryFromMultipleTimeSeries.VerifyFullPolicyExecution(store, config.Collections["Users"]);
            }
        }
        public async Task CanWorkWithRollupTimeSeries2()
        {
            using (var store = GetDocumentStore())
            {
                var raw = new RawTimeSeriesPolicy(TimeSpan.FromHours(24));

                var p1 = new TimeSeriesPolicy("By6Hours", TimeSpan.FromHours(6), raw.RetentionTime * 4);
                var p2 = new TimeSeriesPolicy("By1Day", TimeSpan.FromDays(1), raw.RetentionTime * 5);
                var p3 = new TimeSeriesPolicy("By30Minutes", TimeSpan.FromMinutes(30), raw.RetentionTime * 2);
                var p4 = new TimeSeriesPolicy("By1Hour", TimeSpan.FromMinutes(60), raw.RetentionTime * 3);

                var config = new TimeSeriesConfiguration
                {
                    Collections = new Dictionary <string, TimeSeriesCollectionConfiguration>
                    {
                        ["Users"] = new TimeSeriesCollectionConfiguration
                        {
                            RawPolicy = raw,
                            Policies  = new List <TimeSeriesPolicy>
                            {
                                p1, p2, p3, p4
                            }
                        },
                    },
                    PolicyCheckFrequency = TimeSpan.FromSeconds(1)
                };
                await store.Maintenance.SendAsync(new ConfigureTimeSeriesOperation(config));

                await store.TimeSeries.RegisterAsync <User, StockPrice>();

                var database = await GetDocumentDatabaseInstanceFor(store);

                var now      = DateTime.UtcNow;
                var baseline = now.AddDays(-12);
                var total    = TimeSpan.FromDays(12).TotalMinutes;

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "Karmel"
                    }, "users/karmel");
                    var ts    = session.TimeSeriesFor <StockPrice>("users/karmel");
                    var entry = new StockPrice();
                    for (int i = 0; i <= total; i++)
                    {
                        entry.Open   = i;
                        entry.Close  = i + 100_000;
                        entry.High   = i + 200_000;
                        entry.Low    = i + 300_000;
                        entry.Volume = i + 400_000;
                        ts.Append(baseline.AddMinutes(i), entry, "watches/fitbit");
                    }
                    session.SaveChanges();
                }

                await database.TimeSeriesPolicyRunner.RunRollups();

                await database.TimeSeriesPolicyRunner.DoRetention();

                await QueryFromMultipleTimeSeries.VerifyFullPolicyExecution(store, config.Collections["Users"], rawName: "StockPrices");

                using (var session = store.OpenSession())
                {
                    var query = session.Query <User>()
                                .Select(u =>
                                        RavenQuery.TimeSeries <StockPrice>(u, "StockPrices").Select(x =>
                                                                                                    new {
                        First = x.First(),
                        Last  = x.Last(),
                        Min   = x.Min(),
                        Max   = x.Max(),
                        Sum   = x.Sum(),
                        Count = x.Count(),
                        Avg   = x.Average()
                    })
                                        .ToList());

                    var result = query.Single();

                    Assert.Equal(1, result.Results.Length);
                    var r = result.Results[0];
                    Assert.NotNull(r.First);
                    Assert.NotNull(r.Last);
                    Assert.NotNull(r.Min);
                    Assert.NotNull(r.Max);
                    Assert.NotNull(r.Sum);
                    Assert.NotNull(r.Count);
                    Assert.NotNull(r.Average);
                }

                using (var session = store.OpenSession())
                {
                    var query = session.Query <User>()
                                .Select(u =>
                                        RavenQuery.TimeSeries <StockPrice>(u, "StockPrices")
                                        .GroupBy(x => x.Hours(3))
                                        .Select(x =>
                                                new
                    {
                        First = x.First(),
                        Last  = x.Last(),
                        Min   = x.Min(),
                        Max   = x.Max(),
                        Sum   = x.Sum(),
                        Count = x.Count(),
                        Avg   = x.Average()
                    })
                                        .ToList());

                    var result = query.Single();
                    var r      = result.Results[0];
                    Assert.NotNull(r.First);
                    Assert.NotNull(r.Last);
                    Assert.NotNull(r.Min);
                    Assert.NotNull(r.Max);
                    Assert.NotNull(r.Sum);
                    Assert.NotNull(r.Count);
                    Assert.NotNull(r.Average);
                }

                using (var session = store.OpenSession())
                {
                    var ts1 = session.TimeSeriesRollupFor <StockPrice>("users/karmel", p1.Name);
                    var r   = ts1.Get().First();
                    Assert.NotNull(r.First);
                    Assert.NotNull(r.Last);
                    Assert.NotNull(r.Min);
                    Assert.NotNull(r.Max);
                    Assert.NotNull(r.Sum);
                    Assert.NotNull(r.Count);
                    Assert.NotNull(r.Average);
                }
            }
        }
        public async Task CanWorkWithRollupTimeSeries()
        {
            using (var store = GetDocumentStore())
            {
                var raw = new RawTimeSeriesPolicy(TimeSpan.FromHours(24));

                var p1 = new TimeSeriesPolicy("By6Hours", TimeSpan.FromHours(6), raw.RetentionTime * 4);
                var p2 = new TimeSeriesPolicy("By1Day", TimeSpan.FromDays(1), raw.RetentionTime * 5);
                var p3 = new TimeSeriesPolicy("By30Minutes", TimeSpan.FromMinutes(30), raw.RetentionTime * 2);
                var p4 = new TimeSeriesPolicy("By1Hour", TimeSpan.FromMinutes(60), raw.RetentionTime * 3);

                var config = new TimeSeriesConfiguration
                {
                    Collections = new Dictionary <string, TimeSeriesCollectionConfiguration>
                    {
                        ["Users"] = new TimeSeriesCollectionConfiguration
                        {
                            RawPolicy = raw,
                            Policies  = new List <TimeSeriesPolicy>
                            {
                                p1, p2, p3, p4
                            }
                        },
                    },
                    PolicyCheckFrequency = TimeSpan.FromSeconds(1)
                };
                await store.Maintenance.SendAsync(new ConfigureTimeSeriesOperation(config));

                await store.TimeSeries.RegisterAsync <User, StockPrice>();

                var database = await GetDocumentDatabaseInstanceFor(store);

                var now        = DateTime.UtcNow;
                var nowMinutes = now.Minute;
                now = now.AddMinutes(-nowMinutes);
                database.Time.UtcDateTime = () => DateTime.UtcNow.AddMinutes(-nowMinutes);

                var baseline = now.AddDays(-12);
                var total    = TimeSpan.FromDays(12).TotalMinutes;

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "Karmel"
                    }, "users/karmel");
                    var ts    = session.TimeSeriesFor <StockPrice>("users/karmel");
                    var entry = new StockPrice();
                    for (int i = 0; i <= total; i++)
                    {
                        entry.Open   = i;
                        entry.Close  = i + 100_000;
                        entry.High   = i + 200_000;
                        entry.Low    = i + 300_000;
                        entry.Volume = i + 400_000;
                        ts.Append(baseline.AddMinutes(i), entry, "watches/fitbit");
                    }
                    session.SaveChanges();
                }

                await database.TimeSeriesPolicyRunner.RunRollups();

                await database.TimeSeriesPolicyRunner.DoRetention();

                await QueryFromMultipleTimeSeries.VerifyFullPolicyExecution(store, config.Collections["Users"], rawName: "StockPrices");

                using (var session = store.OpenSession())
                {
                    var query = session.Advanced.RawQuery <TimeSeriesRawResult <StockPrice> >(@"
declare timeseries out()
{
    from StockPrices
    between $start and $end
}
from Users as u
select out()
")
                                .AddParameter("start", baseline.AddDays(-1))
                                .AddParameter("end", now.AddDays(1));

                    var result = query.Single();
                    var count  = result.Results.Length;

                    Assert.Equal(5, result.Results[count - 1440].Values.Length);

                    foreach (var res in result.Results)
                    {
                        Assert.Equal(5, res.Values.Length);
                    }
                }

                using (var session = store.OpenSession())
                {
                    // test the same query using linq

                    var query = session.Query <User>()
                                .Select(u =>
                                        RavenQuery.TimeSeries <StockPrice>(u, "StockPrices", baseline.AddDays(-1), now.AddDays(1))
                                        .ToList());

                    var result = query.Single();
                    var count  = result.Results.Length;

                    Assert.Equal(5, result.Results[count - 1440].Values.Length);

                    foreach (var res in result.Results)
                    {
                        Assert.Equal(5, res.Values.Length);
                    }
                }

                now = DateTime.UtcNow;
                using (var session = store.OpenSession())
                {
                    var ts = session.TimeSeriesRollupFor <StockPrice>("users/karmel", p1.Name);
                    var a  = new TimeSeriesRollupEntry <StockPrice>(DateTime.Now)
                    {
                        Max = new StockPrice
                        {
                            Close = 1
                        }
                    };
                    ts.Append(a);
                    session.SaveChanges();
                }

                using (var session = store.OpenSession())
                {
                    var ts  = session.TimeSeriesRollupFor <StockPrice>("users/karmel", p1.Name);
                    var res = ts.Get(from: now.AddMilliseconds(-1)).ToList();
                    Assert.Equal(1, res.Count);
                    Assert.Equal(1, res[0].Max.Close);
                }
            }
        }