anki/ts/graphs/today.ts
Damien Elmes 37151213cd Move more of the graph processing into the backend
The existing architecture serializes all cards and revlog entries in
the search range into a protobuf message, which the web frontend needs
to decode and then process. The thinking at the time was that this would
make it easier for add-ons to add extra graphs, but in the ~2.5 years
since the new graphs were introduced, no add-ons appear to have taken
advantage of it.

The cards and revlog entries can grow quite large on large collections -
on a collection I tested with approximately 2.5M reviews, the serialized
data is about 110MB, which is a lot to have to deserialize in JavaScript.

This commit shifts the preliminary processing of the data to the Rust end,
which means the data is able to be processed faster, and less needs to
be sent to the frontend. On the test collection above, this reduces the
serialized data from about 110MB to about 160KB, resulting in a more
than 2x performance improvement, and reducing frontend memory usage from
about 400MB to about 40MB.

This also makes #2043 more feasible - while it is still about 50-100%
slower than protobufjs, with the much smaller message size, the difference
is only about 10ms.
2022-12-16 21:42:17 +10:00

52 lines
1.7 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import * as tr from "@tslib/ftl";
import { localizedNumber } from "@tslib/i18n";
import type { Stats } from "@tslib/proto";
import { studiedToday } from "@tslib/time";
export interface TodayData {
title: string;
lines: string[];
}
export function gatherData(data: Stats.GraphsResponse): TodayData {
let lines: string[];
const today = data.today!;
if (today.answerCount) {
const studiedTodayText = studiedToday(today.answerCount, today.answerMillis / 1000);
const againCount = today.answerCount - today.correctCount;
let againCountText = tr.statisticsTodayAgainCount();
againCountText += ` ${againCount} (${
localizedNumber(
(againCount / today.answerCount) * 100,
)
}%)`;
const typeCounts = tr.statisticsTodayTypeCounts({
learnCount: today.learnCount,
reviewCount: today.reviewCount,
relearnCount: today.relearnCount,
filteredCount: today.earlyReviewCount,
});
let matureText: string;
if (today.matureCount) {
matureText = tr.statisticsTodayCorrectMature({
correct: today.matureCorrect,
total: today.matureCount,
percent: (today.matureCorrect / today.matureCount) * 100,
});
} else {
matureText = tr.statisticsTodayNoMatureCards();
}
lines = [studiedTodayText, againCountText, typeCounts, matureText];
} else {
lines = [tr.statisticsTodayNoCards()];
}
return {
title: tr.statisticsTodayTitle(),
lines,
};
}