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",
|
|
|
|
*/
|
|
|
|
|
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 type { Stats, Cards } from "../lib/proto";
|
2021-01-30 01:13:47 +01:00
|
|
|
import {
|
|
|
|
extent,
|
|
|
|
histogram,
|
|
|
|
rollup,
|
|
|
|
sum,
|
|
|
|
scaleLinear,
|
|
|
|
scaleSequential,
|
|
|
|
interpolateGreens,
|
|
|
|
} from "d3";
|
|
|
|
import type { Bin } from "d3";
|
2021-10-07 15:31:49 +02:00
|
|
|
import * as tr from "../lib/ftl";
|
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";
|
2020-11-01 05:26:58 +01:00
|
|
|
import type { HistogramData } from "./histogram-graph";
|
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";
|
2021-03-26 11:23:43 +01:00
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
import { GraphRange } from "./graph-helpers";
|
2021-01-26 12:41:22 +01:00
|
|
|
import type { TableDatum, SearchDispatch } from "./graph-helpers";
|
2020-06-26 11:25:02 +02:00
|
|
|
|
|
|
|
export interface GraphData {
|
|
|
|
dueCounts: Map<number, number>;
|
2020-08-10 06:37:23 +02:00
|
|
|
haveBacklog: boolean;
|
2020-06-26 11:25:02 +02:00
|
|
|
}
|
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
export function gatherData(data: Stats.GraphsResponse): GraphData {
|
2021-07-10 11:52:31 +02:00
|
|
|
const isLearning = (card: Cards.Card): boolean =>
|
2021-01-07 14:43:34 +01:00
|
|
|
[CardQueue.Learn, CardQueue.PreviewRepeat].includes(card.queue);
|
|
|
|
|
2020-08-10 06:37:23 +02:00
|
|
|
let haveBacklog = false;
|
2021-07-10 11:52:31 +02:00
|
|
|
const due = (data.cards as Cards.Card[])
|
|
|
|
.filter((c: Cards.Card) => {
|
2021-01-07 14:43:34 +01:00
|
|
|
// reviews
|
|
|
|
return (
|
2020-07-24 03:09:22 +02:00
|
|
|
[CardQueue.Review, CardQueue.DayLearn].includes(c.queue) ||
|
2021-01-07 14:43:34 +01:00
|
|
|
// or learning cards
|
|
|
|
isLearning(c)
|
|
|
|
);
|
|
|
|
})
|
2021-07-10 11:52:31 +02:00
|
|
|
.map((c: Cards.Card) => {
|
2021-01-07 14:43:34 +01:00
|
|
|
let dueDay: number;
|
|
|
|
|
|
|
|
if (isLearning(c)) {
|
2021-01-07 19:36:08 +01:00
|
|
|
const offset = c.due - data.nextDayAtSecs;
|
2021-01-07 14:43:34 +01:00
|
|
|
dueDay = Math.floor(offset / 86_400) + 1;
|
2020-07-26 02:28:56 +02:00
|
|
|
} else {
|
2020-07-27 04:10:45 +02:00
|
|
|
// - testing just odue fails on day 1
|
|
|
|
// - testing just odid fails on lapsed cards that
|
|
|
|
// have due calculated at regraduation time
|
2020-08-31 09:09:49 +02:00
|
|
|
const due = c.originalDeckId && c.originalDue ? c.originalDue : c.due;
|
2021-01-07 14:43:34 +01:00
|
|
|
dueDay = due - data.daysElapsed;
|
2020-07-26 02:28:56 +02:00
|
|
|
}
|
2021-01-07 14:43:34 +01:00
|
|
|
|
|
|
|
haveBacklog = haveBacklog || dueDay < 0;
|
|
|
|
|
2021-01-07 19:36:08 +01:00
|
|
|
return dueDay;
|
2020-07-26 02:28:56 +02:00
|
|
|
});
|
2020-07-24 03:09:22 +02:00
|
|
|
|
2020-06-26 11:25:02 +02:00
|
|
|
const dueCounts = rollup(
|
|
|
|
due,
|
|
|
|
(v) => v.length,
|
2021-10-19 01:06:00 +02:00
|
|
|
(d) => d,
|
2020-06-26 11:25:02 +02:00
|
|
|
);
|
2020-08-10 06:37:23 +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]);
|
|
|
|
}
|
|
|
|
|
2021-06-20 07:49:20 +02:00
|
|
|
export interface FutureDueResponse {
|
2020-08-10 06:37:23 +02:00
|
|
|
histogramData: HistogramData | null;
|
|
|
|
tableData: TableDatum[];
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:33:18 +01:00
|
|
|
function makeQuery(start: number, end: number): string {
|
|
|
|
if (start === end) {
|
|
|
|
return `"prop:due=${start}"`;
|
2021-01-25 17:36:04 +01:00
|
|
|
} else {
|
2021-01-25 16:33:18 +01:00
|
|
|
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,
|
2020-07-17 05:46:06 +02:00
|
|
|
range: GraphRange,
|
2020-06-28 11:34:19 +02:00
|
|
|
backlog: boolean,
|
2021-01-26 12:41:22 +01:00
|
|
|
dispatch: SearchDispatch,
|
2021-10-19 01:06:00 +02:00
|
|
|
browserLinksSupported: boolean,
|
2021-06-20 07:49:20 +02:00
|
|
|
): FutureDueResponse {
|
2020-08-10 06:37:23 +02:00
|
|
|
const output = { histogramData: null, tableData: [] };
|
2020-06-26 11:25:02 +02:00
|
|
|
// get min/max
|
|
|
|
const data = sourceData.dueCounts;
|
|
|
|
if (!data) {
|
2020-08-10 06:37:23 +02:00
|
|
|
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) {
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.Month:
|
2020-06-26 11:25:02 +02:00
|
|
|
xMax = 31;
|
|
|
|
break;
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.ThreeMonths:
|
2020-06-26 11:25:02 +02:00
|
|
|
xMax = 90;
|
|
|
|
break;
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.Year:
|
2020-06-26 11:25:02 +02:00
|
|
|
xMax = 365;
|
|
|
|
break;
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.AllTime:
|
2020-06-26 11:25:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// cap bars to available range
|
|
|
|
const desiredBars = Math.min(70, xMax! - xMin!);
|
|
|
|
|
2020-08-04 07:15:57 +02:00
|
|
|
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)) {
|
2020-08-10 06:37:23 +02:00
|
|
|
return output;
|
2020-07-06 06:01:49 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 07:35:07 +02:00
|
|
|
const adjustedRange = scaleLinear().range([0.7, 0.3]);
|
2020-06-28 06:21:31 +02:00
|
|
|
const colourScale = scaleSequential((n) =>
|
2021-10-19 01:06:00 +02:00
|
|
|
interpolateGreens(adjustedRange(n)!),
|
2020-08-04 07:15:57 +02:00
|
|
|
).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,
|
2021-10-19 01:06:00 +02:00
|
|
|
_percent: number,
|
2020-06-28 11:34:19 +02:00
|
|
|
): string {
|
2021-03-26 11:23:43 +01:00
|
|
|
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
|
|
|
});
|
2021-03-26 11:23:43 +01:00
|
|
|
const totalLabel = tr.statisticsRunningTotal();
|
2020-06-28 11:34:19 +02:00
|
|
|
|
|
|
|
return `${days}:<br>${cards}<br>${totalLabel}: ${cumulative}`;
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:33:18 +01:00
|
|
|
function onClick(bin: Bin<number, number>): void {
|
2021-01-08 13:59:33 +01:00
|
|
|
const start = bin.x0!;
|
|
|
|
const end = bin.x1! - 1;
|
2021-01-25 16:33:18 +01:00
|
|
|
const query = makeQuery(start, end);
|
|
|
|
dispatch("search", { query });
|
2021-01-08 13:59:33 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 07:15:57 +02:00
|
|
|
const periodDays = xMax! - xMin!;
|
|
|
|
const tableData = [
|
|
|
|
{
|
2021-03-26 11:23:43 +01:00
|
|
|
label: tr.statisticsTotal(),
|
|
|
|
value: tr.statisticsReviews({ reviews: total }),
|
2020-08-04 07:15:57 +02:00
|
|
|
},
|
|
|
|
{
|
2021-03-26 11:23:43 +01:00
|
|
|
label: tr.statisticsAverage(),
|
|
|
|
value: tr.statisticsReviewsPerDay({
|
2020-08-04 07:15:57 +02:00
|
|
|
count: Math.round(total / periodDays),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
2021-03-26 11:23:43 +01:00
|
|
|
label: tr.statisticsDueTomorrow(),
|
|
|
|
value: tr.statisticsReviews({
|
2020-08-04 07:15:57 +02:00
|
|
|
reviews: sourceData.dueCounts.get(1) ?? 0,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-08-10 06:37:23 +02:00
|
|
|
return {
|
|
|
|
histogramData: {
|
2020-08-04 07:15:57 +02:00
|
|
|
scale: x,
|
|
|
|
bins,
|
|
|
|
total,
|
|
|
|
hoverText,
|
2021-01-25 17:36:04 +01:00
|
|
|
onClick: browserLinksSupported ? onClick : null,
|
2020-08-04 07:15:57 +02:00
|
|
|
showArea: true,
|
|
|
|
colourScale,
|
|
|
|
binValue,
|
|
|
|
},
|
|
|
|
tableData,
|
2020-08-10 06:37:23 +02:00
|
|
|
};
|
2020-06-26 11:25:02 +02:00
|
|
|
}
|