Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,28 @@ router.get('/', (req, res) => {
}
db.collection('users').findOne({ id: req.user.id }, (error, result) => {
let logins = 0;
let metricUnits = true;

if (result === null) {
const newUserObj = req.user._json; // eslint-disable-line
newUserObj.ridesFilter = false;
newUserObj.ridesFilter = 'false';
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed as part of the PR

newUserObj.logins = 0;
newUserObj.api = {};

db.collection('users').insertOne(newUserObj, () => {
});
} else if (result.logins !== undefined) {
logins = result.logins;
} else {
if (result.logins !== undefined) {
logins = result.logins;
}
if (result.metricUnits !== undefined) {
metricUnits = result.metricUnits;
}
}
const newVal = { $set: { logins: logins + 1 } };
const newVal = { $set: { logins: logins + 1, metricUnits } };
req.session.metricUnits = metricUnits; // store session-wide
req.session.save();

db.collection('users').updateOne({ id: req.user.id }, newVal, () => {
db.close();
});
Expand Down
6 changes: 5 additions & 1 deletion routes/rides.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ router.get('/get/activity', (req, res, next) => {
} else {
requestify.get(`https://www.strava.com/api/v3/activities/${req.query.id}?access_token=${req.user.accessToken}`).then((response) => {
const activityDetails = JSON.parse(response.body);
activityDetails.metricUnits = req.session.metricUnits;
res.send(activityDetails);
});
}
Expand Down Expand Up @@ -60,6 +61,9 @@ router.get('/get/activities', (req, res, next) => {
if (result.ridesFilter === 'true') {
activities = activities.filter(x => x.type === 'Ride');
}
activities.forEach((activity) => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping response object and adding this as a single attribute is more ideal than redundancy

activity.metricUnits = req.session.metricUnits;
});
res.json(activities);
});
});
Expand All @@ -72,7 +76,7 @@ router.get('/', (req, res) => {
if (!req.isAuthenticated()) {
res.redirect('/');
} else {
res.render('rides', { title: 'Wind Analysis - Rides' });
res.render('rides', { title: 'Wind Analysis - Rides', metricUnits: req.session.metricUnits === 'true' });
}
});

Expand Down
17 changes: 13 additions & 4 deletions routes/segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ router.get('/get/activity', (req, res, next) => {
}
segments.push({ name: segmentData.name,
id: segmentData.id,
distance: (segmentData.distance / 1000).toFixed(2),
distance_metric: (segmentData.distance / 1000).toFixed(2),
distance_imperial: ((segmentData.distance / 1000) * 0.621371).toFixed(2), // #90
average_grade: segmentData.average_grade,
maximum_grade: segmentData.maximum_grade,
ranking: prRank,
Expand All @@ -49,6 +50,10 @@ router.get('/get/activity', (req, res, next) => {
unselectedFilters.forEach((filter) => {
if (filterMap[filter] !== undefined) {
const f = filterMap[filter];
segments.forEach((segment) => {
const modifiedSegment = segment; // do not modify parameter to lambda function
modifiedSegment.distance = (req.session.metricUnits === 'true') ? segment.distance_metric : segment.distance_imperial;
});
segments = segments.filter(x => f(x));
}
});
Expand All @@ -57,7 +62,7 @@ router.get('/get/activity', (req, res, next) => {
// Resolves @69: Filter virtual rides / resolve Zwift segment pollution on segments selection page
segments = segments.filter(segment => segment.activity_type !== 'Ride');

res.send({ segmentsArr: segments });
res.send({ segmentsArr: segments, metricUnits: req.session.metricUnits });
});
}
});
Expand Down Expand Up @@ -156,13 +161,14 @@ router.get('/details', (req, res) => {
const segmentData = JSON.parse(segmentResponse.body);
segmentData.athleteID = athleteID;
segmentData.distance_raw = segmentData.distance;
segmentData.distance /= 1000;
segmentData.distance = segmentData.distance.toFixed(2);
segmentData.distance = (segmentData.distance / 1000).toFixed(2);
segmentData.distance_imperial = (segmentData.distance * 0.621371).toFixed(2);
segmentData.leaderboard = leaderboard;
segmentData.participants = leaderboardResponse.entry_count;
segmentData.leaderboardLink = `https://www.strava.com/segments/${segmentID}?filter=overall`;
segmentData.latitude = segmentData.start_latlng[0];
segmentData.longitude = segmentData.start_latlng[1];
segmentData.metricUnits = req.session.metricUnits === 'true';
let polyline = '';
for (let x = 0; x < segmentData.map.polyline.length; x += 1) {
if (segmentData.map.polyline.charAt(x) !== '\\') {
Expand Down Expand Up @@ -214,12 +220,15 @@ router.get('/details', (req, res) => {
effort.rank = `${effort.rank}th`;
}
effort.speed = `${(((segmentData.distance_raw * 3.6) / effort.elapsed_time).toFixed(2))}km/h`;
effort.speed_imperial = `${(((segmentData.distance_raw * 2.2369356) / effort.elapsed_time).toFixed(2))}mph`;

darkskydatahandler.getWeatherDetails(config.weatherKey, req.user.id, segmentData.latitude, segmentData.longitude, effort.start_date_iso).then((windData) => {
const date = new Date(effort.start_date_iso);

effort.wind_speed = windData.hourly.data[date.getHours()].windSpeed;
effort.wind_speed_str = effort.wind_speed.toFixed(2);
effort.wind_speed_imperial = effort.wind_speed * 0.621371;
effort.wind_speed_imperial_str = effort.wind_speed_imperial.toFixed(2);


effort.wind_bearing = windData.hourly.data[date.getHours()].windBearing;
Expand Down
31 changes: 28 additions & 3 deletions routes/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ router.get('/get/rides-filter', (req, res) => {
}
});

router.get('/get/metric-units', (req, res) => {
if (!req.isAuthenticated()) {
res.redirect('/');
} else if (req.session.metricUnits !== undefined) {
res.send(req.session.metricUnits);
} else {
MongoClient.connect(config.mongoDBUrl, (err, db) => {
db.collection('users').findOne({ id: req.user.id }, (error, result) => {
if (error) {
console.log(error);
}
db.close();
if (result.metricUnits === undefined) {
res.send(true);
} else {
res.send(result.metricUnits);
}
});
});
}
});

router.put('/put/reset-cache', (req, res) => {
if (!req.isAuthenticated()) {
res.redirect('/');
Expand All @@ -79,12 +101,13 @@ router.put('/put/reset-cache', (req, res) => {
}
});

router.put('/put/rides-filter', (req, res) => {
if (!req.isAuthenticated() || req.query.ridesFilter === undefined) {
// Expects both settings to be supplemented in the query string
router.put('/put/update-settings', (req, res) => {
if (!req.isAuthenticated() || req.query.ridesFilter === undefined || req.query.metricUnits === undefined) {
res.redirect('/');
} else {
MongoClient.connect(config.mongoDBUrl, (err, db) => {
const newVal = { $set: { ridesFilter: req.query.ridesFilter } };
const newVal = { $set: { ridesFilter: req.query.ridesFilter, metricUnits: req.query.metricUnits } };
db.collection('users').updateOne({ id: req.user.id }, newVal, (error) => {
if (error) {
console.log(error);
Expand All @@ -94,6 +117,8 @@ router.put('/put/rides-filter', (req, res) => {
db.close();
});
});
req.session.metricUnits = req.query.metricUnits; // update session-wide metricUnits store
req.session.save();
}
});

Expand Down
51 changes: 36 additions & 15 deletions views/details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@
<ul>
<li> Average Slope: <div class="slab-100" style="display: inline"> {{average_grade}}% </div></li>
<li> Maximum Slope: <div class="slab-100" style="display: inline"> {{maximum_grade}}% </div></li>
<li> Distance: <div class="slab-100" style="display: inline"> {{distance}}km </div></li>
{{#if metricUnits}}
<li> Distance: <div class="slab-100" style="display: inline"> {{distance}}km </div></li>
{{else}}
<li> Distance: <div class="slab-100" style="display: inline"> {{distance_imperial}} miles </div></li>
{{/if}}
<li> Location: <div class="slab-100" style="display: inline"> {{city}}, {{country}} </div></li>
<li> Participants: <div class="slab-100" style="display: inline"> {{participants}} athletes</div></li>
<li> Official Leaderboard: <div class="slab-100" style="display: inline"> <a href="{{leaderboardLink}}" target="_blank">View on Strava </a></div></li>
Expand Down Expand Up @@ -134,20 +138,37 @@
</tr>
</thead>
<tbody class="fw-300">
{{#each leaderboard}}
{{#with this}}
<tr class="fw-100" onclick="showModal({{athlete_id}});">
<td>{{rank}}</td>
<td>{{athlete_name}}</td>
<td>{{start_date}}</td>
<td>{{speed}}</td>
<td>{{wind_speed_str}}km/h</td>
<td>{{wind_bearing_str}}&deg;</td>
<td>{{ride_bearing_str}}&deg;</td>
<td>{{coefficient_str}}</td>
</tr>
{{/with}}
{{/each}}
{{#if metricUnits}}
{{#each leaderboard}}
{{#with this}}
<tr class="fw-100" onclick="showModal({{athlete_id}});">
<td>{{rank}}</td>
<td>{{athlete_name}}</td>
<td>{{start_date}}</td>
<td>{{speed}}</td>
<td>{{wind_speed_str}}km/h</td>
<td>{{wind_bearing_str}}&deg;</td>
<td>{{ride_bearing_str}}&deg;</td>
<td>{{coefficient_str}}</td>
</tr>
{{/with}}
{{/each}}
{{else}}
{{#each leaderboard}}
{{#with this}}
<tr class="fw-100" onclick="showModal({{athlete_id}});">
<td>{{rank}}</td>
<td>{{athlete_name}}</td>
<td>{{start_date}}</td>
<td>{{speed_imperial}}</td>
<td>{{wind_speed_imperial_str}}mph</td>
<td>{{wind_bearing_str}}&deg;</td>
<td>{{ride_bearing_str}}&deg;</td>
<td>{{coefficient_str}}</td>
</tr>
{{/with}}
{{/each}}
{{/if}}
</tbody>
</table>
<div id="influenceContainer">
Expand Down
Loading