Anki/ts/routes/deck-options/FsrsOptions.svelte
Damien Elmes 9f55cf26fc
Switch to SvelteKit (#3077)
* Update to latest Node LTS

* Add sveltekit

* Split tslib into separate @generated and @tslib components

SvelteKit's path aliases don't support multiple locations, so our old
approach of using @tslib to refer to both ts/lib and out/ts/lib will no
longer work. Instead, all generated sources and their includes are
placed in a separate out/ts/generated folder, and imported via @generated
instead. This also allows us to generate .ts files, instead of needing
to output separate .d.ts and .js files.

* Switch package.json to module type

* Avoid usage of baseUrl

Incompatible with SvelteKit

* Move sass into ts; use relative links

SvelteKit's default sass support doesn't allow overriding loadPaths

* jest->vitest, graphs example working with yarn dev

* most pages working in dev mode

* Some fixes after rebasing

* Fix/silence some svelte-check errors

* Get image-occlusion working with Fabric types

* Post-rebase lock changes

* Editor is now checked

* SvelteKit build integrated into ninja

* Use the new SvelteKit entrypoint for pages like congrats/deck options/etc

* Run eslint once for ts/**; fix some tests

* Fix a bunch of issues introduced when rebasing over latest main

* Run eslint fix

* Fix remaining eslint+pylint issues; tests now all pass

* Fix some issues with a clean build

* Latest bufbuild no longer requires @__PURE__ hack

* Add a few missed dependencies

* Add yarn.bat to fix Windows build

* Fix pages failing to show when ANKI_API_PORT not defined

* Fix svelte-check and vitest on Windows

* Set node path in ./yarn

* Move svelte-kit output to ts/.svelte-kit

Sadly, I couldn't figure out a way to store it in out/ if out/ is
a symlink, as it breaks module resolution when SvelteKit is run.

* Allow HMR inside Anki

* Skip SvelteKit build when HMR is defined

* Fix some post-rebase issues

I should have done a normal merge instead.
2024-03-31 09:16:31 +01:00

375 lines
13 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import {
ComputeRetentionProgress,
type ComputeWeightsProgress,
} from "@generated/anki/collection_pb";
import { ComputeOptimalRetentionRequest } from "@generated/anki/scheduler_pb";
import {
computeFsrsWeights,
computeOptimalRetention,
evaluateWeights,
setWantsAbort,
} from "@generated/backend";
import * as tr from "@generated/ftl";
import { runWithBackendProgress } from "@tslib/progress";
import SettingTitle from "$lib/components/SettingTitle.svelte";
import SwitchRow from "$lib/components/SwitchRow.svelte";
import DateInput from "./DateInput.svelte";
import GlobalLabel from "./GlobalLabel.svelte";
import type { DeckOptionsState } from "./lib";
import SpinBoxFloatRow from "./SpinBoxFloatRow.svelte";
import SpinBoxRow from "./SpinBoxRow.svelte";
import Warning from "./Warning.svelte";
import WeightsInputRow from "./WeightsInputRow.svelte";
export let state: DeckOptionsState;
export let openHelpModal: (String) => void;
const presetName = state.currentPresetName;
const config = state.currentConfig;
const defaults = state.defaults;
const fsrsReschedule = state.fsrsReschedule;
const daysSinceLastOptimization = state.daysSinceLastOptimization;
$: lastOptimizationWarning =
$daysSinceLastOptimization > 30 ? tr.deckConfigOptimizeAllTip() : "";
let computeWeightsProgress: ComputeWeightsProgress | undefined;
let computingWeights = false;
let checkingWeights = false;
let computingRetention = false;
let optimalRetention = 0;
$: if ($presetName) {
optimalRetention = 0;
}
$: computing = computingWeights || checkingWeights || computingRetention;
$: defaultWeightSearch = `preset:"${state.getCurrentName()}" -is:suspended`;
$: desiredRetentionWarning = getRetentionWarning($config.desiredRetention);
$: retentionWarningClass = getRetentionWarningClass($config.desiredRetention);
let computeRetentionProgress:
| ComputeWeightsProgress
| ComputeRetentionProgress
| undefined;
const optimalRetentionRequest = new ComputeOptimalRetentionRequest({
daysToSimulate: 365,
lossAversion: 2.5,
});
$: if (optimalRetentionRequest.daysToSimulate > 3650) {
optimalRetentionRequest.daysToSimulate = 3650;
}
function getRetentionWarning(retention: number): string {
const decay = -0.5;
const factor = 0.9 ** (1 / decay) - 1;
const stability = 100;
const days = Math.round(
(stability / factor) * (Math.pow(retention, 1 / decay) - 1),
);
if (days === 100) {
return "";
}
return tr.deckConfigA100DayInterval({ days });
}
function getRetentionWarningClass(retention: number): string {
if (retention < 0.7 || retention > 0.97) {
return "alert-danger";
} else if (retention < 0.8 || retention > 0.95) {
return "alert-warning";
} else {
return "alert-info";
}
}
function getIgnoreRevlogsBeforeMs() {
return BigInt(
$config.ignoreRevlogsBeforeDate
? new Date($config.ignoreRevlogsBeforeDate).getTime()
: 0,
);
}
async function computeWeights(): Promise<void> {
if (computingWeights) {
await setWantsAbort({});
return;
}
if (state.presetAssignmentsChanged()) {
alert(tr.deckConfigPleaseSaveYourChangesFirst());
return;
}
computingWeights = true;
computeWeightsProgress = undefined;
try {
await runWithBackendProgress(
async () => {
const resp = await computeFsrsWeights({
search: $config.weightSearch
? $config.weightSearch
: defaultWeightSearch,
ignoreRevlogsBeforeMs: getIgnoreRevlogsBeforeMs(),
currentWeights: $config.fsrsWeights,
});
if (
($config.fsrsWeights.length &&
$config.fsrsWeights.every(
(n, i) => n.toFixed(4) === resp.weights[i].toFixed(4),
)) ||
resp.weights.length === 0
) {
alert(tr.deckConfigFsrsParamsOptimal());
}
if (computeWeightsProgress) {
computeWeightsProgress.current = computeWeightsProgress.total;
}
$config.fsrsWeights = resp.weights;
},
(progress) => {
if (progress.value.case === "computeWeights") {
computeWeightsProgress = progress.value.value;
}
},
);
} finally {
computingWeights = false;
}
}
async function checkWeights(): Promise<void> {
if (checkingWeights) {
await setWantsAbort({});
return;
}
if (state.presetAssignmentsChanged()) {
alert(tr.deckConfigPleaseSaveYourChangesFirst());
return;
}
checkingWeights = true;
computeWeightsProgress = undefined;
try {
await runWithBackendProgress(
async () => {
const search = $config.weightSearch
? $config.weightSearch
: defaultWeightSearch;
const resp = await evaluateWeights({
weights: $config.fsrsWeights,
search,
ignoreRevlogsBeforeMs: getIgnoreRevlogsBeforeMs(),
});
if (computeWeightsProgress) {
computeWeightsProgress.current = computeWeightsProgress.total;
}
setTimeout(
() =>
alert(
`Log loss: ${resp.logLoss.toFixed(4)}, RMSE(bins): ${(
resp.rmseBins * 100
).toFixed(2)}%. ${tr.deckConfigSmallerIsBetter()}`,
),
200,
);
},
(progress) => {
if (progress.value.case === "computeWeights") {
computeWeightsProgress = progress.value.value;
}
},
);
} finally {
checkingWeights = false;
}
}
async function computeRetention(): Promise<void> {
if (computingRetention) {
await setWantsAbort({});
return;
}
if (state.presetAssignmentsChanged()) {
alert(tr.deckConfigPleaseSaveYourChangesFirst());
return;
}
computingRetention = true;
computeRetentionProgress = undefined;
try {
await runWithBackendProgress(
async () => {
optimalRetentionRequest.maxInterval = $config.maximumReviewInterval;
optimalRetentionRequest.weights = $config.fsrsWeights;
optimalRetentionRequest.search = `preset:"${state.getCurrentName()}" -is:suspended`;
const resp = await computeOptimalRetention(optimalRetentionRequest);
optimalRetention = resp.optimalRetention;
computeRetentionProgress = undefined;
},
(progress) => {
if (progress.value.case === "computeRetention") {
computeRetentionProgress = progress.value.value;
}
},
);
} finally {
computingRetention = false;
}
}
$: computeWeightsProgressString = renderWeightProgress(computeWeightsProgress);
$: computeRetentionProgressString = renderRetentionProgress(
computeRetentionProgress,
);
function renderWeightProgress(val: ComputeWeightsProgress | undefined): String {
if (!val || !val.total) {
return "";
}
const pct = ((val.current / val.total) * 100).toFixed(1);
if (val instanceof ComputeRetentionProgress) {
return `${pct}%`;
} else {
return tr.deckConfigPercentOfReviews({ pct, reviews: val.reviews });
}
}
function renderRetentionProgress(
val: ComputeRetentionProgress | undefined,
): String {
if (!val) {
return "";
}
return tr.deckConfigIterations({ count: val.current });
}
function estimatedRetention(retention: number): String {
if (!retention) {
return "";
}
return tr.deckConfigPredictedOptimalRetention({ num: retention.toFixed(2) });
}
</script>
<SpinBoxFloatRow
bind:value={$config.desiredRetention}
defaultValue={defaults.desiredRetention}
min={0.7}
max={0.99}
>
<SettingTitle on:click={() => openHelpModal("desiredRetention")}>
{tr.deckConfigDesiredRetention()}
</SettingTitle>
</SpinBoxFloatRow>
<Warning warning={desiredRetentionWarning} className={retentionWarningClass} />
<div class="ms-1 me-1">
<WeightsInputRow
bind:value={$config.fsrsWeights}
defaultValue={[]}
defaults={defaults.fsrsWeights}
>
<SettingTitle on:click={() => openHelpModal("modelWeights")}>
{tr.deckConfigWeights()}
</SettingTitle>
</WeightsInputRow>
<input
bind:value={$config.weightSearch}
placeholder={defaultWeightSearch}
class="w-100 mb-1"
/>
<DateInput bind:date={$config.ignoreRevlogsBeforeDate}>
<SettingTitle on:click={() => openHelpModal("ignoreBefore")}>
{tr.deckConfigIgnoreBefore()}
</SettingTitle>
</DateInput>
<button
class="btn {computingWeights ? 'btn-warning' : 'btn-primary'}"
disabled={!computingWeights && computing}
on:click={() => computeWeights()}
>
{#if computingWeights}
{tr.actionsCancel()}
{:else}
{tr.deckConfigOptimizeButton()}
{/if}
</button>
<button
class="btn {checkingWeights ? 'btn-warning' : 'btn-primary'}"
disabled={!checkingWeights && computing}
on:click={() => checkWeights()}
>
{#if checkingWeights}
{tr.actionsCancel()}
{:else}
{tr.deckConfigEvaluateButton()}
{/if}
</button>
{#if computingWeights || checkingWeights}<div>
{computeWeightsProgressString}
</div>{/if}
<Warning warning={lastOptimizationWarning} className="alert-warning" />
</div>
<div class="m-2">
<SwitchRow bind:value={$fsrsReschedule} defaultValue={false}>
<SettingTitle on:click={() => openHelpModal("rescheduleCardsOnChange")}>
<GlobalLabel title={tr.deckConfigRescheduleCardsOnChange()} />
</SettingTitle>
</SwitchRow>
{#if $fsrsReschedule}
<Warning warning={tr.deckConfigRescheduleCardsWarning()} />
{/if}
</div>
<div class="m-2">
<details>
<summary>{tr.deckConfigComputeOptimalRetention()} (experimental)</summary>
<SpinBoxRow
bind:value={optimalRetentionRequest.daysToSimulate}
defaultValue={365}
min={1}
max={3650}
>
<SettingTitle on:click={() => openHelpModal("computeOptimalRetention")}>
Days to simulate
</SettingTitle>
</SpinBoxRow>
<button
class="btn {computingRetention ? 'btn-warning' : 'btn-primary'}"
disabled={!computingRetention && computing}
on:click={() => computeRetention()}
>
{#if computingRetention}
{tr.actionsCancel()}
{:else}
{tr.deckConfigComputeButton()}
{/if}
</button>
{#if optimalRetention}
{estimatedRetention(optimalRetention)}
{#if parseFloat(optimalRetention.toFixed(2)) > $config.desiredRetention}
<Warning
warning="Your desired retention is below optimal. Increasing it is recommended."
className="alert-warning"
/>
{/if}
{/if}
<div>{computeRetentionProgressString}</div>
</details>
</div>
<style>
</style>