From be5e76a4e01b9ec36768c880fcc0e5d9b3795c05 Mon Sep 17 00:00:00 2001
From: 沈斌 <bluelazysb@hotmail.com>
Date: Wed, 24 Jan 2018 14:22:55 +0800
Subject: [PATCH] updated
---
create_data.js | 32 ++++++++++++++++++++++----------
1 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/create_data.js b/create_data.js
index ea70279..30d7694 100644
--- a/create_data.js
+++ b/create_data.js
@@ -18,18 +18,21 @@
var start = moment(date_hour).add(k, 'minutes').format("YYYY-MM-DD HH:mm:00");
var end = moment(date_hour).add(k+1, 'minutes').format("YYYY-MM-DD HH:mm:00");
- fetchDeviceMinutelyData(start, end);
+ fetchDeviceMinutelyData(start, end, days, (i+1)*(j+1)*(k+1));
}
}
-}
-function fetchDeviceMinutelyData(start, end) {
- pool.getConnection(function (err, connection) {
+
+function fetchDeviceMinutelyData(start, end, d, t) {
+ pool.getConnection(function (err, conn) {
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;
+ conn.query(sql, function (error, results, fields) {
+ if (error) {
+ console.log("error1: " + error.message);
+ return;
+ }
//console.log(start + " : " + end);
@@ -51,15 +54,20 @@
device_data.push(devices[mac]);
}
- // console.log(JSON.stringify(device_data));
if(device_data.length > 0) {
- insertDeviceData(device_data, connection);
+ insertDeviceData(device_data);
+ }
+
+ conn.release();
+
+ if(t == (d+1)*24*60) {
+ pool.end();
}
});
});
}
-function insertDeviceData(docs, connection) {
+function insertDeviceData(docs) {
var data = [];
docs.forEach(function(doc) {
var mac = doc.mac;
@@ -69,8 +77,12 @@
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;
+ if (error) {
+ console.log("error2: " + error.message);
+ return;
+ }
connection.destroy();
});
--
Gitblit v1.8.0