From a1068cf77fe3c444480fc37558c40b97d9956277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=A1sz?= <> Date: Sat, 25 Mar 2023 20:21:00 +0100 Subject: [PATCH] Fix type conversion in the curveMetrics Fix the string conversion of the curveMetrics and push the formatting logic to the consumer that require this (eg. WorkoutRecorder). This enables other consumers of curveMetrics to use non-rounded, "raw" data instead of preformatted as well as avoid potential future bugs from the type conversion. --- app/client/components/DashboardForceCurve.js | 4 ++-- app/client/lib/helper.js | 19 +++++++++++++------ app/engine/Rower.js | 6 +++--- app/engine/RowingStatistics.js | 6 +++--- app/engine/WorkoutRecorder.js | 4 ++-- app/engine/utils/curveMetrics.js | 4 ++-- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/client/components/DashboardForceCurve.js b/app/client/components/DashboardForceCurve.js index 74a8b7a..1d064e1 100644 --- a/app/client/components/DashboardForceCurve.js +++ b/app/client/components/DashboardForceCurve.js @@ -39,7 +39,7 @@ export class DashboardForceCurve extends AppElement { datasets: [ { fill: true, - data: this.value?.map((data, index) => ({ y: parseInt(data, 10), x: index })), + data: this.value?.map((data, index) => ({ y: data, x: index })), pointRadius: 1, borderColor: 'rgb(255,255,255)', backgroundColor: 'rgb(220,220,220)' @@ -113,7 +113,7 @@ export class DashboardForceCurve extends AppElement { render () { if (this._chart?.data) { - this._chart.data.datasets[0].data = this.value?.map((data, index) => ({ y: parseInt(data, 10), x: index })) + this._chart.data.datasets[0].data = this.value?.map((data, index) => ({ y: data, x: index })) this.forceCurve = this.value this._chart.update() } diff --git a/app/client/lib/helper.js b/app/client/lib/helper.js index e4fab7c..444de37 100644 --- a/app/client/lib/helper.js +++ b/app/client/lib/helper.js @@ -22,7 +22,7 @@ export function filterObjectByKeys (object, keys) { /** * Pipe for converting seconds to pace format 00:00 * - * @param seconds The actual time in seconds. + * @param {number} seconds The actual time in seconds. */ export function secondsToPace (seconds) { const hours = Math.floor((seconds % 86400) / 3600) @@ -41,8 +41,8 @@ export function secondsToPace (seconds) { /** * Pipe for formatting distance in meters with units * - * @param value The distance in meters. - * @param showInMiles Boolean whether to use imperial metric (default: false). + * @param {number} value The distance in meters. + * @param {boolean} showInMiles Boolean whether to use imperial metric (default: false). */ export function formatDistance (value, showInMiles = false) { if (showInMiles === false) { @@ -57,8 +57,8 @@ export function formatDistance (value, showInMiles = false) { /** * Pipe for formatting numbers to specific decimal * - * @param value The number. - * @param decimalPlaces The number of decimal places to round to (default: 0). + * @param {number} value The number. + * @param {number} decimalPlaces The number of decimal places to round to (default: 0). */ export function formatNumber (value, decimalPlaces = 0) { const decimal = Math.pow(10, decimalPlaces) @@ -67,6 +67,13 @@ export function formatNumber (value, decimalPlaces = 0) { return Math.round(value * decimal) / decimal } -export function simpleMetricFactory (value, unit, icon) { +/** + * Helper function to create a simple metric tile + * + * @param {string | number} value The metric to show + * @param {string} unit The unit of the metric. + * @param {string | import('lit').TemplateResult<2>} icon The number of decimal places to round to (default: 0). +*/ +export function simpleMetricFactory (value = '--', unit = '', icon = '') { return html`` } diff --git a/app/engine/Rower.js b/app/engine/Rower.js index 82f73e2..e66b466 100644 --- a/app/engine/Rower.js +++ b/app/engine/Rower.js @@ -20,9 +20,9 @@ const log = loglevel.getLogger('RowingEngine') function createRower (rowerSettings) { const flywheel = createFlywheel(rowerSettings) const sprocketRadius = rowerSettings.sprocketRadius / 100 - const driveHandleForce = createCurveMetrics(2) - const driveHandleVelocity = createCurveMetrics(3) - const driveHandlePower = createCurveMetrics(1) + const driveHandleForce = createCurveMetrics() + const driveHandleVelocity = createCurveMetrics() + const driveHandlePower = createCurveMetrics() let _strokeState = 'WaitingForDrive' let _totalNumberOfStrokes = -1.0 let recoveryPhaseStartTime = 0.0 diff --git a/app/engine/RowingStatistics.js b/app/engine/RowingStatistics.js index c7dc1ae..155a7b6 100644 --- a/app/engine/RowingStatistics.js +++ b/app/engine/RowingStatistics.js @@ -402,9 +402,9 @@ function createRowingStatistics (config) { driveDistance: driveDistance.clean() >= 0 && sessionStatus === 'Rowing' ? driveDistance.clean() : NaN, // meters driveAverageHandleForce: driveAverageHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? driveAverageHandleForce.clean() : NaN, drivePeakHandleForce: drivePeakHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? drivePeakHandleForce.clean() : NaN, - driveHandleForceCurve: drivePeakHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? driveHandleForceCurve.lastCompleteCurve() : [NaN], - driveHandleVelocityCurve: drivePeakHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? driveHandleVelocityCurve.lastCompleteCurve() : [NaN], - driveHandlePowerCurve: drivePeakHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? driveHandlePowerCurve.lastCompleteCurve() : [NaN], + driveHandleForceCurve: drivePeakHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? driveHandleForceCurve.lastCompleteCurve() : [], + driveHandleVelocityCurve: drivePeakHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? driveHandleVelocityCurve.lastCompleteCurve() : [], + driveHandlePowerCurve: drivePeakHandleForce.clean() > 0 && sessionStatus === 'Rowing' ? driveHandlePowerCurve.lastCompleteCurve() : [], recoveryDuration: recoveryDuration.clean() >= config.rowerSettings.minimumRecoveryTime && totalNumberOfStrokes > 0 && sessionStatus === 'Rowing' ? recoveryDuration.clean() : NaN, // seconds dragFactor: dragFactor > 0 ? dragFactor : config.rowerSettings.dragFactor, // Dragfactor instantPower: instantPower > 0 && rower.strokeState() === 'Drive' ? instantPower : 0, diff --git a/app/engine/WorkoutRecorder.js b/app/engine/WorkoutRecorder.js index 693d455..b538f47 100644 --- a/app/engine/WorkoutRecorder.js +++ b/app/engine/WorkoutRecorder.js @@ -83,8 +83,8 @@ function createWorkoutRecorder () { `${currentstroke.cycleStrokeRate.toFixed(1)},${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.cyclePace.toFixed(2) : NaN)},${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.cyclePower.toFixed(0) : NaN)},` + `${currentstroke.cycleDistance.toFixed(2)},${(currentstroke.driveDuration * 1000).toFixed(0)},${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.driveLength.toFixed(2) : NaN)},${(currentstroke.recoveryDuration * 1000).toFixed(0)},` + `${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.cycleLinearVelocity.toFixed(2) : 0)},${currentstroke.totalLinearDistance.toFixed(1)},${currentstroke.totalCalories.toFixed(1)},${currentstroke.dragFactor.toFixed(1)},` + - `${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.drivePeakHandleForce.toFixed(1) : NaN)},${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.driveAverageHandleForce.toFixed(1) : 0)},"${currentstroke.driveHandleForceCurve}",` + - `"${currentstroke.driveHandleVelocityCurve}","${currentstroke.driveHandlePowerCurve}"\n` + `${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.drivePeakHandleForce.toFixed(1) : NaN)},${(currentstroke.totalNumberOfStrokes > 0 ? currentstroke.driveAverageHandleForce.toFixed(1) : 0)},"${currentstroke.driveHandleForceCurve.map(value => value.toFixed(2))}",` + + `"${currentstroke.driveHandleVelocityCurve.map(value => value.toFixed(3))}","${currentstroke.driveHandlePowerCurve.map(value => value.toFixed(1))}"\n` i++ } diff --git a/app/engine/utils/curveMetrics.js b/app/engine/utils/curveMetrics.js index 4725ae7..1236424 100644 --- a/app/engine/utils/curveMetrics.js +++ b/app/engine/utils/curveMetrics.js @@ -6,7 +6,7 @@ */ import { createSeries } from './Series.js' -function createCurveMetrics (precission = 0) { +function createCurveMetrics () { const _curve = createSeries() let _max = 0 let totalInputXTime = 0 @@ -15,7 +15,7 @@ function createCurveMetrics (precission = 0) { function push (deltaTime, inputValue) { // add the new dataPoint to the array, we have to move datapoints starting at the oldst ones if (inputValue > 0) { - _curve.push(inputValue.toFixed(precission)) + _curve.push(inputValue) _max = Math.max(_max, inputValue) totalInputXTime += deltaTime * inputValue totaltime += deltaTime