-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
222 lines (201 loc) · 8.05 KB
/
server.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var sqlite3 = require('sqlite3').verbose();
var WebSocketServer = require('ws').Server;
app.use(bodyParser.urlencoded({extended: true}));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post('/', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
//db.run("DELETE FROM vid_stats where 1=1");
db.serialize(function () {
var stmt = db.prepare("INSERT OR REPLACE INTO vid_stats" + "(session_id, video_id, time_from, time_to, time_total) VALUES" + "(?, ?, ?, ?, ?)");
var stmt2 = db.prepare("INSERT OR REPLACE INTO sessions (session_id) VALUES (?)");
var stmt3 = db.prepare("INSERT OR REPLACE INTO browsers (name, session_id) VALUES (?,?)");
if (req.body.intervals) {
req.body.intervals.forEach(function (interval) {
stmt.run(req.body.sessionId, req.body.videoId, interval.time_from, interval.time_to, req.body.videoLength);
});
stmt2.run(req.body.sessionId);
stmt3.run(req.body.browserName, req.body.sessionId);
console.log(req.body.browserName);
stmt.finalize();
stmt2.finalize();
stmt3.finalize();
}
});
db.close();
res.send(req.body);
});
app.get('/api', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
var stmt;
if (req.query.multipleLines === 'false') {
stmt = "SELECT vid_stats.*, group_table.time_watched, strftime('%d-%m-%Y', time_added) date_added FROM (SELECT session_id, sum(time_to - time_from) AS time_watched FROM vid_stats GROUP BY session_id) as group_table JOIN vid_stats using (session_id) JOIN sessions ON (vid_stats.session_id = sessions.session_id) GROUP BY sessions.session_id";
}
else {
stmt = "SELECT vid_stats.*, group_table.time_watched, strftime('%d-%m-%Y', time_added) date_added FROM (SELECT session_id, sum(time_to - time_from) AS time_watched FROM vid_stats GROUP BY session_id) as group_table JOIN vid_stats using (session_id) JOIN sessions ON (vid_stats.session_id = sessions.session_id) ";
}
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
var data = all;
console.log(err);
db.close();
res.send(data);
});
});
app.get('/api/counts', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
var stmt = 'SELECT count(DISTINCT session_id) as session_count, count(DISTINCT video_id) as video_count, sum(time_to - time_from) as time_watched, total_time FROM vid_stats JOIN (SELECT sum(time_total) total_time FROM (SELECT DISTINCT session_id, time_total FROM vid_stats))';
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
//console.log(response);
db.close();
res.send(data);
});
});
app.get('/api/browsers', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
var stmt = 'SELECT count(DISTINCT session_id) count, name from browsers GROUP BY name';
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
//console.log(response);
db.close();
res.send(data);
});
});
app.get('/api/sessions', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
if (typeof req.query.sessionId !== 'undefined') {
var stmt = "SELECT * FROM (SELECT session_id, video_id, time_total, sum(time_to - time_from) as time_watched FROM vid_stats GROUP BY session_id) JOIN sessions using (session_id) WHERE session_id = '" + req.query.sessionId + "'";
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
//console.log(response);
db.close();
res.send(data);
});
} else {
var stmt = "SELECT vid_stats.session_id, video_id, GROUP_CONCAT(time_from) as all_time_from, GROUP_CONCAT(time_to) as all_time_to, sum(time_to - time_from) as time_watched, time_added FROM vid_stats JOIN sessions ON (vid_stats.session_id = sessions.session_id) GROUP BY sessions.session_id";
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
db.close();
res.send(data);
});
}
});
app.get('/api/videos/video', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
if (typeof req.query.videoId !== 'undefined') {
var stmt = "SELECT vid_stats.session_id, GROUP_CONCAT(time_from) as all_time_from, GROUP_CONCAT(time_to) as all_time_to FROM vid_stats WHERE video_id='" + req.query.videoId + "' GROUP BY session_id";
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
//console.log(response);
db.close();
res.send(data);
});
}
});
app.get('/api/videos/lastweek', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
var stmt = "SELECT count(DISTINCT sessions.session_id) views, strftime('%d-%m-%Y', time_added) date from vid_stats JOIN sessions ON (vid_stats.session_id = sessions.session_id) WHERE time_added >= date('now', '-7 days') GROUP BY date";
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
//console.log(response);
db.close();
res.send(data);
});
});
app.get('/api/videos/views', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
var stmt = "SELECT count(DISTINCT session_id) views_count, video_id FROM vid_stats GROUP BY video_id";
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
//console.log(response);
db.close();
res.send(data);
});
});
app.get('/api/customquery', function (req, res) {
var db = new sqlite3.Database('databases/stats.db');
var stmt = req.query.customQuery;
var allRecords = function (callback) {
db.all(stmt, function (err, all) {
callback(err, all);
});
};
allRecords(function (err, all) {
console.log(err);
var data = all;
//console.log(response);
db.close();
res.send(data);
});
});
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8888;
var server = app.listen(port, ipaddress, function () {
var host = server.address().address;
console.log((new Date()) + ' app listening at http://%s:%s', host, port)
});
console.log("Server has started.");
wss = new WebSocketServer({
server: server,
autoAcceptConnections: false
});
wss.on('connection', function (ws) {
console.log("New connection");
ws.on('message', function (message) {
ws.send("Received: " + message);
});
ws.send('Welcome!');
});