Fix lap limits and five-minute stint statistics
This commit is contained in:
1 parent
f561dd8feb
commit
483a9dbd5a
14 files changed
+356
-110
No files matched your search
+84
-46
@@ -52,6 +52,9 @@ export function normalizeTrainingState(training, { uid }) {
|
||||
eventId: String(entry?.eventId || ""),
|
||||
eventName: String(entry?.eventName || "").trim(),
|
||||
driverId: String(entry?.driverId || ""),
|
||||
sourceId: String(entry?.sourceId || entry?.driverId || ""),
|
||||
excludedFromPace: Boolean(entry?.excludedFromPace),
|
||||
stintBoundary: Boolean(entry?.stintBoundary),
|
||||
timestamp,
|
||||
lapMs: Math.max(0, Math.round(Number(entry?.lapMs || 0) || 0)),
|
||||
recordedAt: String(entry?.recordedAt || (timestamp ? new Date(timestamp).toISOString() : new Date().toISOString())),
|
||||
@@ -134,6 +137,9 @@ export function syncTrainingLedger(training, state, { uid }) {
|
||||
eventId: String(session?.eventId || ""),
|
||||
eventName: String(event?.name || "").trim(),
|
||||
driverId,
|
||||
sourceId: String(passing?.competitorKey || passing?.carId || passing?.transponder || driverId),
|
||||
excludedFromPace: Boolean(passing?.excludedFromPace || passing?.validationWarning === "above_max_stint"),
|
||||
stintBoundary: Boolean(passing?.stintBoundary || passing?.validationWarning === "above_max_stint"),
|
||||
timestamp,
|
||||
lapMs,
|
||||
recordedAt: timestamp ? new Date(timestamp).toISOString() : new Date().toISOString(),
|
||||
@@ -159,6 +165,10 @@ export function getDriverTrainingLaps(state, driverId, classId = "") {
|
||||
sessionId: String(entry.sessionId || ""),
|
||||
sessionName: String(entry.sessionName || ""),
|
||||
eventName: String(entry.eventName || ""),
|
||||
driverId: String(entry.driverId || ""),
|
||||
sourceId: String(entry.sourceId || entry.driverId || ""),
|
||||
excludedFromPace: Boolean(entry.excludedFromPace),
|
||||
stintBoundary: Boolean(entry.stintBoundary),
|
||||
timestamp: Number(entry.timestamp || 0) || 0,
|
||||
lapMs: Number(entry.lapMs || 0),
|
||||
}))
|
||||
@@ -202,7 +212,7 @@ export function getDriverTrainingMarkers(state, driverId, classId = "") {
|
||||
}
|
||||
|
||||
export function buildTrainingPaceBuckets(state, driverId, classId = "", bucketMinutes = 30) {
|
||||
const laps = getDriverTrainingLaps(state, driverId, classId);
|
||||
const laps = getDriverTrainingLaps(state, driverId, classId).filter((lap) => !lap.excludedFromPace);
|
||||
const bucketMs = Math.max(5, Number(bucketMinutes || 30)) * 60 * 1000;
|
||||
const buckets = new Map();
|
||||
laps.forEach((lap) => {
|
||||
@@ -238,7 +248,9 @@ export function buildTrainingPaceBuckets(state, driverId, classId = "", bucketMi
|
||||
|
||||
export function buildTrainingWeekdayStats(state, driverId, classId = "") {
|
||||
const groups = Array.from({ length: 7 }, (_, weekday) => ({ weekday, laps: [] }));
|
||||
getDriverTrainingLaps(state, driverId, classId).forEach((lap) => groups[new Date(lap.timestamp).getDay()].laps.push(lap.lapMs));
|
||||
getDriverTrainingLaps(state, driverId, classId)
|
||||
.filter((lap) => !lap.excludedFromPace)
|
||||
.forEach((lap) => groups[new Date(lap.timestamp).getDay()].laps.push(lap.lapMs));
|
||||
return groups.map((group) => ({
|
||||
weekday: group.weekday,
|
||||
lapCount: group.laps.length,
|
||||
@@ -248,7 +260,7 @@ export function buildTrainingWeekdayStats(state, driverId, classId = "") {
|
||||
}
|
||||
|
||||
export function buildTrainingMarkerComparisons(state, driverId, classId = "", windowMinutes = 30) {
|
||||
const laps = getDriverTrainingLaps(state, driverId, classId);
|
||||
const laps = getDriverTrainingLaps(state, driverId, classId).filter((lap) => !lap.excludedFromPace);
|
||||
const windowMs = Math.max(5, Number(windowMinutes || 30)) * 60 * 1000;
|
||||
return getDriverTrainingMarkers(state, driverId, classId).map((marker) => {
|
||||
const comparableLaps = marker.sessionId ? laps.filter((lap) => lap.sessionId === marker.sessionId) : laps;
|
||||
@@ -298,6 +310,7 @@ export function buildTrainingStintAnalysis(state, driverId, classId = "") {
|
||||
bestLapMs: values.length ? Math.min(...values) : 0,
|
||||
paceMs: trimmedAverage(values),
|
||||
racePace10Ms: values.length >= 10 ? getBestConsecutiveAverage(laps, 10) : 0,
|
||||
fiveMinuteResult: getBestTrainingDurationResult([stint], 5 * 60 * 1000),
|
||||
mistakes,
|
||||
mistakePct: values.length ? (mistakes / values.length) * 100 : 0,
|
||||
consistencyPct: values.length >= 3 ? consistencySpread(values) : null,
|
||||
@@ -307,9 +320,11 @@ export function buildTrainingStintAnalysis(state, driverId, classId = "") {
|
||||
insufficient: values.length < 3,
|
||||
};
|
||||
});
|
||||
const eligible = rows.filter((row) => !row.insufficient);
|
||||
const visibleRows = rows.filter((row) => row.lapCount > 0);
|
||||
visibleRows.forEach((row, index) => { row.number = index + 1; });
|
||||
const eligible = visibleRows.filter((row) => !row.insufficient);
|
||||
const best = eligible.reduce((current, row) => (!current || row.paceMs < current.paceMs ? row : current), null);
|
||||
return { rows: rows.sort((left, right) => right.startedAt - left.startedAt), best };
|
||||
return { rows: visibleRows.sort((left, right) => right.startedAt - left.startedAt), best };
|
||||
}
|
||||
|
||||
export function buildTrainingAchievements(stats) {
|
||||
@@ -375,42 +390,62 @@ function getControlledLapStreak(lapTimes, controlTargetMs) {
|
||||
}
|
||||
|
||||
export function splitTrainingStints(laps, breakThresholdMs = 2 * 60 * 1000) {
|
||||
const sorted = (Array.isArray(laps) ? laps : [])
|
||||
const normalized = (Array.isArray(laps) ? laps : [])
|
||||
.filter((lap) => Number(lap?.lapMs || 0) > 500 && Number(lap?.timestamp || 0) > 0)
|
||||
.map((lap) => ({ ...lap, timestamp: Number(lap.timestamp), lapMs: Number(lap.lapMs) }))
|
||||
.sort((left, right) => left.timestamp - right.timestamp);
|
||||
const stints = [];
|
||||
|
||||
sorted.forEach((lap) => {
|
||||
const previousStint = stints.at(-1);
|
||||
const previousLap = previousStint?.laps.at(-1);
|
||||
const lapStartedAt = Math.max(0, lap.timestamp - lap.lapMs);
|
||||
const breakMs = previousLap ? Math.max(0, lap.timestamp - previousLap.timestamp) : 0;
|
||||
const startsNewStint = !previousStint
|
||||
|| String(previousLap.sessionId || "") !== String(lap.sessionId || "")
|
||||
|| breakMs > breakThresholdMs;
|
||||
const normalizedLap = {
|
||||
.map((lap) => ({
|
||||
...lap,
|
||||
excludedFromPace: Boolean(previousLap && breakMs > breakThresholdMs && lap.lapMs > breakThresholdMs),
|
||||
};
|
||||
sessionId: String(lap.sessionId || ""),
|
||||
sourceId: String(lap.sourceId || lap.driverId || lap.competitorKey || lap.transponder || "default"),
|
||||
timestamp: Number(lap.timestamp),
|
||||
lapMs: Number(lap.lapMs),
|
||||
}));
|
||||
const streams = new Map();
|
||||
|
||||
if (startsNewStint) {
|
||||
stints.push({
|
||||
sessionId: String(lap.sessionId || ""),
|
||||
startedAt: lapStartedAt,
|
||||
endedAt: lap.timestamp,
|
||||
durationMs: lap.lapMs,
|
||||
laps: [normalizedLap],
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
previousStint.laps.push(normalizedLap);
|
||||
previousStint.endedAt = lap.timestamp;
|
||||
previousStint.durationMs = Math.max(0, previousStint.endedAt - previousStint.startedAt);
|
||||
normalized.forEach((lap) => {
|
||||
const streamKey = `${lap.sessionId}:${lap.sourceId}`;
|
||||
if (!streams.has(streamKey)) streams.set(streamKey, []);
|
||||
streams.get(streamKey).push(lap);
|
||||
});
|
||||
|
||||
return stints;
|
||||
const stints = [];
|
||||
streams.forEach((streamLaps) => {
|
||||
const sorted = streamLaps.sort((left, right) => left.timestamp - right.timestamp);
|
||||
let currentStint = null;
|
||||
|
||||
sorted.forEach((lap) => {
|
||||
const previousLap = currentStint?.laps.at(-1);
|
||||
const lapStartedAt = Math.max(0, lap.timestamp - lap.lapMs);
|
||||
const breakMs = previousLap ? Math.max(0, lap.timestamp - previousLap.timestamp) : 0;
|
||||
const detectedBreak = Boolean(previousLap && breakMs > breakThresholdMs);
|
||||
const explicitBoundary = Boolean(lap.stintBoundary);
|
||||
const startsNewStint = !currentStint || detectedBreak || explicitBoundary;
|
||||
const normalizedLap = {
|
||||
...lap,
|
||||
excludedFromPace: Boolean(lap.excludedFromPace || explicitBoundary || (detectedBreak && lap.lapMs > breakThresholdMs)),
|
||||
stintBoundary: Boolean(explicitBoundary || detectedBreak),
|
||||
};
|
||||
|
||||
if (startsNewStint) {
|
||||
const boundaryLap = Boolean(normalizedLap.excludedFromPace && (explicitBoundary || detectedBreak));
|
||||
currentStint = {
|
||||
sessionId: lap.sessionId,
|
||||
sourceId: lap.sourceId,
|
||||
startedAt: boundaryLap ? lap.timestamp : lapStartedAt,
|
||||
endedAt: lap.timestamp,
|
||||
durationMs: boundaryLap ? 0 : lap.lapMs,
|
||||
laps: [normalizedLap],
|
||||
};
|
||||
stints.push(currentStint);
|
||||
return;
|
||||
}
|
||||
|
||||
currentStint.laps.push(normalizedLap);
|
||||
currentStint.endedAt = lap.timestamp;
|
||||
currentStint.durationMs = Math.max(0, currentStint.endedAt - currentStint.startedAt);
|
||||
});
|
||||
});
|
||||
|
||||
return stints.sort((left, right) => left.startedAt - right.startedAt);
|
||||
}
|
||||
|
||||
export function getBestConsecutiveTrainingLaps(stints, lapCount = 3) {
|
||||
@@ -457,6 +492,7 @@ export function getBestTrainingDurationResult(stints, targetMs = 5 * 60 * 1000)
|
||||
const lastLap = counted.at(-1);
|
||||
if (!lastLap || lastLap.timestamp < cutoffAt) continue;
|
||||
const totalMs = Math.max(0, lastLap.timestamp - startedAt);
|
||||
if (totalMs > target + lastLap.lapMs) continue;
|
||||
const lapCount = counted.length;
|
||||
const candidate = {
|
||||
targetMs: target,
|
||||
@@ -478,13 +514,14 @@ export function getBestTrainingDurationResult(stints, targetMs = 5 * 60 * 1000)
|
||||
|
||||
export function buildDriverTrainingStats(state, driverId, classId = "") {
|
||||
const laps = getDriverTrainingLaps(state, driverId, classId);
|
||||
const lapTimes = laps.map((lap) => lap.lapMs);
|
||||
const paceLaps = laps.filter((lap) => !lap.excludedFromPace);
|
||||
const lapTimes = paceLaps.map((lap) => lap.lapMs);
|
||||
const recentLapTimes = lapTimes.slice(-30);
|
||||
const previousWindowLapTimes = lapTimes.slice(Math.max(0, lapTimes.length - 60), Math.max(0, lapTimes.length - 30));
|
||||
const earlierLapTimes = lapTimes.slice(0, Math.max(0, lapTimes.length - recentLapTimes.length));
|
||||
const totalLaps = laps.length;
|
||||
const bestLapMs = totalLaps ? Math.min(...lapTimes) : 0;
|
||||
const averageLapMs = totalLaps ? Math.round(lapTimes.reduce((sum, value) => sum + value, 0) / totalLaps) : 0;
|
||||
const bestLapMs = lapTimes.length ? Math.min(...lapTimes) : 0;
|
||||
const averageLapMs = average(lapTimes);
|
||||
const recentAverageMs = average(recentLapTimes);
|
||||
const previousRecentAverageMs = average(previousWindowLapTimes);
|
||||
const consistencySpreadPct = consistencySpread(recentLapTimes);
|
||||
@@ -495,7 +532,7 @@ export function buildDriverTrainingStats(state, driverId, classId = "") {
|
||||
const personalBestGainMs = previousBestMs && recentBestMs ? Math.max(0, previousBestMs - recentBestMs) : 0;
|
||||
const controlTargetMs = bestLapMs ? Math.round(bestLapMs * 1.08) : 0;
|
||||
const controlledLapStreak = getControlledLapStreak(lapTimes, controlTargetMs);
|
||||
const dailySummaries = buildDailySummaries(laps);
|
||||
const dailySummaries = buildDailySummaries(paceLaps);
|
||||
const stints = splitTrainingStints(laps);
|
||||
const completedFiveMinuteStints = stints.filter((stint) => getBestTrainingDurationResult([stint], 5 * 60 * 1000)).length;
|
||||
const bestThreeConsecutive = getBestConsecutiveTrainingLaps(stints, 3);
|
||||
@@ -503,7 +540,7 @@ export function buildDriverTrainingStats(state, driverId, classId = "") {
|
||||
const todayKey = getLocalDayKey(Date.now());
|
||||
const todayLaps = laps.filter((lap) => getLocalDayKey(lap.timestamp) === todayKey);
|
||||
const todayStints = splitTrainingStints(todayLaps);
|
||||
const todayLapTimes = todayLaps.map((lap) => lap.lapMs);
|
||||
const todayLapTimes = todayLaps.filter((lap) => !lap.excludedFromPace).map((lap) => lap.lapMs);
|
||||
|
||||
const stats = {
|
||||
totalLaps,
|
||||
@@ -609,19 +646,20 @@ export function buildDriverActivePracticeStats(state, driverId, nowTs = Date.now
|
||||
.map((passing) => ({
|
||||
timestamp: Math.max(0, Number(passing.timestamp || 0) || 0),
|
||||
lapMs: Math.round(Number(passing.lapMs || 0) || 0),
|
||||
excludedFromPace: Boolean(passing.excludedFromPace || passing.validationWarning === "above_max_stint"),
|
||||
}))
|
||||
.sort((left, right) => left.timestamp - right.timestamp);
|
||||
const lapTimes = laps.map((lap) => lap.lapMs);
|
||||
const paceLapTimes = laps.filter((lap) => !lap.excludedFromPace).map((lap) => lap.lapMs);
|
||||
|
||||
return {
|
||||
sessionId: String(session.id || ""),
|
||||
sessionName: String(session.name || "").trim(),
|
||||
startedAt,
|
||||
elapsedMs: startedAt ? Math.max(0, Number(nowTs || 0) - startedAt) : 0,
|
||||
laps: lapTimes.length,
|
||||
bestLapMs: lapTimes.length ? Math.min(...lapTimes) : 0,
|
||||
averageLapMs: lapTimes.length ? Math.round(lapTimes.reduce((sum, lapMs) => sum + lapMs, 0) / lapTimes.length) : 0,
|
||||
lastLapMs: lapTimes.at(-1) || 0,
|
||||
laps: laps.length,
|
||||
bestLapMs: paceLapTimes.length ? Math.min(...paceLapTimes) : 0,
|
||||
averageLapMs: average(paceLapTimes),
|
||||
lastLapMs: laps.at(-1)?.lapMs || 0,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user