-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformula1_race_detail.pp
264 lines (235 loc) · 5.65 KB
/
formula1_race_detail.pp
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
dashboard "formula1_race_detail" {
title = "Formula 1 Race Detail"
documentation = file("./docs/formula1_race_detail.md")
input "race_id" {
title = "Select a Race:"
query = query.formula1_race_input
width = 4
}
container {
card {
query = query.formula1_race_winner
width = 3
type = "info"
args = [self.input.race_id.value]
}
card {
query = query.formula1_race_average_speed
width = 3
type = "info"
args = [self.input.race_id.value]
}
card {
query = query.formula1_race_attendance
width = 3
type = "info"
args = [self.input.race_id.value]
}
card {
query = query.formula1_race_fastest_lap
width = 3
type = "info"
args = [self.input.race_id.value]
}
}
container {
container {
width = 12
table {
title = "Race Overview"
type = "line"
width = 6
query = query.formula1_race_overview
args = [self.input.race_id.value]
}
chart {
title = "Team Points Distribution"
width = 6
type = "pie"
query = query.formula1_race_team_points_distribution
args = [self.input.race_id.value]
}
}
container {
width = 12
table {
title = "Top 10 Constructor Standings"
width = 6
query = query.top_10_formula1_race_constructor_standings
args = [self.input.race_id.value]
}
table {
title = "Top 10 Driver Standings"
width = 6
query = query.top_10_formula1_race_driver_standings
args = [self.input.race_id.value]
}
}
container {
width = 12
table {
title = "Top 10 Race Results"
width = 6
query = query.top_10_formula1_race_results
args = [self.input.race_id.value]
}
}
}
}
# Input query
query "formula1_race_input" {
sql = <<-EOQ
select
name as label,
raceId as value
from
races
order by
name;
EOQ
}
# Card queries
query "formula1_race_winner" {
sql = <<-EOQ
select
'Winner' as label,
drivers.forename || ' ' || drivers.surname as value
from
results
join drivers on results.driverId = drivers.driverId
where
results.raceId = $1
and results.position = 1;
EOQ
}
query "formula1_race_average_speed" {
sql = <<-EOQ
select
'Average Speed' as label,
avg(results.fastestLapSpeed) as value
from
results
where
results.raceId = $1;
EOQ
}
query "formula1_race_attendance" {
sql = <<-EOQ
select
'Attendance' as label,
count(*) as value
from
results
where
results.raceId = $1;
EOQ
}
query "formula1_race_fastest_lap" {
sql = <<-EOQ
select
'No of Laps' as label,
results.laps as value
from
results
where
results.raceId = $1;
EOQ
}
# Other detail page queries
query "formula1_race_overview" {
sql = <<-EOQ
select
races.name as "Race Name",
races.date as "Date",
races.time as "Time",
circuits.name as "Circuit",
circuits.location as "Location",
races.round as "Round",
circuits.country as "Country"
from
races
join circuits on races.circuitId = circuits.circuitId
where
races.raceId = $1;
EOQ
}
query "formula1_race_team_points_distribution" {
sql = <<-EOQ
select
constructors.name as "Constructor",
sum(results.points) as "Total Points"
from
results
join constructors on results.constructorId = constructors.constructorId
where
results.raceId = $1
group by
constructors.name
order by
"Total Points" desc;
EOQ
}
query "top_10_formula1_race_constructor_standings" {
sql = <<-EOQ
select
constructors.name as "Constructor",
constructor_standings.points as "Points",
constructor_standings.position as "Position",
constructor_standings.wins as "Wins"
from
constructor_standings
join constructors on constructor_standings.constructorId = constructors.constructorId
where
constructor_standings.raceId = $1
order by
constructor_standings.position
limit 10;
EOQ
}
query "top_10_formula1_race_driver_standings" {
sql = <<-EOQ
select
drivers.forename || ' ' || drivers.surname as "Driver",
driver_standings.points as "Points",
driver_standings.position as "Position",
driver_standings.wins as "Wins",
avg(laptimes.milliseconds) as "Average Lap Time",
min(laptimes.milliseconds) as "Best Lap Time",
max(laptimes.milliseconds) as "Worst Lap Time"
from
driver_standings
join drivers on driver_standings.driverId = drivers.driverId
join laptimes on driver_standings.driverId = laptimes.driverId
where
driver_standings.raceId = $1
group by
drivers.forename || ' ' || drivers.surname,
driver_standings.points,
driver_standings.position,
driver_standings.wins
order by
driver_standings.position, "Average Lap Time"
limit 10;
EOQ
}
query "top_10_formula1_race_results" {
sql = <<-EOQ
select
results.position as "Position",
drivers.forename || ' ' || drivers.surname as "Driver",
constructors.name as "Constructor",
results.points as "Points",
results.laps as "Laps",
results.time as "Time",
results.statusId as "Status"
from
results
join drivers on results.driverId = drivers.driverId
join constructors on results.constructorId = constructors.constructorId
where
results.raceId = $1
order by
results.position
limit 10;
EOQ
}