f1vis/js/queries.js

180 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
/*
* This file defines functions that create d3 datasets from the rawData provided by preprocessor.getResults()
*/
var queries = {
/*
* Count drivers belonging to nationalities
*/
getDriversByNationality: function() {
var rawData = preprocessor.getResults();
2017-12-13 00:12:11 +01:00
// Extract interesting data from raw data.
var nationalities = {};
for(var i in rawData.drivers) {
var d = rawData.drivers[i];
var nationality = d["nationality"];
if(nationalities[nationality] === undefined) {
nationalities[nationality] = 1;
} else {
nationalities[nationality] += 1;
}
}
// 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;
},
getStatusById: function(statusId){
var rawData = preprocessor.getResults();
return rawData.drivers[driverId];
},
2017-12-08 12:44:34 +01:00
getDriverById: function(driverId) {
var rawData = preprocessor.getResults();
return rawData.drivers[driverId];
},
2017-12-08 12:44:34 +01:00
getDriversByRaceId: function(raceId) {
var rawData = preprocessor.getResults();
2017-12-08 12:44:34 +01:00
var result = queries.getResultsByRaceId(raceId).map((cur) => queries.getDriverById(cur.driverId));
2017-12-08 12:44:34 +01:00
return result;
},
getResultsByRaceId: function(raceId) {
2017-12-08 12:44:34 +01:00
var rawData = preprocessor.getResults();
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
tempList.sort((o1,o2) => o1["positionOrder"] - o2["positionOrder"]);
2017-12-08 12:44:34 +01:00
return tempList;
},
2017-12-08 12:44:34 +01:00
getPitStopsByRaceId: function(raceId) {
var rawData = preprocessor.getResults();
var tempList = [];
for(var key in rawData.pitStops){
2017-12-29 20:46:13 +01:00
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"]);
//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();
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();
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-08 12:44:34 +01:00
myMap.forEach((d,i) => d.sort((o1,o2) => o1["position"] - o2["position"]));
return myMap;
},
getRaceById: function(raceId){
var rawData = preprocessor.getResults();
for(var key in rawData.races){
if(rawData.races[key].raceId == raceId){
return rawData.races[key];
}
}
return null;
},
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
},
getSeasons: function(){
var rawData = preprocessor.getResults();
return rawData.seasons;
},
getLapTimes: function(){
var rawData = preprocessor.getResults();
return rawData.lapTimes;
},
getRaces: function(){
var rawData = preprocessor.getResults();
return rawData.races;
},
getStatus: function(){
var rawData = preprocessor.getResults();
return rawData.status;
},
getRaceIdWithLapTimes: function(){
return queries.getLapTimes().map(time => time.raceId).reduce(removeDuplicates,[]);
},
2017-12-08 12:44:34 +01:00
getQualifingDataByRaceId: function(raceId) {
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;
}
};