From 3c67f8004e684fa7738028eaf3e72e6356f5d2d4 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Tue, 26 Jan 2016 22:00:33 -0600 Subject: [PATCH] Added function to retrieve latest values of all tags. Added SQL code to create minExpected and maxExpected for data visualization --- www/dbcreate_SQLite.sql | 2 ++ www/functions_SQLite.js | 18 ++++++++++++- www/public/js/controller.js | 41 +++++++++++++++++++++++++++--- www/public/partials/dashboard.html | 24 ++++++++++++++++- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/www/dbcreate_SQLite.sql b/www/dbcreate_SQLite.sql index fb89b4c..a8fa8bc 100644 --- a/www/dbcreate_SQLite.sql +++ b/www/dbcreate_SQLite.sql @@ -3,6 +3,8 @@ CREATE TABLE IF NOT EXISTS tags ( tagName TEXT, dateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP, units TEXT, + minExpected REAL, + maxExpected REAL, deleted INTEGER DEFAULT 0 ); diff --git a/www/functions_SQLite.js b/www/functions_SQLite.js index 29c1067..3512d06 100644 --- a/www/functions_SQLite.js +++ b/www/functions_SQLite.js @@ -191,5 +191,21 @@ exports.getTag = function(req, res){ }; exports.allValues = function(req, res){ - res.json({status: "error", message: "not implemented"}); + var sqlite3 = require('sqlite3').verbose(); + var db = new sqlite3.Database(dbFile); + + db.serialize(function(){ + var query = "SELECT t.tagName as tagName, t.units as units, t.id as t_id, MAX(v.id) as v_id, v.val as val, v.dateAdded as dtime FROM vals v JOIN tags t ON v.tagID = t.id WHERE t.deleted = 0 GROUP BY v.tagID"; + var prepQuery = db.prepare(query); + prepQuery.all(req.params.id, function(err, rows) { + prepQuery.finalize(); + db.close(); + if (err) { + res.json({status:"error", message:err}); + console.log(err); + } else { + res.json({status:"OK", vals:rows}); + } + }); + }); }; diff --git a/www/public/js/controller.js b/www/public/js/controller.js index 8f7da58..9b3ac09 100644 --- a/www/public/js/controller.js +++ b/www/public/js/controller.js @@ -92,10 +92,29 @@ tsCtrlrs.factory('tags',function($q, $http, $log){ return deferred.promise; }; + var getCurrentValues = function(){ + var deferred = $q.defer(); + $http.get('/json/all').success(function(data) { + if(data.status == "OK"){ + deferred.resolve({ + vals:data.vals, + status: data.status + }); + } else { + deferred.resolve({ + status:data.status, + message: data.message + }); + } + }); + return deferred.promise; + } + return { getTag: getTag, getTagList: getTagList, - getTagHistory: getTagHistory + getTagHistory: getTagHistory, + getCurrentValues: getCurrentValues }; }); @@ -104,12 +123,25 @@ tsCtrlrs.controller('mainCtrl', function($scope, Page, Alerts) { $scope.Alerts = Alerts; }); -tsCtrlrs.controller('dashboardCtrl', function($scope, $route, $http, $routeParams, Page, Alerts, $log) { +tsCtrlrs.controller('dashboardCtrl', function($scope, $route, $http, $routeParams, Page, Alerts, $log, tags) { Page.setTitle('Dashboard'); Page.setPage('dashboard'); + $scope.loading = true; + var getCurrentValues = tags.getCurrentValues(); + getCurrentValues.then(function(data) { + $scope.loading = false; + if (data.status == "OK"){ + $scope.vals = data.vals; + $scope.error = false; + } else { + $scope.error = data.message; + } + }); }); tsCtrlrs.controller('tagsCtrl', function($scope, $route, $http, $routeParams, Page, Alerts, $log, tags) { + Page.setTitle('Tags'); + Page.setPage('tags'); $scope.loading = true; var getTagList = tags.getTagList(); getTagList.then(function(data) { @@ -118,25 +150,26 @@ tsCtrlrs.controller('tagsCtrl', function($scope, $route, $http, $routeParams, Pa $scope.tags = data.tags; $scope.error = false; } else { - $scope.tags = []; $scope.error = data.message; } }); }); tsCtrlrs.controller('tagValsCtrl', function($scope, $route, $http, $routeParams, Page, Alerts, $log, tags) { + Page.setTitle('Tag Series'); + Page.setPage('dashboard'); $scope.loading = true; var getTag = tags.getTag($routeParams.tagID); getTag.then(function(tagData){ if (tagData.status == "OK"){ $scope.tag = tagData.tag; + Page.setTitle('Tag Series: '+ tagData.tag.tagName); var getTagHistory = tags.getTagHistory($routeParams.tagID); getTagHistory.then(function(data) { $scope.loading = false; if (data.status == "OK"){ $scope.vals = data.vals; $scope.error = false; - var getTag } else { $scope.error = data.message; } diff --git a/www/public/partials/dashboard.html b/www/public/partials/dashboard.html index 579bb7f..aac28bf 100644 --- a/www/public/partials/dashboard.html +++ b/www/public/partials/dashboard.html @@ -1 +1,23 @@ -
Dashboard coming soon...
\ No newline at end of file +
+
+
+

Error Caught!

+
{{message}}
+
+
+
+ +
+
+
+

Current Values

+
    +
  • {{val.tagName}} @ {{val.dateAdded}} - {{val.val}} {{val.units}}
  • +
+ +
+
+
+
{{vals}}
+
+
\ No newline at end of file