2020-06-26 02:42:10 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-06-26 06:05:58 +02:00
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-non-null-assertion: "off",
|
|
|
|
@typescript-eslint/no-explicit-any: "off",
|
|
|
|
*/
|
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
import { Stats } from "lib/proto";
|
2021-01-30 01:13:47 +01:00
|
|
|
import {
|
|
|
|
interpolateBlues,
|
|
|
|
select,
|
|
|
|
pointer,
|
|
|
|
scaleLinear,
|
|
|
|
scaleBand,
|
|
|
|
scaleSequential,
|
|
|
|
axisBottom,
|
|
|
|
axisLeft,
|
|
|
|
axisRight,
|
|
|
|
area,
|
|
|
|
curveBasis,
|
|
|
|
} from "d3";
|
2021-03-21 13:47:52 +01:00
|
|
|
|
2020-06-26 06:05:58 +02:00
|
|
|
import { showTooltip, hideTooltip } from "./tooltip";
|
2020-07-17 05:46:06 +02:00
|
|
|
import {
|
|
|
|
GraphBounds,
|
|
|
|
setDataAvailable,
|
|
|
|
GraphRange,
|
|
|
|
millisecondCutoffForRange,
|
2020-11-01 05:26:58 +01:00
|
|
|
} from "./graph-helpers";
|
2021-03-21 13:47:52 +01:00
|
|
|
import { oddTickClass } from "./graph-styles";
|
2021-04-22 19:55:26 +02:00
|
|
|
import * as tr from "lib/i18n";
|
2020-06-26 02:42:10 +02:00
|
|
|
|
|
|
|
interface Hour {
|
|
|
|
hour: number;
|
|
|
|
totalCount: number;
|
|
|
|
correctCount: number;
|
|
|
|
}
|
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
const ReviewKind = Stats.RevlogEntry.ReviewKind;
|
2020-06-26 02:42:10 +02:00
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
function gatherData(data: Stats.GraphsResponse, range: GraphRange): Hour[] {
|
2020-06-26 06:05:58 +02:00
|
|
|
const hours = [...Array(24)].map((_n, idx: number) => {
|
|
|
|
return { hour: idx, totalCount: 0, correctCount: 0 } as Hour;
|
2020-06-26 02:42:10 +02:00
|
|
|
});
|
2020-07-17 05:46:06 +02:00
|
|
|
const cutoff = millisecondCutoffForRange(range, data.nextDayAtSecs);
|
2020-06-26 02:42:10 +02:00
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
for (const review of data.revlog as Stats.RevlogEntry[]) {
|
2021-02-10 00:36:51 +01:00
|
|
|
switch (review.reviewKind) {
|
|
|
|
case ReviewKind.LEARNING:
|
|
|
|
case ReviewKind.REVIEW:
|
|
|
|
case ReviewKind.RELEARNING:
|
|
|
|
break; // from switch
|
|
|
|
case ReviewKind.EARLY_REVIEW:
|
|
|
|
case ReviewKind.MANUAL:
|
|
|
|
continue; // next loop iteration
|
2020-06-26 02:42:10 +02:00
|
|
|
}
|
2020-07-17 05:46:06 +02:00
|
|
|
if (cutoff && (review.id as number) < cutoff) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-26 02:42:10 +02:00
|
|
|
|
2020-06-26 06:05:58 +02:00
|
|
|
const hour = Math.floor(
|
|
|
|
(((review.id as number) / 1000 + data.localOffsetSecs) / 3600) % 24
|
|
|
|
);
|
2020-06-26 02:42:10 +02:00
|
|
|
hours[hour].totalCount += 1;
|
|
|
|
if (review.buttonChosen != 1) {
|
|
|
|
hours[hour].correctCount += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 05:46:06 +02:00
|
|
|
return hours;
|
2020-06-26 02:42:10 +02:00
|
|
|
}
|
2020-06-26 06:05:58 +02:00
|
|
|
|
|
|
|
export function renderHours(
|
|
|
|
svgElem: SVGElement,
|
|
|
|
bounds: GraphBounds,
|
2021-07-10 13:58:34 +02:00
|
|
|
origData: Stats.GraphsResponse,
|
2020-07-17 05:46:06 +02:00
|
|
|
range: GraphRange
|
2020-06-26 06:05:58 +02:00
|
|
|
): void {
|
2020-07-17 05:46:06 +02:00
|
|
|
const data = gatherData(origData, range);
|
2020-06-26 06:05:58 +02:00
|
|
|
|
|
|
|
const yMax = Math.max(...data.map((d) => d.totalCount));
|
|
|
|
|
|
|
|
const svg = select(svgElem);
|
|
|
|
const trans = svg.transition().duration(600) as any;
|
|
|
|
|
2020-07-06 06:01:49 +02:00
|
|
|
if (!yMax) {
|
|
|
|
setDataAvailable(svg, false);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
setDataAvailable(svg, true);
|
|
|
|
}
|
|
|
|
|
2020-06-26 06:05:58 +02:00
|
|
|
const x = scaleBand()
|
|
|
|
.domain(data.map((d) => d.hour.toString()))
|
|
|
|
.range([bounds.marginLeft, bounds.width - bounds.marginRight])
|
|
|
|
.paddingInner(0.1);
|
|
|
|
svg.select<SVGGElement>(".x-ticks")
|
2021-03-21 13:47:52 +01:00
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(axisBottom(x).tickSizeOuter(0))
|
|
|
|
)
|
|
|
|
.selectAll(".tick")
|
2020-08-05 10:58:31 +02:00
|
|
|
.selectAll("text")
|
2021-04-02 05:25:38 +02:00
|
|
|
.classed(oddTickClass, (d: any): boolean => d % 2 != 0)
|
|
|
|
.attr("direction", "ltr");
|
2020-06-26 06:05:58 +02:00
|
|
|
|
2020-06-30 08:23:46 +02:00
|
|
|
const cappedRange = scaleLinear().range([0.1, 0.8]);
|
2020-09-29 14:13:25 +02:00
|
|
|
const colour = scaleSequential((n) => interpolateBlues(cappedRange(n)!)).domain([
|
2020-06-30 08:23:46 +02:00
|
|
|
0,
|
|
|
|
yMax,
|
|
|
|
]);
|
2020-06-26 06:05:58 +02:00
|
|
|
|
|
|
|
// y scale
|
|
|
|
|
|
|
|
const y = scaleLinear()
|
|
|
|
.range([bounds.height - bounds.marginBottom, bounds.marginTop])
|
2020-08-05 06:54:46 +02:00
|
|
|
.domain([0, yMax])
|
|
|
|
.nice();
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".y-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(
|
|
|
|
axisLeft(y)
|
|
|
|
.ticks(bounds.height / 50)
|
|
|
|
.tickSizeOuter(0)
|
|
|
|
)
|
2021-03-21 13:47:52 +01:00
|
|
|
)
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr");
|
2020-06-26 06:05:58 +02:00
|
|
|
|
|
|
|
const yArea = y.copy().domain([0, 1]);
|
|
|
|
|
|
|
|
// x bars
|
|
|
|
|
|
|
|
const updateBar = (sel: any): any => {
|
|
|
|
return sel
|
|
|
|
.attr("width", x.bandwidth())
|
|
|
|
.transition(trans)
|
|
|
|
.attr("x", (d: Hour) => x(d.hour.toString())!)
|
|
|
|
.attr("y", (d: Hour) => y(d.totalCount)!)
|
2020-09-29 14:13:25 +02:00
|
|
|
.attr("height", (d: Hour) => y(0)! - y(d.totalCount)!)
|
2020-06-26 06:05:58 +02:00
|
|
|
.attr("fill", (d: Hour) => colour(d.totalCount!));
|
|
|
|
};
|
|
|
|
|
|
|
|
svg.select("g.bars")
|
|
|
|
.selectAll("rect")
|
|
|
|
.data(data)
|
|
|
|
.join(
|
|
|
|
(enter) =>
|
|
|
|
enter
|
|
|
|
.append("rect")
|
|
|
|
.attr("rx", 1)
|
|
|
|
.attr("x", (d: Hour) => x(d.hour.toString())!)
|
2020-09-29 14:13:25 +02:00
|
|
|
.attr("y", y(0)!)
|
2020-06-26 06:05:58 +02:00
|
|
|
.attr("height", 0)
|
2020-06-30 08:23:46 +02:00
|
|
|
// .attr("opacity", 0.7)
|
2020-06-26 06:05:58 +02:00
|
|
|
.call(updateBar),
|
|
|
|
(update) => update.call(updateBar),
|
|
|
|
(remove) =>
|
|
|
|
remove.call((remove) =>
|
2020-09-29 14:13:25 +02:00
|
|
|
remove.transition(trans).attr("height", 0).attr("y", y(0)!)
|
2020-06-26 06:05:58 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".y2-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(
|
|
|
|
axisRight(yArea)
|
|
|
|
.ticks(bounds.height / 50)
|
|
|
|
.tickFormat((n: any) => `${Math.round(n * 100)}%`)
|
|
|
|
.tickSizeOuter(0)
|
|
|
|
)
|
2021-03-21 13:47:52 +01:00
|
|
|
)
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr");
|
2020-08-05 06:54:46 +02:00
|
|
|
|
2021-03-21 10:50:35 +01:00
|
|
|
svg.select("path.cumulative-overlay")
|
2020-06-26 06:05:58 +02:00
|
|
|
.datum(data)
|
|
|
|
.attr(
|
|
|
|
"d",
|
|
|
|
area<Hour>()
|
|
|
|
.curve(curveBasis)
|
2020-08-05 07:25:56 +02:00
|
|
|
.defined((d) => d.totalCount > 0)
|
2020-06-26 06:05:58 +02:00
|
|
|
.x((d: Hour) => {
|
|
|
|
return x(d.hour.toString())! + x.bandwidth() / 2;
|
|
|
|
})
|
|
|
|
.y0(bounds.height - bounds.marginBottom)
|
|
|
|
.y1((d: Hour) => {
|
|
|
|
const correctRatio = d.correctCount! / d.totalCount!;
|
2020-09-29 14:13:25 +02:00
|
|
|
return yArea(isNaN(correctRatio) ? 0 : correctRatio)!;
|
2020-06-26 06:05:58 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-06-28 12:52:38 +02:00
|
|
|
function tooltipText(d: Hour): string {
|
2021-03-26 11:23:43 +01:00
|
|
|
const hour = tr.statisticsHoursRange({
|
2020-06-28 12:52:38 +02:00
|
|
|
hourStart: d.hour,
|
|
|
|
hourEnd: d.hour + 1,
|
|
|
|
});
|
2021-03-26 11:23:43 +01:00
|
|
|
const correct = tr.statisticsHoursCorrect({
|
2020-06-28 12:52:38 +02:00
|
|
|
correct: d.correctCount,
|
|
|
|
total: d.totalCount,
|
|
|
|
percent: d.totalCount ? (d.correctCount / d.totalCount) * 100 : 0,
|
|
|
|
});
|
|
|
|
return `${hour}<br>${correct}`;
|
|
|
|
}
|
|
|
|
|
2020-06-26 06:05:58 +02:00
|
|
|
// hover/tooltip
|
2021-03-21 10:58:39 +01:00
|
|
|
svg.select("g.hover-columns")
|
2020-06-26 06:05:58 +02:00
|
|
|
.selectAll("rect")
|
|
|
|
.data(data)
|
|
|
|
.join("rect")
|
|
|
|
.attr("x", (d: Hour) => x(d.hour.toString())!)
|
|
|
|
.attr("y", () => y(yMax)!)
|
|
|
|
.attr("width", x.bandwidth())
|
2020-09-29 14:13:25 +02:00
|
|
|
.attr("height", () => y(0)! - y(yMax!)!)
|
2021-01-30 01:13:47 +01:00
|
|
|
.on("mousemove", (event: MouseEvent, d: Hour) => {
|
2021-01-30 02:35:33 +01:00
|
|
|
const [x, y] = pointer(event, document.body);
|
2020-06-26 06:05:58 +02:00
|
|
|
showTooltip(tooltipText(d), x, y);
|
|
|
|
})
|
|
|
|
.on("mouseout", hideTooltip);
|
|
|
|
}
|