2017-11-18 12:49:33 +01:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-19 14:57:53 +01:00
|
|
|
/*
|
|
|
|
* This file defines functions that create d3 datasets from the rawData provided by preprocessor.getResults()
|
|
|
|
*/
|
|
|
|
|
2017-11-18 12:49:33 +01:00
|
|
|
var queries = {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count drivers belonging to nationalities
|
|
|
|
*/
|
|
|
|
getDriversByNationality: function() {
|
|
|
|
var rawData = preprocessor.getResults();
|
2017-12-13 00:12:11 +01:00
|
|
|
|
2017-11-18 12:49:33 +01:00
|
|
|
// Extract interesting data from raw data.
|
|
|
|
var nationalities = {};
|
2017-12-08 12:53:55 +01:00
|
|
|
for(var i in rawData.drivers) {
|
|
|
|
var d = rawData.drivers[i];
|
2017-11-18 12:49:33 +01:00
|
|
|
var nationality = d["nationality"];
|
|
|
|
if(nationalities[nationality] === undefined) {
|
|
|
|
nationalities[nationality] = 1;
|
|
|
|
} else {
|
|
|
|
nationalities[nationality] += 1;
|
|
|
|
}
|
2017-12-08 12:53:55 +01:00
|
|
|
}
|
2017-11-18 12:49:33 +01:00
|
|
|
|
|
|
|
// Transform into d3 dataset format
|
|
|
|
var dataset = [];
|
|
|
|
var n;
|
|
|
|
for(n in nationalities) {
|
|
|
|
dataset.push({
|
|
|
|
label: n,
|
|
|
|
count: nationalities[n]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return d3 dataset
|
|
|
|
return dataset;
|
|
|
|
},
|
|
|
|
|
2017-12-08 12:44:34 +01:00
|
|
|
getDriverById: function(driverId) {
|
2017-12-01 14:04:32 +01:00
|
|
|
var rawData = preprocessor.getResults();
|
2017-12-08 13:37:04 +01:00
|
|
|
return rawData.drivers[driverId];
|
|
|
|
},
|
2017-12-08 12:44:34 +01:00
|
|
|
|
2017-12-08 13:37:04 +01:00
|
|
|
getDriversByRaceId: function(raceId) {
|
|
|
|
var rawData = preprocessor.getResults();
|
2017-12-08 12:44:34 +01:00
|
|
|
|
2017-12-08 13:37:04 +01:00
|
|
|
var result = queries.getResultsByRaceId(raceId).map((cur) => queries.getDriverById(cur.driverId));
|
2017-12-08 12:44:34 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2017-12-08 13:37:04 +01:00
|
|
|
getResultsByRaceId: function(raceId) {
|
2017-12-08 12:44:34 +01:00
|
|
|
var rawData = preprocessor.getResults();
|
|
|
|
|
2017-12-08 13:37:04 +01:00
|
|
|
var tempList = [];
|
|
|
|
for(var key in rawData.results){
|
|
|
|
if(rawData.results[key].raceId == raceId){
|
|
|
|
tempList.push(rawData.results[key]);
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 12:44:34 +01:00
|
|
|
|
2017-12-08 13:37:04 +01:00
|
|
|
tempList.sort((o1,o2) => o1["positionOrder"] - o2["positionOrder"]);
|
2017-12-08 12:44:34 +01:00
|
|
|
|
2017-12-08 13:37:04 +01:00
|
|
|
return tempList;
|
|
|
|
},
|
2017-12-08 12:44:34 +01:00
|
|
|
|
2017-12-08 13:37:04 +01:00
|
|
|
getPitStopsByRaceId: function(raceId) {
|
|
|
|
var rawData = preprocessor.getResults();
|
|
|
|
|
|
|
|
var tempList = [];
|
|
|
|
for(var key in rawData.pitStops){
|
|
|
|
if(rawData.pitStops[key].raceId == raceId){
|
|
|
|
tempList.push(rawData.pitStops[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:00:44 +01:00
|
|
|
tempList.sort((o1,o2) => o1["stop"] - o2["stop"]);
|
2017-12-08 13:37:04 +01:00
|
|
|
//var tempList = rawData.pitStops.filter(cur => cur.raceId == raceId);
|
|
|
|
|
|
|
|
return tempList;
|
2017-12-08 12:44:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getLapDataByRaceId: function(raceId) {
|
|
|
|
var rawData = preprocessor.getResults();
|
2017-12-08 13:37:04 +01:00
|
|
|
|
|
|
|
var lapTimes = [];
|
|
|
|
for(var key in rawData.lapTimes){
|
|
|
|
if(rawData.lapTimes[key].raceId == raceId){
|
|
|
|
lapTimes.push(rawData.lapTimes[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 12:44:34 +01:00
|
|
|
var myMap = new Map();
|
2017-12-01 14:04:32 +01:00
|
|
|
lapTimes.forEach(function(d,i) {
|
|
|
|
var lapNum = d["lap"];
|
|
|
|
var driverId = d["driverId"];
|
2017-12-08 12:44:34 +01:00
|
|
|
if(!myMap.has(lapNum)){
|
|
|
|
var data = [];
|
|
|
|
myMap.set(lapNum,data);
|
|
|
|
}
|
|
|
|
var dt = myMap.get(lapNum);
|
2017-12-13 00:12:11 +01:00
|
|
|
dt.push(d);
|
2017-12-08 12:44:34 +01:00
|
|
|
myMap.set(lapNum,dt);
|
2017-12-01 14:04:32 +01:00
|
|
|
});
|
2017-12-08 12:44:34 +01:00
|
|
|
|
|
|
|
myMap.forEach((d,i) => d.sort((o1,o2) => o1["position"] - o2["position"]));
|
|
|
|
|
|
|
|
return myMap;
|
|
|
|
},
|
|
|
|
|
2017-12-08 13:50:59 +01:00
|
|
|
getRaceById: function(raceId){
|
|
|
|
var rawData = preprocessor.getResults();
|
2017-12-13 00:12:11 +01:00
|
|
|
return rawData.races[raceId];
|
2017-12-08 13:50:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getRacesByYear: function(year){
|
|
|
|
var rawData = preprocessor.getResults();
|
|
|
|
|
|
|
|
var races = [];
|
|
|
|
for(var key in rawData.races){
|
|
|
|
if(rawData.races[key].year == year){
|
|
|
|
races.push(rawData.races[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
races.sort((o1,o2) => o1["round"] - o2["round"]);
|
|
|
|
|
|
|
|
return races;
|
2017-12-13 00:12:11 +01:00
|
|
|
|
2017-12-08 13:50:59 +01:00
|
|
|
},
|
|
|
|
|
2017-12-08 12:44:34 +01:00
|
|
|
getQualifingDataByRaceId: function(raceId) {
|
2017-12-08 13:37:04 +01:00
|
|
|
var rawData = preprocessor.getResults();
|
|
|
|
|
|
|
|
var qualifingData = [];
|
|
|
|
for(var key in rawData.qualifying){
|
|
|
|
if(rawData.qualifying[key].raceId == raceId){
|
|
|
|
qualifingData.push(rawData.qualifying[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//var result = rawData.qualifying.filter((cur) => (cur.raceId == raceId));
|
|
|
|
qualifingData.sort((o1,o2) => o1["position"] - o2["position"]);
|
|
|
|
return qualifingData;
|
2017-12-01 14:04:32 +01:00
|
|
|
}
|
|
|
|
|
2017-11-18 12:49:33 +01:00
|
|
|
};
|