anki/ts/card-info/CardStats.svelte
Damien Elmes 6941bccde4 Add support for proto3 optional scalars
Protobuf 3.15 introduced support for marking scalar fields like
uint32 as optional, and all of our tooling appears to support it
now. This allows us to use simple optional/null checks in our Rust/
TypeScript code, without having to resort to an inner message.

I had to apply a minor patch to protobufjs to get this working with
the json-module output; this has also been submitted upstream:
https://github.com/protobufjs/protobuf.js/pull/1693

I've modified CardStatsResponse as an example of the new syntax.

One thing to note: while the Rust and TypeScript bindings use optional/
null fields, as that is the norm in those languages, Google's Python
bindings are not very Pythonic. Referencing an optional field that is
missing will yield the default value, and a separate HasField() call
is required, eg:

```
>>> from anki.stats_pb2 import CardStatsResponse as R
... msg = R.FromString(b"")
... print(msg.first_review)
... print(msg.HasField("first_review"))
0
False
```
2022-02-27 19:42:06 +10:00

113 lines
3.2 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import * as tr2 from "../lib/ftl";
import { Stats } from "../lib/proto";
import { DAY, timeSpan, Timestamp } from "../lib/time";
export let stats: Stats.CardStatsResponse;
function dateString(timestamp: number): string {
return new Timestamp(timestamp).dateString();
}
interface StatsRow {
label: string;
value: string | number;
}
function rowsFromStats(stats: Stats.CardStatsResponse): StatsRow[] {
const statsRows: StatsRow[] = [];
statsRows.push({ label: tr2.cardStatsAdded(), value: dateString(stats.added) });
if (stats.firstReview != null) {
statsRows.push({
label: tr2.cardStatsFirstReview(),
value: dateString(stats.firstReview),
});
}
if (stats.latestReview != null) {
statsRows.push({
label: tr2.cardStatsLatestReview(),
value: dateString(stats.latestReview),
});
}
if (stats.dueDate != null) {
statsRows.push({
label: tr2.statisticsDueDate(),
value: dateString(stats.dueDate),
});
}
if (stats.duePosition != null) {
statsRows.push({
label: tr2.cardStatsNewCardPosition(),
value: stats.duePosition,
});
}
if (stats.interval) {
statsRows.push({
label: tr2.cardStatsInterval(),
value: timeSpan(stats.interval * DAY),
});
}
if (stats.ease) {
statsRows.push({
label: tr2.cardStatsEase(),
value: `${stats.ease / 10}%`,
});
}
statsRows.push({ label: tr2.cardStatsReviewCount(), value: stats.reviews });
statsRows.push({ label: tr2.cardStatsLapseCount(), value: stats.lapses });
if (stats.totalSecs) {
statsRows.push({
label: tr2.cardStatsAverageTime(),
value: timeSpan(stats.averageSecs),
});
statsRows.push({
label: tr2.cardStatsTotalTime(),
value: timeSpan(stats.totalSecs),
});
}
statsRows.push({ label: tr2.cardStatsCardTemplate(), value: stats.cardType });
statsRows.push({ label: tr2.cardStatsNoteType(), value: stats.notetype });
statsRows.push({ label: tr2.cardStatsDeckName(), value: stats.deck });
statsRows.push({ label: tr2.cardStatsCardId(), value: stats.cardId });
statsRows.push({ label: tr2.cardStatsNoteId(), value: stats.noteId });
return statsRows;
}
let statsRows: StatsRow[];
$: statsRows = rowsFromStats(stats);
</script>
<table class="stats-table align-start">
{#each statsRows as row}
<tr>
<th class="align-start">{row.label}</th>
<td>{row.value}</td>
</tr>
{/each}
</table>
<style>
.stats-table {
width: 100%;
border-spacing: 1em 0;
border-collapse: collapse;
}
.align-start {
text-align: start;
}
</style>