-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
182 lines (158 loc) · 5.21 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var express = require('express');
var app = express();
var deferred = require('deferred');
var najax = require('najax');
var _ = require('underscore-node');
var SG = require('ml-savitzky-golay');
var Sequelize = require('sequelize');
var CronJob = require('cron').CronJob;
var moment = require('moment');
require('dotenv').config() // load config vars from .env into process.env
var db_url = process.env.DATABASE_URL;
var server_port = process.env.PORT || 4000;
var currentTimezone = moment(new Date()).format('Z');
var sequelize = new Sequelize(process.env.DATABASE_URL, {
timezone: currentTimezone
});
app.secretkey = process.env.SECRET_KEY;
/*
* MODEL: DATAPOINT
* ATTRS: [variance, time, diff]
*/
var DataPoint = sequelize.define('datapoint', {
variance: Sequelize.INTEGER,
time: Sequelize.DATE,
diff: Sequelize.BOOLEAN // diff = true, if the current datapoint's
// story list, differs from the previous datapoint's story list.
});
DataPoint.sync().then(function() {
console.log('datapoints - table ready!');
});
var getClosestSmoothVariance = function(dps, i) {
while (i > 1) {
var current = dps[i];
var prev = dps[i-1];
if (!prev.diff && !current.diff) {
var changeMagnitude = Math.abs(current.variance - prev.variance);
changeMagnitude = changeMagnitude === 0 ? 0 : Math.log(changeMagnitude);
return changeMagnitude;
}
i--;
}
return 0;
};
var smoothenData = function(dps) {
if (! dps.length) {
return [];
}
var varianceDps = [];
for (var i=1; i<dps.length; i++) {
var varianceDp = {
time: dps[i].time,
diff: dps[i].diff
};
varianceDp.variance = getClosestSmoothVariance(dps, i);
varianceDps.push(varianceDp);
}
var sharpSignals = varianceDps.map(function(dp) {
return dp.variance;
});
var smoothSignals = SG(sharpSignals, 1, {derivative: 0});
var varianceDps = varianceDps.map(function(dp, i) {
dp.variance = smoothSignals[i];
return dp;
}).filter(function(dp) {
return dp.variance;
});
return varianceDps;
};
var getLastWeekActivity = function() {
var promises = [], dfd = deferred();
var resp = {
'Sunday': null,
'Monday': null,
'Tuesday': null,
'Wednesday': null,
'Thursday': null,
'Friday': null,
'Saturday': null
};
var today = new Date().setHours(0, 0, 0, 0);
today = moment(today);
var lastSaturday = today.clone().startOf('week').subtract(1, 'day');
for (var i=6; i>=0; i--) {
var day = lastSaturday.clone().subtract(i, 'days');
var dateString = day.format('YYYY-MM-DD');
var querySql = "SELECT * FROM datapoints where DATE(time) = DATE('<dateString>') ORDER BY time desc"
.replace(/\<dateString\>/g, dateString);
promises.push(sequelize.query(querySql, {model: DataPoint}))
}
deferred.apply(deferred, promises).then(function() {
var results = arguments[0];
var resp = {};
_.each(results, function(dps) {
var dps = smoothenData(dps);
var avg = dps.map(function(dp) {
return dp.variance
}).reduce(function(prev, next) {
return prev + next;
}) / dps.length;
var day = moment(dps[0].time).format('dddd');
resp[day] = {average: avg};
});
dfd.resolve(resp);
}).catch(function(){
dfd.resolve();
})
return dfd.promise;
};
// home page
app.get('/', function(req, res) {
res.sendFile('index.html', {
root: __dirname + '/public'
});
});
// serve static files directory
app.use('/static', express.static(process.env.PWD+'/public'));
/*
* stream past 24 hours HN activity data points;
*/
app.get('/variance', function (req, res) {
sequelize.query('SELECT * FROM datapoints WHERE time > current_date - 1 ORDER BY time desc', {model: DataPoint})
.then(function(dps) {
var varianceDps = smoothenData(dps);
// return varianceDps;
return res.json({datapoints: varianceDps});
});
});
/*
* Activity summary for last completed week
*/
app.get('/lastweek', function(req, res) {
getLastWeekActivity().then(function(weekactivity) {
if (!weekactivity) {
weekactivity = {lastWeekActivity: {}};
};
return res.json({lastWeekActivity: weekactivity});
});
});
/*
* ALERT: This end point only ease out resetting data in dev env.
* You probably want to remove this end, before hosting in production.
*/
app.get('/reset/:secretkey', function(req, res) {
// drops all tables. authenticate with a secret key.
var secretkey = req.params.secretkey;
if (secretkey === app.secretkey) {
DataPoint.sync({force: true}).then(function() {
Stat.sync({force: true}).then(function() {
return res.json({'status': 'success'});
});
});
} else {
return res.json({'status': 'failure'});
}
});
app.listen(server_port, '127.0.0.1', function() {
console.log('app listening on port ' + server_port + '!');
});