From 3627e83ceb59f83e91ed88166d3bc2ba71588114 Mon Sep 17 00:00:00 2001 From: 沈斌 <bluelazysb@hotmail.com> Date: Mon, 08 Jan 2018 20:17:00 +0800 Subject: [PATCH] updated --- create_data.js | 60 ++++++++++++++++++++++++++++++------------------------------ 1 files changed, 30 insertions(+), 30 deletions(-) diff --git a/create_data.js b/create_data.js index ea70279..ae8d104 100644 --- a/create_data.js +++ b/create_data.js @@ -2,7 +2,7 @@ var moment = require('moment'); var config = require('./config'); -var pool = mysql.createPool(config.mysql); +var mysqlClient = mysql.createConnection(config.mysql); var start_date = moment().add(-1, 'days').format('YYYY-MM-DD');//'2017-11-11'; var end_date = moment().add(-1, 'days').format('YYYY-MM-DD');//'2018-01-07'; @@ -23,43 +23,42 @@ } } +mysqlClient.end(); + function fetchDeviceMinutelyData(start, end) { - pool.getConnection(function (err, connection) { - var devices = {}; - var sql = "SELECT mac, mac_key, avg(mac_value) as avg_val, min(mac_value) as min_val, max(mac_value) as max_val FROM history WHERE time >= '" + start + "' AND time < '" + end + "' GROUP BY mac, mac_key ORDER BY mac"; - //console.log(sql); - connection.query(sql, function (error, results, fields) { - if (error) throw error; + var devices = {}; + var sql = "SELECT mac, mac_key, avg(mac_value) as avg_val, min(mac_value) as min_val, max(mac_value) as max_val FROM history WHERE time >= '" + start + "' AND time < '" + end + "' GROUP BY mac, mac_key ORDER BY mac"; + //console.log(sql); + mysqlClient.query(sql, function (error, results, fields) { + if (error) throw error; - //console.log(start + " : " + end); + //console.log(start + " : " + end); - results.forEach(function(data) { - var mac = data.mac; - var key = data.mac_key; - var val = data.avg_val; - if(devices[mac] == null) { - devices[mac] = { - mac: mac, - time: moment(start).format('YYYY-MM-DD HH:mm:00') - }; - } - devices[mac][key] = [val.toFixed(3), data.min_val, data.max_val] - }); - - var device_data = []; - for(var mac in devices) { - device_data.push(devices[mac]); + results.forEach(function(data) { + var mac = data.mac; + var key = data.mac_key; + var val = data.avg_val; + if(devices[mac] == null) { + devices[mac] = { + mac: mac, + time: moment(start).format('YYYY-MM-DD HH:mm:00') + }; } - - // console.log(JSON.stringify(device_data)); - if(device_data.length > 0) { - insertDeviceData(device_data, connection); - } + devices[mac][key] = [val.toFixed(3), data.min_val, data.max_val] }); + + var device_data = []; + for(var mac in devices) { + device_data.push(devices[mac]); + } + + if(device_data.length > 0) { + insertDeviceData(device_data); + } }); } -function insertDeviceData(docs, connection) { +function insertDeviceData(docs) { var data = []; docs.forEach(function(doc) { var mac = doc.mac; @@ -69,6 +68,7 @@ data.push([mac, time, JSON.stringify(doc)]); }); + var connection = mysql.createConnection(config.mysql); connection.query("INSERT INTO data (mac, time, json) VALUES ?", [data], function (error, results, fields) { if (error) throw error; -- Gitblit v1.8.0