Skip to content
This repository has been archived by the owner on Jun 19, 2018. It is now read-only.

Commit

Permalink
Temporarily connects mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
gongzhang committed Jun 15, 2017
1 parent 85d9b6a commit 7426fc2
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^4.0.0",
"loopback-connector-rest": "^2.1.0",
"mongodb": "^2.2.28",
"mustache": "^2.3.0",
"request-promise": "^4.2.0",
"serve-favicon": "^2.0.1",
Expand Down
41 changes: 41 additions & 0 deletions server/boot/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var sensor = require('../dao/sensor.js'),
user = require('../dao/user.js');

var conn = require('../dao/connection.js');
var dbConn = require('../dao/db_conn');

// Configuration
var limit = 50;
Expand Down Expand Up @@ -139,6 +140,46 @@ module.exports = function(app) {
)
});

router.get('/data/:sensor_id/latest', function (req, res) {
const sensor_id = req.params.sensor_id;

dbConn.latest('First', sensor_id).then(function (data) {
res.send(data);
}).catch(function (err) {
res.send({err: err.message});
});
})

router.get('/data/:sensor_id/history', function (req, res) {
const sensor_id = req.params.sensor_id;

var since_ts = +req.query.since; // required
var until_ts = +req.query.until; // optional
var downsample = +req.query.downsample; // optional

if (!since_ts) {
throw 'Missing or invalid query: "since"';
}

if (!until_ts) {
// until now by default
until_ts = new Date().getTime();
}

var promise;
if (downsample) {
promise = dbConn.downsample('First', sensor_id, since_ts, until_ts, downsample);
} else {
promise = dbConn.history('First', sensor_id, since_ts, until_ts);
}

promise.then(function (arr) {
res.send(arr);
}).catch(function (err) {
res.send({err: err.message});
});
})

// Render Dashboard Index Template
router.get('/dashboard', function (req, res) {
return res.render(path.join(app.get('template') + '/index.html'));
Expand Down
94 changes: 94 additions & 0 deletions server/dao/db_conn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Created by gong on 2017/6/15.
*
* [[[ IMPORTANT NODE ]]]
*
* Scaffold code. Remove this file later.
* (Dashboard should not connect MongoDB directly. Use backend API instead.)
*
*/
'use strict';

var mongodb = require('mongodb');
var mongoUrl = 'mongodb://leaniot:leaniot@119.254.211.60:37017/leaniot?authSource=leaniot';

var mongo;

mongodb.MongoClient.connect(mongoUrl).then(function(db) {
console.log('MongoDB connected.');
mongo = db;
}).catch(function(err) {
console.log('Failed to connect MongoDB: ' + err.message);
});

/**
* 取最近一条数据。
*
* @param {string} coll
* @param {string} sensor_id
* @return {Promise.<object>}
*/
exports.latest = function(coll, sensor_id) {
return mongo.collection(coll).find({
sensor_id: sensor_id
}, {
_id: false, timestamp: true, payload: true
}).sort({timestamp: -1}).limit(1).toArray().then(function(arr) {
return arr.length === 1 ? arr[0] : {};
});
}

/**
* 取一个时间段内的完整数据。
*
* @param {string} coll
* @param {string} sensor_id
* @param {number} start_ts
* @param {number} end_ts
* @return {Promise.<Array>}
*/
exports.history = function(coll, sensor_id, start_ts, end_ts) {
return mongo.collection(coll).find({
sensor_id: sensor_id,
timestamp: {
$gte: start_ts,
$lte: end_ts
}
}, {
_id: false, timestamp: true, payload: true
}).sort({timestamp: 1}).toArray()
}

/**
* 取一个时间段内的数据,下降采样。
*
* @param {string} coll
* @param {string} sensor_id
* @param {number} start_ts
* @param {number} end_ts
* @param {number} buckets
* @return {Promise.<Array>}
*/
exports.downsample = function (coll, sensor_id, start_ts, end_ts, buckets) {
return mongo.collection(coll).aggregate([
{
$match: {
sensor_id: sensor_id,
timestamp: {
$gte: start_ts,
$lte: end_ts
}
}
},
{
$bucketAuto: {
groupBy: "$timestamp",
buckets: buckets,
output: {
timestamp: { $last: "$timestamp" },
payload: { $last: "$payload" } // $max, $min, $first, $last, $avg
}
}
}
]).project({_id: false}).sort({timestamp: 1}).toArray();
}

0 comments on commit 7426fc2

Please sign in to comment.