From b9e092c855e174283c8f6beca0079f6ab5736504 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Fri, 4 Sep 2015 17:09:53 +0800 Subject: [PATCH] Providing stats at /stats --- app.js | 7 +++++++ routes/stats.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 routes/stats.js diff --git a/app.js b/app.js index a2a9e86e1..a4bca44b7 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,7 @@ var path = require('path'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var todos = require('./routes/todos'); +var stats = require('./routes/stats'); var cloud = require('./cloud'); var app = express(); @@ -41,6 +42,12 @@ app.get('/', function(req, res) { res.render('index', { currentTime: new Date() }); }); +app.get('/dash', function(req, res) { + res.render('dashboard'); +}); + +app.use('/stats', stats); + // 可以将一类的路由单独保存在一个文件中 app.use('/todos', todos); diff --git a/routes/stats.js b/routes/stats.js new file mode 100644 index 000000000..ffa9237c1 --- /dev/null +++ b/routes/stats.js @@ -0,0 +1,43 @@ +var router = require('express').Router(); +var AV = require('leanengine'); + +router.get('/', function(req, res, next) { + var from = new Date(req.query.from); + if (req.query.from && from.getYear() > 2000) { + } else { + from = new Date(); + from.setHours(from.getHours() - 48); // default to 48 hours ago + } + var to = new Date(req.query.to); + if (req.query.to && to.getYear() > from.getYear()) { + } else { + to = new Date(); + } + + from.setHours(from.getHours() - 48); // 48 hours ago + AV.Query.doCloudQuery( + "SELECT type, description, start, delta \ + FROM EchoTime \ + WHERE start > date(?) AND start < date(?) ORDER BY start", + [from.toISOString(), to.toISOString()], + { + success: function(result) { + var rows = []; + for (var i=0; i < result.results.length; i++) { + var echo = result.results[i]; + rows[i] = { + type: echo.get('type'), + description: echo.get('description'), + start: echo.get('start'), + delta: echo.get('delta'), + createdAt: echo.getCreatedAt(), + }; + } + res.json(rows); + }, error: function(err) { + console.log(err); + next(err); + }}); +}); + +module.exports = router;