Workout: Uebungsverlauf mit Kurve und letzten Einheiten aus dem ⋯-Menue (#43)
Mitten im Training fehlte die Antwort auf "was habe ich hier letztes Mal und davor gemacht" — die stand nur in Stats, drei Screens weiter weg. Das ⋯-Menue jeder Uebung hat jetzt "Verlauf": ein Sheet mit der Kurve (bester Satz pro Einheit, umschaltbar auf gesch. 1RM; Halten in Sekunden, Cardio in Minuten, nie beladene Uebungen ihre Wdh.) und den letzten zehn Einheiten, neueste zuerst, mit Saetzen, Volumen, 1RM und einem PR-Marker auf der Einheit, in der die Bestleistung zuerst fiel. Aus dem Detail-Sheet (Bibliothek) ist es ueber einen Button erreichbar, sobald es Verlauf gibt. Die Ableitung liegt als reiner Helfer in lib/exercise-history.js (ein Lauf ueber das Log, Aufwaermsaetze wie ueberall ausgeschlossen, Backfill nach Datum sortiert) und wird im Sheet memoisiert, damit der Workout-Screen beim Tick nicht das ganze Log neu liest und LineChart seinen Hover behaelt.
This commit is contained in:
1 parent
b5769b9c38
commit
fbe33600ff
21 files changed
+423
-5
No files matched your search
@@ -0,0 +1,83 @@
|
||||
import { metricModeForEntry, metricRowsForEntry, bestWeightForEntry, modeOf } from './history.js'
|
||||
import { extraVolumeOf } from './workout-model.js'
|
||||
import { bestSetOf } from './onerm.js'
|
||||
|
||||
// One exercise's past, read back for the history sheet (issue #43): a chart series and the
|
||||
// last few sessions, derived in a single pass over the log so the sheet can memoise the
|
||||
// result and never rescan on a re-render.
|
||||
//
|
||||
// The chart plots ONE number per session, chosen the way Stats does it:
|
||||
// reps — the heaviest completed work set (metric 'weight'); an exercise never loaded
|
||||
// (pull-ups, push-ups) plots its best rep count instead (metric 'reps'), because
|
||||
// that is the thing improving
|
||||
// time — the longest completed hold, in seconds (metric 'sec')
|
||||
// cardio — the minutes logged that session (metric 'min')
|
||||
// The mode is the one the exercise was logged in most recently; a session logged in another
|
||||
// mode still appears in the list (its sets are labelled by their own target) but gets no point
|
||||
// and no value, so the curve never mixes seconds with kilos.
|
||||
//
|
||||
// Warm-up rows are excluded from every number here, as everywhere else.
|
||||
|
||||
export const HISTORY_SESSIONS = 10
|
||||
|
||||
const startOf = w => (Number.isFinite(w.start) ? w.start : new Date(w.d + 'T12:00:00').getTime())
|
||||
|
||||
// Volume of the exercise in one session: main set plus its drops/bursts, reps mode only —
|
||||
// there is no honest tonnage for a hold or a run.
|
||||
const entryVolume = rows => rows.reduce((v, s) => v + (s.w || 0) * (s.r || 0) + extraVolumeOf(s), 0)
|
||||
|
||||
export function exerciseHistory(S, exId, { limit = HISTORY_SESSIONS } = {}) {
|
||||
const workouts = S?.workouts || []
|
||||
// Chronological pairs of (workout, entry); the sort covers backfilled sessions, which are
|
||||
// inserted by date rather than appended.
|
||||
const logged = []
|
||||
workouts.forEach(w => {
|
||||
const en = (w.entries || []).find(e => e.id === exId)
|
||||
if (!en) return
|
||||
const mode = metricModeForEntry(en)
|
||||
if (!mode) return
|
||||
const rows = metricRowsForEntry(en, mode)
|
||||
if (rows.length) logged.push({ w, en, mode, rows })
|
||||
})
|
||||
logged.sort((a, b) => startOf(a.w) - startOf(b.w))
|
||||
|
||||
const empty = { mode: modeOf({ id: exId }), metric: 'weight', best: 0, prId: null, total: 0, sessions: [], points: [], e1rmPoints: [] }
|
||||
if (!logged.length) return empty
|
||||
|
||||
const mode = logged[logged.length - 1].mode
|
||||
const repsOnly = mode === 'reps' && !logged.some(l => l.mode === 'reps' && bestWeightForEntry(l.en) > 0)
|
||||
const metric = mode === 'cardio' ? 'min' : mode === 'time' ? 'sec' : repsOnly ? 'reps' : 'weight'
|
||||
const valueOf = ({ en, rows }) => {
|
||||
if (metric === 'min') return rows.reduce((a, s) => a + (Number(s.min) || 0), 0)
|
||||
if (metric === 'sec') return Math.max(0, ...rows.map(s => Number(s.sec) || 0))
|
||||
if (metric === 'reps') return Math.max(0, ...rows.map(s => Number(s.r) || 0))
|
||||
return bestWeightForEntry(en)
|
||||
}
|
||||
|
||||
let best = 0, prId = null
|
||||
const sessions = [], points = [], e1rmPoints = []
|
||||
logged.forEach(({ w, en, mode: m, rows }) => {
|
||||
const same = m === mode
|
||||
const value = same ? valueOf({ en, rows }) : null
|
||||
const e1rm = m === 'reps' ? (bestSetOf(en)?.est ?? null) : null
|
||||
const t = startOf(w)
|
||||
// "PR" goes on the session that first reached the all-time best, not on every session
|
||||
// that later matched it — one marker says where the record was set.
|
||||
if (value != null && value > best) { best = value; prId = w.id }
|
||||
if (value != null && value > 0) points.push({ t, d: w.d, y: value, e1rm })
|
||||
if (e1rm != null) e1rmPoints.push({ t, d: w.d, y: e1rm })
|
||||
sessions.push({
|
||||
id: w.id, d: w.d, t, mode: m, target: en.target || null, sets: rows, value, e1rm,
|
||||
volume: m === 'reps' ? entryVolume(rows) : null,
|
||||
})
|
||||
})
|
||||
// The "first reached" rule only holds for records above zero: a bodyweight session with
|
||||
// no weight logged is not a PR of anything.
|
||||
if (best <= 0) prId = null
|
||||
|
||||
return {
|
||||
mode, metric, best, prId, total: sessions.length,
|
||||
sessions: sessions.slice(-limit).reverse().map(s => ({ ...s, pr: s.id === prId })),
|
||||
points, e1rmPoints,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { exerciseHistory, HISTORY_SESSIONS } from './exercise-history.js'
|
||||
import { estimate1RM } from './onerm.js'
|
||||
|
||||
const DAY = 86400000
|
||||
const T0 = Date.UTC(2026, 0, 5, 10)
|
||||
const iso = i => new Date(T0 + i * DAY).toISOString().slice(0, 10)
|
||||
|
||||
// One workout on day `i` with a single bench entry made of the given rows.
|
||||
const session = (i, rows, extra = {}) => ({
|
||||
id: 'w' + i, d: iso(i), start: T0 + i * DAY, entries: [{ id: 'bench', target: { mode: 'reps' }, sets: rows, ...extra }],
|
||||
})
|
||||
const work = (w, r, done = true) => ({ w, r, done })
|
||||
const warm = (w, r) => ({ w, r, done: true, phase: 'warmup' })
|
||||
|
||||
describe('exerciseHistory', () => {
|
||||
it('is empty when the exercise was never logged', () => {
|
||||
const S = { workouts: [session(0, [work(60, 5)])] }
|
||||
expect(exerciseHistory(S, 'squat')).toMatchObject({ total: 0, best: 0, prId: null, sessions: [], points: [] })
|
||||
expect(exerciseHistory({ workouts: [] }, 'bench').total).toBe(0)
|
||||
expect(exerciseHistory({}, 'bench').total).toBe(0)
|
||||
})
|
||||
|
||||
it('lists sessions newest first with their work sets, value and volume', () => {
|
||||
const S = { workouts: [session(0, [work(60, 5), work(60, 5)]), session(2, [work(65, 5), work(65, 4)])] }
|
||||
const h = exerciseHistory(S, 'bench')
|
||||
expect(h.mode).toBe('reps')
|
||||
expect(h.metric).toBe('weight')
|
||||
expect(h.total).toBe(2)
|
||||
expect(h.sessions.map(s => s.d)).toEqual([iso(2), iso(0)])
|
||||
expect(h.sessions[0]).toMatchObject({ value: 65, volume: 65 * 9, target: { mode: 'reps' } })
|
||||
expect(h.sessions[0].sets).toHaveLength(2)
|
||||
expect(h.sessions[1]).toMatchObject({ value: 60, volume: 600 })
|
||||
// the chart stays chronological
|
||||
expect(h.points.map(p => [p.d, p.y])).toEqual([[iso(0), 60], [iso(2), 65]])
|
||||
})
|
||||
|
||||
it('leaves warm-ups and unfinished rows out of every number', () => {
|
||||
const S = { workouts: [session(0, [warm(40, 8), work(60, 5), work(100, 5, false)])] }
|
||||
const h = exerciseHistory(S, 'bench')
|
||||
expect(h.best).toBe(60)
|
||||
expect(h.sessions[0].sets).toEqual([work(60, 5)])
|
||||
expect(h.sessions[0].volume).toBe(300)
|
||||
expect(h.sessions[0].e1rm).toBe(estimate1RM(60, 5))
|
||||
})
|
||||
|
||||
it('marks the PR on the session that first reached the best weight, once', () => {
|
||||
const S = { workouts: [
|
||||
session(0, [work(60, 5)]), session(1, [work(70, 5)]), session(2, [work(65, 5)]), session(3, [work(70, 3)]),
|
||||
] }
|
||||
const h = exerciseHistory(S, 'bench')
|
||||
expect(h.best).toBe(70)
|
||||
expect(h.prId).toBe('w1')
|
||||
expect(h.sessions.filter(s => s.pr).map(s => s.id)).toEqual(['w1'])
|
||||
})
|
||||
|
||||
it('keeps the last ten sessions in the list but every session on the chart', () => {
|
||||
const S = { workouts: Array.from({ length: 14 }, (_, i) => session(i, [work(50 + i, 5)])) }
|
||||
const h = exerciseHistory(S, 'bench')
|
||||
expect(h.total).toBe(14)
|
||||
expect(h.sessions).toHaveLength(HISTORY_SESSIONS)
|
||||
expect(h.sessions[0].d).toBe(iso(13))
|
||||
expect(h.sessions.at(-1).d).toBe(iso(4))
|
||||
expect(h.points).toHaveLength(14)
|
||||
expect(h.e1rmPoints).toHaveLength(14)
|
||||
// the record lives outside the listed window, so no listed session carries the marker
|
||||
expect(h.prId).toBe('w13')
|
||||
expect(exerciseHistory(S, 'bench', { limit: 3 }).sessions).toHaveLength(3)
|
||||
})
|
||||
|
||||
it('orders backfilled sessions by date, not by position in the log', () => {
|
||||
const S = { workouts: [session(5, [work(60, 5)]), { ...session(1, [work(80, 5)]), start: undefined }] }
|
||||
const h = exerciseHistory(S, 'bench')
|
||||
expect(h.points.map(p => p.d)).toEqual([iso(1), iso(5)])
|
||||
expect(h.sessions.map(s => s.d)).toEqual([iso(5), iso(1)])
|
||||
expect(h.prId).toBe('w1')
|
||||
})
|
||||
|
||||
it('plots reps for an exercise that was never loaded', () => {
|
||||
const S = { workouts: [session(0, [work(0, 8)]), session(1, [work(0, 10), work(0, 9)])] }
|
||||
const h = exerciseHistory(S, 'bench')
|
||||
expect(h.metric).toBe('reps')
|
||||
expect(h.points.map(p => p.y)).toEqual([8, 10])
|
||||
expect(h.best).toBe(10)
|
||||
expect(h.prId).toBe('w1')
|
||||
})
|
||||
|
||||
it('plots the longest hold for timed work and the minutes for cardio', () => {
|
||||
const hold = i => ({ id: 'h' + i, d: iso(i), start: T0 + i * DAY, entries: [{ id: 'plank', target: { mode: 'time' }, sets: [{ sec: 40 + i * 10, done: true }, { sec: 30, done: true }] }] })
|
||||
const run = i => ({ id: 'r' + i, d: iso(i), start: T0 + i * DAY, entries: [{ id: 'run', target: { mode: 'cardio' }, sets: [{ min: 20, speed: 10, done: true }, { min: 5, speed: 12, done: true }] }] })
|
||||
const S = { workouts: [hold(0), hold(1), run(0)] }
|
||||
const plank = exerciseHistory(S, 'plank')
|
||||
expect(plank).toMatchObject({ mode: 'time', metric: 'sec', best: 50, prId: 'h1' })
|
||||
expect(plank.points.map(p => p.y)).toEqual([40, 50])
|
||||
expect(plank.sessions[0].volume).toBeNull()
|
||||
expect(plank.e1rmPoints).toEqual([])
|
||||
const cardio = exerciseHistory(S, 'run')
|
||||
expect(cardio).toMatchObject({ mode: 'cardio', metric: 'min', best: 25 })
|
||||
expect(cardio.points[0].y).toBe(25)
|
||||
})
|
||||
|
||||
it('gives a session logged in another mode no point, but keeps it in the list', () => {
|
||||
const S = { workouts: [
|
||||
{ id: 'a', d: iso(0), start: T0, entries: [{ id: 'x', target: { mode: 'time' }, sets: [{ sec: 30, done: true }] }] },
|
||||
{ id: 'b', d: iso(1), start: T0 + DAY, entries: [{ id: 'x', target: { mode: 'reps' }, sets: [work(20, 10)] }] },
|
||||
] }
|
||||
const h = exerciseHistory(S, 'x')
|
||||
expect(h.mode).toBe('reps')
|
||||
expect(h.points).toHaveLength(1)
|
||||
expect(h.sessions.map(s => [s.id, s.value])).toEqual([['b', 20], ['a', null]])
|
||||
})
|
||||
})
|
||||
@@ -27,11 +27,11 @@ describe('Brazilian Portuguese locale', () => {
|
||||
.sort(byCodeUnit)
|
||||
const fingerprint = createHash('sha256').update(JSON.stringify(inherited)).digest('hex')
|
||||
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(620)
|
||||
expect(inherited).toHaveLength(598)
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(621)
|
||||
expect(inherited).toHaveLength(602)
|
||||
// If this fails, review the changed keys and wording before accepting a new hash. From
|
||||
// frontend/: node scripts/pt-br-inheritance-fingerprint.mjs --list
|
||||
expect(fingerprint, 'pt-PT inheritance changed; review the inherited pt-BR wording').toBe('5f1f5dcac5d51dd96f9d1e3506c6a65fd098071b971739686f3cc0360cca35ec')
|
||||
expect(fingerprint, 'pt-PT inheritance changed; review the inherited pt-BR wording').toBe('fb29e798de972bf0d5a8477958b0a2836c57b2d32f0248847c4d93ebba6813c4')
|
||||
})
|
||||
|
||||
test('does not leak European Portuguese UI terms', () => {
|
||||
|
||||
Reference in new issue
Block a user