private dynamic RenderSeries(dynamic req) { try { var model = new RenderSeriesModel(); this.BindTo(model, new BindingConfig { BodyOnly = false }); var tgt = (string)Request.Form.target; var now = DateTime.UtcNow; var from = DateTimeParser.ParseDateTime(model.From, now, now.AddHours(-1)); var until = DateTimeParser.ParseDateTime(model.Until, now, now); var targets = string.Join("', '", model.Target); log.Debug("started rendering metrics using '{0}' from {1:s} to {2:s} ({3})", targets, from, until, until - from); var environment = new Statsify.Core.Expressions.Environment { MetricRegistry = metricRegistry, QueuedMetricDatapoints = metricAggregator.Queue }; var evalContext = new EvalContext(@from, until); var expressions = expressionCompiler.Parse(model.Target). Select(e => e is MetricSelectorExpression ? new EvaluatingMetricSelectorExpression(e as MetricSelectorExpression) : e). ToList(); log.Debug("started evaluating expression"); var r = expressions.SelectMany(e => (Core.Model.Metric[])e.Evaluate(environment, evalContext)).ToArray(); log.Debug("evaluated expression"); var metrics = new List <Core.Model.Metric>(r); var seriesViewList = metrics. Select(m => new SeriesView { Target = m.Name, Datapoints = m.Series.Datapoints. Select(d => new[] { d.Value, d.Timestamp.ToUnixTimestamp() }). ToArray() }). ToArray(); log.Debug("rendered {0} metrics using '{1}' from {2:s} to {3:s}", seriesViewList.Length, targets, from, until); return(Response.AsJson(seriesViewList)); } catch (Exception e) { return(Response.AsJson(new { e.Message, e.StackTrace }, HttpStatusCode.InternalServerError)); } }
private dynamic GetSeries(IMetricRegistry metricRegistry, IMetricAggregator metricAggregator, ExpressionCompiler expressionCompiler) { try { var model = new SeriesQueryModel(); this.BindTo(model, new BindingConfig { BodyOnly = false }); var now = DateTime.UtcNow; var from = DateTimeParser.ParseDateTime(model.From, now, now.AddHours(-1)); var until = DateTimeParser.ParseDateTime(model.Until, now, now); var environment = new Statsify.Core.Expressions.Environment { MetricRegistry = metricRegistry, QueuedMetricDatapoints = metricAggregator.Queue }; var evalContext = new EvalContext(@from, until); var metrics = new List <Core.Model.Metric>(); foreach (var expression in model.Expression.Select(HttpUtility.UrlDecode)) { var e = expressionCompiler.Parse(expression).Single(); if (e is MetricSelectorExpression) { e = new EvaluatingMetricSelectorExpression(e as MetricSelectorExpression); } // if var r = (Core.Model.Metric[])e.Evaluate(environment, evalContext); metrics.AddRange(r); } // foreach var seriesViewList = metrics. Select(m => new SeriesView { Target = m.Name, Datapoints = m.Series.Datapoints. Select(d => new[] { d.Value, d.Timestamp.ToUnixTimestamp() }). ToArray() }). ToArray(); return(Response.AsJson(seriesViewList)); } catch (Exception e) { return(Response.AsJson(new { e.Message, e.StackTrace }, HttpStatusCode.InternalServerError)); } }