-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
124 lines (108 loc) · 3.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var express = require('express'),
app = express(),
_ = require('lodash'),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server),
EventEmitter = require('events').EventEmitter,
config = require('./server/config'),
schedule = require('node-schedule'),
DateUtil = require('./server/dateUtil').DateUtil,
KanbanItemDataMassager = require('./server/kanbanItemDataMassager').KanbanItemDataMassager,
KanbanStorage = require('./server/kanbanStorage'),
kanbanProvider = require('./server/dataprovider/' + config.kanbanProvider);
app.configure(function() {
app.use(express.urlencoded());
app.use(express.json());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/client/'));
app.use(app.router);
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
server.listen(config.port);
app.get('/', function(req, res) {
res.sendfile(__dirname + '/client/cfd/cfd.html');
});
function responseConstructor(res, isSuccess, result) {
var responseData = {
status: isSuccess,
errMsg: isSuccess ? '' : result.message,
result: result
};
if (isSuccess) {
res.json(200, responseData);
} else {
res.json(500, responseData);
}
}
var kanbanStorage = new KanbanStorage();
app.post('/initHistoricalData', function(req, res) {
var startDate = DateUtil.getDate(req.param('startDate'));
kanbanProvider.getHistoricalKanbanStatus(startDate)
.then(function(kanbanItems) {
return kanbanStorage.initHistoricalData(kanbanItems, startDate);
})
.then(function(result) {
responseConstructor(res, true, result);
})
.catch(responseConstructor.bind(this, res, false));
});
app.get('/snapshot', function(req, res) {
var startDate = DateUtil.getDate(req.param('startDate')),
endDate = DateUtil.getDate(req.param('endDate'));
kanbanStorage.getSnapshot({
startDate: startDate,
endDate: endDate
})
.then(responseConstructor.bind(this, res, true))
.catch(responseConstructor.bind(this, res, false));
});
app.get('/itemDetail', function(req, res) {
var startDate = DateUtil.getDate(req.param('startDate')),
endDate = DateUtil.getDate(req.param('endDate'));
kanbanStorage.getItems({
startDate: startDate,
endDate: endDate
})
.then(function(items) {
return _.map(items, function(item) {
item.blockLog = KanbanItemDataMassager.massageBlockLog(item);
return item;
});
})
.then(responseConstructor.bind(this, res, true))
.catch(responseConstructor.bind(this, res, false));
});
var statusEmitter = new EventEmitter();
statusEmitter.on('error', function(err) {
console.error('Something wrong happened.');
console.error(err);
});
// Run on scheduled time to collect today's kanban item status
var rule = new schedule.RecurrenceRule();
if (config.ignoreWeekend) {
rule.dayOfWeek = [new schedule.Range(1, 5)]; // Just week day
}
rule.hour = [config.dataCollectTime];
rule.minute = 0;
schedule.scheduleJob(rule, function() {
// Only need to capture the change happen today as it is supposed that
// previous item detail has described the Kanban status correctly.
kanbanProvider.getHistoricalKanbanStatus()
.then(function(kanbanItems) {
return kanbanStorage.initHistoricalData(kanbanItems, DateUtil.getDate());
})
.then(function(result) {
statusEmitter.emit('dailyUpdate', result);
return result;
})
.catch(function(e) {
console.log('Daily capture failed.');
console.log(e);
});
});
io.sockets.on('connection', function(socket) {
statusEmitter.on('dailyUpdate', function(snapshots) {
socket.emit('dailyUpdate', snapshots);
});
});