From 5c67ea10fee1330bd2e239ccb14252aaf3fbbfdb Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Thu, 28 Jan 2016 17:00:32 -0600 Subject: [PATCH] Added ability to download CSV of data series --- www/app.js | 1 + www/functions_SQLite.js | 31 ++++++++++++++++++++++++++++++ www/public/partials/dashboard.html | 7 +++++-- www/public/partials/tagVals.html | 13 +++++++++++-- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/www/app.js b/www/app.js index e4e4092..6913382 100644 --- a/www/app.js +++ b/www/app.js @@ -72,6 +72,7 @@ app.get('/json/tag/:id', fns.getTag); // Lists all app.get('/json/tag', fns.getAllTags); // Lists all tags in the scan list app.get('/json/val/:tag', fns.latestValueSingleTag); // Gets the latest value of a single tag app.get('/json/series/:tag/:hours', fns.seriesTagValues); // Gets all the values of a tag for the last X hours +app.get('/json/CSV/:tag/:hours', fns.seriesCSV); // Gets all the values of a tag for the last X hours app.get('/json/all', fns.latestValueAllTags); // Gets the latest values of all tags in the scan list app.get('*', angular); diff --git a/www/functions_SQLite.js b/www/functions_SQLite.js index 7d055f6..48fd0fb 100644 --- a/www/functions_SQLite.js +++ b/www/functions_SQLite.js @@ -122,6 +122,37 @@ exports.seriesTagValues = function(req, res){ }); }; +exports.seriesCSV = function(req, res){ + var sqlite3 = require('sqlite3').verbose(); + var db = new sqlite3.Database(dbFile); + + db.serialize(function(){ + var query = "SELECT * FROM vals WHERE tagID = ? AND dateAdded > DATETIME('now', '-1 HOUR')"; + var prepQuery = db.prepare(query); + prepQuery.all(parseInt(req.params.tag), function(err, rows){ + prepQuery.finalize(); + db.close(); + if (err){ + console.log(err); + res.json({status:"error", message:err, query:query}); + } else { + var csvString = ""; + + for (var i= 0; i < rows.length; i++){ + var r = [rows[i].id, rows[i].val, rows[i].dateAdded]; + csvString = csvString + r.join(",") + "\r"; + } + + res.set('Content-Type', 'text/csv'); + res.set('Content-Disposition', "attachment;filename=tagdata.csv"); + res.send(csvString); + } + }) + }); + + +} + exports.latestValueSingleTag = function(req, res){ var sqlite3 = require('sqlite3').verbose(); var db = new sqlite3.Database(dbFile); diff --git a/www/public/partials/dashboard.html b/www/public/partials/dashboard.html index 0d03a23..f6c302d 100644 --- a/www/public/partials/dashboard.html +++ b/www/public/partials/dashboard.html @@ -13,17 +13,20 @@

Error Caught!

{{message}}
- +
- +
+
diff --git a/www/public/partials/tagVals.html b/www/public/partials/tagVals.html index 6c7f5a9..d5c057b 100644 --- a/www/public/partials/tagVals.html +++ b/www/public/partials/tagVals.html @@ -1,3 +1,11 @@ + +
@@ -13,14 +21,15 @@

Error Caught!

{{message}}
- +
- + + Download Data