Exemplo n.º 1
0
        public static QueriedMetricResult GetMetricValuesBetweenDates(SQLiteConnection connection, Metric metric,
                                                                      DateTime startDate, DateTime endDate,
                                                                      QueryResolution resolution)
        {
            if (connection == null)
                throw new ArgumentNullException("connection");

            if (metric == null)
                throw new ArgumentNullException("metric");

            // Impossible to have values when the start date is after the end date, so don't even try
            if (endDate < startDate)
                return new QueriedMetricResult { Metric = metric, Values = new KeyValuePair<DateTime, decimal>[0] };

            var query = GenerateQueryAtResolution(resolution);
            var values = new List<KeyValuePair<DateTime, decimal>>();
            using (var command = new SQLiteCommand(query, connection))
            {
                command.Parameters.AddWithValue("@device", metric.Device);
                command.Parameters.AddWithValue("@category", metric.Category);
                command.Parameters.AddWithValue("@name", metric.Name);
                command.Parameters.AddWithValue("@start", startDate.ToUtcUnixTimestamp());
                command.Parameters.AddWithValue("@end", endDate.ToUtcUnixTimestamp());

                using (var reader = command.ExecuteReader())
                    while (reader.Read())
                        ReadQueryResult(resolution, reader, values);
            }

            return new QueriedMetricResult
            {
                Metric = metric,
                Values = values.ToArray()
            };
        }
Exemplo n.º 2
0
 public void Can_Create_Metric_From_Parsable_Metric_String()
 {
     const string parsableString = "(device|category|name|suffix)";
     var expectedMetric = new Metric("device", "category", "name", "suffix");
     var actualMetric = Metric.Create(parsableString);
     Assert.AreEqual(expectedMetric, actualMetric);
 }
Exemplo n.º 3
0
        public void Can_Create_Parsable_String_From_Metric_Details()
        {
            var metric = new Metric("device", "category", "name", "suffix");

            const string expectedResult = "(device|category|name|suffix)";
            var actualResult = metric.ToParsableString();

            Assert.AreEqual(expectedResult, actualResult);
        }
        public IEnumerable<KeyValuePair<Metric, decimal>> GetCurrentMetricValues()
        {
            const string device = "Processes";
            const string openCategory = "Open";

            var processes = Process.GetProcesses();
            foreach (var process in processes)
            {
                var name = process.ProcessName;
                var metric = new Metric(device, openCategory, name, string.Empty);
                yield return new KeyValuePair<Metric, decimal>(metric, 1m);
            }
        }
Exemplo n.º 5
0
        public void List_Returns_Correct_Metrics()
        {
            var metric1 = new Metric("device1", "category1", "name1", "suffix");
            var metric2 = new Metric("device2", "category2", "name2", "suffix");
            _storageEngine.Setup(x => x.GetKnownMetrics()).Returns(new[] { metric1, metric2 });

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

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode, "Incorrect status code returned");
            _storageEngine.Verify(x => x.GetKnownMetrics(), Times.Once());
            Assert.IsNotNull(metrics, "Metrics array was null");
            Assert.AreEqual(2, metrics.Length, "Metrics array length was incorrect");
            Assert.IsTrue(metrics.Contains(metric1), "First metric is not in the returned array");
            Assert.IsTrue(metrics.Contains(metric2), "Second metric is not in the returned array");
        }
        public IEnumerable<KeyValuePair<Metric, decimal>> GetCurrentMetricValues()
        {
            // Loop through all the sensors
            foreach (var hardware in _computer.Hardware)
            {
                foreach (var sensor in hardware.Sensors)
                {
                    var device = hardware.Name;
                    var category = sensor.SensorType.ToString();
                    var name = sensor.Name;
                    var value = Convert.ToDecimal(sensor.Value);

                    // TODO: Set suffix based on sensor type
                    var metric = new Metric(device, category, name, "");
                    yield return new KeyValuePair<Metric, decimal>(metric, value);
                }
            }
        }
Exemplo n.º 7
0
        public void Query_Allows_Multiple_Metrics_Passed_In()
        {
            var metric1 = new Metric("device", "category", "name", "suffix");
            var metric2 = new Metric("device2", "category2", "name2", "suffix2");

            var path = "/metrics/query";
            var response = _browser.Get(path, with =>
            {
                with.HttpRequest();
                with.Query("metric", metric1.ToParsableString());
                with.Query("metric", metric2.ToParsableString());
            });

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Incorrect status code returned");
            _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.Metrics != null)), Times.Once());
            _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.Metrics.Length == 2)), Times.Once());
            _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.Metrics.Contains(metric1))), Times.Once());
            _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.Metrics.Contains(metric2))), Times.Once());
        }
Exemplo n.º 8
0
        public void Can_Retrieve_Number_Of_Snapshots_Returned()
        {
            var metric = new Metric("device", "category", "name", "suffix");
            var snapshot1 = new Snapshot
            {
                Date = DateTime.Now,
                MetricValues = new[] {new KeyValuePair<Metric, decimal>(metric, 1m)}
            };

            var snapshot2 = new Snapshot
            {
                Date = DateTime.Now.AddMinutes(5),
                MetricValues = new[] { new KeyValuePair<Metric, decimal>(metric, 1m) }
            };

            _storageEngine.StoreSnapshot(snapshot1);
            _storageEngine.StoreSnapshot(snapshot2);
            var snapshotCount = _storageEngine.GetSnapshotCount();

            Assert.AreEqual(2, snapshotCount, "Incorrect number of snapshots reported");
        }
Exemplo n.º 9
0
        public void Can_Get_List_Of_All_Metrics_Stored()
        {
            var metric1 = new Metric("device1", "category1", "name1", "suffix1");
            var metric2 = new Metric("device2", "category2", "name2", "suffix2");
            var metric3 = new Metric("device3", "category3", "name3", "suffix3");
            var metric4 = new Metric("device4", "category4", "name4", "suffix4");

            var snapshot1 = new Snapshot
            {
                Date = DateTime.Now,
                MetricValues = new KeyValuePair<Metric, decimal>[]
                {
                    new KeyValuePair<Metric, decimal>(metric1, 1m),
                    new KeyValuePair<Metric, decimal>(metric2, 1m),
                }
            };

            var snapshot2 = new Snapshot
            {
                Date = DateTime.Now,
                MetricValues = new KeyValuePair<Metric, decimal>[]
                {
                    new KeyValuePair<Metric, decimal>(metric3, 1m),
                    new KeyValuePair<Metric, decimal>(metric4, 1m),
                }
            };

            _storageEngine.StoreSnapshot(snapshot1);
            _storageEngine.StoreSnapshot(snapshot2);

            var results = _storageEngine.GetKnownMetrics();
            Assert.IsNotNull(results, "Returned metric result was null");

            var array = results.ToArray();
            Assert.AreEqual(4, array.Length, "Returned metric enumerable had an incorrect number of elements");
            Assert.IsTrue(array.Contains(metric1), "Metric enumerable did not contain metric1");
            Assert.IsTrue(array.Contains(metric2), "Metric enumerable did not contain metric2");
            Assert.IsTrue(array.Contains(metric3), "Metric enumerable did not contain metric3");
            Assert.IsTrue(array.Contains(metric4), "Metric enumerable did not contain metric4");
        }
Exemplo n.º 10
0
        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());
            };
        }
Exemplo n.º 11
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.º 12
0
        public void Query_Builds_Correct_Query_From_Parameters()
        {
            var metric = new Metric("device", "category", "name", "suffix");
            var path = "/metrics/query";
            var startDate = DateTime.Now;
            var endDate = DateTime.Now.AddMinutes(10);

            // Set dates to their parsed dates to make sure they are equal to how they will
            // be read by the query string
            startDate = DateTime.Parse(startDate.ToString());
            endDate = DateTime.Parse(endDate.ToString());

            var response = _browser.Get(path, with =>
            {
                with.HttpRequest();
                with.Query("start", startDate.ToString());
                with.Query("end", endDate.ToString());
                with.Query("metric", metric.ToParsableString());
            });

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Incorrect status code returned");
            _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.StartDate == startDate)), Times.Once());
            _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.EndDate == endDate)), Times.Once());
            _storageEngine.Verify(x => x.ExecuteQuery(It.Is<Query>(y => y.Metrics.Contains(metric))), Times.Once());
        }
Exemplo n.º 13
0
        public void Can_Retrieve_Storage_Size()
        {
            var metric = new Metric("device", "category", "name", "suffix");
            var snapshot1 = new Snapshot
            {
                Date = DateTime.Now,
                MetricValues = new[] { new KeyValuePair<Metric, decimal>(metric, 1m) }
            };

            _storageEngine.StoreSnapshot(snapshot1);
            var size = _storageEngine.GetStoredSize();

            // Since we cant predict how much space the stored snapshot will be
            // just make sure it's more than nothing
            Assert.IsTrue(size > 0, "Reported size was less than or equal to zero");
        }
Exemplo n.º 14
0
        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");
        }
Exemplo n.º 15
0
        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");
        }
Exemplo n.º 16
0
        public void Known_Metrics_Returned_Ordered_By_Device_Category_Then_Name()
        {
            var metric1 = new Metric("device1", "category1", "name1", "suffix1");
            var metric2 = new Metric("device1", "category1", "name2", "suffix2");
            var metric3 = new Metric("device1", "category2", "name1", "suffix3");
            var metric4 = new Metric("device2", "category1", "name1", "suffix4");

            var snapshot1 = new Snapshot
            {
                Date = DateTime.Now,
                MetricValues = new KeyValuePair<Metric, decimal>[]
                {
                    new KeyValuePair<Metric, decimal>(metric3, 1m),
                    new KeyValuePair<Metric, decimal>(metric4, 1m),
                    new KeyValuePair<Metric, decimal>(metric1, 1m),
                    new KeyValuePair<Metric, decimal>(metric2, 1m),
                }
            };

            _storageEngine.StoreSnapshot(snapshot1);

            var results = _storageEngine.GetKnownMetrics();
            Assert.IsNotNull(results, "Returned metric result was null");

            var array = results.ToArray();
            Assert.AreEqual(4, array.Length, "Returned metric enumerable had an incorrect number of elements");
            Assert.AreEqual(metric1, array[0], "First metric was incorrect");
            Assert.AreEqual(metric2, array[1], "Second metric was incorrect");
            Assert.AreEqual(metric3, array[2], "Third metric was incorrect");
            Assert.AreEqual(metric4, array[3], "Fourth metric was incorrect");
        }