anki/ts/graphs/future-due.ts

208 lines
5.6 KiB
TypeScript
Raw Normal View History

2020-06-26 11:25:02 +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-explicit-any: "off",
*/
import type { Bin } from "d3";
import {
extent,
histogram,
interpolateGreens,
rollup,
scaleLinear,
scaleSequential,
sum,
} from "d3";
update to latest rules_nodejs & switch to ts_project ts_library() is deprecated and will presumably be dropped from a future rules_nodejs, and it wasn't working with the jest tests after updating, so we switch over to ts_project(). There are some downsides: - It's a bit slower, as the worker mode doesn't appear to function at the moment. - Getting it working with a mix of source files and generated files was quite tricky, especially as things behave differently on Windows, and differently when editing with VS Code. Solved with a small patch to the rules, and a wrapper script that copies everything into the bin folder first. To keep VS Code working correctly as well, the built files are symlinked into the source folder. - TS libraries are not implicitly linked to node_modules, so they can't be imported with an absolute name like "lib/proto" - we need to use relative paths like "../lib/proto" instead. Adjusting "paths" in tsconfig.json makes it work for TS compilation, but then it fails at the esbuild stage. We could resolve it by wrapping the TS libraries in a subsequent js_library() call, but that has the downside of losing the transient dependencies, meaning they need to be listed again. Alternatively we might be able to solve it in the future by adjusting esbuild, but for now the paths have been made relative to keep things simple. Upsides: - Along with updates to the Svelte tooling, Svelte typing has improved. All exports made in a Svelte file are now visible to other files that import them, and we no longer rebuild the Svelte files when TS files are updated, as the Svelte files do no type checking themselves, and are just a simple transpilation. Svelte-check now works on Windows again, and there should be no errors when editing in VS Code after you've built the project. The only downside seems to be that cmd+clicking on a Svelte imports jumps to the .d.ts file instead of the original now; presumably they'll fix that in a future plugin update. - Each subfolder now has its own tsconfig.json, and tsc can be called directly for testing purposes (but beware it will place build products in the source tree): ts/node_modules/.bin/tsc -b ts - We can drop the custom esbuild_toolchain, as it's included in the latest rules_nodejs. Other changes: - "image_module_support" is moved into lib/, and imported with <reference types=...> - Images are now imported directly from their npm package; the extra copy step has been removed. Windows users may need to use "bazel clean" before building this, due to old files lying around in the build folder.
2021-09-30 14:16:29 +02:00
import { CardQueue } from "../lib/cards";
import * as tr from "../lib/ftl";
import { localizedNumber } from "../lib/i18n";
import type { Cards, Stats } from "../lib/proto";
update to latest rules_nodejs & switch to ts_project ts_library() is deprecated and will presumably be dropped from a future rules_nodejs, and it wasn't working with the jest tests after updating, so we switch over to ts_project(). There are some downsides: - It's a bit slower, as the worker mode doesn't appear to function at the moment. - Getting it working with a mix of source files and generated files was quite tricky, especially as things behave differently on Windows, and differently when editing with VS Code. Solved with a small patch to the rules, and a wrapper script that copies everything into the bin folder first. To keep VS Code working correctly as well, the built files are symlinked into the source folder. - TS libraries are not implicitly linked to node_modules, so they can't be imported with an absolute name like "lib/proto" - we need to use relative paths like "../lib/proto" instead. Adjusting "paths" in tsconfig.json makes it work for TS compilation, but then it fails at the esbuild stage. We could resolve it by wrapping the TS libraries in a subsequent js_library() call, but that has the downside of losing the transient dependencies, meaning they need to be listed again. Alternatively we might be able to solve it in the future by adjusting esbuild, but for now the paths have been made relative to keep things simple. Upsides: - Along with updates to the Svelte tooling, Svelte typing has improved. All exports made in a Svelte file are now visible to other files that import them, and we no longer rebuild the Svelte files when TS files are updated, as the Svelte files do no type checking themselves, and are just a simple transpilation. Svelte-check now works on Windows again, and there should be no errors when editing in VS Code after you've built the project. The only downside seems to be that cmd+clicking on a Svelte imports jumps to the .d.ts file instead of the original now; presumably they'll fix that in a future plugin update. - Each subfolder now has its own tsconfig.json, and tsc can be called directly for testing purposes (but beware it will place build products in the source tree): ts/node_modules/.bin/tsc -b ts - We can drop the custom esbuild_toolchain, as it's included in the latest rules_nodejs. Other changes: - "image_module_support" is moved into lib/, and imported with <reference types=...> - Images are now imported directly from their npm package; the extra copy step has been removed. Windows users may need to use "bazel clean" before building this, due to old files lying around in the build folder.
2021-09-30 14:16:29 +02:00
import { dayLabel } from "../lib/time";
import type { SearchDispatch, TableDatum } from "./graph-helpers";
import { GraphRange } from "./graph-helpers";
import type { HistogramData } from "./histogram-graph";
2020-06-26 11:25:02 +02:00
export interface GraphData {
dueCounts: Map<number, number>;
haveBacklog: boolean;
2020-06-26 11:25:02 +02:00
}
export function gatherData(data: Stats.GraphsResponse): GraphData {
2021-07-10 11:52:31 +02:00
const isLearning = (card: Cards.Card): boolean =>
[CardQueue.Learn, CardQueue.PreviewRepeat].includes(card.queue);
let haveBacklog = false;
2021-07-10 11:52:31 +02:00
const due = (data.cards as Cards.Card[])
.filter((c: Cards.Card) => {
// reviews
return (
[CardQueue.Review, CardQueue.DayLearn].includes(c.queue) ||
// or learning cards
isLearning(c)
);
})
2021-07-10 11:52:31 +02:00
.map((c: Cards.Card) => {
// - testing just odue fails on day 1
// - testing just odid fails on lapsed cards that
// have due calculated at regraduation time
const due = c.originalDeckId && c.originalDue ? c.originalDue : c.due;
let dueDay: number;
if (isLearning(c)) {
const offset = due - data.nextDayAtSecs;
dueDay = Math.floor(offset / 86_400) + 1;
} else {
dueDay = due - data.daysElapsed;
}
haveBacklog = haveBacklog || dueDay < 0;
2021-01-07 19:36:08 +01:00
return dueDay;
});
2020-06-26 11:25:02 +02:00
const dueCounts = rollup(
due,
(v) => v.length,
(d) => d,
2020-06-26 11:25:02 +02:00
);
return { dueCounts, haveBacklog };
2020-06-26 11:25:02 +02:00
}
function binValue(d: Bin<Map<number, number>, number>): number {
return sum(d, (d) => d[1]);
}
export interface FutureDueResponse {
histogramData: HistogramData | null;
tableData: TableDatum[];
}
function makeQuery(start: number, end: number): string {
if (start === end) {
return `"prop:due=${start}"`;
2021-01-25 17:36:04 +01:00
} else {
const fromQuery = `"prop:due>=${start}"`;
const tillQuery = `"prop:due<=${end}"`;
return `${fromQuery} AND ${tillQuery}`;
}
}
2020-06-26 11:25:02 +02:00
export function buildHistogram(
sourceData: GraphData,
range: GraphRange,
2020-06-28 11:34:19 +02:00
backlog: boolean,
dispatch: SearchDispatch,
browserLinksSupported: boolean,
): FutureDueResponse {
const output = { histogramData: null, tableData: [] };
2020-06-26 11:25:02 +02:00
// get min/max
const data = sourceData.dueCounts;
if (!data) {
return output;
2020-06-26 11:25:02 +02:00
}
2020-06-28 11:34:19 +02:00
const [xMinOrig, origXMax] = extent<number>(data.keys());
let xMin = xMinOrig;
if (!backlog) {
xMin = 0;
}
2020-06-26 11:25:02 +02:00
let xMax = origXMax;
// cap max to selected range
switch (range) {
case GraphRange.Month:
2020-06-26 11:25:02 +02:00
xMax = 31;
break;
case GraphRange.ThreeMonths:
2020-06-26 11:25:02 +02:00
xMax = 90;
break;
case GraphRange.Year:
2020-06-26 11:25:02 +02:00
xMax = 365;
break;
case GraphRange.AllTime:
2020-06-26 11:25:02 +02:00
break;
}
// cap bars to available range
const desiredBars = Math.min(70, xMax! - xMin!);
const x = scaleLinear().domain([xMin!, xMax!]);
2020-06-26 11:25:02 +02:00
const bins = histogram()
.value((m) => {
return m[0];
})
.domain(x.domain() as any)
.thresholds(x.ticks(desiredBars))(data.entries() as any);
2020-07-06 06:01:49 +02:00
// empty graph?
if (!sum(bins, (bin) => bin.length)) {
return output;
2020-07-06 06:01:49 +02:00
}
const xTickFormat = (n: number): string => localizedNumber(n);
const adjustedRange = scaleLinear().range([0.7, 0.3]);
2020-06-28 06:21:31 +02:00
const colourScale = scaleSequential((n) =>
interpolateGreens(adjustedRange(n)!),
).domain([xMin!, xMax!]);
2020-06-26 11:25:02 +02:00
const total = sum(bins as any, binValue);
2020-06-28 11:34:19 +02:00
function hoverText(
2021-01-30 02:08:01 +01:00
bin: Bin<number, number>,
2020-06-28 11:34:19 +02:00
cumulative: number,
_percent: number,
2020-06-28 11:34:19 +02:00
): string {
const days = dayLabel(bin.x0!, bin.x1!);
const cards = tr.statisticsCardsDue({
2021-01-30 02:08:01 +01:00
cards: binValue(bin as any),
2020-06-28 11:34:19 +02:00
});
const totalLabel = tr.statisticsRunningTotal();
2020-06-28 11:34:19 +02:00
return `${days}:<br>${cards}<br>${totalLabel}: ${localizedNumber(cumulative)}`;
2020-06-28 11:34:19 +02:00
}
function onClick(bin: Bin<number, number>): void {
const start = bin.x0!;
const end = bin.x1! - 1;
const query = makeQuery(start, end);
dispatch("search", { query });
}
const periodDays = xMax! - xMin!;
const tableData = [
{
label: tr.statisticsTotal(),
value: tr.statisticsReviews({ reviews: total }),
},
{
label: tr.statisticsAverage(),
value: tr.statisticsReviewsPerDay({
count: Math.round(total / periodDays),
}),
},
{
label: tr.statisticsDueTomorrow(),
value: tr.statisticsReviews({
reviews: sourceData.dueCounts.get(1) ?? 0,
}),
},
];
return {
histogramData: {
scale: x,
bins,
total,
hoverText,
2021-01-25 17:36:04 +01:00
onClick: browserLinksSupported ? onClick : null,
showArea: true,
colourScale,
binValue,
xTickFormat,
},
tableData,
};
2020-06-26 11:25:02 +02:00
}