コード例 #1
0
ファイル: Query.cs プロジェクト: radtek/ravendb
        public TimeValue ToTimeValue()
        {
            if (Ticks != 0)
            {
                return(TimeValue.FromSeconds((int)(Ticks / 10_000_000)));
            }

            return(TimeValue.FromMonths(Months));
        }
コード例 #2
0
        private void GetTimeSeriesQueryString(DocumentDatabase database, DocumentsOperationContext context, out IncludeTimeSeriesCommand includeTimeSeries)
        {
            includeTimeSeries = null;

            var timeSeriesNames      = GetStringValuesQueryString("timeseries", required: false);
            var timeSeriesTimeNames  = GetStringValuesQueryString("timeseriestime", required: false);
            var timeSeriesCountNames = GetStringValuesQueryString("timeseriescount", required: false);

            if (timeSeriesNames.Count == 0 && timeSeriesTimeNames.Count == 0 && timeSeriesCountNames.Count == 0)
            {
                return;
            }

            if (timeSeriesNames.Count > 1 && timeSeriesNames.Contains(Constants.TimeSeries.All))
            {
                throw new InvalidOperationException($"Cannot have more than one include on '{Constants.TimeSeries.All}'.");
            }
            if (timeSeriesTimeNames.Count > 1 && timeSeriesTimeNames.Contains(Constants.TimeSeries.All))
            {
                throw new InvalidOperationException($"Cannot have more than one include on '{Constants.TimeSeries.All}'.");
            }
            if (timeSeriesCountNames.Count > 1 && timeSeriesCountNames.Contains(Constants.TimeSeries.All))
            {
                throw new InvalidOperationException($"Cannot have more than one include on '{Constants.TimeSeries.All}'.");
            }

            var fromList = GetStringValuesQueryString("from", required: false);
            var toList   = GetStringValuesQueryString("to", required: false);

            if (timeSeriesNames.Count != fromList.Count || fromList.Count != toList.Count)
            {
                throw new InvalidOperationException("Parameters 'timeseriesNames', 'fromList' and 'toList' must be of equal length. " +
                                                    $"Got : timeseriesNames.Count = {timeSeriesNames.Count}, fromList.Count = {fromList.Count}, toList.Count = {toList.Count}.");
            }

            var timeTypeList  = GetStringValuesQueryString("timeType", required: false);
            var timeValueList = GetStringValuesQueryString("timeValue", required: false);
            var timeUnitList  = GetStringValuesQueryString("timeUnit", required: false);

            if (timeSeriesTimeNames.Count != timeTypeList.Count || timeTypeList.Count != timeValueList.Count || timeValueList.Count != timeUnitList.Count)
            {
                throw new InvalidOperationException($"Parameters '{nameof(timeSeriesTimeNames)}', '{nameof(timeTypeList)}', '{nameof(timeValueList)}' and '{nameof(timeUnitList)}' must be of equal length. " +
                                                    $"Got : {nameof(timeSeriesTimeNames)}.Count = {timeSeriesTimeNames.Count}, {nameof(timeTypeList)}.Count = {timeTypeList.Count}, {nameof(timeValueList)}.Count = {timeValueList.Count}, {nameof(timeUnitList)}.Count = {timeUnitList.Count}.");
            }

            var countTypeList  = GetStringValuesQueryString("countType", required: false);
            var countValueList = GetStringValuesQueryString("countValue", required: false);

            if (timeSeriesCountNames.Count != countTypeList.Count || countTypeList.Count != countValueList.Count)
            {
                throw new InvalidOperationException($"Parameters '{nameof(timeSeriesCountNames)}', '{nameof(countTypeList)}', '{nameof(countValueList)}' must be of equal length. " +
                                                    $"Got : {nameof(timeSeriesCountNames)}.Count = {timeSeriesCountNames.Count}, {nameof(countTypeList)}.Count = {countTypeList.Count}, {nameof(countValueList)}.Count = {countValueList.Count}.");
            }

            var hs = new HashSet <AbstractTimeSeriesRange>(AbstractTimeSeriesRangeComparer.Instance);

            for (int i = 0; i < timeSeriesNames.Count; i++)
            {
                hs.Add(new TimeSeriesRange
                {
                    Name = timeSeriesNames[i],
                    From = string.IsNullOrEmpty(fromList[i])
                        ? DateTime.MinValue
                        : TimeSeriesHandler.ParseDate(fromList[i], "from"),
                    To = string.IsNullOrEmpty(toList[i])
                        ? DateTime.MaxValue
                        : TimeSeriesHandler.ParseDate(toList[i], "to")
                });
            }

            for (int i = 0; i < timeSeriesTimeNames.Count; i++)
            {
                var timeValueUnit = (TimeValueUnit)Enum.Parse(typeof(TimeValueUnit), timeUnitList[i]);
                if (timeValueUnit == TimeValueUnit.None)
                {
                    throw new InvalidOperationException($"Got unexpected {nameof(TimeValueUnit)} '{nameof(TimeValueUnit.None)}'. Only the following are supported: '{nameof(TimeValueUnit.Second)}' or '{nameof(TimeValueUnit.Month)}'.");
                }

                if (int.TryParse(timeValueList[i], out int res) == false)
                {
                    throw new InvalidOperationException($"Could not parse timeseries time range value.");
                }

                hs.Add(new TimeSeriesTimeRange
                {
                    Name = timeSeriesTimeNames[i],
                    Type = (TimeSeriesRangeType)Enum.Parse(typeof(TimeSeriesRangeType), timeTypeList[i]),
                    Time = timeValueUnit == TimeValueUnit.Second ? TimeValue.FromSeconds(res) : TimeValue.FromMonths(res)
                });
            }

            for (int i = 0; i < timeSeriesCountNames.Count; i++)
            {
                if (int.TryParse(countValueList[i], out int res) == false)
                {
                    throw new InvalidOperationException($"Could not parse timeseries count value.");
                }

                hs.Add(new TimeSeriesCountRange
                {
                    Name  = timeSeriesCountNames[i],
                    Type  = (TimeSeriesRangeType)Enum.Parse(typeof(TimeSeriesRangeType), countTypeList[i]),
                    Count = res
                });
            }

            includeTimeSeries = new IncludeTimeSeriesCommand(context, new Dictionary <string, HashSet <AbstractTimeSeriesRange> > {
                { string.Empty, hs }
            });
        }
コード例 #3
0
        public void CanCreateSubscriptionWithIncludeTimeSeries_LastRangeByTime()
        {
            var now = DateTime.UtcNow.EnsureMilliseconds();

            using (var store = GetDocumentStore())
            {
                var name = store.Subscriptions
                           .Create(new SubscriptionCreationOptions <Company>()
                {
                    Includes = builder => builder
                               .IncludeTimeSeries("StockPrice", TimeSeriesRangeType.Last, TimeValue.FromMonths(1))
                });

                var mre    = new ManualResetEventSlim();
                var worker = store.Subscriptions.GetSubscriptionWorker <Company>(name);
                worker.Run(batch =>
                {
                    using (var session = batch.OpenSession())
                    {
                        Assert.Equal(0, session.Advanced.NumberOfRequests);

                        var company = session.Load <Company>("companies/1");
                        Assert.Equal(0, session.Advanced.NumberOfRequests);

                        var timeSeries        = session.TimeSeriesFor(company, "StockPrice");
                        var timeSeriesEntries = timeSeries.Get(from: now.AddDays(-7));

                        Assert.Equal(1, timeSeriesEntries.Length);
                        Assert.Equal(now, timeSeriesEntries[0].Timestamp);
                        Assert.Equal(10, timeSeriesEntries[0].Value);

                        Assert.Equal(0, session.Advanced.NumberOfRequests);
                    }

                    mre.Set();
                });

                using (var session = store.OpenSession())
                {
                    var company = new Company {
                        Id = "companies/1", Name = "HR"
                    };
                    session.Store(company);

                    session.TimeSeriesFor(company, "StockPrice").Append(now, 10);

                    session.SaveChanges();
                }

                Assert.True(mre.Wait(TimeSpan.FromSeconds(30)));
            }
        }
コード例 #4
0
ファイル: Query.cs プロジェクト: radtek/ravendb
        public static TimeValue ParseTimePeriodFromString(string source)
        {
            TimeValue result;
            var       offset = 0;

            var duration = ParseNumber(source, ref offset);

            if (offset >= source.Length)
            {
                throw new ArgumentException("Unable to find range specification in: " + source);
            }

            while (char.IsWhiteSpace(source[offset]) && offset < source.Length)
            {
                offset++;
            }

            if (offset >= source.Length)
            {
                throw new ArgumentException("Unable to find range specification in: " + source);
            }

            switch (char.ToLower(source[offset++]))
            {
            case 's':
                if (TryConsumeMatch(source, ref offset, "seconds") == false)
                {
                    TryConsumeMatch(source, ref offset, "second");
                }

                result = TimeValue.FromSeconds((int)duration);
                break;

            case 'm':
                if (TryConsumeMatch(source, ref offset, "minutes") ||
                    TryConsumeMatch(source, ref offset, "minute") ||
                    TryConsumeMatch(source, ref offset, "min"))
                {
                    result = TimeValue.FromMinutes((int)duration);
                    break;
                }

                if (TryConsumeMatch(source, ref offset, "ms") ||
                    TryConsumeMatch(source, ref offset, "milliseconds") ||
                    TryConsumeMatch(source, ref offset, "milli"))
                {
                    // TODO use TimeValue.FromMilliseconds when RavenDB-14988 is fixed
                    throw new NotSupportedException("Unsupported time period. Using milliseconds in Last/First is not supported : " + source);
                }

                if (TryConsumeMatch(source, ref offset, "months") ||
                    TryConsumeMatch(source, ref offset, "month") ||
                    TryConsumeMatch(source, ref offset, "mon") ||
                    TryConsumeMatch(source, ref offset, "mo"))
                {
                    result = TimeValue.FromMonths((int)duration);
                    break;
                }
                goto default;

            case 'h':
                if (TryConsumeMatch(source, ref offset, "hours") == false)
                {
                    TryConsumeMatch(source, ref offset, "hour");
                }

                result = TimeValue.FromHours((int)duration);
                break;

            case 'd':
                if (TryConsumeMatch(source, ref offset, "days") == false)
                {
                    TryConsumeMatch(source, ref offset, "day");
                }
                result = TimeValue.FromDays((int)duration);
                break;

            case 'q':
                if (TryConsumeMatch(source, ref offset, "quarters") == false)
                {
                    TryConsumeMatch(source, ref offset, "quarter");
                }

                duration *= 3;
                AssertValidDurationInMonths(duration);
                result = TimeValue.FromMonths((int)duration);
                break;

            case 'y':
                if (TryConsumeMatch(source, ref offset, "years") == false)
                {
                    TryConsumeMatch(source, ref offset, "year");
                }
                duration *= 12;
                AssertValidDurationInMonths(duration);
                result = TimeValue.FromMonths((int)duration);
                break;

            default:
                throw new ArgumentException($"Unable to understand time range: '{source}'");
            }

            while (offset < source.Length && char.IsWhiteSpace(source[offset]))
            {
                offset++;
            }

            if (offset != source.Length)
            {
                throw new ArgumentException("After range specification, found additional unknown data: " + source);
            }

            return(result);
        }