mirror of
https://github.com/jhbruhn/respira.git
synced 2026-01-27 02:13:41 +00:00
Merge pull request #24 from jhbruhn/fix/pattern-time-calculation
Some checks are pending
Build, Test, and Lint / Build, Test, and Lint (push) Waiting to run
Draft Release / Draft Release (push) Waiting to run
Draft Release / Build Web App (push) Blocked by required conditions
Draft Release / Build Release - macos-latest (push) Blocked by required conditions
Draft Release / Build Release - ubuntu-latest (push) Blocked by required conditions
Draft Release / Build Release - windows-latest (push) Blocked by required conditions
Draft Release / Upload to GitHub Release (push) Blocked by required conditions
Some checks are pending
Build, Test, and Lint / Build, Test, and Lint (push) Waiting to run
Draft Release / Draft Release (push) Waiting to run
Draft Release / Build Web App (push) Blocked by required conditions
Draft Release / Build Release - macos-latest (push) Blocked by required conditions
Draft Release / Build Release - ubuntu-latest (push) Blocked by required conditions
Draft Release / Build Release - windows-latest (push) Blocked by required conditions
Draft Release / Upload to GitHub Release (push) Blocked by required conditions
fix: Correct total time calculation and add detailed logging
This commit is contained in:
commit
72c6cf6eea
1 changed files with 102 additions and 11 deletions
|
|
@ -5,13 +5,26 @@
|
|||
* - 3000ms startup time
|
||||
* - Result in minutes (rounded up)
|
||||
*/
|
||||
export function convertStitchesToMinutes(stitchCount: number): number {
|
||||
if (stitchCount <= 1) return 0;
|
||||
export function convertStitchesToMinutes(
|
||||
stitchCount: number,
|
||||
logPrefix = "",
|
||||
): number {
|
||||
if (stitchCount <= 1) {
|
||||
console.log(
|
||||
`${logPrefix}[convertStitchesToMinutes] stitchCount=${stitchCount} <= 1, returning 0`,
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const timeMs = (stitchCount - 1) * 150 + 3000;
|
||||
const timeMin = Math.ceil(timeMs / 60000);
|
||||
const result = timeMin < 1 ? 1 : timeMin;
|
||||
|
||||
return timeMin < 1 ? 1 : timeMin;
|
||||
console.log(
|
||||
`${logPrefix}[convertStitchesToMinutes] stitchCount=${stitchCount}, timeMs=${timeMs}, timeMin=${timeMin}, result=${result}`,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -26,36 +39,114 @@ export function calculatePatternTime(
|
|||
elapsedMinutes: number;
|
||||
remainingMinutes: number;
|
||||
} {
|
||||
console.log(
|
||||
`\n[calculatePatternTime] Starting calculation with ${colorBlocks.length} blocks, currentStitch=${currentStitch}`,
|
||||
);
|
||||
|
||||
// Step 1: Calculate total time for ALL blocks
|
||||
console.log("\n[calculatePatternTime] STEP 1: Calculating total time");
|
||||
let totalMinutes = 0;
|
||||
for (let i = 0; i < colorBlocks.length; i++) {
|
||||
const block = colorBlocks[i];
|
||||
const blockTime = convertStitchesToMinutes(
|
||||
block.stitchCount,
|
||||
` Total Block ${i + 1} `,
|
||||
);
|
||||
totalMinutes += blockTime;
|
||||
console.log(
|
||||
` [calculatePatternTime] Block ${i + 1}: ${block.stitchCount} stitches = ${blockTime} min. Total now: ${totalMinutes} min`,
|
||||
);
|
||||
}
|
||||
console.log(
|
||||
`[calculatePatternTime] Total time for all blocks: ${totalMinutes} min`,
|
||||
);
|
||||
|
||||
// Step 2: Calculate elapsed time based on currentStitch
|
||||
console.log(
|
||||
`\n[calculatePatternTime] STEP 2: Calculating elapsed time for currentStitch=${currentStitch}`,
|
||||
);
|
||||
let elapsedMinutes = 0;
|
||||
let cumulativeStitches = 0;
|
||||
|
||||
// Calculate time per color block
|
||||
for (const block of colorBlocks) {
|
||||
totalMinutes += convertStitchesToMinutes(block.stitchCount);
|
||||
for (let i = 0; i < colorBlocks.length; i++) {
|
||||
const block = colorBlocks[i];
|
||||
const prevCumulativeStitches = cumulativeStitches;
|
||||
cumulativeStitches += block.stitchCount;
|
||||
|
||||
console.log(
|
||||
`\n[calculatePatternTime] Block ${i + 1}/${colorBlocks.length}: stitchCount=${block.stitchCount}`,
|
||||
);
|
||||
console.log(
|
||||
` [calculatePatternTime] Cumulative stitches: ${prevCumulativeStitches} + ${block.stitchCount} = ${cumulativeStitches}`,
|
||||
);
|
||||
|
||||
if (cumulativeStitches < currentStitch) {
|
||||
// This entire block is completed
|
||||
elapsedMinutes += convertStitchesToMinutes(block.stitchCount);
|
||||
console.log(
|
||||
` [calculatePatternTime] Block completed (${cumulativeStitches} < ${currentStitch})`,
|
||||
);
|
||||
const elapsed = convertStitchesToMinutes(
|
||||
block.stitchCount,
|
||||
` Elapsed Block ${i + 1} `,
|
||||
);
|
||||
elapsedMinutes += elapsed;
|
||||
console.log(
|
||||
` [calculatePatternTime] Added ${elapsed} min to elapsed. Elapsed now: ${elapsedMinutes} min`,
|
||||
);
|
||||
} else if (cumulativeStitches === currentStitch) {
|
||||
// We just completed this block
|
||||
elapsedMinutes += convertStitchesToMinutes(block.stitchCount);
|
||||
console.log(
|
||||
` [calculatePatternTime] Block just completed (${cumulativeStitches} === ${currentStitch})`,
|
||||
);
|
||||
const elapsed = convertStitchesToMinutes(
|
||||
block.stitchCount,
|
||||
` Elapsed Block ${i + 1} `,
|
||||
);
|
||||
elapsedMinutes += elapsed;
|
||||
console.log(
|
||||
` [calculatePatternTime] Added ${elapsed} min to elapsed. Elapsed now: ${elapsedMinutes} min`,
|
||||
);
|
||||
console.log(
|
||||
` [calculatePatternTime] Breaking elapsed calculation at block ${i + 1}`,
|
||||
);
|
||||
break;
|
||||
} else {
|
||||
// We're partway through this block
|
||||
// We're partway through this block (or haven't started)
|
||||
const stitchesInBlock =
|
||||
currentStitch - (cumulativeStitches - block.stitchCount);
|
||||
elapsedMinutes += convertStitchesToMinutes(stitchesInBlock);
|
||||
console.log(
|
||||
` [calculatePatternTime] Partway through block (${cumulativeStitches} > ${currentStitch})`,
|
||||
);
|
||||
console.log(
|
||||
` [calculatePatternTime] Stitches in this block: ${currentStitch} - ${cumulativeStitches - block.stitchCount} = ${stitchesInBlock}`,
|
||||
);
|
||||
const elapsed = convertStitchesToMinutes(
|
||||
stitchesInBlock,
|
||||
` Elapsed Partial Block ${i + 1} `,
|
||||
);
|
||||
elapsedMinutes += elapsed;
|
||||
console.log(
|
||||
` [calculatePatternTime] Added ${elapsed} min to elapsed. Elapsed now: ${elapsedMinutes} min`,
|
||||
);
|
||||
console.log(
|
||||
` [calculatePatternTime] Breaking elapsed calculation at block ${i + 1}`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
const result = {
|
||||
totalMinutes,
|
||||
elapsedMinutes,
|
||||
remainingMinutes: Math.max(0, totalMinutes - elapsedMinutes),
|
||||
};
|
||||
|
||||
console.log(`\n[calculatePatternTime] Final result:`, result);
|
||||
console.log(
|
||||
` Total: ${result.totalMinutes} min, Elapsed: ${result.elapsedMinutes} min, Remaining: ${result.remainingMinutes} min\n`,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue