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.
243 lines
7.5 KiB
TypeScript
243 lines
7.5 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 {
|
|
axisBottom,
|
|
axisLeft,
|
|
interpolateRdYlGn,
|
|
pointer,
|
|
scaleBand,
|
|
scaleLinear,
|
|
scaleSequential,
|
|
select,
|
|
sum,
|
|
} from "d3";
|
|
|
|
import type { GraphBounds } from "./graph-helpers";
|
|
import { GraphRange } from "./graph-helpers";
|
|
import { setDataAvailable } from "./graph-helpers";
|
|
import { hideTooltip, showTooltip } from "./tooltip";
|
|
|
|
/// 4 element array
|
|
type ButtonCounts = number[];
|
|
|
|
export interface GraphData {
|
|
learning: ButtonCounts;
|
|
young: ButtonCounts;
|
|
mature: ButtonCounts;
|
|
}
|
|
|
|
export function gatherData(data: Stats.GraphsResponse, range: GraphRange): GraphData {
|
|
const buttons = data.buttons!;
|
|
switch (range) {
|
|
case GraphRange.Month:
|
|
return buttons.oneMonth!;
|
|
case GraphRange.ThreeMonths:
|
|
return buttons.threeMonths!;
|
|
case GraphRange.Year:
|
|
return buttons.oneYear!;
|
|
case GraphRange.AllTime:
|
|
return buttons.allTime!;
|
|
}
|
|
}
|
|
|
|
type GroupKind = "learning" | "young" | "mature";
|
|
|
|
interface Datum {
|
|
buttonNum: number;
|
|
group: GroupKind;
|
|
count: number;
|
|
}
|
|
|
|
interface TotalCorrect {
|
|
total: number;
|
|
correct: number;
|
|
percent: string;
|
|
}
|
|
|
|
export function renderButtons(
|
|
svgElem: SVGElement,
|
|
bounds: GraphBounds,
|
|
origData: Stats.GraphsResponse,
|
|
range: GraphRange,
|
|
): void {
|
|
const sourceData = gatherData(origData, range);
|
|
const data = [
|
|
...sourceData.learning.map((count: number, idx: number) => {
|
|
return {
|
|
buttonNum: idx + 1,
|
|
group: "learning",
|
|
count,
|
|
} as Datum;
|
|
}),
|
|
...sourceData.young.map((count: number, idx: number) => {
|
|
return {
|
|
buttonNum: idx + 1,
|
|
group: "young",
|
|
count,
|
|
} as Datum;
|
|
}),
|
|
...sourceData.mature.map((count: number, idx: number) => {
|
|
return {
|
|
buttonNum: idx + 1,
|
|
group: "mature",
|
|
count,
|
|
} as Datum;
|
|
}),
|
|
];
|
|
|
|
const totalCorrect = (kind: GroupKind): TotalCorrect => {
|
|
const groupData = data.filter((d) => d.group == kind);
|
|
const total = sum(groupData, (d) => d.count);
|
|
const correct = sum(
|
|
groupData.filter((d) => d.buttonNum > 1),
|
|
(d) => d.count,
|
|
);
|
|
const percent = total ? localizedNumber((correct / total) * 100) : "0";
|
|
return { total, correct, percent };
|
|
};
|
|
|
|
const totalPressedStr = (data: Datum): string => {
|
|
const groupTotal = totalCorrect(data.group).total;
|
|
const buttonTotal = data.count;
|
|
const percent = groupTotal
|
|
? localizedNumber((buttonTotal / groupTotal) * 100)
|
|
: "0";
|
|
|
|
return `${localizedNumber(buttonTotal)} (${percent}%)`;
|
|
};
|
|
|
|
const yMax = Math.max(...data.map((d) => d.count));
|
|
|
|
const svg = select(svgElem);
|
|
const trans = svg.transition().duration(600) as any;
|
|
|
|
if (!yMax) {
|
|
setDataAvailable(svg, false);
|
|
return;
|
|
} else {
|
|
setDataAvailable(svg, true);
|
|
}
|
|
|
|
const xGroup = scaleBand()
|
|
.domain(["learning", "young", "mature"])
|
|
.range([bounds.marginLeft, bounds.width - bounds.marginRight]);
|
|
svg.select<SVGGElement>(".x-ticks")
|
|
.call((selection) =>
|
|
selection.transition(trans).call(
|
|
axisBottom(xGroup)
|
|
.tickFormat(
|
|
((d: GroupKind) => {
|
|
let kind: string;
|
|
switch (d) {
|
|
case "learning":
|
|
kind = tr.statisticsCountsLearningCards();
|
|
break;
|
|
case "young":
|
|
kind = tr.statisticsCountsYoungCards();
|
|
break;
|
|
case "mature":
|
|
default:
|
|
kind = tr.statisticsCountsMatureCards();
|
|
break;
|
|
}
|
|
return `${kind} \u200e(${totalCorrect(d).percent}%)`;
|
|
}) as any,
|
|
)
|
|
.tickSizeOuter(0),
|
|
)
|
|
)
|
|
.attr("direction", "ltr");
|
|
|
|
const xButton = scaleBand()
|
|
.domain(["1", "2", "3", "4"])
|
|
.range([0, xGroup.bandwidth()])
|
|
.paddingOuter(1)
|
|
.paddingInner(0.1);
|
|
|
|
const colour = scaleSequential(interpolateRdYlGn).domain([1, 4]);
|
|
|
|
// y scale
|
|
const yTickFormat = (n: number): string => localizedNumber(n);
|
|
|
|
const y = scaleLinear()
|
|
.range([bounds.height - bounds.marginBottom, bounds.marginTop])
|
|
.domain([0, yMax]);
|
|
svg.select<SVGGElement>(".y-ticks")
|
|
.call((selection) =>
|
|
selection.transition(trans).call(
|
|
axisLeft(y)
|
|
.ticks(bounds.height / 50)
|
|
.tickSizeOuter(0)
|
|
.tickFormat(yTickFormat as any),
|
|
)
|
|
)
|
|
.attr("direction", "ltr");
|
|
|
|
// x bars
|
|
|
|
const updateBar = (sel: any): any => {
|
|
return sel
|
|
.attr("width", xButton.bandwidth())
|
|
.attr("opacity", 0.8)
|
|
.transition(trans)
|
|
.attr(
|
|
"x",
|
|
(d: Datum) => xGroup(d.group)! + xButton(d.buttonNum.toString())!,
|
|
)
|
|
.attr("y", (d: Datum) => y(d.count)!)
|
|
.attr("height", (d: Datum) => y(0)! - y(d.count)!)
|
|
.attr("fill", (d: Datum) => colour(d.buttonNum));
|
|
};
|
|
|
|
svg.select("g.bars")
|
|
.selectAll("rect")
|
|
.data(data)
|
|
.join(
|
|
(enter) =>
|
|
enter
|
|
.append("rect")
|
|
.attr("rx", 1)
|
|
.attr(
|
|
"x",
|
|
(d: Datum) => xGroup(d.group)! + xButton(d.buttonNum.toString())!,
|
|
)
|
|
.attr("y", y(0)!)
|
|
.attr("height", 0)
|
|
.call(updateBar),
|
|
(update) => update.call(updateBar),
|
|
(remove) => remove.call((remove) => remove.transition(trans).attr("height", 0).attr("y", y(0)!)),
|
|
);
|
|
|
|
// hover/tooltip
|
|
|
|
function tooltipText(d: Datum): string {
|
|
const button = tr.statisticsAnswerButtonsButtonNumber();
|
|
const timesPressed = tr.statisticsAnswerButtonsButtonPressed();
|
|
const correctStr = tr.statisticsHoursCorrect(totalCorrect(d.group));
|
|
const pressedStr = `${timesPressed}: ${totalPressedStr(d)}`;
|
|
return `${button}: ${d.buttonNum}<br>${pressedStr}<br>${correctStr}`;
|
|
}
|
|
|
|
svg.select("g.hover-columns")
|
|
.selectAll("rect")
|
|
.data(data)
|
|
.join("rect")
|
|
.attr("x", (d: Datum) => xGroup(d.group)! + xButton(d.buttonNum.toString())!)
|
|
.attr("y", () => y(yMax!)!)
|
|
.attr("width", xButton.bandwidth())
|
|
.attr("height", () => y(0)! - y(yMax!)!)
|
|
.on("mousemove", (event: MouseEvent, d: Datum) => {
|
|
const [x, y] = pointer(event, document.body);
|
|
showTooltip(tooltipText(d), x, y);
|
|
})
|
|
.on("mouseout", hideTooltip);
|
|
}
|