forked from joelewis/hnlive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (166 loc) · 5.44 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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 currentTimezone = moment(new Date()).format('Z');
console.log(currentTimezone);
var sequelize = new Sequelize(process.env.DATABASE_URL, {
timezone: currentTimezone
});
app.secretkey = process.env.SECRET_KEY;
var maxDataLength = 86400; //12;
// first define the model
var DataPoint = sequelize.define('datapoint', {
variance: Sequelize.INTEGER,
time: Sequelize.DATE,
diff: Sequelize.BOOLEAN
});
var Stat = sequelize.define('daystats', {
average: Sequelize.INTEGER,
day: Sequelize.DATE
});
DataPoint.sync().then(function() {
console.log('datapoints - table ready!');
});
Stat.sync().then(function() {
console.log('daystats - table ready!');
});
// API Ends
var topStoriesAPI = "https://hacker-news.firebaseio.com/v0/topstories.json";
var itemAPI = "https://hacker-news.firebaseio.com/v0/item/{{itemId}}.json";
var algoliaFrontPageAPI = "http://hn.algolia.com/api/v1/search?tags=front_page";
var updateInterval = 60000;
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 dow = {
0: 'Sunday',
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday'
};
var getLastWeekActivity = function() {
var promises = [];
var 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;
};
app.get('/', function(req, res) {
res.sendFile('index.html', {
root: __dirname + '/public'
});
});
app.use('/static', express.static(process.env.PWD+'/public'));
app.get('/variance', function (req, res) {
// stream past 24 hours HN activity data points;
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});
});
});
app.get('/lastweek', function(req, res) {
getLastWeekActivity().then(function(weekactivity) {
if (!weekactivity) {
weekactivity = {lastWeekActivity: {}};
};
return res.json({lastWeekActivity: weekactivity});
});
});
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(process.env.PORT || 4000, '127.0.0.1', function() {
console.log('app listening on port 4000!');
});