public static void AddSnapshot(SQLiteConnection connection, Snapshot snapshot) { const string insertNonQuery = @"insert into MetricData (Date, MetricDevice, MetricCategory, MetricName, SuffixLabel, Value) Values (@date, @device, @category, @name, @suffix, @value);"; if (connection == null) throw new ArgumentNullException("connection"); if (snapshot == null) throw new ArgumentNullException("snapshot"); if (snapshot.MetricValues != null) { foreach (var metricValuePair in snapshot.MetricValues) { using (var command = new SQLiteCommand(insertNonQuery, connection)) { command.Parameters.AddWithValue("@date", snapshot.Date.ToUtcUnixTimestamp()); command.Parameters.AddWithValue("@device", metricValuePair.Key.Device); command.Parameters.AddWithValue("@category", metricValuePair.Key.Category); command.Parameters.AddWithValue("@name", metricValuePair.Key.Name); command.Parameters.AddWithValue("@suffix", metricValuePair.Key.SuffixLabel); command.Parameters.AddWithValue("@value", metricValuePair.Value); command.ExecuteNonQuery(); } } } }
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"); }
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"); }
public void StoreSnapshot(Snapshot snapshot) { DatabaseQueries.AddSnapshot(_db, snapshot); }
private void ProcessMetricRequests() { _logger.Debug("Beginning to process metric requests"); var storageEngine = new SqliteStorageEngine(ApplicationSettings.DatabaseName); // Start the webserver _logger.Debug("Beginning webserver on port {0}", _webPortNumber); var bootstrapper = new OverlookBootStrapper(storageEngine); var uri = new Uri("http://localhost:" + _webPortNumber); var webServer = new NancyHost(uri, bootstrapper); webServer.Start(); _logger.Debug("Web server started"); var retrievers = new IMetricRetriever[] { new OpenProcessMetricRetriever(), new OpenHardwareMonitorMetricRetriever() }; UpdateDisplays(storageEngine); var lastSnapshotTime = DateTime.MinValue; var secondsBetweenChecks = ApplicationSettings.SecondsBetweenSnapshots; while (!_cancellationTokenSource.Token.IsCancellationRequested) { if ((DateTime.Now - lastSnapshotTime).TotalSeconds > secondsBetweenChecks) { _logger.Debug("Generating snapshot"); var snapshot = new Snapshot { Date = DateTime.Now, MetricValues = retrievers.SelectMany(x => x.GetCurrentMetricValues()) .ToArray() }; _logger.Debug("Storing Snapshot"); storageEngine.StoreSnapshot(snapshot); _logger.Debug("Snapshot stored"); UpdateDisplays(storageEngine); lastSnapshotTime = DateTime.Now; } else { Thread.Sleep(100); } } _logger.Debug("Stopping webserver"); webServer.Stop(); _logger.Debug("Webserver stopped"); }
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"); }
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"); }
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"); }