-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
350 lines (331 loc) · 9.59 KB
/
app.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var express = require("express");
var app = express();
var ErgastClient = require("ergast-client");
var WikiClient = require("wikipedia-js");
// makes use of ergast node js client wrapper for fetching data from Ergast developer Api's
var ergast = new ErgastClient();
var bodyParser = require("body-parser");
var async = require("async");
var fs = require("fs");
bodyParser.urlencoded({
extended: "true"
});
app.use(bodyParser.json());
var port = 8081 || process.env.PORT;
var hostname = "127.0.0.1";
/* set this to true if there is a problem in fetching data from Ergast.
* if true the data for lap charts is fetched from static prefetched json files
* This data is only available for 2017 season.
*/
const fetchStatic = true;
//use static middleware to serve static content.
app.use(express.static(__dirname + "/bower_components"));
app.use(express.static(__dirname + "/public"));
app.listen(port, hostname, function() {
console.log("Up an running... at", hostname + ":" + port);
});
/**
* Get Schedule for a season.
*/
app.get("/season/:seasonid", function(request, response) {
var year = request.params.seasonid;
var data = {};
var latitudes = [];
var longitudes = [];
var images = [];
var races = [];
var wikiUrl = "";
/**
* SVG path for target icon
*/
var targetSVG =
"M9,0C4.029,0,0,4.029,0,9s4.029,9,9,9s9-4.029,9-9S13.971,0,9,0z M9,15.93 c-3.83,0-6.93-3.1-6.93-6.93S5.17,2.07,9,2.07s6.93,3.1,6.93,6.93S12.83,15.93,9,15.93 M12.5,9c0,1.933-1.567,3.5-3.5,3.5S5.5,10.933,5.5,9S7.067,5.5,9,5.5 S12.5,7.067,12.5,9z";
ergast.getSeason(year, function(err, season) {
races = season.races;
races.map(function(race) {
var imageDetails = {};
imageDetails.info = {};
var location = race.circuit.location;
imageDetails.type = "circle";
imageDetails.color = "#000000";
imageDetails.title = race.raceName + "," + race.circuit.circuitName;
imageDetails.grandPrixUrl = race.url;
imageDetails.circuitUrl = race.circuit.url;
imageDetails.latitude = location.lat;
imageDetails.longitude = location.long;
imageDetails.selectable = true;
imageDetails.info.id = race.raceName + "," + race.circuit.circuitName;
imageDetails.info.raceUrl = race.url;
imageDetails.info.circuitUrl = race.circuit.url;
wikiUrl = race.url;
imageDetails.info.round = race.round;
imageDetails.info.date = race.date;
imageDetails.info.time = race.time;
latitudes.push(location.lat);
longitudes.push(location.long);
images.push(imageDetails);
});
data.latitudes = latitudes;
data.longitudes = longitudes;
data.images = images;
response.send(data);
});
});
/**
* Get Drivers Data for a Season,right now hard coded to 2018
*/
app.get("/drivers", function(request, response) {
ergast.getDrivers("2018", function(request, driverList) {
response.send(driverList);
});
});
/**
* Get lap chart data for a round in a season.
*/
app.get("/getLapChartData/:seasonid/:round", function(request, response) {
var MECHANICAL_PROBLEMS_STATUSES = [
"Clutch",
"Electrical",
"Engine",
"Gearbox",
"Hydraulics",
"Transmission",
"Suspension",
"Brakes",
"Mechanical",
"Tyre",
"Puncture",
"Wheel",
"Heat shield fire",
"Oil leak",
"Water leak",
"Wheel nut",
"Rear wing",
"Engine misfire",
"Vibrations",
"Alternator",
"Collision damage",
"Pneumatics",
"Fuel system",
"Technical",
"Oil pressure",
"Drivetrain",
"Turbo",
"ERS",
"Power Unit",
"Water pressure",
"Fuel pressure",
"Throttle",
"Steering",
"Electronics",
"Exhaust",
"Retired",
"Withdrew",
"Power loss"
];
var ACCIDENT_STATUSES = ["Accident", "Collision", "Spun off"];
var season = request.params.seasonid;
var round = request.params.round;
/**
*
* 1.Get ergastRaceResults, ergastDrivers, ergastLaps, ergastPitStops
* 2.Send the data to chartConverter.
*/
var data = {};
var tasks = {
raceResults: function(callback) {
setTimeout(function() {
ergast.getRaceResults(season, round, function(error, raceData) {
if (!error) {
callback(null, raceData);
} else {
callback(error);
}
});
}, 100);
},
raceDrivers: function(callback) {
setTimeout(function() {
ergast.getDrivers(season, function(error, driverData) {
if (!error) {
callback(null, driverData);
} else {
callback(error);
}
});
}, 200);
},
raceLaps: function(callback) {
setTimeout(function() {
ergast.getLaps(season, round, function(error, lapData) {
if (!error) {
callback(null, lapData);
} else {
callback(error);
}
});
}, 300);
},
racePitStops: function(callback) {
setTimeout(function() {
ergast.getPitStops(season, round, function(error, pitStopData) {
if (!error) {
callback(null, pitStopData);
} else {
callback(error);
}
});
}, 400);
}
};
if (fetchStatic) {
var responseData;
setTimeout(function() {
fs.readFile(
__dirname + "/data/" + season + "/" + round + ".json",
function(err, data) {
if (err) {
throw err;
}
response.send(JSON.parse(data));
}
);
}, 2000);
} else {
async.parallel(async.reflectAll(tasks), function(error, results) {
data = results;
convertToChartData(data);
});
}
/**
* Parse Lap Chart Data from Ergast
* @param {lap chart data for a round in a season} data
*/
function convertToChartData(data) {
/**
*
* Create lapCount field.
* Create laps field - [{"name":Driver Name,
* "placing":[//plcaing array],
* "pitstops":[//pitstops array],
* "mechanical":[//mech failure],
* "accident":[//accident array],
* "lapped":[//lapped array],
* "safety":[//saftey car array]}]
*
*/
var result = [];
var lapsData = [];
var pitStopData = [];
var resultObj = {};
resultObj.lapCount = data.raceLaps.value.length;
resultObj.laps = [];
resultObj.safety = [];
resultObj.lapped = [];
driversData = getLapsData(data.raceResults.value.driverResults, resultObj);
lapsData = getRacePositions(data.raceLaps.value, driversData);
pitStopsData = getPitStopsInfo(
data.racePitStops.value,
data.raceDrivers.value.drivers,
lapsData
);
finalData = setRaceStatus(
data.raceResults.value.driverResults,
pitStopsData
);
response.send(finalData);
}
/**
* Populate Laps Data to result
* @param {Race results JSON from Ergast} raceResults
* @param {Populate result} result
*/
function getLapsData(raceResults, result) {
var gridPosZero = 0;
raceResults.forEach(function(driverResult, i) {
var driverObj = {};
driverObj.driverId = driverResult.driver.driverId;
driverObj.name =
driverResult.driver.givenName + " " + driverResult.driver.familyName;
driverObj.placing = [];
if (driverResult.grid === 0) {
driverResult.grid = raceResults.length - gridPosZero;
gridPosZero++;
}
driverObj.placing.push(driverResult.grid);
driverObj.pitstops = [];
driverObj.mechanical = [];
driverObj.accident = [];
driverObj.disqualified = [];
result.laps.push(driverObj);
});
return result;
}
/**
* Populate Race Positions to result
* @param {Laps Infor from Ergast} raceLaps
* @param {Result to be populated} result
*/
function getRacePositions(raceLaps, result) {
raceLaps.forEach(function(raceLap) {
raceLap.laps.forEach(function(lapData) {
result.laps.find(function(lap, i) {
if (lap.driverId === lapData.driverId) {
result.laps[i].placing.push(lapData.position);
}
});
});
});
return result;
}
/**
* Populate Pit Stop Information to result
* @param {Pit Stops Information from Ergast} racePitStops
* @param {Drivers Infromation from Ergast} raceDrivers
* @param {Result to be Populated} result
*/
function getPitStopsInfo(racePitStops, raceDrivers, result) {
racePitStops.pitStops.forEach(function(pitData) {
result.laps.find(function(driverData, i) {
if (pitData.driverId === driverData.driverId) {
result.laps[i].pitstops.push(pitData.lap);
}
});
});
return result;
}
/**
* Set Race Status for each Driver
* @param {results JSON from Ergast} raceResults
* @param {populate result to send to client} result
*/
function setRaceStatus(raceResults, result) {
raceResults.forEach(function(raceResult) {
result.laps.find(function(driverData, i) {
if (
driverData.driverId === raceResult.driver.driverId &&
MECHANICAL_PROBLEMS_STATUSES.indexOf(raceResult.status) !== -1
) {
result.laps[i].mechanical.push(raceResult.laps);
}
if (
driverData.driverId === raceResult.driver.driverId &&
ACCIDENT_STATUSES.indexOf(raceResult.status) !== -1
) {
result.laps[i].accident.push(raceResult.laps);
}
if (
driverData.driverId === raceResult.driver.driverId &&
raceResult.status === "Disqualified"
) {
result.laps[i].disqualified.push(raceResult.laps);
}
});
});
return result;
}
});
app.get("*", function(req, res) {
// body...
res.sendFile("/public/index.html");
});