-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
288 lines (253 loc) · 9.14 KB
/
server.js
File metadata and controls
288 lines (253 loc) · 9.14 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"use strict"
var pg = require("pg");
var _ = require("lodash");
var url = require("url");
var auth = require("http-auth");
var throng = require("throng");
var compression = require("compression");
var constants = require("./analysis-constants.json");
var PORT = process.env.PORT || 5000;
var WORKERS = process.env.WEB_CONCURRENCY || 1;
throng({
workers: WORKERS,
lifetime: Infinity,
start: start
});
function start() {
// Configure and initialize the Postgres connection pool
// Get the DATABASE_URL config var and parse it into its components
var params = url.parse(process.env.HEROKU_POSTGRESQL_COPPER_URL);
var authParams = params.auth.split(":");
var pgConfig = {
user: authParams[0],
password: authParams[1],
host: params.hostname,
port: params.port,
database: params.pathname.split("/")[1],
ssl: true,
max: 16 / WORKERS, // Maximum number of clients in the pool
idleTimeoutMillis: 30000 // Duration a client can remain idle before being closed
};
var pool = new pg.Pool(pgConfig);
// Create an Express server
var express = require("express");
var server = express();
// Add user authentication if AUTHENTICATION isn't set to 'off'
if (process.env.AUTHENTICATION.toLowerCase() !== "off") {
var basic = auth.basic(
{ },
(username, password, callback) => {
callback(username === process.env.AUTHENTICATION_USER && password === process.env.AUTHENTICATION_PASSWORD);
}
);
server.use(auth.connect(basic));
}
// Serve static files, including the Vue application in public/index.html
server.use(express.static("public"));
server.use(express.static("node_modules/vue/dist"));
server.use(express.static("node_modules/vue-router/dist"));
server.use(express.static("node_modules/lodash"));
server.use(compression());
//
// Handle GET request for players api
//
server.get("/api/players/", function(request, response) {
// Create query string
var queryString = "SELECT result1.*, result2.gp"
+ " FROM "
+ " ( "
+ " SELECT s.team, s.player_id, r.first, r.last, r.position, s.score_sit, s.strength_sit,"
+ " SUM(toi) AS toi, SUM(ig) AS ig, SUM(\"is\") AS \"is\", (SUM(\"is\") + SUM(ibs) + SUM(ims)) AS ic, SUM(ia1) AS ia1, SUM(ia2) AS ia2,"
+ " SUM(gf) AS gf, SUM(ga) AS ga, SUM(sf) AS sf, SUM(sa) AS sa, (SUM(sf) + SUM(bsf) + SUM(msf)) AS cf, (SUM(sa) + SUM(bsa) + SUM(msa)) AS ca,"
+ " SUM(cf_off) AS cf_off, SUM(ca_off) AS ca_off "
+ " FROM game_stats AS s"
+ " LEFT JOIN game_rosters AS r"
+ " ON s.player_id = r.player_id AND s.season = r.season AND s.game_id = r.game_id"
+ " WHERE s.player_id > 2 AND r.position <> 'na' AND r.position <> 'g'"
+ " GROUP BY s.team, s.player_id, r.first, r.last, r.position, s.score_sit, s.strength_sit"
+ " ) AS result1"
+ " LEFT JOIN"
+ " ( "
+ " SELECT player_id, COUNT(DISTINCT game_id) AS gp"
+ " FROM game_rosters"
+ " WHERE position != 'na'"
+ " GROUP BY player_id"
+ " ) AS result2"
+ " ON result1.player_id = result2.player_id";
// Run query
var statRows;
query(queryString, [], function(err, rows) {
if (err) { return response.status(500).send("Error running query: " + err); }
statRows = rows;
processResults();
});
// Process query results
function processResults() {
// Postgres aggregate functions like SUM return strings, so cast them as ints
// Calculate score-adjusted corsi
statRows.forEach(function(r) {
["gp", "toi", "ig", "is", "ic", "ia1", "ia2", "gf", "ga", "sf", "sa", "cf", "ca", "cf_off", "ca_off"].forEach(function(col) {
r[col] = +r[col];
});
r["cf_adj"] = constants["cfWeights"][r["score_sit"]] * r["cf"];
r["ca_adj"] = constants["cfWeights"][-1 * r["score_sit"]] * r["ca"];
});
// Group rows by playerId:
// { 123: [rows for player 123], 234: [rows for player 234] }
var groupedRows = _.groupBy(statRows, "player_id");
// Structure results as an array of objects:
// [ { playerId: 123, data: [rows for player 123] }, { playerId: 234, data: [rows for player 234] } ]
var result = { players: [] };
for (var pId in groupedRows) {
if (!groupedRows.hasOwnProperty(pId)) {
continue;
}
// Get all teams and positions the player has been on
var teams = _.uniqBy(groupedRows[pId], "teams").map(function(d) { return d.team; });
var positions = _.uniqBy(groupedRows[pId], "position").map(function(d) { return d.position; });
result["players"].push({
player_id: +pId,
teams: teams,
positions: positions,
first: groupedRows[pId][0]["first"],
last: groupedRows[pId][0]["last"],
gp: groupedRows[pId][0]["gp"],
data: groupedRows[pId]
});
// Set redundant properties in 'data' to be undefined - this removes them from the response
// Setting the properties to undefined is ~10sec faster than deleting the properties completely
result["players"].forEach(function(p) {
p.data.forEach(function(r) {
r.team = undefined;
r.player_id = undefined;
r.first = undefined;
r.last = undefined;
r.position = undefined;
r.gp = undefined;
});
});
}
return response.status(200).send(result);
}
});
//
// Handle GET request for teams api
//
server.get("/api/teams/", function(request, response) {
// Create query string for stats by game
var statQueryString = "SELECT result1.*, result2.gp"
+ " FROM "
+ " ( "
+ " SELECT team, score_sit, strength_sit, SUM(toi) AS toi,"
+ " SUM(gf) AS gf, SUM(ga) AS ga, SUM(sf) AS sf, SUM(sa) AS sa, (SUM(sf) + SUM(bsf) + SUM(msf)) AS cf, (SUM(sa) + SUM(bsa) + SUM(msa)) AS ca"
+ " FROM game_stats"
+ " WHERE player_id < 2 "
+ " GROUP BY team, score_sit, strength_sit"
+ " ) AS result1"
+ " LEFT JOIN"
+ " ( "
+ " SELECT team, COUNT(DISTINCT game_id) AS gp"
+ " FROM game_rosters"
+ " GROUP BY team"
+ " ) AS result2"
+ " ON result1.team = result2.team";
// Create query string for wins and losses - exclude playoff games
var resultQueryString = "SELECT * FROM game_results WHERE game_id < 30000";
// Run queries
var statRows;
var resultRows;
query(statQueryString, [], function(err, rows) {
if (err) { return response.status(500).send("Error running query: " + err); }
statRows = rows;
processResults();
});
query(resultQueryString, [], function(err, rows) {
if (err) { return response.status(500).send("Error running query: " + err); }
resultRows = rows;
processResults();
});
// Process query results
function processResults() {
// Only start processing once all queries are finished
if (!statRows || !resultRows) {
return;
}
// Postgres aggregate functions like SUM return strings, so cast them as ints
// Calculate score-adjusted corsi
statRows.forEach(function(r) {
["gp", "toi", "gf", "ga", "sf", "sa", "cf", "ca"].forEach(function(col) {
r[col] = +r[col];
});
r["cf_adj"] = constants["cfWeights"][r["score_sit"]] * r["cf"];
r["ca_adj"] = constants["cfWeights"][-1 * r["score_sit"]] * r["ca"];
});
// Group rows by team:
// { "edm": [rows for edm], "tor": [rows for tor] }
var groupedRows = _.groupBy(statRows, "team");
//
// Calculate the number of points won
//
// Initialize points counter
for (var tricode in groupedRows) {
if (groupedRows.hasOwnProperty(tricode)) {
groupedRows[tricode]["pts"] = 0;
}
}
// Loop through game_result rows and increment points
resultRows.forEach(function(r) {
if (r["a_final"] > r["h_final"]) {
groupedRows[r["a_team"]].pts += 2;
if (r["periods"] > 3) {
groupedRows[r["h_team"]].pts += 1;
}
} else if (r["h_final"] > r["a_final"]) {
groupedRows[r["h_team"]].pts += 2;
if (r["periods"] > 3) {
groupedRows[r["a_team"]].pts += 1;
}
}
});
// Structure results as an array of objects:
// [ { team: "edm", data: [rows for edm] }, { team: "tor", data: [rows for tor] } ]
var result = { teams: [] };
for (var tricode in groupedRows) {
if (!groupedRows.hasOwnProperty(tricode)) {
continue;
}
result["teams"].push({
team: tricode,
pts: groupedRows[tricode]["pts"],
gp: groupedRows[tricode][0]["gp"],
data: groupedRows[tricode]
});
// Set redundant properties in 'data' to be undefined - this removes them from the response
result["teams"].forEach(function(t) {
t.data.forEach(function(r) {
r.team = undefined;
r.gp = undefined;
});
});
}
return response.status(200).send(result);
}
});
// Start listening for requests
server.listen(PORT, function(error) {
if (error) { throw error; }
console.log("Listening on " + PORT);
});
// Query the database and return result rows in json format
// 'values' is an array of values for parameterized queries
function query(text, values, cb) {
pool.connect(function(err, client, done) {
if (err) { returnError("Error fetching client from pool: " + err); }
client.query(text, values, function(err, result) {
done();
// result.rows is is an array of Anonymous objects
// Convert it to json using stringify and parse before returning it
var returnedRows = err ? [] : JSON.parse(JSON.stringify(result.rows));
cb(err, returnedRows);
});
});
}
}