anki/ts/graphs/ease.ts

134 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-06-23 12:43:19 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
/* eslint
@typescript-eslint/no-non-null-assertion: "off",
@typescript-eslint/no-explicit-any: "off",
*/
refactor protobuf handling for split/import In order to split backend.proto into a more manageable size, the protobuf handling needed to be updated. This took more time than I would have liked, as each language handles protobuf differently: - The Python Protobuf code ignores "package" directives, and relies solely on how the files are laid out on disk. While it would have been nice to keep the generated files in a private subpackage, Protobuf gets confused if the files are located in a location that does not match their original .proto layout, so the old approach of storing them in _backend/ will not work. They now clutter up pylib/anki instead. I'm rather annoyed by that, but alternatives seem to be having to add an extra level to the Protobuf path, making the other languages suffer, or trying to hack around the issue by munging sys.modules. - Protobufjs fails to expose packages if they don't start with a capital letter, despite the fact that lowercase packages are the norm in most languages :-( This required a patch to fix. - Rust was the easiest, as Prost is relatively straightforward compared to Google's tools. The Protobuf files are now stored in /proto/anki, with a separate package for each file. I've split backend.proto into a few files as a test, but the majority of that work is still to come. The Python Protobuf building is a bit of a hack at the moment, hard-coding "proto" as the top level folder, but it seems to get the job done for now. Also changed the workspace name, as there seems to be a number of Bazel repos moving away from the more awkward reverse DNS naming style.
2021-07-10 09:50:18 +02:00
import type { Backend } from "lib/proto";
import {
extent,
histogram,
sum,
scaleLinear,
scaleSequential,
interpolateRdYlGn,
} from "d3";
2021-01-30 21:50:05 +01:00
import type { Bin, ScaleLinear } from "d3";
import { CardType } from "lib/cards";
import type { HistogramData } from "./histogram-graph";
import type { TableDatum, SearchDispatch } from "./graph-helpers";
import * as tr from "lib/i18n";
2020-06-23 12:43:19 +02:00
export interface GraphData {
eases: number[];
}
refactor protobuf handling for split/import In order to split backend.proto into a more manageable size, the protobuf handling needed to be updated. This took more time than I would have liked, as each language handles protobuf differently: - The Python Protobuf code ignores "package" directives, and relies solely on how the files are laid out on disk. While it would have been nice to keep the generated files in a private subpackage, Protobuf gets confused if the files are located in a location that does not match their original .proto layout, so the old approach of storing them in _backend/ will not work. They now clutter up pylib/anki instead. I'm rather annoyed by that, but alternatives seem to be having to add an extra level to the Protobuf path, making the other languages suffer, or trying to hack around the issue by munging sys.modules. - Protobufjs fails to expose packages if they don't start with a capital letter, despite the fact that lowercase packages are the norm in most languages :-( This required a patch to fix. - Rust was the easiest, as Prost is relatively straightforward compared to Google's tools. The Protobuf files are now stored in /proto/anki, with a separate package for each file. I've split backend.proto into a few files as a test, but the majority of that work is still to come. The Python Protobuf building is a bit of a hack at the moment, hard-coding "proto" as the top level folder, but it seems to get the job done for now. Also changed the workspace name, as there seems to be a number of Bazel repos moving away from the more awkward reverse DNS naming style.
2021-07-10 09:50:18 +02:00
export function gatherData(data: Backend.GraphsResponse): GraphData {
const eases = (data.cards as Backend.Card[])
.filter((c) => [CardType.Review, CardType.Relearn].includes(c.ctype))
.map((c) => c.easeFactor / 10);
2020-06-23 12:43:19 +02:00
return { eases };
}
function makeQuery(start: number, end: number): string {
if (start === end) {
return `"prop:ease=${start / 100}"`;
}
const fromQuery = `"prop:ease>=${start / 100}"`;
const tillQuery = `"prop:ease<${(end + 1) / 100}"`;
return `${fromQuery} AND ${tillQuery}`;
}
2021-01-30 21:50:05 +01:00
function getAdjustedScaleAndTicks(
min: number,
max: number,
desiredBars: number
): [ScaleLinear<number, number, never>, number[]] {
const prescale = scaleLinear().domain([min, max]).nice();
const ticks = prescale.ticks(desiredBars);
const predomain = prescale.domain() as [number, number];
const minOffset = min - predomain[0];
const tickSize = ticks[1] - ticks[0];
if (minOffset === 0 || (minOffset % tickSize !== 0 && tickSize % minOffset !== 0)) {
return [prescale, ticks];
}
const add = (n: number): number => n + minOffset;
return [
scaleLinear().domain(predomain.map(add) as [number, number]),
ticks.map(add),
];
}
2020-08-21 04:58:02 +02:00
export function prepareData(
data: GraphData,
dispatch: SearchDispatch,
2021-01-25 17:36:04 +01:00
browserLinksSupported: boolean
2020-08-21 04:58:02 +02:00
): [HistogramData | null, TableDatum[]] {
2020-06-23 12:43:19 +02:00
// get min/max
const allEases = data.eases;
if (!allEases.length) {
2020-08-21 04:58:02 +02:00
return [null, []];
2020-06-23 12:43:19 +02:00
}
const total = allEases.length;
const [, origXMax] = extent(allEases);
2020-06-23 12:43:19 +02:00
const xMin = 130;
2021-01-30 21:50:05 +01:00
const xMax = origXMax! + 1;
2020-06-23 12:43:19 +02:00
const desiredBars = 20;
2021-01-30 21:50:05 +01:00
const [scale, ticks] = getAdjustedScaleAndTicks(xMin, xMax, desiredBars);
2020-06-23 12:43:19 +02:00
const bins = histogram()
2021-01-30 21:50:05 +01:00
.domain(scale.domain() as [number, number])
.thresholds(ticks)(allEases);
2020-06-23 12:43:19 +02:00
const colourScale = scaleSequential(interpolateRdYlGn).domain([xMin, 300]);
2021-01-30 02:08:01 +01:00
function hoverText(bin: Bin<number, number>, _percent: number): string {
2020-06-28 12:52:38 +02:00
const minPct = Math.floor(bin.x0!);
const maxPct = Math.floor(bin.x1!);
const percent = maxPct - minPct <= 10 ? `${bin.x0}%` : `${bin.x0}%-${bin.x1}%`;
return tr.statisticsCardEaseTooltip({
2020-06-28 12:52:38 +02:00
cards: bin.length,
percent,
});
}
function onClick(bin: Bin<number, number>): void {
const start = bin.x0!;
const end = bin.x1! - 1;
const query = makeQuery(start, end);
dispatch("search", { query });
}
2020-08-21 04:58:02 +02:00
const xTickFormat = (num: number): string => `${num.toFixed(0)}%`;
const tableData = [
{
label: tr.statisticsAverageEase(),
2020-08-21 04:58:02 +02:00
value: xTickFormat(sum(allEases) / total),
},
];
return [
{
scale,
bins,
total,
hoverText,
2021-01-25 17:36:04 +01:00
onClick: browserLinksSupported ? onClick : null,
colourScale,
showArea: false,
xTickFormat,
},
2020-08-21 04:58:02 +02:00
tableData,
];
2020-06-23 12:43:19 +02:00
}