File size: 8,856 Bytes
2d12f2a
 
 
 
 
 
f77afce
2d12f2a
 
 
 
 
 
 
f77afce
 
1415d69
f77afce
 
 
 
 
2d12f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4971268
2d12f2a
 
 
 
 
 
 
 
0bd345a
2d12f2a
 
 
 
 
 
 
 
 
 
 
0bd345a
2d12f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bd345a
2d12f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bd345a
2d12f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datetime
import os

import fastf1
import pandas as pd
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse
from pydantic import BaseModel

import available_data

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/", response_model=None)
async def root():
    return HTMLResponse(
        content="""<iframe src="https://tracinginsights-f1-analysis.hf.space" frameborder="0" style="width:100%; height:100%;" scrolling="yes" allowfullscreen:"yes"></iframe>""",
        status_code=200)

@app.get("/years", response_model=None)
def years_available() -> any:
    # make a list from 2018 to current year
    current_year = datetime.datetime.now().year
    years = list(range(2018, current_year+1))
    # reverse the list to get the latest year first
    years.reverse()
    years = [{"label": str(year), "value": year} for year in years]
    return {"years": years}


# format for events {"events":[{"label":"Saudi Arabian Grand Prix","value":2},{"label":"Bahrain Grand Prix","value":1},{"label":"Pre-Season Testing","value":"t1"}]}


@app.get("/{year}", response_model=None)
def events_available(year: int) -> any:
    # get events available for a given year
    data = available_data.LatestData(year)
    events = data.get_events()
    events = [{"label": event, "value": event} for i, event in enumerate(events)]
    events.reverse()

    return {"events": events}

# format for sessions {"sessions":[{"label":"FP1","value":"FP1"},{"label":"FP2","value":"FP2"},{"label":"FP3","value":"FP3"},{"label":"Qualifying","value":"Q"},{"label":"Race","value":"R"}]}


@app.get("/{year}/{event}", response_model=None)
def sessions_available(year: int, event: str | int) -> any:
    # get sessions available for a given year and event
    data = available_data.LatestData(year)
    sessions = data.get_sessions(event)
    sessions = [{"label": session, "value": session} for session in sessions]

    return {"sessions": sessions}

# format for drivers {"drivers":[{"color":"#fff500","label":"RIC","value":"RIC"},{"color":"#ff8700","label":"NOR","value":"NOR"},{"color":"#c00000","label":"VET","value":"VET"},{"color":"#0082fa","label":"LAT","value":"LAT"},{"color":"#787878","label":"GRO","value":"GRO"},{"color":"#ffffff","label":"GAS","value":"GAS"},{"color":"#f596c8","label":"STR","value":"STR"},{"color":"#787878","label":"MAG","value":"MAG"},{"color":"#0600ef","label":"ALB","value":"ALB"},{"color":"#ffffff","label":"KVY","value":"KVY"},{"color":"#fff500","label":"OCO","value":"OCO"},{"color":"#0600ef","label":"VER","value":"VER"},{"color":"#00d2be","label":"HAM","value":"HAM"},{"color":"#ff8700","label":"SAI","value":"SAI"},{"color":"#00d2be","label":"BOT","value":"BOT"},{"color":"#960000","label":"GIO","value":"GIO"}]}


@app.get("/{year}/{event}/{session}", response_model=None)
def session_drivers(year: int, event: str | int, session: str) -> any:
    # get drivers available for a given year, event and session
    f1session = fastf1.get_session(year, event, session)
    f1session.load(telemetry=False, weather=False, messages=False)
    laps = f1session.laps
    team_colors = available_data.team_colors(year)
    # add team_colors dict to laps on Team column

    drivers = laps.Driver.unique()
    # for each driver in drivers, get the Team column from laps and get the color from team_colors dict
    drivers = [{"color": team_colors[laps[laps.Driver ==
                                          driver].Team.iloc[0]], "label": driver, "value": driver} for driver in drivers]

    return {"drivers": drivers}


# format for chartData {"chartData":[{"lapnumber":1},{
    # "VER":91.564,
    # "VER_compound":"SOFT",
    # "VER_compound_color":"#FF5733",
    # "lapnumber":2
    # },{"lapnumber":3},{"VER":90.494,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":4},{"lapnumber":5},{"VER":90.062,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":6},{"lapnumber":7},{"VER":89.815,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":8},{"VER":105.248,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":9},{"lapnumber":10},{"VER":89.79,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":11},{"VER":145.101,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":12},{"lapnumber":13},{"VER":89.662,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":14},{"lapnumber":15},{"VER":89.617,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":16},{"lapnumber":17},{"VER":140.717,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":18}]}


@app.get("/{year}/{event}/{session}/{driver}", response_model=None)
def laps_data(year: int, event: str | int, session: str, driver: str) -> any:

    # get drivers available for a given year, event and session
    f1session = fastf1.get_session(year, event, session)
    f1session.load(telemetry=False, weather=False, messages=False)
    laps = f1session.laps
    team_colors = available_data.team_colors(year)
    # add team_colors dict to laps on Team column

    drivers = laps.Driver.unique()
    # for each driver in drivers, get the Team column from laps and get the color from team_colors dict
    drivers = [{"color": team_colors[laps[laps.Driver ==
                                          driver].Team.iloc[0]], "label": driver, "value": driver} for driver in drivers]

    driver_laps = laps.pick_driver(driver)
    driver_laps['LapTime'] = driver_laps['LapTime'].dt.total_seconds()
    compound_colors = {
        "SOFT": "#FF0000",
        "MEDIUM": "#FFFF00",
        "HARD": "#FFFFFF",
        "INTERMEDIATE": "#00FF00",
        "WET": "#088cd0",

    }

    driver_laps_data = []

    for _, row in driver_laps.iterrows():

        if row['LapTime'] > 0:
            lap = {f"{driver}": row['LapTime'],
                   f"{driver}_compound": row['Compound'],
                   f"{driver}_compound_color": compound_colors[row['Compound']],
                   "lapnumber": row['LapNumber']}
        else:
            lap = {"lapnumber": row['LapNumber']}

        driver_laps_data.append(lap)

    return {"chartData": driver_laps_data}


@app.get("/{year}/{event}/{session}/{driver}/{lap_number}", response_model=None)
def telemetry_data(year: int, event: str | int, session: str, driver: str, lap_number: int) -> any:

    f1session = fastf1.get_session(year, event, session)
    f1session.load(telemetry=True, weather=False, messages=False)
    laps = f1session.laps
    
    driver_laps = laps.pick_driver(driver)
    driver_laps['LapTime'] = driver_laps['LapTime'].dt.total_seconds()

    # get the telemetry for lap_number
    selected_lap = driver_laps[driver_laps.LapNumber == lap_number]

    telemetry = selected_lap.get_telemetry()
    telemetry['Time'] =  telemetry['Time'].dt.total_seconds()
    
    laptime = selected_lap.LapTime.values[0]
    data_key = f"{driver} - Lap {int(lap_number)} - {year} {session} [{int(laptime//60)}:{laptime%60}]"
    
    brake_tel = []
    drs_tel = []
    gear_tel = []
    rpm_tel = []
    speed_tel = []
    throttle_tel = []
    time_tel = []
    track_map = []
    
    for _, row in telemetry.iterrows():
        
        brake = {"x": row['Distance'],
                    "y": row['Brake'],
                    }
        brake_tel.append(brake)        
        
        drs = {"x": row['Distance'],
                    "y": row['DRS'],
                    }
        drs_tel.append(drs)
        
        gear = {"x": row['Distance'],
                    "y": row['nGear'],
                    }
        gear_tel.append(gear)
        
        rpm = {"x": row['Distance'],
                    "y": row['RPM'],
                    }
        rpm_tel.append(rpm)
        
        speed = {"x": row['Distance'],
                    "y": row['Speed'],
                    }
        speed_tel.append(speed)
        
        throttle = {"x": row['Distance'],
                    "y": row['Throttle'],
                    } 
        throttle_tel.append(throttle)
        
        time = {"x": row['Distance'],
                    "y": row['Time'],
                    } 
        time_tel.append(time)
        
        track = {"x": row['X'],
                    "y": row['Y'],
                    } 
        track_map.append(track)
        
    telemetry_data = {
        "telemetryData":{
            "brake": brake_tel,
            "dataKey": data_key,
            "drs": drs_tel,
            "gear": gear_tel,
            "rpm": rpm_tel,
            "speed": speed_tel,
            "throttle": throttle_tel,
            "time": time_tel,
            "trackMap": track_map,
        }
    }
    
    return telemetry_data