anki/ts/src/stats/intervals.ts

135 lines
3.8 KiB
TypeScript
Raw Normal View History

// 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",
2020-06-23 05:43:23 +02:00
*/
2020-06-23 05:43:23 +02:00
import pb from "../backend/proto";
2020-08-04 07:28:41 +02:00
import { extent, histogram, quantile, sum, mean } from "d3-array";
2020-06-23 12:43:19 +02:00
import { scaleLinear, scaleSequential } from "d3-scale";
import { CardQueue } from "../cards";
2020-06-23 11:07:47 +02:00
import { HistogramData } from "./histogram-graph";
2020-06-23 12:43:19 +02:00
import { interpolateBlues } from "d3-scale-chromatic";
2020-06-28 12:52:38 +02:00
import { I18n } from "../i18n";
2020-08-04 07:28:41 +02:00
import { TableDatum } from "./graphs";
import { timeSpan } from "../time";
export interface IntervalGraphData {
intervals: number[];
}
export enum IntervalRange {
Month = 0,
Percentile50 = 1,
Percentile95 = 2,
2020-08-05 06:50:08 +02:00
All = 3,
}
export function gatherIntervalData(data: pb.BackendProto.GraphsOut): IntervalGraphData {
2020-06-24 01:41:07 +02:00
const intervals = (data.cards as pb.BackendProto.Card[])
.filter((c) => c.queue == CardQueue.Review)
.map((c) => c.interval);
return { intervals };
}
2020-06-28 12:52:38 +02:00
export function intervalLabel(
i18n: I18n,
daysStart: number,
daysEnd: number,
cards: number
2020-06-24 01:41:07 +02:00
): string {
2020-06-28 12:52:38 +02:00
if (daysEnd - daysStart <= 1) {
// singular
return i18n.tr(i18n.TR.STATISTICS_INTERVALS_DAY_SINGLE, {
day: daysStart,
cards,
});
} else {
// range
return i18n.tr(i18n.TR.STATISTICS_INTERVALS_DAY_RANGE, {
daysStart,
daysEnd: daysEnd - 1,
2020-06-28 12:52:38 +02:00
cards,
});
}
2020-06-23 10:40:53 +02:00
}
2020-06-23 11:07:47 +02:00
export function prepareIntervalData(
data: IntervalGraphData,
2020-06-28 12:52:38 +02:00
range: IntervalRange,
i18n: I18n
2020-08-04 07:28:41 +02:00
): [HistogramData | null, TableDatum[]] {
// get min/max
const allIntervals = data.intervals;
2020-06-23 12:43:19 +02:00
if (!allIntervals.length) {
2020-08-04 07:28:41 +02:00
return [null, []];
2020-06-23 12:43:19 +02:00
}
2020-06-28 12:52:38 +02:00
const [_xMinOrig, origXMax] = extent(allIntervals);
2020-06-23 05:43:23 +02:00
let xMax = origXMax;
// cap max to selected range
switch (range) {
2020-06-23 05:43:23 +02:00
case IntervalRange.Month:
xMax = Math.min(xMax!, 31);
break;
case IntervalRange.Percentile50:
xMax = quantile(allIntervals, 0.5);
break;
case IntervalRange.Percentile95:
xMax = quantile(allIntervals, 0.95);
break;
case IntervalRange.All:
break;
}
2020-06-28 12:52:38 +02:00
const xMin = 0;
2020-06-23 10:40:53 +02:00
xMax = xMax! + 1;
2020-06-23 05:43:23 +02:00
// cap bars to available range
const desiredBars = Math.min(70, xMax! - xMin!);
2020-06-23 05:43:23 +02:00
2020-06-23 10:40:53 +02:00
const scale = scaleLinear().domain([xMin!, xMax!]).nice();
const bins = histogram()
.domain(scale.domain() as any)
.thresholds(scale.ticks(desiredBars))(allIntervals);
2020-06-23 05:43:23 +02:00
2020-07-06 06:01:49 +02:00
// empty graph?
const totalInPeriod = sum(bins, (bin) => bin.length);
if (!totalInPeriod) {
2020-08-04 07:28:41 +02:00
return [null, []];
2020-07-06 06:01:49 +02:00
}
const adjustedRange = scaleLinear().range([0.7, 0.3]);
const colourScale = scaleSequential((n) =>
interpolateBlues(adjustedRange(n))
).domain([xMax!, xMin!]);
2020-06-23 12:43:19 +02:00
2020-06-28 12:52:38 +02:00
function hoverText(
data: HistogramData,
binIdx: number,
_cumulative: number,
percent: number
): string {
const bin = data.bins[binIdx];
// const day = dayLabel(i18n, bin.x0!, bin.x1!);
const interval = intervalLabel(i18n, bin.x0!, bin.x1!, bin.length);
const total = i18n.tr(i18n.TR.STATISTICS_RUNNING_TOTAL);
return `${interval}<br>${total}: \u200e${percent.toFixed(1)}%`;
2020-06-28 12:52:38 +02:00
}
2020-08-04 07:28:41 +02:00
const meanInterval = Math.round(mean(allIntervals) ?? 0);
const meanIntervalString = timeSpan(i18n, meanInterval * 86400, false);
const tableData = [
{
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE_INTERVAL),
value: meanIntervalString,
},
];
return [
{ scale, bins, total: totalInPeriod, hoverText, colourScale, showArea: true },
tableData,
];
}