From 8115d8b6601e070f50d337c6ed9436f55a798271 Mon Sep 17 00:00:00 2001 From: ns17 <ns17@inf.ufpr.br> Date: Wed, 17 Jun 2020 12:02:26 -0300 Subject: [PATCH 1/3] Adding csv download of geolocation data and category --- common/models/category.js | 61 ++++++++++++++++++++ common/models/geolocation.js | 105 +++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) diff --git a/common/models/category.js b/common/models/category.js index 101e682..4425169 100644 --- a/common/models/category.js +++ b/common/models/category.js @@ -21,4 +21,65 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>. 'use strict'; module.exports = function(Category) { + Category.csvexport = function(type, res, callback ) { + var datetime = new Date(); + var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7) + res.set('Expires', datetimeFuture+''); + res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate, proxy-revalidate'); + res.set('Last-Modified', datetime+''); + res.set('Content-Type','application/force-download'); + res.set('Content-Type','application/octet-stream'); + res.set('Content-Type','application/download'); + res.set('Content-Disposition','attachment;filename=category.csv'); + res.set('Content-Transfer-Encoding','binary'); + + Category.find(function(err, categories) { + // conversão JSON para CSV + var itemsFormatted = []; + + // cabeçalho do csv + var headers = { + category_name: "category_name", + category_description: "category_description", + id: "id" + }; + + categories.forEach((item) => { + itemsFormatted.push({ + category_name: item.category_name, + category_description: item.category_description, + id: item.id + }); + }); + + if (headers) { + itemsFormatted.unshift(headers); + } + + var jsonObject = JSON.stringify(itemsFormatted); + var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject; + var csv = ''; + + for (var i = 0; i < array.length; i++) { + var line = ''; + for (var index in array[i]) { + if (line != '') line += ';' // aqui muda o separador do csv + line += array[i][index]; + } + csv += line + '\r\n'; + } + res.send(csv); + }); + }; + + Category.remoteMethod('csvexport', + { + accepts: [ + {arg: 'type', type: 'string', required: false}, + {arg: 'res', type: 'object', 'http': {source: 'res'}} + ], + returns: {}, + // Para usar o 'type' -> http: {path: '/csv/:type', verb: 'get'} + http: {path: '/csv', verb: 'get'}, + }); }; diff --git a/common/models/geolocation.js b/common/models/geolocation.js index 4dff1e0..bbb00b4 100644 --- a/common/models/geolocation.js +++ b/common/models/geolocation.js @@ -21,5 +21,110 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>. 'use strict'; module.exports = function(Geolocation) { + Geolocation.csvexport = function(type, res, callback ) { + if (type == "padr" || type == "andr"){ + var datetime = new Date(); + var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7) + res.set('Expires', datetimeFuture+''); + res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate, proxy-revalidate'); + res.set('Last-Modified', datetime +''); + res.set('Content-Type','application/force-download'); + res.set('Content-Type','application/octet-stream'); + res.set('Content-Type','application/download'); + res.set('Content-Disposition','attachment;filename=geolocation.csv'); + res.set('Content-Transfer-Encoding','binary'); + + Geolocation.find(function(err, geolocations) { + + // conversão JSON para CSV + var itemsFormatted = []; + + if (type == "padr"){ + // cabeçalho do csv + var headers = { + category_id: "category_id", + geolocation_name: "geolocation_name", + latitude: "latitude", + longitude: "longitude", + when_sent: "when_sent", + description_location: "description_location", + user_id: "user_id", + id: "id" + }; + + geolocations.forEach((item) => { + itemsFormatted.push({ + category_id: item.category_id, + geolocation_name: item.geolocation_name, + latitude: item.latitude, + longitude: item.longitude, + when_sent: item.when_sent, + description_location: item.description_location, + user_id: item.user_id, + id: item.id + }); + }); + } + + if (type == "andr"){ + // cabeçalho do csv + var headers = { + category_id: "category_id", + geolocation_name: "geolocation_name", + latitude: "latitude", + longitude: "longitude", + android_id: "android_id", + when_sent: "when_sent", + description_location: "description_location", + user_id: "user_id", + id: "id" + }; + + geolocations.forEach((item) => { + itemsFormatted.push({ + category_id: item.category_id, + geolocation_name: item.geolocation_name, + latitude: item.latitude, + longitude: item.longitude, + android_id: item.android_id, + when_sent: item.when_sent, + description_location: item.description_location, + user_id: item.user_id, + id: item.id + }); + }); + } + if (headers) { + itemsFormatted.unshift(headers); + } + + var jsonObject = JSON.stringify(itemsFormatted); + var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject; + var csv = ''; + + for (var i = 0; i < array.length; i++) { + var line = ''; + for (var index in array[i]) { + if (line != '') line += ';' // aqui muda o separador do csv + line += array[i][index]; + } + csv += line + '\r\n'; + } + res.send(csv); + }); + } + else + console.log('Use as rotas /padr ou /andr') + }; + + Geolocation.remoteMethod('csvexport', + { + accepts: [ + {arg: 'type', type: 'string'}, + {arg: 'res', type: 'object', 'http': {source: 'res'}} + ], + returns: {}, + http: {path: '/csv/:type', verb: 'get'} + }); }; -- GitLab From 066cd2c6d3e93208626dc7b83c1c2807a450f834 Mon Sep 17 00:00:00 2001 From: Henrique Varella Ehrenfried <hvehrenfried@inf.ufpr.br> Date: Thu, 18 Jun 2020 10:02:58 -0300 Subject: [PATCH 2/3] Refactor code Signed-off-by: Henrique Varella Ehrenfried <hvehrenfried@inf.ufpr.br> --- common/models/geolocation.js | 88 ++++++++++++------------------------ 1 file changed, 29 insertions(+), 59 deletions(-) diff --git a/common/models/geolocation.js b/common/models/geolocation.js index bbb00b4..f2249bc 100644 --- a/common/models/geolocation.js +++ b/common/models/geolocation.js @@ -22,8 +22,7 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>. module.exports = function(Geolocation) { Geolocation.csvexport = function(type, res, callback ) { - - if (type == "padr" || type == "andr"){ + if (type == "padrao" || type == "android"){ var datetime = new Date(); var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7) res.set('Expires', datetimeFuture+''); @@ -35,14 +34,11 @@ module.exports = function(Geolocation) { res.set('Content-Disposition','attachment;filename=geolocation.csv'); res.set('Content-Transfer-Encoding','binary'); - Geolocation.find(function(err, geolocations) { - + Geolocation.find(function(err, geolocations) { // conversão JSON para CSV var itemsFormatted = []; - - if (type == "padr"){ - // cabeçalho do csv - var headers = { + // cabeçalho do csv + var headers = { category_id: "category_id", geolocation_name: "geolocation_name", latitude: "latitude", @@ -51,71 +47,45 @@ module.exports = function(Geolocation) { description_location: "description_location", user_id: "user_id", id: "id" - }; - - geolocations.forEach((item) => { - itemsFormatted.push({ - category_id: item.category_id, - geolocation_name: item.geolocation_name, - latitude: item.latitude, - longitude: item.longitude, - when_sent: item.when_sent, - description_location: item.description_location, - user_id: item.user_id, - id: item.id - }); - }); + }; + + // cabeçalho do csv - Adiciona android_id + if (type == "android"){ + headers['android_id'] = "android_id" } - if (type == "andr"){ - // cabeçalho do csv - var headers = { - category_id: "category_id", - geolocation_name: "geolocation_name", - latitude: "latitude", - longitude: "longitude", - android_id: "android_id", - when_sent: "when_sent", - description_location: "description_location", - user_id: "user_id", - id: "id" - }; - - geolocations.forEach((item) => { - itemsFormatted.push({ - category_id: item.category_id, - geolocation_name: item.geolocation_name, - latitude: item.latitude, - longitude: item.longitude, - android_id: item.android_id, - when_sent: item.when_sent, - description_location: item.description_location, - user_id: item.user_id, - id: item.id - }); - }); - } - if (headers) { - itemsFormatted.unshift(headers); - } + //Ordena header + var ordered_header = {} + Object.keys(headers).sort().forEach(function(key) { + ordered_header[key] = headers[key]; + }); + + //Adiciona os headers no CSV + geolocations.unshift(headers); - var jsonObject = JSON.stringify(itemsFormatted); + // Posição do Android ID 4 atualmente esta em 8 + var jsonObject = JSON.stringify(geolocations); var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject; var csv = ''; - for (var i = 0; i < array.length; i++) { + for (let i = 0; i < array.length; i++) { var line = ''; - for (var index in array[i]) { + var aux = {} + Object.keys(array[i]).sort().forEach(function(key) { + aux[key] = array[i][key]; + }); + if (type == "padrao"){ + delete aux["android_id"]; + } + for (let index in aux) { if (line != '') line += ';' // aqui muda o separador do csv line += array[i][index]; } - csv += line + '\r\n'; + csv += line + '\n'; } res.send(csv); }); } - else - console.log('Use as rotas /padr ou /andr') }; Geolocation.remoteMethod('csvexport', -- GitLab From 24f16d41e3e57f249e666ed552c6d7a4a84b9a5c Mon Sep 17 00:00:00 2001 From: ns17 <ns17@inf.ufpr.br> Date: Thu, 18 Jun 2020 11:43:13 -0300 Subject: [PATCH 3/3] code cleaning --- common/models/category.js | 26 +++++++------------------- common/models/geolocation.js | 1 - 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/common/models/category.js b/common/models/category.js index 4425169..86cbfd6 100644 --- a/common/models/category.js +++ b/common/models/category.js @@ -21,7 +21,7 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>. 'use strict'; module.exports = function(Category) { - Category.csvexport = function(type, res, callback ) { + Category.csvexport = function(res, callback ) { var datetime = new Date(); var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7) res.set('Expires', datetimeFuture+''); @@ -33,10 +33,9 @@ module.exports = function(Category) { res.set('Content-Disposition','attachment;filename=category.csv'); res.set('Content-Transfer-Encoding','binary'); - Category.find(function(err, categories) { + Category.find(function(err, categories) + { // conversão JSON para CSV - var itemsFormatted = []; - // cabeçalho do csv var headers = { category_name: "category_name", @@ -44,19 +43,10 @@ module.exports = function(Category) { id: "id" }; - categories.forEach((item) => { - itemsFormatted.push({ - category_name: item.category_name, - category_description: item.category_description, - id: item.id - }); - }); - - if (headers) { - itemsFormatted.unshift(headers); - } + //Adiciona os headers no CSV + categories.unshift(headers); - var jsonObject = JSON.stringify(itemsFormatted); + var jsonObject = JSON.stringify(categories); var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject; var csv = ''; @@ -66,7 +56,7 @@ module.exports = function(Category) { if (line != '') line += ';' // aqui muda o separador do csv line += array[i][index]; } - csv += line + '\r\n'; + csv += line + '\n'; } res.send(csv); }); @@ -75,11 +65,9 @@ module.exports = function(Category) { Category.remoteMethod('csvexport', { accepts: [ - {arg: 'type', type: 'string', required: false}, {arg: 'res', type: 'object', 'http': {source: 'res'}} ], returns: {}, - // Para usar o 'type' -> http: {path: '/csv/:type', verb: 'get'} http: {path: '/csv', verb: 'get'}, }); }; diff --git a/common/models/geolocation.js b/common/models/geolocation.js index f2249bc..cbf8f0f 100644 --- a/common/models/geolocation.js +++ b/common/models/geolocation.js @@ -36,7 +36,6 @@ module.exports = function(Geolocation) { Geolocation.find(function(err, geolocations) { // conversão JSON para CSV - var itemsFormatted = []; // cabeçalho do csv var headers = { category_id: "category_id", -- GitLab