From 29c3dc7199d27044cc3728aa3ba1fefa3e1569b3 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Thu, 11 Feb 2016 14:11:04 -0600 Subject: [PATCH] ability to download all values in CSV format --- www/app.js | 2 ++ www/functions_SQLite.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/www/app.js b/www/app.js index 23e77c2..8756bd3 100644 --- a/www/app.js +++ b/www/app.js @@ -73,6 +73,7 @@ app.get('/json/tag', fns.getAllTags); 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/valBetween/:tag/:startDatetime/:endDatetime', fns.seriesTagValuesBetween); // Gets the values of a tag between the start time and end time +app.get('/json/CSV/all', fns.allDataCSV); // Gets a CSV of all values stored app.get('/json/CSV/:tag/:startDatetime/:endDatetime', fns.seriesCSV); // Gets a CSV of the values of a tag between the start time and end time app.get('/json/CSV/:tag/:hours', fns.seriesCSV); // Gets a CSV of 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 @@ -82,6 +83,7 @@ app.get('/json/logger/status', fns.checkLoggerS app.get('/json/logger/restart', fns.restartLogger); // Restarts the data logger + app.get('*', angular); /** diff --git a/www/functions_SQLite.js b/www/functions_SQLite.js index 096424d..ad5c6b9 100644 --- a/www/functions_SQLite.js +++ b/www/functions_SQLite.js @@ -177,6 +177,36 @@ exports.seriesTagValuesBetween = function(req, res){ }); }; +exports.allDataCSV = function(req, res){ + var sqlite3 = require('sqlite3').verbose(); + var db = new sqlite3.Database(dbFile); + + db.serialize(function(){ + var query = "SELECT v.id, t.vanityName, v.val, v.dateAdded FROM tags t JOIN vals v ON t.id = v.tagID"; + var prepQuery = db.prepare(query); + prepQuery.all( function(err, rows){ + prepQuery.finalize(); + db.close(); + if (err){ + console.log(err); + res.json({status:"error", message:err, query:query}); + } else { + var csvString = ""; + var h = ["ID", "Tag Name", "Value", "DateAdded"]; + csvString = csvString + h.join(",") + "\r"; + 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.seriesCSV = function(req, res){ var sqlite3 = require('sqlite3').verbose(); var db = new sqlite3.Database(dbFile);