Skip to content

Commit

Permalink
Add wind chill, snowiness and raininess to riders/distance vs weather
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinorg committed Jan 20, 2025
1 parent 4d66eb6 commit b90b986
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 17 deletions.
4 changes: 2 additions & 2 deletions freezing/web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@
</a>
<ul class="dropdown-menu" aria-labelledby="exploreDropdown">
<li>
<a class="dropdown-item" href="/explore/distance_by_lowtemp">Distance vs. Avg Low Temp</a>
<a class="dropdown-item" href="/explore/distance_by_lowtemp">Distance vs Weather</a>
</li>
<li>
<a class="dropdown-item" href="/explore/riders_by_lowtemp">Riders vs. Avg Low Temp</a>
<a class="dropdown-item" href="/explore/riders_by_lowtemp">Riders vs Weather</a>
</li>
<li>
<a class="dropdown-item" href="/explore/indiv_elev_dist">Individual Dist/Elev/Speed</a>
Expand Down
34 changes: 27 additions & 7 deletions freezing/web/templates/explore/distance_by_lowtemp.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,40 @@
$.ajax({
url: '/chartdata/distance_by_lowtemp',
success: function(data) {
var maxDistance = 0, maxRaininess = 0, maxSnowiness = 0;
for (const {distance, raininess, snowiness} of data.data) {
if (distance > maxDistance) maxDistance = distance;
if (raininess > maxRaininess) maxRaininess = raininess;
if (snowiness > maxSnowiness) maxSnowiness = snowiness;
}
c3.generate({
data: {
x: 'x',
columns: [
['x'].concat(data.data.map(val => new Date(val.date.year, val.date.month - 1, val.date.day))),
['distance'].concat(data.data.map(val => val.distance)),
['low_temp'].concat(data.data.map(val => val.low_temp))
['low_temp'].concat(data.data.map(val => val.low_temp)),
['wind_chill'].concat(data.data.map(val => val.wind_chill)),
['raininess'].concat(data.data.map(val => maxRaininess ? val.raininess * maxDistance / maxRaininess : 0)),
['snowiness'].concat(data.data.map(val => maxSnowiness ? val.snowiness * maxDistance / maxSnowiness : 0)),
],
type: 'bar',
types: {
low_temp: 'spline'
low_temp: 'spline',
wind_chill: 'spline',
raininess: 'spline',
snowiness: 'spline',
},
axes: {
low_temp: 'y2'
low_temp: 'y2',
wind_chill: 'y2',
},
names: {
distance: 'Total Distance',
low_temp: 'Temperature'
low_temp: 'Low Temperature',
wind_chill: 'Wind Chill',
raininess: 'Raininess',
snowiness: 'Snowiness',
},
},
axis: {
Expand Down Expand Up @@ -65,7 +81,11 @@
tooltip: {
format: {
value: (value, ratio, id) =>
Math.round(value) + (id === 'distance' ? ' mi' : '°F')
id === 'raininess' ?
+(value * maxRaininess / maxDistance).toFixed(2) :
id === 'snowiness' ?
+(value * maxSnowiness / maxDistance).toFixed(2) :
Math.round(value) + (id === 'distance' ? '&nbsp;mi' : '°F')
}
}
});
Expand All @@ -76,10 +96,10 @@
{% endblock %}
{% block content %}
<div class="text-center">
<strong>Miles Logged vs. Avg. Low Temp</strong>
<strong>Distance vs Weather</strong>
</div>
<div class="text-center text-muted small">
Relationship between total number of miles logged riders and the average low daily temperatures.
Relationship between total number of miles logged and the ambient weather conditions.
</div>
<!--Div that will hold our leaderboard chart-->
<div id="chart" class="c3-wide">
Expand Down
34 changes: 27 additions & 7 deletions freezing/web/templates/explore/riders_by_lowtemp.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,40 @@
$.ajax({
url: '/chartdata/riders_by_lowtemp',
success: function(data) {
var maxRiders = 0, maxRaininess = 0, maxSnowiness = 0;
for (const {riders, raininess, snowiness} of data.data) {
if (riders > maxRiders) maxRiders = riders;
if (raininess > maxRaininess) maxRaininess = raininess;
if (snowiness > maxSnowiness) maxSnowiness = snowiness;
}
c3.generate({
data: {
x: 'x',
columns: [
['x'].concat(data.data.map(val => new Date(val.date.year, val.date.month - 1, val.date.day))),
['riders'].concat(data.data.map(val => val.riders)),
['low_temp'].concat(data.data.map(val => val.low_temp))
['low_temp'].concat(data.data.map(val => val.low_temp)),
['wind_chill'].concat(data.data.map(val => val.wind_chill)),
['raininess'].concat(data.data.map(val => maxRaininess ? val.raininess * maxRiders / maxRaininess : 0)),
['snowiness'].concat(data.data.map(val => maxSnowiness ? val.snowiness * maxRiders / maxSnowiness : 0)),
],
type: 'bar',
types: {
low_temp: 'spline'
low_temp: 'spline',
wind_chill: 'spline',
raininess: 'spline',
snowiness: 'spline',
},
axes: {
low_temp: 'y2'
low_temp: 'y2',
wind_chill: 'y2',
},
names: {
riders: 'Riders',
low_temp: 'Temperature'
low_temp: 'Temperature',
wind_chill: 'Wind Chill',
raininess: 'Raininess',
snowiness: 'Snowiness',
},
},
axis: {
Expand Down Expand Up @@ -65,7 +81,11 @@
tooltip: {
format: {
value: (value, ratio, id) =>
Math.round(value) + (id === 'riders' ? ' mi' : '°F')
id === 'raininess' ?
+(value * maxRaininess / maxRiders).toFixed(2) :
id === 'snowiness' ?
+(value * maxSnowiness / maxRiders).toFixed(2) :
Math.round(value) + (id === 'riders' ? '' : '°F')
}
}
});
Expand All @@ -76,10 +96,10 @@
{% endblock %}
{% block content %}
<div class="text-center">
<strong>Riders vs. Avg. Low Temp</strong>
<strong>Riders vs Weather</strong>
</div>
<div class="text-center text-muted small">
Relationship between number of participating riders and the average low daily temperatures.
Relationship between number of participating riders and the ambient weather conditions.
</div>
<!--Div that will hold our leaderboard chart-->
<div id="chart" class="c3-wide">
Expand Down
18 changes: 17 additions & 1 deletion freezing/web/views/chartdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,17 @@ def indiv_elev_dist():

@blueprint.route("/riders_by_lowtemp")
def riders_by_lowtemp():
""" """
"""
Snowiness and raininess are in the average inches per hour of snowfall during rides.
A better metric would probably be total rain/snow at DCA on the day, but this is the measure we have.
"""
q = text(
"""
select date(start_date) as start_date,
avg(W.day_temp_min) as low_temp,
avg(W.ride_windchill_avg) as wind_chill,
cast(sum(W.ride_rain) * 3600 / sum(R.moving_time) as float) as raininess,
cast(sum(W.ride_snow) * 3600 / sum(R.moving_time) as float) as snowiness,
count(distinct R.athlete_id) as riders
from rides R join ride_weather W on W.ride_id = R.id
group by date(start_date)
Expand All @@ -907,11 +913,15 @@ def riders_by_lowtemp():
continue
# res['start_date']
dt = res["start_date"]

rows.append(
{
"date": {"year": dt.year, "month": dt.month, "day": dt.day},
"riders": res["riders"],
"low_temp": res["low_temp"],
"wind_chill": res["low_temp"],
"raininess": res["raininess"],
"snowiness": res["snowiness"],
}
)

Expand All @@ -925,6 +935,9 @@ def distance_by_lowtemp():
"""
select date(start_date) as start_date,
avg(W.day_temp_min) as low_temp,
avg(W.ride_windchill_avg) as wind_chill,
cast(sum(W.ride_rain) * 3600 / sum(R.moving_time) as float) as raininess,
cast(sum(W.ride_snow) * 3600 / sum(R.moving_time) as float) as snowiness,
sum(R.distance) as distance
from rides R join ride_weather W on W.ride_id = R.id
group by date(start_date)
Expand All @@ -944,6 +957,9 @@ def distance_by_lowtemp():
"date": {"year": dt.year, "month": dt.month, "day": dt.day},
"distance": res["distance"],
"low_temp": res["low_temp"],
"wind_chill": res["low_temp"],
"raininess": res["raininess"],
"snowiness": res["snowiness"],
}
)

Expand Down

0 comments on commit b90b986

Please sign in to comment.