Skip to content

Commit

Permalink
Providing stats at /stats
Browse files Browse the repository at this point in the history
  • Loading branch information
juvenn committed Sep 4, 2015
1 parent 7551a91 commit b9e092c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
43 changes: 43 additions & 0 deletions routes/stats.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit b9e092c

Please sign in to comment.