Exemplo n.º 1
0
        public void Query_Returns_Results_From_Storage_Engine()
        {
            var metric1 = new Metric("device", "category", "name", "suffix");
            var value1 = 3m;
            var date1 = DateTime.Parse(DateTime.Now.ToString());
            var expectedResults = new QueriedMetricResult
            {
                Metric = metric1,
                Values = new[] {new KeyValuePair<DateTime, decimal>(date1, value1)}
            };

            _storageEngine.Setup(x => x.ExecuteQuery(It.IsAny<Query>()))
                          .Returns(new[] {expectedResults});

            var response = _browser.Get("/metrics/query", with => with.HttpRequest());
            var result = JsonConvert.DeserializeObject<QueriedMetricResult[]>(response.Body.AsString());

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Incorrect status code returned");
            Assert.IsNotNull(result, "Returned result was not a valid QueriedMetricResult array");
            Assert.AreEqual(1, result.Length, "Returned array had an incorrect number of elements");
            Assert.AreEqual(metric1, result[0].Metric, "Returned array's metric was incorrect");
            Assert.AreEqual(1, result[0].Values.Length, "Value array had an incorrect number of elements");
            Assert.AreEqual(date1, result[0].Values[0].Key, "Value array's date was incorrect");
            Assert.AreEqual(value1, result[0].Values[0].Value, "Value array's value was incorrect");
        }
Exemplo n.º 2
0
        private void PlotMetricData(QueriedMetricResult metricData)
        {
            var model = new PlotModel("Metrics");
            var dateAxis = new DateTimeAxis();
            var lineAxis = new LinearAxis();
            model.Axes.Add(dateAxis);
            model.Axes.Add(lineAxis);

            if (metricData != null)
            {
                var series = new LineSeries
                {
                    MarkerFill = OxyColors.SkyBlue,
                    MarkerSize = 5,
                    MarkerStroke = OxyColors.White,
                    MarkerType = MarkerType.Circle,
                    LineStyle = LineStyle.Solid,
                    DataFieldX = "Date",
                    DataFieldY = "Value"
                };

                // Restrict displayed data to daily averages
                var intervalData = metricData.Values
                                          .GroupBy(x =>
                                          {
                                              var timestamp = x.Key;
                                              timestamp = timestamp.AddMinutes(-timestamp.Minute);
                                              timestamp = timestamp.AddHours(-timestamp.Hour);
                                              timestamp = timestamp.AddSeconds(-timestamp.Second);
                                              timestamp = timestamp.AddMilliseconds(-timestamp.Millisecond);
                                              return timestamp;
                                          })
                                          .Select(x => new KeyValuePair<DateTime, decimal>(x.Key, x.Average(y => y.Value)))
                                          .ToArray();

                foreach (var dataPair in intervalData)
                {
                    series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(dataPair.Key), (double)dataPair.Value));
                }

                model.Series.Add(series);
            }

            PlotModel = model;
        }