private string GetDateValue(Expression exp)
        {
            if (exp is ConstantExpression constant)
            {
                if (constant.Value is DateTime dt)
                {
                    return(dt.EnsureUtc().GetDefaultRavenFormat());
                }

                return(constant.Value.ToString());
            }

            if (exp is ParameterExpression p)
            {
                Parameters ??= new List <string>();
                Parameters.Add(p.Name);
                return(p.Name);
            }

            LinqPathProvider.GetValueFromExpressionWithoutConversion(exp, out var value);

            if (value is string s)
            {
                return(s);
            }

            if (!(value is DateTime d))
            {
                throw new InvalidOperationException("Invalid from/to arguments" + exp);
            }

            return(d.EnsureUtc().GetDefaultRavenFormat());
        }
        private void AddSelectMethod(MethodCallExpression call)
        {
            if (call.Arguments.Count == 0)
            {
                AddSelectField(call.Method.Name);
                return;
            }

            Debug.Assert(call.Arguments.Count == 1 && call.Method.Name.Equals(nameof(AggregationType.Percentile)),
                         "wrong number of arguments passed to method " + call.Method.Name);

            object value;

            if (call.Arguments[0] is ConstantExpression constant)
            {
                value = constant.Value;
            }
            else
            {
                LinqPathProvider.GetValueFromExpressionWithoutConversion(call.Arguments[0], out value);
            }

            if (!(value is double d))
            {
                throw new InvalidOperationException("Invalid Select argument " + call.Arguments[0]);
            }

            AddSelectField(call.Method.Name, d);
        }
        private static string GroupByWith(ref MethodCallExpression callExpression)
        {
            if (callExpression.Method.Name != nameof(ITimeSeriesAggregationOperations.WithOptions))
            {
                return(null);
            }

            if (callExpression.Method.DeclaringType != typeof(ITimeSeriesAggregationOperations))
            {
                ThrowInvalidMethodArgument(callExpression, nameof(ITimeSeriesQueryable.GroupBy));
                return(null);
            }

            if (!(callExpression.Object is MethodCallExpression inner))
            {
                ThrowInvalidMethodArgument(callExpression, nameof(ITimeSeriesQueryable.GroupBy));
                return(null);
            }

            LinqPathProvider.GetValueFromExpressionWithoutConversion(callExpression.Arguments[0], out var value);
            if (!(value is TimeSeriesAggregationOptions options))
            {
                ThrowInvalidMethodArgument(callExpression.Arguments[0], nameof(ITimeSeriesAggregationOperations.WithOptions));
                return(null);
            }

            callExpression = inner;

            if (options.Interpolation == InterpolationType.None)
            {
                return(null);
            }

            return($"with interpolation({options.Interpolation})");
        }
Exemple #4
0
        private ParameterExpression AddAsQueryParameter(Expression node)
        {
            LinqPathProvider.GetValueFromExpressionWithoutConversion(node, out var value);
            var queryParameter = _documentQuery.ProjectionParameter(value);

            return(Expression.Parameter(node.Type, queryParameter));
        }
        private void Scale(Expression expression)
        {
            if (expression.Type != typeof(double))
            {
                throw new InvalidOperationException($"Invalid argument in method '{nameof(ITimeSeriesQueryable.Scale)}'. " +
                                                    $"Expected argument of type 'double' but got : '{expression.Type}'");
            }

            LinqPathProvider.GetValueFromExpressionWithoutConversion(expression, out var value);

            _scale = $" scale {value}";
        }
        private void Offset(Expression expression)
        {
            if (expression.Type != typeof(TimeSpan))
            {
                throw new InvalidOperationException($"Invalid argument in method '{nameof(ITimeSeriesAggregationQueryable.Offset)}'. " +
                                                    $"Expected argument of type 'TimeSpan' but got : '{expression.Type}'");
            }

            LinqPathProvider.GetValueFromExpressionWithoutConversion(expression, out var ts);

            _offset = $" offset '{ts}'";
        }
 private static string TryGetValueFromArgument(Expression argument, bool groupByArgument)
 {
     try
     {
         if (LinqPathProvider.GetValueFromExpressionWithoutConversion(argument, out object value) == false)
         {
             throw new NotSupportedException("Unsupported node type: " + argument.NodeType);
         }
         return(value.ToString());
     }
     catch (Exception e)
     {
         ThrowFailedToEvaluateArgument(argument, groupByArgument, e);
         return(null);
     }
 }
Exemple #8
0
        private void GroupBy(Expression expression)
        {
            string timePeriod;
            string with       = null;
            string groupByTag = null;

            switch (expression)
            {
            case ConstantExpression constantExpression:
                if (constantExpression.Value is Action <ITimePeriodBuilder> action)
                {
                    timePeriod = GetGroupByArgsFromAction(action, out with, out groupByTag);
                    break;
                }
                timePeriod = constantExpression.Value.ToString();
                break;

            case LambdaExpression lambda:
            {
                if (!(lambda.Body is MethodCallExpression methodCall) || lambda.Type != typeof(Action <ITimePeriodBuilder>))
                {
                    ThrowInvalidMethodArgument(lambda, nameof(ITimeSeriesQueryable.GroupBy));
                    return;
                }

                var typedLambda    = Expression.Lambda <Action <ITimePeriodBuilder> >(methodCall, lambda.TailCall, lambda.Parameters);
                var compiledAction = typedLambda.Compile();
                timePeriod = GetGroupByArgsFromAction(compiledAction, out with, out groupByTag);
                break;
            }

            default:
                if (LinqPathProvider.GetValueFromExpressionWithoutConversion(expression, out object value) == false)
                {
                    ThrowFailedToEvaluateArgument(expression, groupBy: true, new NotSupportedException("Unsupported node type: " + expression.NodeType));
                }

                GroupBy(Expression.Constant(value));
                return;
            }

            _groupBy = $" group by '{timePeriod}' {groupByTag} {with}";
        }
Exemple #9
0
        private string GetNameFromArgument(Expression argument)
        {
            if (argument is ConstantExpression constantExpression)
            {
                return(constantExpression.Value.ToString());
            }

            if (JavascriptConversionExtensions.IsWrappedConstantExpression(argument))
            {
                LinqPathProvider.GetValueFromExpressionWithoutConversion(argument, out var value);
                return(value.ToString());
            }

            if (argument is ParameterExpression p)
            {
                Parameters ??= new List <string>();
                Parameters.Add(p.Name);
                return(p.Name);
            }

            throw new InvalidOperationException("Invalid TimeSeries argument " + argument);
        }
Exemple #10
0
            public override void ConvertToJavascript(JavascriptConversionContext context)
            {
                var methodCallExpression = context.Node as MethodCallExpression;

                var nameAttribute = methodCallExpression?
                                    .Method
                                    .GetCustomAttributes(typeof(JavascriptMethodNameAttribute), false)
                                    .OfType <JavascriptMethodNameAttribute>()
                                    .FirstOrDefault();

                if (nameAttribute == null)
                {
                    return;
                }
                context.PreventDefault();

                var javascriptWriter = context.GetWriter();

                javascriptWriter.Write(".");
                javascriptWriter.Write(nameAttribute.Name);
                javascriptWriter.Write("(");

                var args = new List <Expression>();

                foreach (var expr in methodCallExpression.Arguments)
                {
                    var expression = expr as NewArrayExpression;
                    if (expression != null)
                    {
                        args.AddRange(expression.Expressions);
                    }
                    else
                    {
                        args.Add(expr);
                    }
                }

                for (var i = 0; i < args.Count; i++)
                {
                    var name = $"arg_{Parameters.Count}_{Suffix}";
                    if (i != 0)
                    {
                        javascriptWriter.Write(", ");
                    }
                    javascriptWriter.Write("args.");
                    javascriptWriter.Write(name);
                    object val;
                    if (LinqPathProvider.GetValueFromExpressionWithoutConversion(args[i], out val))
                    {
                        Parameters[name] = val;
                    }
                }
                if (nameAttribute.PositionalArguments != null)
                {
                    for (int i = args.Count;
                         i < nameAttribute.PositionalArguments.Length;
                         i++)
                    {
                        if (i != 0)
                        {
                            javascriptWriter.Write(", ");
                        }
                        context.Visitor.Visit(Expression.Constant(nameAttribute.PositionalArguments[i]));
                    }
                }

                javascriptWriter.Write(")");
            }