Improve training statistics, achievements and stable pace audio
This commit is contained in:
1 parent
82d52ed6cd
commit
4f847610dc
16 files changed
+315
-50
No files matched your search
+86
-15
@@ -288,17 +288,47 @@ function getBestConsecutiveAverage(laps, count) {
|
||||
return best;
|
||||
}
|
||||
|
||||
function getLongestControlledLapStreak(stints, controlTargetMs) {
|
||||
let longest = 0;
|
||||
(Array.isArray(stints) ? stints : []).forEach((stint) => {
|
||||
let current = 0;
|
||||
(Array.isArray(stint?.laps) ? stint.laps : []).forEach((lap) => {
|
||||
if (lap.excludedFromPace || !controlTargetMs || Number(lap.lapMs || 0) > controlTargetMs) {
|
||||
current = 0;
|
||||
return;
|
||||
}
|
||||
current += 1;
|
||||
longest = Math.max(longest, current);
|
||||
});
|
||||
});
|
||||
return longest;
|
||||
}
|
||||
|
||||
function getTrainingStintMetrics(stint) {
|
||||
const laps = (Array.isArray(stint?.laps) ? stint.laps : []).filter((lap) => !lap.excludedFromPace);
|
||||
const values = laps.map((lap) => Number(lap.lapMs || 0)).filter((lapMs) => lapMs > 500);
|
||||
const medianMs = percentile(values, 0.5);
|
||||
const outlierThresholdMs = medianMs ? medianMs * 1.15 : 0;
|
||||
const outlierLaps = values.filter((lapMs) => outlierThresholdMs && lapMs > outlierThresholdMs).length;
|
||||
const comparisonSize = values.length >= 6 ? Math.max(3, Math.floor(values.length / 3)) : 0;
|
||||
const startPaceMs = comparisonSize ? trimmedAverage(values.slice(0, comparisonSize)) : 0;
|
||||
const endPaceMs = comparisonSize ? trimmedAverage(values.slice(-comparisonSize)) : 0;
|
||||
return {
|
||||
laps,
|
||||
values,
|
||||
outlierLaps,
|
||||
comparisonSize,
|
||||
startPaceMs,
|
||||
endPaceMs,
|
||||
endChangeMs: startPaceMs && endPaceMs ? startPaceMs - endPaceMs : null,
|
||||
strongFinish: Boolean(comparisonSize && endPaceMs <= startPaceMs * 1.01),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildTrainingStintAnalysis(state, driverId, classId = "") {
|
||||
const stints = splitTrainingStints(getDriverTrainingLaps(state, driverId, classId));
|
||||
const rows = stints.map((stint, index) => {
|
||||
const laps = stint.laps.filter((lap) => !lap.excludedFromPace);
|
||||
const values = laps.map((lap) => lap.lapMs);
|
||||
const medianMs = percentile(values, 0.5);
|
||||
const mistakeThresholdMs = medianMs ? medianMs * 1.15 : 0;
|
||||
const mistakes = values.filter((lapMs) => mistakeThresholdMs && lapMs > mistakeThresholdMs).length;
|
||||
const comparisonSize = values.length >= 6 ? Math.max(3, Math.floor(values.length / 3)) : 0;
|
||||
const startPaceMs = comparisonSize ? trimmedAverage(values.slice(0, comparisonSize)) : 0;
|
||||
const endPaceMs = comparisonSize ? trimmedAverage(values.slice(-comparisonSize)) : 0;
|
||||
const { laps, values, outlierLaps, startPaceMs, endPaceMs, endChangeMs, strongFinish } = getTrainingStintMetrics(stint);
|
||||
return {
|
||||
id: `${stint.sessionId}:${stint.startedAt}:${index}`,
|
||||
number: index + 1,
|
||||
@@ -311,12 +341,13 @@ export function buildTrainingStintAnalysis(state, driverId, classId = "") {
|
||||
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,
|
||||
mistakes: outlierLaps,
|
||||
mistakePct: values.length ? (outlierLaps / values.length) * 100 : 0,
|
||||
consistencyPct: values.length >= 3 ? consistencySpread(values) : null,
|
||||
startPaceMs,
|
||||
endPaceMs,
|
||||
endChangeMs: startPaceMs && endPaceMs ? startPaceMs - endPaceMs : null,
|
||||
endChangeMs,
|
||||
strongFinish,
|
||||
insufficient: values.length < 3,
|
||||
};
|
||||
});
|
||||
@@ -344,6 +375,8 @@ export function buildTrainingAchievements(stats) {
|
||||
["five_minute", source.bestFiveMinuteResult ? 1 : 0, 1],
|
||||
["five_minutes_3", Number(source.completedFiveMinuteStints || 0), 3],
|
||||
["consistency_5", source.recentLapCount >= 30 && source.consistencySpreadPct <= 5 ? 1 : 0, 1],
|
||||
["strong_finish", Number(source.strongFinishStints || 0), 1],
|
||||
["clean_five_minute", Number(source.cleanFiveMinuteStints || 0), 1],
|
||||
["pace_gain", source.trendSampleReady && source.averageTrendMs > 0 ? 1 : 0, 1],
|
||||
];
|
||||
return definitions.map(([id, current, target]) => ({
|
||||
@@ -380,7 +413,7 @@ function buildDailySummaries(laps) {
|
||||
.sort((left, right) => right.latestTimestamp - left.latestTimestamp);
|
||||
}
|
||||
|
||||
function getControlledLapStreak(lapTimes, controlTargetMs) {
|
||||
export function getControlledLapStreak(lapTimes, controlTargetMs) {
|
||||
let streak = 0;
|
||||
for (let index = lapTimes.length - 1; index >= 0; index -= 1) {
|
||||
if (!controlTargetMs || lapTimes[index] > controlTargetMs) break;
|
||||
@@ -530,16 +563,22 @@ export function buildDriverTrainingStats(state, driverId, classId = "") {
|
||||
const previousBestMs = earlierLapTimes.length ? Math.min(...earlierLapTimes) : 0;
|
||||
const recentBestMs = recentLapTimes.length ? Math.min(...recentLapTimes) : 0;
|
||||
const personalBestGainMs = previousBestMs && recentBestMs ? Math.max(0, previousBestMs - recentBestMs) : 0;
|
||||
const controlZoneMinMs = bestLapMs;
|
||||
const controlTargetMs = bestLapMs ? Math.round(bestLapMs * 1.08) : 0;
|
||||
const controlledLapStreak = getControlledLapStreak(lapTimes, controlTargetMs);
|
||||
const dailySummaries = buildDailySummaries(paceLaps);
|
||||
const stints = splitTrainingStints(laps);
|
||||
const stintMetrics = stints.map((stint) => ({ stint, ...getTrainingStintMetrics(stint) }));
|
||||
const controlledLapStreak = getLongestControlledLapStreak(stints, controlTargetMs);
|
||||
const completedFiveMinuteStints = stints.filter((stint) => getBestTrainingDurationResult([stint], 5 * 60 * 1000)).length;
|
||||
const cleanFiveMinuteStints = stintMetrics.filter(({ stint, outlierLaps }) => !outlierLaps && getBestTrainingDurationResult([stint], 5 * 60 * 1000)).length;
|
||||
const strongFinishStints = stintMetrics.filter((metrics) => metrics.strongFinish).length;
|
||||
const outlierLaps = stintMetrics.reduce((sum, metrics) => sum + metrics.outlierLaps, 0);
|
||||
const bestThreeConsecutive = getBestConsecutiveTrainingLaps(stints, 3);
|
||||
const bestFiveMinuteResult = getBestTrainingDurationResult(stints, 5 * 60 * 1000);
|
||||
const todayKey = getLocalDayKey(Date.now());
|
||||
const todayLaps = laps.filter((lap) => getLocalDayKey(lap.timestamp) === todayKey);
|
||||
const todayStints = splitTrainingStints(todayLaps);
|
||||
const todayStintMetrics = todayStints.map((stint) => ({ stint, ...getTrainingStintMetrics(stint) }));
|
||||
const todayLapTimes = todayLaps.filter((lap) => !lap.excludedFromPace).map((lap) => lap.lapMs);
|
||||
|
||||
const stats = {
|
||||
@@ -554,12 +593,20 @@ export function buildDriverTrainingStats(state, driverId, classId = "") {
|
||||
recentBestMs,
|
||||
previousBestMs,
|
||||
personalBestGainMs,
|
||||
controlZoneMinMs,
|
||||
controlZoneMaxMs: controlTargetMs,
|
||||
controlledLapStreak,
|
||||
outlierLaps,
|
||||
strongFinishStints,
|
||||
cleanFiveMinuteStints,
|
||||
todayLaps: todayLaps.length,
|
||||
todayBestLapMs: todayLapTimes.length ? Math.min(...todayLapTimes) : 0,
|
||||
todayAverageLapMs: average(todayLapTimes),
|
||||
todayControlledLapStreak: getControlledLapStreak(todayLapTimes, controlTargetMs),
|
||||
todayControlledLapStreak: getLongestControlledLapStreak(todayStints, controlTargetMs),
|
||||
todayOutlierLaps: todayStintMetrics.reduce((sum, metrics) => sum + metrics.outlierLaps, 0),
|
||||
todayStrongFinishStints: todayStintMetrics.filter((metrics) => metrics.strongFinish).length,
|
||||
todayCompletedFiveMinuteStints: todayStints.filter((stint) => getBestTrainingDurationResult([stint], 5 * 60 * 1000)).length,
|
||||
todayCleanFiveMinuteStints: todayStintMetrics.filter(({ stint, outlierLaps: count }) => !count && getBestTrainingDurationResult([stint], 5 * 60 * 1000)).length,
|
||||
consistencySpreadPct,
|
||||
previousConsistencySpreadPct,
|
||||
consistencyTrendPct: trendSampleReady ? previousConsistencySpreadPct - consistencySpreadPct : 0,
|
||||
@@ -611,12 +658,24 @@ export function buildDriverTimingChannelStats(state, driverId, classId = "") {
|
||||
pitStops.sort((left, right) => Number(left.enteredAt || 0) - Number(right.enteredAt || 0));
|
||||
const splitTimes = splits.map((split) => split.splitMs);
|
||||
const pitTimes = pitStops.map((stop) => stop.pitStopMs);
|
||||
let runningBestSplitMs = 0;
|
||||
let latestSplitGainMs = 0;
|
||||
let latestSplitGainAt = 0;
|
||||
splits.forEach((split) => {
|
||||
if (runningBestSplitMs && split.splitMs < runningBestSplitMs) {
|
||||
latestSplitGainMs = runningBestSplitMs - split.splitMs;
|
||||
latestSplitGainAt = Number(split.timestamp || 0);
|
||||
}
|
||||
if (!runningBestSplitMs || split.splitMs < runningBestSplitMs) runningBestSplitMs = split.splitMs;
|
||||
});
|
||||
return {
|
||||
splitCount: splits.length,
|
||||
splitSessions: new Set(splits.map((split) => split.sessionId)).size,
|
||||
bestSplitMs: splitTimes.length ? Math.min(...splitTimes) : 0,
|
||||
averageSplitMs: splitTimes.length ? Math.round(splitTimes.reduce((sum, value) => sum + value, 0) / splitTimes.length) : 0,
|
||||
lastSplitMs: splitTimes.at(-1) || 0,
|
||||
latestSplitGainMs,
|
||||
latestSplitGainAt,
|
||||
pitStopCount: pitStops.length,
|
||||
pitSessions: new Set(pitStops.map((stop) => stop.sessionId)).size,
|
||||
totalPitMs: pitTimes.reduce((sum, value) => sum + value, 0),
|
||||
@@ -644,12 +703,18 @@ export function buildDriverActivePracticeStats(state, driverId, nowTs = Date.now
|
||||
&& (!startedAt || Number(passing?.timestamp || 0) >= startedAt)
|
||||
))
|
||||
.map((passing) => ({
|
||||
sessionId: String(session.id || ""),
|
||||
sourceId: String(passing.competitorKey || passing.carId || passing.transponder || passing.driverId || "default"),
|
||||
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"),
|
||||
stintBoundary: Boolean(passing.stintBoundary || passing.validationWarning === "above_max_stint"),
|
||||
}))
|
||||
.sort((left, right) => left.timestamp - right.timestamp);
|
||||
const paceLapTimes = laps.filter((lap) => !lap.excludedFromPace).map((lap) => lap.lapMs);
|
||||
const bestLapMs = paceLapTimes.length ? Math.min(...paceLapTimes) : 0;
|
||||
const controlZoneMaxMs = bestLapMs ? Math.round(bestLapMs * 1.08) : 0;
|
||||
const activeStints = splitTrainingStints(laps);
|
||||
|
||||
return {
|
||||
sessionId: String(session.id || ""),
|
||||
@@ -657,9 +722,12 @@ export function buildDriverActivePracticeStats(state, driverId, nowTs = Date.now
|
||||
startedAt,
|
||||
elapsedMs: startedAt ? Math.max(0, Number(nowTs || 0) - startedAt) : 0,
|
||||
laps: laps.length,
|
||||
bestLapMs: paceLapTimes.length ? Math.min(...paceLapTimes) : 0,
|
||||
bestLapMs,
|
||||
averageLapMs: average(paceLapTimes),
|
||||
lastLapMs: laps.at(-1)?.lapMs || 0,
|
||||
controlZoneMinMs: bestLapMs,
|
||||
controlZoneMaxMs,
|
||||
controlledLapStreak: getLongestControlledLapStreak(activeStints, controlZoneMaxMs),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -730,6 +798,9 @@ export function buildTrainingHighlights(stats) {
|
||||
const highlights = [];
|
||||
if (source.todayLaps > 0) highlights.push({ id: "today_laps", value: Number(source.todayLaps) });
|
||||
if (source.personalBestGainMs > 0) highlights.push({ id: "personal_best", value: Number(source.personalBestGainMs) });
|
||||
if (source.latestSplitGainMs > 0) highlights.push({ id: "sector_gain", value: Number(source.latestSplitGainMs) });
|
||||
if (source.todayStrongFinishStints > 0) highlights.push({ id: "strong_finish", value: Number(source.todayStrongFinishStints) });
|
||||
if (source.todayCleanFiveMinuteStints > 0) highlights.push({ id: "clean_five_minute", value: Number(source.todayCleanFiveMinuteStints) });
|
||||
if (source.trendSampleReady && source.averageTrendMs > 0) highlights.push({ id: "pace_gain", value: Number(source.averageTrendMs) });
|
||||
if (source.trendSampleReady && source.consistencyTrendPct > 0.1) highlights.push({ id: "consistency_gain", value: Number(source.consistencyTrendPct) });
|
||||
if (source.todayControlledLapStreak >= 5) highlights.push({ id: "controlled_streak", value: Number(source.todayControlledLapStreak) });
|
||||
|
||||
Reference in new issue
Block a user