public IEnumerable<QueriedMetricResult> ExecuteQuery(Query query) { if (query.Metrics == null) yield break; lock(_sqliteEngineLock) { foreach (var metric in query.Metrics) { yield return DatabaseQueries.GetMetricValuesBetweenDates(_db, metric, query.StartDate, query.EndDate, query.Resolution); } } }
public MetricsModule(IStorageEngine storageEngine) : base("/metrics") { _storageEngine = storageEngine; Get["/list"] = parameters => { var metrics = _storageEngine.GetKnownMetrics(); return Response.AsJson(metrics.ToArray()); }; Get["/query"] = parameters => { DateTime start, end; DateTime.TryParse(Request.Query.start, out start); if (!DateTime.TryParse(Request.Query.end, out end)) end = DateTime.MaxValue; string rawMetrics = Convert.ToString(Request.Query.metric.Value); var metrics = new Metric[0]; if (rawMetrics != null) { var splitRawMetrics = rawMetrics.Split(','); metrics = splitRawMetrics.Select(Metric.Create).ToArray(); } var query = new Query { StartDate = start, EndDate = end, Metrics = metrics }; var results = _storageEngine.ExecuteQuery(query); return Response.AsJson(results.ToArray()); }; }
public void Query_Results_Returned_As_Average_Per_Year() { const string device = "device"; const string category = "category"; const string name1 = "name1"; const string suffix = "suffix"; const decimal metricValue1 = 0m; const decimal metricValue2 = 50m; const decimal metricValue3 = 75m; const decimal expectedAverage = (metricValue1 + metricValue2) / 2; var metric1 = new Metric(device, category, name1, suffix); var snapshot1 = new Snapshot { Date = new DateTime(2013, 5, 1, 0, 0, 0), MetricValues = new[] { new KeyValuePair<Metric, decimal>(metric1, metricValue1) } }; var snapshot2 = new Snapshot { Date = snapshot1.Date.AddMonths(6), MetricValues = new[] { new KeyValuePair<Metric, decimal>(metric1, metricValue2) } }; var snapshot3 = new Snapshot { Date = snapshot1.Date.AddMonths(12), MetricValues = new[] { new KeyValuePair<Metric, decimal>(metric1, metricValue3) } }; _storageEngine.StoreSnapshot(snapshot1); _storageEngine.StoreSnapshot(snapshot2); _storageEngine.StoreSnapshot(snapshot3); var query = new Query { StartDate = snapshot1.Date.AddDays(-1), EndDate = snapshot3.Date.AddDays(1), Metrics = new[] { metric1 }, Resolution = QueryResolution.Year }; var results = _storageEngine.ExecuteQuery(query); var expectedDate1 = snapshot1.Date.AddMonths(-snapshot1.Date.Month + 1); var expectedDate2 = snapshot3.Date.AddMonths(-snapshot3.Date.Month + 1); Assert.IsNotNull(results, "Null results returned"); var resultsArray = results.ToArray(); Assert.AreEqual(1, resultsArray.Length, "Incorrect number of results were returned"); Assert.AreEqual(metric1, resultsArray[0].Metric, "Returned metric was not correct"); Assert.IsNotNull(resultsArray[0].Values, "Returned metric values array was null"); Assert.AreEqual(2, resultsArray[0].Values.Length, "Incorrect number of metric values returned"); Assert.AreEqual(expectedDate1, resultsArray[0].Values[0].Key, "Incorrect metric date for first result"); Assert.AreEqual(expectedAverage, resultsArray[0].Values[0].Value, "Incorrect metric value for first result"); Assert.AreEqual(expectedDate2, resultsArray[0].Values[1].Key, "Incorrect metric date for second result"); Assert.AreEqual(metricValue3, resultsArray[0].Values[1].Value, "Incorrect metric value for second result"); }
public void Multiple_Values_Returned_In_Order_Of_Snapshot_Date() { const string device = "device"; const string category = "category"; const string name1 = "name1"; const string suffix = "suffix"; const decimal metricValue1 = 123.4m; const decimal metricValue2 = 50m; var metric1 = new Metric(device, category, name1, suffix); var snapshot1 = new Snapshot { Date = DateTime.Now.ToComparableDateTime(), MetricValues = new KeyValuePair<Metric, decimal>[] { new KeyValuePair<Metric, decimal>(metric1, metricValue1), } }; var snapshot2 = new Snapshot { Date = DateTime.Now.AddMinutes(-5).ToComparableDateTime(), MetricValues = new KeyValuePair<Metric, decimal>[] { new KeyValuePair<Metric, decimal>(metric1, metricValue2), } }; _storageEngine.StoreSnapshot(snapshot1); _storageEngine.StoreSnapshot(snapshot2); var query = new Query { StartDate = snapshot2.Date.AddMinutes(-1), EndDate = snapshot1.Date.AddMinutes(1), Metrics = new[] { metric1 } }; var results = _storageEngine.ExecuteQuery(query); Assert.IsNotNull(results, "Null results returned"); var resultsArray = results.ToArray(); Assert.AreEqual(1, resultsArray.Length, "Incorrect number of results were returned"); Assert.AreEqual(metric1, resultsArray[0].Metric, "Returned metric was not correct"); Assert.IsNotNull(resultsArray[0].Values, "Returned metric values array was null"); Assert.AreEqual(2, resultsArray[0].Values.Length, "Incorrect number of metric values returned"); Assert.AreEqual(snapshot2.Date, resultsArray[0].Values[0].Key, "Incorrect metric date for first result"); Assert.AreEqual(metricValue2, resultsArray[0].Values[0].Value, "Incorrect metric value for first result"); Assert.AreEqual(snapshot1.Date, resultsArray[0].Values[1].Key, "Incorrect metric date for second result"); Assert.AreEqual(metricValue1, resultsArray[0].Values[1].Value, "Incorrect metric value for second result"); }