37151213cd
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.
190 lines
5.4 KiB
TypeScript
190 lines
5.4 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
/* eslint
|
|
@typescript-eslint/no-explicit-any: "off",
|
|
*/
|
|
|
|
import * as tr from "@tslib/ftl";
|
|
import { localizedNumber } from "@tslib/i18n";
|
|
import type { Stats } from "@tslib/proto";
|
|
import {
|
|
arc,
|
|
cumsum,
|
|
interpolate,
|
|
pie,
|
|
scaleLinear,
|
|
schemeBlues,
|
|
schemeGreens,
|
|
schemeOranges,
|
|
schemeReds,
|
|
select,
|
|
sum,
|
|
} from "d3";
|
|
|
|
import type { GraphBounds } from "./graph-helpers";
|
|
|
|
type Count = [string, number, boolean, string];
|
|
export interface GraphData {
|
|
title: string;
|
|
counts: Count[];
|
|
totalCards: string;
|
|
}
|
|
|
|
const barColours = [
|
|
schemeBlues[5][2], /* new */
|
|
schemeOranges[5][2], /* learn */
|
|
schemeReds[5][2], /* relearn */
|
|
schemeGreens[5][2], /* young */
|
|
schemeGreens[5][3], /* mature */
|
|
"#FFDC41", /* suspended */
|
|
"grey", /* buried */
|
|
];
|
|
|
|
function countCards(data: Stats.GraphsResponse, separateInactive: boolean): Count[] {
|
|
const countData = separateInactive ? data.cardCounts!.excludingInactive! : data.cardCounts!.includingInactive!;
|
|
|
|
const extraQuery = separateInactive ? "AND -(\"is:buried\" OR \"is:suspended\")" : "";
|
|
|
|
const counts: Count[] = [
|
|
[tr.statisticsCountsNewCards(), countData.newCards, true, `"is:new"${extraQuery}`],
|
|
[
|
|
tr.statisticsCountsLearningCards(),
|
|
countData.learn,
|
|
true,
|
|
`(-"is:review" AND "is:learn")${extraQuery}`,
|
|
],
|
|
[
|
|
tr.statisticsCountsRelearningCards(),
|
|
countData.relearn,
|
|
true,
|
|
`("is:review" AND "is:learn")${extraQuery}`,
|
|
],
|
|
[
|
|
tr.statisticsCountsYoungCards(),
|
|
countData.young,
|
|
true,
|
|
`("is:review" AND -"is:learn") AND "prop:ivl<21"${extraQuery}`,
|
|
],
|
|
[
|
|
tr.statisticsCountsMatureCards(),
|
|
countData.mature,
|
|
true,
|
|
`("is:review" -"is:learn") AND "prop:ivl>=21"${extraQuery}`,
|
|
],
|
|
[
|
|
tr.statisticsCountsSuspendedCards(),
|
|
countData.suspended,
|
|
separateInactive,
|
|
"\"is:suspended\"",
|
|
],
|
|
[tr.statisticsCountsBuriedCards(), countData.buried, separateInactive, "\"is:buried\""],
|
|
];
|
|
|
|
return counts;
|
|
}
|
|
|
|
export function gatherData(
|
|
data: Stats.GraphsResponse,
|
|
separateInactive: boolean,
|
|
): GraphData {
|
|
const counts = countCards(data, separateInactive);
|
|
const totalCards = localizedNumber(sum(counts, e => e[1]));
|
|
|
|
return {
|
|
title: tr.statisticsCountsTitle(),
|
|
counts,
|
|
totalCards,
|
|
};
|
|
}
|
|
|
|
export interface SummedDatum {
|
|
label: string;
|
|
// count of this particular item
|
|
count: number;
|
|
// show up in the table
|
|
show: boolean;
|
|
query: string;
|
|
// running total
|
|
total: number;
|
|
}
|
|
|
|
export interface TableDatum {
|
|
label: string;
|
|
count: string;
|
|
query: string;
|
|
percent: string;
|
|
colour: string;
|
|
}
|
|
|
|
export function renderCards(
|
|
svgElem: SVGElement,
|
|
bounds: GraphBounds,
|
|
sourceData: GraphData,
|
|
): TableDatum[] {
|
|
const summed = cumsum(sourceData.counts, (d: Count) => d[1]);
|
|
const data = Array.from(summed).map((n, idx) => {
|
|
const count = sourceData.counts[idx];
|
|
return {
|
|
label: count[0],
|
|
count: count[1],
|
|
show: count[2],
|
|
query: count[3],
|
|
idx,
|
|
total: n,
|
|
} as SummedDatum;
|
|
});
|
|
// ensuring a non-zero range makes the percentages not break
|
|
// in an empty collection
|
|
const xMax = Math.max(1, summed.slice(-1)[0]);
|
|
const x = scaleLinear().domain([0, xMax]);
|
|
const svg = select(svgElem);
|
|
const paths = svg.select(".counts");
|
|
const pieData = pie()(sourceData.counts.map((d: Count) => d[1]));
|
|
const radius = bounds.height / 2 - bounds.marginTop - bounds.marginBottom;
|
|
const arcGen = arc().innerRadius(0).outerRadius(radius);
|
|
const trans = svg.transition().duration(600) as any;
|
|
|
|
paths
|
|
.attr("transform", `translate(${radius},${radius + bounds.marginTop})`)
|
|
.selectAll("path")
|
|
.data(pieData)
|
|
.join(
|
|
(enter) =>
|
|
enter
|
|
.append("path")
|
|
.attr("fill", (_d, idx) => {
|
|
return barColours[idx];
|
|
})
|
|
.attr("d", arcGen as any),
|
|
function(update) {
|
|
return update.call((d) =>
|
|
d.transition(trans).attrTween("d", (d) => {
|
|
const interpolator = interpolate(
|
|
{ startAngle: 0, endAngle: 0 },
|
|
d,
|
|
);
|
|
return (t): string => arcGen(interpolator(t) as any) as string;
|
|
})
|
|
);
|
|
},
|
|
);
|
|
|
|
x.range([bounds.marginLeft, bounds.width - bounds.marginRight]);
|
|
|
|
const tableData = data.flatMap((d: SummedDatum, idx: number) => {
|
|
const percent = localizedNumber((d.count / xMax) * 100, 2);
|
|
return d.show
|
|
? ({
|
|
label: d.label,
|
|
count: localizedNumber(d.count),
|
|
percent: `${percent}%`,
|
|
colour: barColours[idx],
|
|
query: d.query,
|
|
} as TableDatum)
|
|
: [];
|
|
});
|
|
|
|
return tableData;
|
|
}
|