Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Finish: a workout closed hours later ends at its last set, not at the tap

Alessandro Morettid1a2f8906c67
diff --git a/frontend/src/lib/finish-workout.js b/frontend/src/lib/finish-workout.js
index 8869526b9..7c30f6255 100644
--- a/frontend/src/lib/finish-workout.js
+++ b/frontend/src/lib/finish-workout.js
@@ -28,6 +28,30 @@ function finishedRow(set) {
return planSec == null || rest.done ? rest : { ...rest, sec: planSec }
}
+// A session left open and finished hours later (the phone went back in the bag, Finish was
+// tapped the next morning) used to be stored with that tap as its end: a 50-minute workout
+// read as 30 hours in History and Stats. Sets are stamped with `at` when ticked, so the end is
+// the last tick of the unbroken run of work — a gap longer than FORGOTTEN_GAP_MS means the
+// session was over, and ticks after it (catching up the log later) stay logged but do not
+// stretch the clock. Finishing within the gap keeps the tap, cool-down included. Sessions from
+// builds that did not stamp sets keep the old behaviour.
+export const FORGOTTEN_GAP_MS = 20 * 60 * 1000
+
+export function sessionEnd(active, now = Date.now(), gap = FORGOTTEN_GAP_MS) {
+ const ticks = (active?.entries || [])
+ .flatMap(e => e.sets || [])
+ .filter(s => hasCompletedWork(s) && Number.isFinite(s.at))
+ .map(s => s.at)
+ .sort((a, b) => a - b)
+ if (!ticks.length) return now
+ let end = ticks[0]
+ for (const at of ticks.slice(1)) {
+ if (at - end > gap) break
+ end = at
+ }
+ return now - end > gap ? end : now
+}
+
export function buildCompletedWorkout(active, { end = Date.now(), prs = [], snapshotFor } = {}) {
const entries = (active?.entries || []).map(entry => {
const completed = {
diff --git a/frontend/src/lib/finish-workout.test.js b/frontend/src/lib/finish-workout.test.js
index 8fa41fddf..9b0fed082 100644
--- a/frontend/src/lib/finish-workout.test.js
+++ b/frontend/src/lib/finish-workout.test.js
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { buildCompletedWorkout } from './finish-workout.js'
+import { buildCompletedWorkout, sessionEnd, FORGOTTEN_GAP_MS } from './finish-workout.js'
import { cascadeWeight } from './history.js'
import { buildSessionEntries } from './session-start.js'
import { makeSideSet, setSideField, toggleSide, WEIGHT_ORIGIN_MANUAL } from './workout-model.js'
@@ -261,3 +261,40 @@ describe('superset groups', () => {
expect(entries[0].sg).toBe('sg1')
})
})
+
+describe('sessionEnd: a workout finished long after its last set', () => {
+ const MIN = 60 * 1000
+ const T0 = 1_000_000_000_000
+ const at = (...mins) => ({ entries: [{ id: 'x', sets: mins.map(m => ({ done: true, w: 50, r: 8, at: T0 + m * MIN })) }] })
+
+ it('keeps the Finish tap when it comes right after the last set', () => {
+ expect(sessionEnd(at(0, 15, 30, 45), T0 + 50 * MIN)).toBe(T0 + 50 * MIN)
+ })
+
+ it('ends at the last set when Finish is tapped the next day', () => {
+ expect(sessionEnd(at(0, 15, 30, 45), T0 + 30 * 60 * MIN)).toBe(T0 + 45 * MIN)
+ })
+
+ it('does not let a set ticked after a long break stretch the session', () => {
+ // 45 minutes of work, then one leftover set ticked the next morning just before Finish.
+ expect(sessionEnd(at(0, 15, 30, 45, 24 * 60), T0 + 24 * 60 * MIN + 1)).toBe(T0 + 45 * MIN)
+ })
+
+ it('ignores unticked sets and sets from builds that did not stamp them', () => {
+ const active = { entries: [{ id: 'x', sets: [
+ { done: true, w: 50, r: 8, at: T0 },
+ { done: false, w: 50, r: 8, at: T0 + 30 * MIN },
+ { done: true, w: 50, r: 8 },
+ ] }] }
+ expect(sessionEnd(active, T0 + 5 * 60 * MIN)).toBe(T0)
+ expect(sessionEnd({ entries: [{ id: 'x', sets: [{ done: true, w: 50, r: 8 }] }] }, 42)).toBe(42)
+ })
+
+ it('counts a per-side set once one side is done', () => {
+ const active = { entries: [{ id: 'x', sets: [{
+ w: 20, r: 8, done: false, at: T0,
+ sides: { L: { w: 20, r: 8, done: true }, R: { w: 20, r: 8, done: false } },
+ }] }] }
+ expect(sessionEnd(active, T0 + FORGOTTEN_GAP_MS + 1)).toBe(T0)
+ })
+})
diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx
index 71020da59..7bb5b8dd2 100644
--- a/frontend/src/sheets.jsx
+++ b/frontend/src/sheets.jsx
@@ -34,7 +34,7 @@ import { policyFor, defaultIncrement, POLICIES_FOR, POLICY_NAME, POLICY_DESC, MA
import { normalizeRepRange } from './lib/rep-range.js'
import { MOBILE, shareExport, printHtml } from './lib/mobile.js'
import { speedUnitOf, toSpeed, fromSpeed } from './lib/speed.js'
-import { buildCompletedWorkout } from './lib/finish-workout.js'
+import { buildCompletedWorkout, sessionEnd } from './lib/finish-workout.js'
import { isWarmupRow, hasCompletedWork } from './lib/workout-model.js'
import { saveSessionAsRoutine } from './lib/session-routines.js'
import { nextUnfinishedUnit } from './lib/supersetFlow.js'
@@ -2656,7 +2656,7 @@ function doFinishWorkout() {
if (rec && !prs.includes(e.id)) e1prs.push({ id: e.id, ...rec })
})
const w = buildCompletedWorkout(A, {
- end: past ? backfillEnd(A) : Date.now(),
+ end: past ? backfillEnd(A) : sessionEnd(A),
prs,
snapshotFor: e => EXIDX[e.id]?.custom ? exerciseMuscleSnapshot(EXIDX[e.id]) : null,
})
diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx
index 39340a4ed..2fc7f91d9 100644
--- a/frontend/src/views/Workout.jsx
+++ b/frontend/src/views/Workout.jsx
@@ -1079,6 +1079,8 @@ function ActiveWorkout() {
if (side) e.sets[i] = toggleSide(e.sets[i], side)
else e.sets[i].done = !e.sets[i].done
checked = e.sets[i].done
+ // When the work happened, for the session's end (lib/finish-workout.js sessionEnd).
+ if (!editing && (side ? e.sets[i].sides[side].done : checked)) e.sets[i].at = Date.now()
// A finished row has no use for a plan set aside by a hold that did not finish.
if (checked && e.sets[i].planSec != null) delete e.sets[i].planSec
if (e.sets[i].done && !editing) {