Skip to content

fix(analytics): normalize weight units (kg/lbs) in exercise stats - #227

Open
evan188199-tech wants to merge 1 commit into
Snouzy:mainfrom
evan188199-tech:fix/exercise-stats-normalize-weight-units
Open

evan188199-tech wants to merge 1 commit into
Snouzy:mainfrom
evan188199-tech:fix/exercise-stats-normalize-weight-units

Conversation

@evan188199-tech

Copy link
Copy Markdown

What

All three exercise statistics routes plot/aggregate raw weight values without normalizing units, so a user who switches between kg and lb (or mixes them across sessions) gets a misleading trend line. A 50 lb set and a 50 kg set both read as "50", and a 110 lb set sits far below a 50 kg set on the same axis.

Bug found while auditing the analytics code — not covered by any existing issue/PR.

Root cause

Each route reads the weight straight out of the value array and never consults units[weightIndex]:

  • weight-progression/route.ts: const weight = set.valuesInt[weightIndex]
  • volume/route.ts: volume = reps × set.valuesInt[weightIndex]
  • one-rep-max/route.ts: const weight = set.valuesInt[weightIndex]

The set stores its unit ("kg" / "lbs") at units[weightIndex], but it's ignored. The app already ships convertWeight() / WEIGHT_CONVERSION in src/shared/lib/weight-conversion.ts.

Fix

Normalize each weight to kg before aggregating, in all three routes:

import { convertWeight } from "@/shared/lib/weight-conversion";

const rawWeight = set.valuesInt[weightIndex];
const unit = set.units?.[weightIndex] === "lbs" ? "lbs" : "kg";
const weight = convertWeight(rawWeight, unit, "kg");

Now a 50 lb and a 50 kg set are no longer treated as equal, and mid-history unit switches don't make the line jump. Aggregates (per-session max weight, total volume, estimated 1RM) become unit-consistent.

Fixes #226.

The weight-progression, volume, and one-rep-max statistics routes all read
the raw weight value (valuesInt[weightIndex]) and never consulted
units[weightIndex], so a 50 lb set and a 50 kg set both read as 50 and a
mid-history unit switch made the trend line jump. Normalize each weight to
kg via the existing convertWeight() before aggregating, in all three routes.

Fixes Snouzy#226
@vercel

vercel Bot commented Jul 7, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Workoutcool Team Team on Vercel.

A member of the Team first needs to authorize it.

@evan188199-tech

Copy link
Copy Markdown
Author

此 PR 由 GLM-5.2 修复。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Exercise weight-progression/volume/1RM stats ignore weight units (kg vs lbs)

1 participant