2020-06-30 07:09:20 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
Migrate to protobuf-es (#2547)
* Fix .no-reduce-motion missing from graphs spinner, and not being honored
* Begin migration from protobuf.js -> protobuf-es
Motivation:
- Protobuf-es has a nicer API: messages are represented as classes, and
fields which should exist are not marked as nullable.
- As it uses modules, only the proto messages we actually use get included
in our bundle output. Protobuf.js put everything in a namespace, which
prevented tree-shaking, and made it awkward to access inner messages.
- ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff
is slower decoding/encoding (#2043), but that was mainly a concern for the
graphs page, and was unblocked by
https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78
Approach/notes:
- We generate the new protobuf-es interface in addition to existing
protobuf.js interface, so we can migrate a module at a time, starting
with the graphs module.
- rslib:proto now generates RPC methods for TS in addition to the Python
interface. The input-arg-unrolling behaviour of the Python generation is
not required here, as we declare the input arg as a PlainMessage<T>, which
marks it as requiring all fields to be provided.
- i64 is represented as bigint in protobuf-es. We were using a patch to
protobuf.js to get it to output Javascript numbers instead of long.js
types, but now that our supported browser versions support bigint, it's
probably worth biting the bullet and migrating to bigint use. Our IDs
fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future
fields we add.
- Oneofs are handled differently in protobuf-es, and are going to need
some refactoring.
Other notable changes:
- Added a --mkdir arg to our build runner, so we can create a dir easily
during the build on Windows.
- Simplified the preference handling code, by wrapping the preferences
in an outer store, instead of a separate store for each individual
preference. This means a change to one preference will trigger a redraw
of all components that depend on the preference store, but the redrawing
is cheap after moving the data processing to Rust, and it makes the code
easier to follow.
- Drop async(Reactive).ts in favour of more explicit handling with await
blocks/updating.
- Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding
dependencies to parent groups. Renamed add() -> add_action() for clarity.
* Remove a couple of unused proto imports
* Migrate card info
* Migrate congrats, image occlusion, and tag editor
+ Fix imports for multi-word proto files.
* Migrate change-notetype
* Migrate deck options
* Bump target to es2020; simplify ts lib list
Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome
on Android support the full es2017-es2020 features.
* Migrate import-csv
* Migrate i18n and fix missing output types in .js
* Migrate custom scheduling, and remove protobuf.js
To mostly maintain our old API contract, we make use of protobuf-es's
ability to convert to JSON, which follows the same format as protobuf.js
did. It doesn't cover all case: users who were previously changing the
variant of a type will need to update their code, as assigning to a new
variant no longer automatically removes the old one, which will cause an
error when we try to convert back from JSON. But I suspect the large majority
of users are adjusting the current variant rather than creating a new one,
and this saves us having to write proxy wrappers, so it seems like a
reasonable compromise.
One other change I made at the same time was to rename value->kind for
the oneofs in our custom study protos, as 'value' was easily confused
with the 'case/value' output that protobuf-es has.
With protobuf.js codegen removed, touching a proto file and invoking
./run drops from about 8s to 6s.
This closes #2043.
* Allow tree-shaking on protobuf types
* Display backend error messages in our ts alert()
* Make sourcemap generation opt-in for ts-run
Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
|
|
|
import type { GraphsResponse } from "@tslib/anki/stats_pb";
|
|
|
|
import { GraphPreferences_Weekday as Weekday } from "@tslib/anki/stats_pb";
|
Move away from Bazel (#2202)
(for upgrading users, please see the notes at the bottom)
Bazel brought a lot of nice things to the table, such as rebuilds based on
content changes instead of modification times, caching of build products,
detection of incorrect build rules via a sandbox, and so on. Rewriting the build
in Bazel was also an opportunity to improve on the Makefile-based build we had
prior, which was pretty poor: most dependencies were external or not pinned, and
the build graph was poorly defined and mostly serialized. It was not uncommon
for fresh checkouts to fail due to floating dependencies, or for things to break
when trying to switch to an older commit.
For day-to-day development, I think Bazel served us reasonably well - we could
generally switch between branches while being confident that builds would be
correct and reasonably fast, and not require full rebuilds (except on Windows,
where the lack of a sandbox and the TS rules would cause build breakages when TS
files were renamed/removed).
Bazel achieves that reliability by defining rules for each programming language
that define how source files should be turned into outputs. For the rules to
work with Bazel's sandboxing approach, they often have to reimplement or
partially bypass the standard tools that each programming language provides. The
Rust rules call Rust's compiler directly for example, instead of using Cargo,
and the Python rules extract each PyPi package into a separate folder that gets
added to sys.path.
These separate language rules allow proper declaration of inputs and outputs,
and offer some advantages such as caching of build products and fine-grained
dependency installation. But they also bring some downsides:
- The rules don't always support use-cases/platforms that the standard language
tools do, meaning they need to be patched to be used. I've had to contribute a
number of patches to the Rust, Python and JS rules to unblock various issues.
- The dependencies we use with each language sometimes make assumptions that do
not hold in Bazel, meaning they either need to be pinned or patched, or the
language rules need to be adjusted to accommodate them.
I was hopeful that after the initial setup work, things would be relatively
smooth-sailing. Unfortunately, that has not proved to be the case. Things
frequently broke when dependencies or the language rules were updated, and I
began to get frustrated at the amount of Anki development time I was instead
spending on build system upkeep. It's now about 2 years since switching to
Bazel, and I think it's time to cut losses, and switch to something else that's
a better fit.
The new build system is based on a small build tool called Ninja, and some
custom Rust code in build/. This means that to build Anki, Bazel is no longer
required, but Ninja and Rust need to be installed on your system. Python and
Node toolchains are automatically downloaded like in Bazel.
This new build system should result in faster builds in some cases:
- Because we're using cargo to build now, Rust builds are able to take advantage
of pipelining and incremental debug builds, which we didn't have with Bazel.
It's also easier to override the default linker on Linux/macOS, which can
further improve speeds.
- External Rust crates are now built with opt=1, which improves performance
of debug builds.
- Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript
compiler. This results in faster builds, by deferring typechecking to test/check
time, and by allowing more work to happen in parallel.
As an example of the differences, when testing with the mold linker on Linux,
adding a new message to tags.proto (which triggers a recompile of the bulk of
the Rust and TypeScript code) results in a compile that goes from about 22s on
Bazel to about 7s in the new system. With the standard linker, it's about 9s.
Some other changes of note:
- Our Rust workspace now uses cargo-hakari to ensure all packages agree on
available features, preventing unnecessary rebuilds.
- pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge
source files and generated files into a single folder for running. By telling
VSCode about the extra search path, code completion now works with generated
files without needing to symlink them into the source folder.
- qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py.
Instead, the generated files are now placed in a separate _aqt package that's
added to the path.
- ts/lib is now exposed as @tslib, so the source code and generated code can be
provided under the same namespace without a merging step.
- MyPy and PyLint are now invoked once for the entire codebase.
- dprint will be used to format TypeScript/json files in the future instead of
the slower prettier (currently turned off to avoid causing conflicts). It can
automatically defer to prettier when formatting Svelte files.
- svelte-check is now used for typechecking our Svelte code, which revealed a
few typing issues that went undetected with the old system.
- The Jest unit tests now work on Windows as well.
If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes:
- please remove node_modules and .bazel
- install rustup (https://rustup.rs/)
- install rsync if not already installed (on windows, use pacman - see docs/windows.md)
- install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and
place on your path, or from your distro/homebrew if it's 1.10+)
- update .vscode/settings.json from .vscode.dist
2022-11-27 06:24:20 +01:00
|
|
|
import * as tr from "@tslib/ftl";
|
|
|
|
import { localizedDate, weekdayLabel } from "@tslib/i18n";
|
2022-02-04 09:36:34 +01:00
|
|
|
import type { CountableTimeInterval } from "d3";
|
2023-03-28 07:35:06 +02:00
|
|
|
import { timeHour } from "d3";
|
2021-01-18 23:27:57 +01:00
|
|
|
import {
|
2021-01-30 01:13:47 +01:00
|
|
|
interpolateBlues,
|
|
|
|
pointer,
|
|
|
|
scaleLinear,
|
|
|
|
scaleSequentialSqrt,
|
2022-02-04 09:36:34 +01:00
|
|
|
select,
|
2021-01-18 23:27:57 +01:00
|
|
|
timeDay,
|
|
|
|
timeFriday,
|
2022-02-04 09:36:34 +01:00
|
|
|
timeMonday,
|
2021-01-18 23:27:57 +01:00
|
|
|
timeSaturday,
|
2022-02-04 09:36:34 +01:00
|
|
|
timeSunday,
|
|
|
|
timeYear,
|
2021-01-30 01:13:47 +01:00
|
|
|
} from "d3";
|
2021-03-21 13:47:52 +01:00
|
|
|
|
2022-11-28 00:33:04 +01:00
|
|
|
import type { GraphBounds, SearchDispatch } from "./graph-helpers";
|
Move away from Bazel (#2202)
(for upgrading users, please see the notes at the bottom)
Bazel brought a lot of nice things to the table, such as rebuilds based on
content changes instead of modification times, caching of build products,
detection of incorrect build rules via a sandbox, and so on. Rewriting the build
in Bazel was also an opportunity to improve on the Makefile-based build we had
prior, which was pretty poor: most dependencies were external or not pinned, and
the build graph was poorly defined and mostly serialized. It was not uncommon
for fresh checkouts to fail due to floating dependencies, or for things to break
when trying to switch to an older commit.
For day-to-day development, I think Bazel served us reasonably well - we could
generally switch between branches while being confident that builds would be
correct and reasonably fast, and not require full rebuilds (except on Windows,
where the lack of a sandbox and the TS rules would cause build breakages when TS
files were renamed/removed).
Bazel achieves that reliability by defining rules for each programming language
that define how source files should be turned into outputs. For the rules to
work with Bazel's sandboxing approach, they often have to reimplement or
partially bypass the standard tools that each programming language provides. The
Rust rules call Rust's compiler directly for example, instead of using Cargo,
and the Python rules extract each PyPi package into a separate folder that gets
added to sys.path.
These separate language rules allow proper declaration of inputs and outputs,
and offer some advantages such as caching of build products and fine-grained
dependency installation. But they also bring some downsides:
- The rules don't always support use-cases/platforms that the standard language
tools do, meaning they need to be patched to be used. I've had to contribute a
number of patches to the Rust, Python and JS rules to unblock various issues.
- The dependencies we use with each language sometimes make assumptions that do
not hold in Bazel, meaning they either need to be pinned or patched, or the
language rules need to be adjusted to accommodate them.
I was hopeful that after the initial setup work, things would be relatively
smooth-sailing. Unfortunately, that has not proved to be the case. Things
frequently broke when dependencies or the language rules were updated, and I
began to get frustrated at the amount of Anki development time I was instead
spending on build system upkeep. It's now about 2 years since switching to
Bazel, and I think it's time to cut losses, and switch to something else that's
a better fit.
The new build system is based on a small build tool called Ninja, and some
custom Rust code in build/. This means that to build Anki, Bazel is no longer
required, but Ninja and Rust need to be installed on your system. Python and
Node toolchains are automatically downloaded like in Bazel.
This new build system should result in faster builds in some cases:
- Because we're using cargo to build now, Rust builds are able to take advantage
of pipelining and incremental debug builds, which we didn't have with Bazel.
It's also easier to override the default linker on Linux/macOS, which can
further improve speeds.
- External Rust crates are now built with opt=1, which improves performance
of debug builds.
- Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript
compiler. This results in faster builds, by deferring typechecking to test/check
time, and by allowing more work to happen in parallel.
As an example of the differences, when testing with the mold linker on Linux,
adding a new message to tags.proto (which triggers a recompile of the bulk of
the Rust and TypeScript code) results in a compile that goes from about 22s on
Bazel to about 7s in the new system. With the standard linker, it's about 9s.
Some other changes of note:
- Our Rust workspace now uses cargo-hakari to ensure all packages agree on
available features, preventing unnecessary rebuilds.
- pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge
source files and generated files into a single folder for running. By telling
VSCode about the extra search path, code completion now works with generated
files without needing to symlink them into the source folder.
- qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py.
Instead, the generated files are now placed in a separate _aqt package that's
added to the path.
- ts/lib is now exposed as @tslib, so the source code and generated code can be
provided under the same namespace without a merging step.
- MyPy and PyLint are now invoked once for the entire codebase.
- dprint will be used to format TypeScript/json files in the future instead of
the slower prettier (currently turned off to avoid causing conflicts). It can
automatically defer to prettier when formatting Svelte files.
- svelte-check is now used for typechecking our Svelte code, which revealed a
few typing issues that went undetected with the old system.
- The Jest unit tests now work on Windows as well.
If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes:
- please remove node_modules and .bazel
- install rustup (https://rustup.rs/)
- install rsync if not already installed (on windows, use pacman - see docs/windows.md)
- install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and
place on your path, or from your distro/homebrew if it's 1.10+)
- update .vscode/settings.json from .vscode.dist
2022-11-27 06:24:20 +01:00
|
|
|
import { RevlogRange, setDataAvailable } from "./graph-helpers";
|
2021-03-21 13:47:52 +01:00
|
|
|
import { clickableClass } from "./graph-styles";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { hideTooltip, showTooltip } from "./tooltip";
|
2020-06-30 07:09:20 +02:00
|
|
|
|
|
|
|
export interface GraphData {
|
|
|
|
// indexed by day, where day is relative to today
|
|
|
|
reviewCount: Map<number, number>;
|
2021-01-18 23:27:57 +01:00
|
|
|
timeFunction: CountableTimeInterval;
|
2021-01-21 19:31:47 +01:00
|
|
|
weekdayLabels: number[];
|
2023-03-28 07:35:06 +02:00
|
|
|
rolloverHour: number;
|
2020-06-30 07:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface DayDatum {
|
|
|
|
day: number;
|
|
|
|
count: number;
|
|
|
|
// 0-51
|
|
|
|
weekNumber: number;
|
|
|
|
// 0-6
|
|
|
|
weekDay: number;
|
|
|
|
date: Date;
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:56:41 +01:00
|
|
|
export function gatherData(
|
Migrate to protobuf-es (#2547)
* Fix .no-reduce-motion missing from graphs spinner, and not being honored
* Begin migration from protobuf.js -> protobuf-es
Motivation:
- Protobuf-es has a nicer API: messages are represented as classes, and
fields which should exist are not marked as nullable.
- As it uses modules, only the proto messages we actually use get included
in our bundle output. Protobuf.js put everything in a namespace, which
prevented tree-shaking, and made it awkward to access inner messages.
- ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff
is slower decoding/encoding (#2043), but that was mainly a concern for the
graphs page, and was unblocked by
https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78
Approach/notes:
- We generate the new protobuf-es interface in addition to existing
protobuf.js interface, so we can migrate a module at a time, starting
with the graphs module.
- rslib:proto now generates RPC methods for TS in addition to the Python
interface. The input-arg-unrolling behaviour of the Python generation is
not required here, as we declare the input arg as a PlainMessage<T>, which
marks it as requiring all fields to be provided.
- i64 is represented as bigint in protobuf-es. We were using a patch to
protobuf.js to get it to output Javascript numbers instead of long.js
types, but now that our supported browser versions support bigint, it's
probably worth biting the bullet and migrating to bigint use. Our IDs
fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future
fields we add.
- Oneofs are handled differently in protobuf-es, and are going to need
some refactoring.
Other notable changes:
- Added a --mkdir arg to our build runner, so we can create a dir easily
during the build on Windows.
- Simplified the preference handling code, by wrapping the preferences
in an outer store, instead of a separate store for each individual
preference. This means a change to one preference will trigger a redraw
of all components that depend on the preference store, but the redrawing
is cheap after moving the data processing to Rust, and it makes the code
easier to follow.
- Drop async(Reactive).ts in favour of more explicit handling with await
blocks/updating.
- Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding
dependencies to parent groups. Renamed add() -> add_action() for clarity.
* Remove a couple of unused proto imports
* Migrate card info
* Migrate congrats, image occlusion, and tag editor
+ Fix imports for multi-word proto files.
* Migrate change-notetype
* Migrate deck options
* Bump target to es2020; simplify ts lib list
Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome
on Android support the full es2017-es2020 features.
* Migrate import-csv
* Migrate i18n and fix missing output types in .js
* Migrate custom scheduling, and remove protobuf.js
To mostly maintain our old API contract, we make use of protobuf-es's
ability to convert to JSON, which follows the same format as protobuf.js
did. It doesn't cover all case: users who were previously changing the
variant of a type will need to update their code, as assigning to a new
variant no longer automatically removes the old one, which will cause an
error when we try to convert back from JSON. But I suspect the large majority
of users are adjusting the current variant rather than creating a new one,
and this saves us having to write proxy wrappers, so it seems like a
reasonable compromise.
One other change I made at the same time was to rename value->kind for
the oneofs in our custom study protos, as 'value' was easily confused
with the 'case/value' output that protobuf-es has.
With protobuf.js codegen removed, touching a proto file and invoking
./run drops from about 8s to 6s.
This closes #2043.
* Allow tree-shaking on protobuf types
* Display backend error messages in our ts alert()
* Make sourcemap generation opt-in for ts-run
Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
|
|
|
data: GraphsResponse,
|
|
|
|
firstDayOfWeek: Weekday,
|
2021-01-22 14:56:41 +01:00
|
|
|
): GraphData {
|
Move more of the graph processing into the backend
The existing architecture serializes all cards and revlog entries in
the search range into a protobuf message, which the web frontend needs
to decode and then process. The thinking at the time was that this would
make it easier for add-ons to add extra graphs, but in the ~2.5 years
since the new graphs were introduced, no add-ons appear to have taken
advantage of it.
The cards and revlog entries can grow quite large on large collections -
on a collection I tested with approximately 2.5M reviews, the serialized
data is about 110MB, which is a lot to have to deserialize in JavaScript.
This commit shifts the preliminary processing of the data to the Rust end,
which means the data is able to be processed faster, and less needs to
be sent to the frontend. On the test collection above, this reduces the
serialized data from about 110MB to about 160KB, resulting in a more
than 2x performance improvement, and reducing frontend memory usage from
about 400MB to about 40MB.
This also makes #2043 more feasible - while it is still about 50-100%
slower than protobufjs, with the much smaller message size, the difference
is only about 10ms.
2022-12-09 08:35:15 +01:00
|
|
|
const reviewCount = new Map(
|
|
|
|
Object.entries(data.reviews!.count).map(([k, v]) => {
|
|
|
|
return [Number(k), v.learn + v.relearn + v.mature + v.filtered + v.young];
|
|
|
|
}),
|
|
|
|
);
|
2021-04-23 05:00:18 +02:00
|
|
|
const timeFunction = timeFunctionForDay(firstDayOfWeek);
|
2021-01-21 19:31:47 +01:00
|
|
|
const weekdayLabels: number[] = [];
|
|
|
|
for (let i = 0; i < 7; i++) {
|
2021-01-22 14:56:41 +01:00
|
|
|
weekdayLabels.push((firstDayOfWeek + i) % 7);
|
2021-01-20 21:17:36 +01:00
|
|
|
}
|
|
|
|
|
2023-03-28 07:35:06 +02:00
|
|
|
return { reviewCount, timeFunction, weekdayLabels, rolloverHour: data.rolloverHour };
|
2020-06-30 07:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderCalendar(
|
|
|
|
svgElem: SVGElement,
|
|
|
|
bounds: GraphBounds,
|
|
|
|
sourceData: GraphData,
|
2021-01-26 12:41:22 +01:00
|
|
|
dispatch: SearchDispatch,
|
2020-06-30 07:09:20 +02:00
|
|
|
targetYear: number,
|
2020-08-21 05:41:34 +02:00
|
|
|
nightMode: boolean,
|
2021-01-22 17:51:15 +01:00
|
|
|
revlogRange: RevlogRange,
|
2021-10-19 01:06:00 +02:00
|
|
|
setFirstDayOfWeek: (d: number) => void,
|
2020-06-30 07:09:20 +02:00
|
|
|
): void {
|
|
|
|
const svg = select(svgElem);
|
|
|
|
const now = new Date();
|
|
|
|
const nowForYear = new Date();
|
|
|
|
nowForYear.setFullYear(targetYear);
|
|
|
|
|
|
|
|
const x = scaleLinear()
|
|
|
|
.range([bounds.marginLeft, bounds.width - bounds.marginRight])
|
2021-01-20 21:17:36 +01:00
|
|
|
.domain([-1, 53]);
|
|
|
|
|
2020-06-30 07:09:20 +02:00
|
|
|
// map of 0-365 -> day
|
|
|
|
const dayMap: Map<number, DayDatum> = new Map();
|
|
|
|
let maxCount = 0;
|
|
|
|
for (const [day, count] of sourceData.reviewCount.entries()) {
|
2023-03-28 07:35:06 +02:00
|
|
|
let date = timeDay.offset(now, day);
|
|
|
|
// anki day does not necessarily roll over at midnight, we account for this when mapping onto calendar days
|
|
|
|
date = timeHour.offset(date, -1 * sourceData.rolloverHour);
|
2022-01-11 07:26:26 +01:00
|
|
|
if (count > maxCount) {
|
|
|
|
maxCount = count;
|
|
|
|
}
|
2020-06-30 07:09:20 +02:00
|
|
|
if (date.getFullYear() != targetYear) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-18 23:23:55 +01:00
|
|
|
const weekNumber = sourceData.timeFunction.count(timeYear(date), date);
|
|
|
|
const weekDay = timeDay.count(sourceData.timeFunction(date), date);
|
2020-06-30 07:09:20 +02:00
|
|
|
const yearDay = timeDay.count(timeYear(date), date);
|
|
|
|
dayMap.set(yearDay, { day, count, weekNumber, weekDay, date } as DayDatum);
|
|
|
|
}
|
2020-07-06 06:01:49 +02:00
|
|
|
|
|
|
|
if (!maxCount) {
|
|
|
|
setDataAvailable(svg, false);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
setDataAvailable(svg, true);
|
|
|
|
}
|
|
|
|
|
2023-03-28 07:35:06 +02:00
|
|
|
// fill in any blanks, including the current calendar day even if the anki day has not rolled over
|
2020-06-30 07:09:20 +02:00
|
|
|
const startDate = timeYear(nowForYear);
|
2020-08-21 05:41:34 +02:00
|
|
|
const oneYearAgoFromNow = new Date(now);
|
|
|
|
oneYearAgoFromNow.setFullYear(now.getFullYear() - 1);
|
2020-08-21 05:40:50 +02:00
|
|
|
for (let i = 0; i < 365; i++) {
|
2023-03-28 07:35:06 +02:00
|
|
|
const date = timeDay.offset(startDate, i);
|
2020-06-30 07:09:20 +02:00
|
|
|
if (date > now) {
|
|
|
|
// don't fill out future dates
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-21 05:41:34 +02:00
|
|
|
if (revlogRange == RevlogRange.Year && date < oneYearAgoFromNow) {
|
|
|
|
// don't fill out dates older than a year
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-30 07:09:20 +02:00
|
|
|
const yearDay = timeDay.count(timeYear(date), date);
|
|
|
|
if (!dayMap.has(yearDay)) {
|
2021-01-18 23:23:55 +01:00
|
|
|
const weekNumber = sourceData.timeFunction.count(timeYear(date), date);
|
|
|
|
const weekDay = timeDay.count(sourceData.timeFunction(date), date);
|
2020-06-30 07:09:20 +02:00
|
|
|
dayMap.set(yearDay, {
|
|
|
|
day: yearDay,
|
|
|
|
count: 0,
|
|
|
|
weekNumber,
|
|
|
|
weekDay,
|
|
|
|
date,
|
|
|
|
} as DayDatum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const data = Array.from(dayMap.values());
|
2020-06-30 08:23:46 +02:00
|
|
|
const cappedRange = scaleLinear().range([0.2, nightMode ? 0.8 : 1]);
|
2021-01-22 22:39:29 +01:00
|
|
|
const blues = scaleSequentialSqrt()
|
|
|
|
.domain([0, maxCount])
|
|
|
|
.interpolator((n) => interpolateBlues(cappedRange(n)!));
|
2020-06-30 07:09:20 +02:00
|
|
|
|
|
|
|
function tooltipText(d: DayDatum): string {
|
2021-12-29 06:04:15 +01:00
|
|
|
const date = localizedDate(d.date, {
|
2020-06-30 07:09:20 +02:00
|
|
|
weekday: "long",
|
|
|
|
year: "numeric",
|
|
|
|
month: "long",
|
|
|
|
day: "numeric",
|
|
|
|
});
|
2021-03-26 11:23:43 +01:00
|
|
|
const cards = tr.statisticsReviews({ reviews: d.count });
|
2020-06-30 07:09:20 +02:00
|
|
|
return `${date}<br>${cards}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const height = bounds.height / 10;
|
2021-01-20 21:17:36 +01:00
|
|
|
const emptyColour = nightMode ? "#333" : "#ddd";
|
|
|
|
|
|
|
|
svg.select("g.weekdays")
|
|
|
|
.selectAll("text")
|
|
|
|
.data(sourceData.weekdayLabels)
|
|
|
|
.join("text")
|
2021-10-07 15:31:49 +02:00
|
|
|
.text((d: number) => weekdayLabel(d))
|
2021-01-20 21:17:36 +01:00
|
|
|
.attr("width", x(-1)! - 2)
|
|
|
|
.attr("height", height - 2)
|
2021-01-20 22:07:02 +01:00
|
|
|
.attr("x", x(1)! - 3)
|
2021-01-20 21:17:36 +01:00
|
|
|
.attr("y", (_d, index) => bounds.marginTop + index * height)
|
2021-01-22 19:02:05 +01:00
|
|
|
.attr("fill", nightMode ? "#ddd" : "black")
|
2021-01-20 21:17:36 +01:00
|
|
|
.attr("dominant-baseline", "hanging")
|
2021-01-20 22:07:02 +01:00
|
|
|
.attr("text-anchor", "end")
|
2021-01-20 21:17:36 +01:00
|
|
|
.attr("font-size", "small")
|
|
|
|
.attr("font-family", "monospace")
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr")
|
2021-01-22 17:51:15 +01:00
|
|
|
.style("user-select", "none")
|
|
|
|
.on("click", null)
|
2021-01-22 18:03:58 +01:00
|
|
|
.filter((d: number) =>
|
|
|
|
[Weekday.SUNDAY, Weekday.MONDAY, Weekday.FRIDAY, Weekday.SATURDAY].includes(
|
2021-10-19 01:06:00 +02:00
|
|
|
d,
|
Move away from Bazel (#2202)
(for upgrading users, please see the notes at the bottom)
Bazel brought a lot of nice things to the table, such as rebuilds based on
content changes instead of modification times, caching of build products,
detection of incorrect build rules via a sandbox, and so on. Rewriting the build
in Bazel was also an opportunity to improve on the Makefile-based build we had
prior, which was pretty poor: most dependencies were external or not pinned, and
the build graph was poorly defined and mostly serialized. It was not uncommon
for fresh checkouts to fail due to floating dependencies, or for things to break
when trying to switch to an older commit.
For day-to-day development, I think Bazel served us reasonably well - we could
generally switch between branches while being confident that builds would be
correct and reasonably fast, and not require full rebuilds (except on Windows,
where the lack of a sandbox and the TS rules would cause build breakages when TS
files were renamed/removed).
Bazel achieves that reliability by defining rules for each programming language
that define how source files should be turned into outputs. For the rules to
work with Bazel's sandboxing approach, they often have to reimplement or
partially bypass the standard tools that each programming language provides. The
Rust rules call Rust's compiler directly for example, instead of using Cargo,
and the Python rules extract each PyPi package into a separate folder that gets
added to sys.path.
These separate language rules allow proper declaration of inputs and outputs,
and offer some advantages such as caching of build products and fine-grained
dependency installation. But they also bring some downsides:
- The rules don't always support use-cases/platforms that the standard language
tools do, meaning they need to be patched to be used. I've had to contribute a
number of patches to the Rust, Python and JS rules to unblock various issues.
- The dependencies we use with each language sometimes make assumptions that do
not hold in Bazel, meaning they either need to be pinned or patched, or the
language rules need to be adjusted to accommodate them.
I was hopeful that after the initial setup work, things would be relatively
smooth-sailing. Unfortunately, that has not proved to be the case. Things
frequently broke when dependencies or the language rules were updated, and I
began to get frustrated at the amount of Anki development time I was instead
spending on build system upkeep. It's now about 2 years since switching to
Bazel, and I think it's time to cut losses, and switch to something else that's
a better fit.
The new build system is based on a small build tool called Ninja, and some
custom Rust code in build/. This means that to build Anki, Bazel is no longer
required, but Ninja and Rust need to be installed on your system. Python and
Node toolchains are automatically downloaded like in Bazel.
This new build system should result in faster builds in some cases:
- Because we're using cargo to build now, Rust builds are able to take advantage
of pipelining and incremental debug builds, which we didn't have with Bazel.
It's also easier to override the default linker on Linux/macOS, which can
further improve speeds.
- External Rust crates are now built with opt=1, which improves performance
of debug builds.
- Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript
compiler. This results in faster builds, by deferring typechecking to test/check
time, and by allowing more work to happen in parallel.
As an example of the differences, when testing with the mold linker on Linux,
adding a new message to tags.proto (which triggers a recompile of the bulk of
the Rust and TypeScript code) results in a compile that goes from about 22s on
Bazel to about 7s in the new system. With the standard linker, it's about 9s.
Some other changes of note:
- Our Rust workspace now uses cargo-hakari to ensure all packages agree on
available features, preventing unnecessary rebuilds.
- pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge
source files and generated files into a single folder for running. By telling
VSCode about the extra search path, code completion now works with generated
files without needing to symlink them into the source folder.
- qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py.
Instead, the generated files are now placed in a separate _aqt package that's
added to the path.
- ts/lib is now exposed as @tslib, so the source code and generated code can be
provided under the same namespace without a merging step.
- MyPy and PyLint are now invoked once for the entire codebase.
- dprint will be used to format TypeScript/json files in the future instead of
the slower prettier (currently turned off to avoid causing conflicts). It can
automatically defer to prettier when formatting Svelte files.
- svelte-check is now used for typechecking our Svelte code, which revealed a
few typing issues that went undetected with the old system.
- The Jest unit tests now work on Windows as well.
If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes:
- please remove node_modules and .bazel
- install rustup (https://rustup.rs/)
- install rsync if not already installed (on windows, use pacman - see docs/windows.md)
- install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and
place on your path, or from your distro/homebrew if it's 1.10+)
- update .vscode/settings.json from .vscode.dist
2022-11-27 06:24:20 +01:00
|
|
|
)
|
2021-01-22 18:03:58 +01:00
|
|
|
)
|
2021-02-04 14:25:51 +01:00
|
|
|
.on("click", (_event: MouseEvent, d: number) => setFirstDayOfWeek(d));
|
2021-01-20 21:17:36 +01:00
|
|
|
|
|
|
|
svg.select("g.days")
|
2020-06-30 07:09:20 +02:00
|
|
|
.selectAll("rect")
|
|
|
|
.data(data)
|
|
|
|
.join("rect")
|
2020-06-30 08:39:30 +02:00
|
|
|
.attr("fill", emptyColour)
|
2021-03-21 13:47:52 +01:00
|
|
|
.attr("width", (d: DayDatum) => x(d.weekNumber + 1)! - x(d.weekNumber)! - 2)
|
2020-06-30 07:09:20 +02:00
|
|
|
.attr("height", height - 2)
|
2021-03-21 13:47:52 +01:00
|
|
|
.attr("x", (d: DayDatum) => x(d.weekNumber + 1)!)
|
|
|
|
.attr("y", (d: DayDatum) => bounds.marginTop + d.weekDay * height)
|
2021-01-30 01:13:47 +01:00
|
|
|
.on("mousemove", (event: MouseEvent, d: DayDatum) => {
|
2021-01-30 02:35:33 +01:00
|
|
|
const [x, y] = pointer(event, document.body);
|
2020-06-30 07:09:20 +02:00
|
|
|
showTooltip(tooltipText(d), x, y);
|
|
|
|
})
|
|
|
|
.on("mouseout", hideTooltip)
|
2021-03-21 13:47:52 +01:00
|
|
|
.attr("class", (d: DayDatum): string => (d.count > 0 ? clickableClass : ""))
|
Move away from Bazel (#2202)
(for upgrading users, please see the notes at the bottom)
Bazel brought a lot of nice things to the table, such as rebuilds based on
content changes instead of modification times, caching of build products,
detection of incorrect build rules via a sandbox, and so on. Rewriting the build
in Bazel was also an opportunity to improve on the Makefile-based build we had
prior, which was pretty poor: most dependencies were external or not pinned, and
the build graph was poorly defined and mostly serialized. It was not uncommon
for fresh checkouts to fail due to floating dependencies, or for things to break
when trying to switch to an older commit.
For day-to-day development, I think Bazel served us reasonably well - we could
generally switch between branches while being confident that builds would be
correct and reasonably fast, and not require full rebuilds (except on Windows,
where the lack of a sandbox and the TS rules would cause build breakages when TS
files were renamed/removed).
Bazel achieves that reliability by defining rules for each programming language
that define how source files should be turned into outputs. For the rules to
work with Bazel's sandboxing approach, they often have to reimplement or
partially bypass the standard tools that each programming language provides. The
Rust rules call Rust's compiler directly for example, instead of using Cargo,
and the Python rules extract each PyPi package into a separate folder that gets
added to sys.path.
These separate language rules allow proper declaration of inputs and outputs,
and offer some advantages such as caching of build products and fine-grained
dependency installation. But they also bring some downsides:
- The rules don't always support use-cases/platforms that the standard language
tools do, meaning they need to be patched to be used. I've had to contribute a
number of patches to the Rust, Python and JS rules to unblock various issues.
- The dependencies we use with each language sometimes make assumptions that do
not hold in Bazel, meaning they either need to be pinned or patched, or the
language rules need to be adjusted to accommodate them.
I was hopeful that after the initial setup work, things would be relatively
smooth-sailing. Unfortunately, that has not proved to be the case. Things
frequently broke when dependencies or the language rules were updated, and I
began to get frustrated at the amount of Anki development time I was instead
spending on build system upkeep. It's now about 2 years since switching to
Bazel, and I think it's time to cut losses, and switch to something else that's
a better fit.
The new build system is based on a small build tool called Ninja, and some
custom Rust code in build/. This means that to build Anki, Bazel is no longer
required, but Ninja and Rust need to be installed on your system. Python and
Node toolchains are automatically downloaded like in Bazel.
This new build system should result in faster builds in some cases:
- Because we're using cargo to build now, Rust builds are able to take advantage
of pipelining and incremental debug builds, which we didn't have with Bazel.
It's also easier to override the default linker on Linux/macOS, which can
further improve speeds.
- External Rust crates are now built with opt=1, which improves performance
of debug builds.
- Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript
compiler. This results in faster builds, by deferring typechecking to test/check
time, and by allowing more work to happen in parallel.
As an example of the differences, when testing with the mold linker on Linux,
adding a new message to tags.proto (which triggers a recompile of the bulk of
the Rust and TypeScript code) results in a compile that goes from about 22s on
Bazel to about 7s in the new system. With the standard linker, it's about 9s.
Some other changes of note:
- Our Rust workspace now uses cargo-hakari to ensure all packages agree on
available features, preventing unnecessary rebuilds.
- pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge
source files and generated files into a single folder for running. By telling
VSCode about the extra search path, code completion now works with generated
files without needing to symlink them into the source folder.
- qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py.
Instead, the generated files are now placed in a separate _aqt package that's
added to the path.
- ts/lib is now exposed as @tslib, so the source code and generated code can be
provided under the same namespace without a merging step.
- MyPy and PyLint are now invoked once for the entire codebase.
- dprint will be used to format TypeScript/json files in the future instead of
the slower prettier (currently turned off to avoid causing conflicts). It can
automatically defer to prettier when formatting Svelte files.
- svelte-check is now used for typechecking our Svelte code, which revealed a
few typing issues that went undetected with the old system.
- The Jest unit tests now work on Windows as well.
If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes:
- please remove node_modules and .bazel
- install rustup (https://rustup.rs/)
- install rsync if not already installed (on windows, use pacman - see docs/windows.md)
- install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and
place on your path, or from your distro/homebrew if it's 1.10+)
- update .vscode/settings.json from .vscode.dist
2022-11-27 06:24:20 +01:00
|
|
|
.on("click", function(_event: MouseEvent, d: DayDatum) {
|
2021-01-18 02:09:04 +01:00
|
|
|
if (d.count > 0) {
|
|
|
|
dispatch("search", { query: `"prop:rated=${d.day}"` });
|
|
|
|
}
|
2021-01-18 01:24:32 +01:00
|
|
|
})
|
2020-06-30 08:39:30 +02:00
|
|
|
.transition()
|
|
|
|
.duration(800)
|
2021-03-21 13:47:52 +01:00
|
|
|
.attr("fill", (d: DayDatum) => (d.count === 0 ? emptyColour : blues(d.count)!));
|
2020-06-30 07:09:20 +02:00
|
|
|
}
|
2021-04-23 05:00:18 +02:00
|
|
|
|
Migrate to protobuf-es (#2547)
* Fix .no-reduce-motion missing from graphs spinner, and not being honored
* Begin migration from protobuf.js -> protobuf-es
Motivation:
- Protobuf-es has a nicer API: messages are represented as classes, and
fields which should exist are not marked as nullable.
- As it uses modules, only the proto messages we actually use get included
in our bundle output. Protobuf.js put everything in a namespace, which
prevented tree-shaking, and made it awkward to access inner messages.
- ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff
is slower decoding/encoding (#2043), but that was mainly a concern for the
graphs page, and was unblocked by
https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78
Approach/notes:
- We generate the new protobuf-es interface in addition to existing
protobuf.js interface, so we can migrate a module at a time, starting
with the graphs module.
- rslib:proto now generates RPC methods for TS in addition to the Python
interface. The input-arg-unrolling behaviour of the Python generation is
not required here, as we declare the input arg as a PlainMessage<T>, which
marks it as requiring all fields to be provided.
- i64 is represented as bigint in protobuf-es. We were using a patch to
protobuf.js to get it to output Javascript numbers instead of long.js
types, but now that our supported browser versions support bigint, it's
probably worth biting the bullet and migrating to bigint use. Our IDs
fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future
fields we add.
- Oneofs are handled differently in protobuf-es, and are going to need
some refactoring.
Other notable changes:
- Added a --mkdir arg to our build runner, so we can create a dir easily
during the build on Windows.
- Simplified the preference handling code, by wrapping the preferences
in an outer store, instead of a separate store for each individual
preference. This means a change to one preference will trigger a redraw
of all components that depend on the preference store, but the redrawing
is cheap after moving the data processing to Rust, and it makes the code
easier to follow.
- Drop async(Reactive).ts in favour of more explicit handling with await
blocks/updating.
- Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding
dependencies to parent groups. Renamed add() -> add_action() for clarity.
* Remove a couple of unused proto imports
* Migrate card info
* Migrate congrats, image occlusion, and tag editor
+ Fix imports for multi-word proto files.
* Migrate change-notetype
* Migrate deck options
* Bump target to es2020; simplify ts lib list
Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome
on Android support the full es2017-es2020 features.
* Migrate import-csv
* Migrate i18n and fix missing output types in .js
* Migrate custom scheduling, and remove protobuf.js
To mostly maintain our old API contract, we make use of protobuf-es's
ability to convert to JSON, which follows the same format as protobuf.js
did. It doesn't cover all case: users who were previously changing the
variant of a type will need to update their code, as assigning to a new
variant no longer automatically removes the old one, which will cause an
error when we try to convert back from JSON. But I suspect the large majority
of users are adjusting the current variant rather than creating a new one,
and this saves us having to write proxy wrappers, so it seems like a
reasonable compromise.
One other change I made at the same time was to rename value->kind for
the oneofs in our custom study protos, as 'value' was easily confused
with the 'case/value' output that protobuf-es has.
With protobuf.js codegen removed, touching a proto file and invoking
./run drops from about 8s to 6s.
This closes #2043.
* Allow tree-shaking on protobuf types
* Display backend error messages in our ts alert()
* Make sourcemap generation opt-in for ts-run
Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
|
|
|
function timeFunctionForDay(firstDayOfWeek: Weekday): CountableTimeInterval {
|
2021-04-23 05:00:18 +02:00
|
|
|
switch (firstDayOfWeek) {
|
|
|
|
case Weekday.MONDAY:
|
|
|
|
return timeMonday;
|
|
|
|
case Weekday.FRIDAY:
|
|
|
|
return timeFriday;
|
|
|
|
case Weekday.SATURDAY:
|
|
|
|
return timeSaturday;
|
|
|
|
default:
|
|
|
|
return timeSunday;
|
|
|
|
}
|
|
|
|
}
|