From 04e606bf4bdf4a8050cc4eaa0696a206d1288bf5 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 15 Jan 2021 01:39:23 +0100 Subject: [PATCH 1/3] Add firstWeekday to GraphsOut --- rslib/backend.proto | 7 +++++++ rslib/src/config.rs | 20 ++++++++++++++++++++ rslib/src/stats/graphs.rs | 2 ++ 3 files changed, 29 insertions(+) diff --git a/rslib/backend.proto b/rslib/backend.proto index 52bb6b24a..848a201b9 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -1082,6 +1082,12 @@ message GraphsIn { } message GraphsOut { + enum Weekday { + SUNDAY = 0; + MONDAY = 1; + FRIDAY = 5; + SATURDAY = 6; + } repeated Card cards = 1; repeated RevlogEntry revlog = 2; uint32 days_elapsed = 3; @@ -1090,6 +1096,7 @@ message GraphsOut { uint32 scheduler_version = 5; /// Seconds to add to UTC timestamps to get local time. int32 local_offset_secs = 7; + Weekday first_weekday = 8; } message RevlogEntry { diff --git a/rslib/src/config.rs b/rslib/src/config.rs index 53838e51c..8e8c0f9f3 100644 --- a/rslib/src/config.rs +++ b/rslib/src/config.rs @@ -18,6 +18,7 @@ pub(crate) fn schema11_config_as_string() -> String { "curDeck": 1, "newSpread": 0, "collapseTime": 1200, + "firstWeekday": 0, "timeLim": 0, "estTimes": true, "dueCounts": true, @@ -47,6 +48,7 @@ pub(crate) enum ConfigKey { ShowRemainingDueCountsInStudy, ShowIntervalsAboveAnswerButtons, NewReviewMix, + FirstWeekday, AnswerTimeLimitSecs, ShowDayLearningCardsFirst, LastUnburiedDay, @@ -75,6 +77,7 @@ impl From for &'static str { ConfigKey::ShowRemainingDueCountsInStudy => "dueCounts", ConfigKey::ShowIntervalsAboveAnswerButtons => "estTimes", ConfigKey::NewReviewMix => "newSpread", + ConfigKey::FirstWeekday => "firstWeekday", ConfigKey::AnswerTimeLimitSecs => "timeLim", ConfigKey::ShowDayLearningCardsFirst => "dayLearnFirst", ConfigKey::LastUnburiedDay => "lastUnburied", @@ -227,6 +230,16 @@ impl Collection { self.set_config(ConfigKey::NewReviewMix, &(mix as u8)) } + + pub(crate) fn get_first_weekday(&self) -> Weekday { + match self.get_config_default::(ConfigKey::FirstWeekday) { + 1 => Weekday::Monday, + 5 => Weekday::Friday, + 6 => Weekday::Saturday, + _ => Weekday::Sunday, + } + } + pub(crate) fn get_show_due_counts(&self) -> bool { self.get_config_optional(ConfigKey::ShowRemainingDueCountsInStudy) .unwrap_or(true) @@ -309,6 +322,13 @@ pub(crate) enum NewReviewMix { NewFirst = 2, } +pub(crate) enum Weekday { + Sunday = 0, + Monday = 1, + Friday = 5, + Saturday = 6, +} + #[cfg(test)] mod test { use super::SortKind; diff --git a/rslib/src/stats/graphs.rs b/rslib/src/stats/graphs.rs index f0e8e7163..90f3139ea 100644 --- a/rslib/src/stats/graphs.rs +++ b/rslib/src/stats/graphs.rs @@ -25,6 +25,7 @@ impl Collection { let offset = self.local_utc_offset_for_user()?; let local_offset_secs = offset.local_minus_utc() as i64; + let cards = self.storage.all_searched_cards()?; let revlog = if all { self.storage.get_all_revlog_entries(revlog_start)? @@ -42,6 +43,7 @@ impl Collection { next_day_at_secs: timing.next_day_at as u32, scheduler_version: self.sched_ver() as u32, local_offset_secs: local_offset_secs as i32, + first_weekday: self.get_first_weekday() as i32, }) } } From 4a733de94ed93ea69cf79712099936dec67380e2 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 18 Jan 2021 23:23:55 +0100 Subject: [PATCH 2/3] Use firstWeekday config in graphs data --- ts/graphs/calendar.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/ts/graphs/calendar.ts b/ts/graphs/calendar.ts index f9e4decfd..58e33d64d 100644 --- a/ts/graphs/calendar.ts +++ b/ts/graphs/calendar.ts @@ -13,12 +13,14 @@ import { select, mouse } from "d3-selection"; import { scaleLinear, scaleSequential } from "d3-scale"; import { showTooltip, hideTooltip } from "./tooltip"; import { GraphBounds, setDataAvailable, RevlogRange } from "./graph-helpers"; -import { timeDay, timeYear, timeWeek } from "d3-time"; +import { timeDay, timeYear, timeSunday, timeMonday, timeFriday, timeSaturday } from "d3-time"; +import type { CountableTimeInterval } from "d3-time"; import type { I18n } from "anki/i18n"; export interface GraphData { // indexed by day, where day is relative to today reviewCount: Map; + timeFunction: CountableTimeInterval, } interface DayDatum { @@ -45,7 +47,15 @@ export function gatherData(data: pb.BackendProto.GraphsOut): GraphData { reviewCount.set(day, count + 1); } - return { reviewCount }; + let timeFunction = data.firstWeekday === 1 + ? timeMonday + : data.firstWeekday === 5 + ? timeFriday + : data.firstWeekday === 6 + ? timeSaturday + : timeSunday; + + return { reviewCount, timeFunction }; } export function renderCalendar( @@ -73,8 +83,8 @@ export function renderCalendar( if (date.getFullYear() != targetYear) { continue; } - const weekNumber = timeWeek.count(timeYear(date), date); - const weekDay = timeDay.count(timeWeek(date), date); + const weekNumber = sourceData.timeFunction.count(timeYear(date), date); + const weekDay = timeDay.count(sourceData.timeFunction(date), date); const yearDay = timeDay.count(timeYear(date), date); dayMap.set(yearDay, { day, count, weekNumber, weekDay, date } as DayDatum); if (count > maxCount) { @@ -105,8 +115,8 @@ export function renderCalendar( } const yearDay = timeDay.count(timeYear(date), date); if (!dayMap.has(yearDay)) { - const weekNumber = timeWeek.count(timeYear(date), date); - const weekDay = timeDay.count(timeWeek(date), date); + const weekNumber = sourceData.timeFunction.count(timeYear(date), date); + const weekDay = timeDay.count(sourceData.timeFunction(date), date); dayMap.set(yearDay, { day: yearDay, count: 0, From a5947e5c65420d16ac57ea48de90996cf6310afb Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 18 Jan 2021 23:27:57 +0100 Subject: [PATCH 3/3] Satisfy formatter --- rslib/src/config.rs | 1 - rslib/src/stats/graphs.rs | 1 - ts/graphs/calendar.ts | 26 +++++++++++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/rslib/src/config.rs b/rslib/src/config.rs index 8e8c0f9f3..9a90b1069 100644 --- a/rslib/src/config.rs +++ b/rslib/src/config.rs @@ -230,7 +230,6 @@ impl Collection { self.set_config(ConfigKey::NewReviewMix, &(mix as u8)) } - pub(crate) fn get_first_weekday(&self) -> Weekday { match self.get_config_default::(ConfigKey::FirstWeekday) { 1 => Weekday::Monday, diff --git a/rslib/src/stats/graphs.rs b/rslib/src/stats/graphs.rs index 90f3139ea..59243e13f 100644 --- a/rslib/src/stats/graphs.rs +++ b/rslib/src/stats/graphs.rs @@ -25,7 +25,6 @@ impl Collection { let offset = self.local_utc_offset_for_user()?; let local_offset_secs = offset.local_minus_utc() as i64; - let cards = self.storage.all_searched_cards()?; let revlog = if all { self.storage.get_all_revlog_entries(revlog_start)? diff --git a/ts/graphs/calendar.ts b/ts/graphs/calendar.ts index 58e33d64d..a5b1deb23 100644 --- a/ts/graphs/calendar.ts +++ b/ts/graphs/calendar.ts @@ -13,14 +13,21 @@ import { select, mouse } from "d3-selection"; import { scaleLinear, scaleSequential } from "d3-scale"; import { showTooltip, hideTooltip } from "./tooltip"; import { GraphBounds, setDataAvailable, RevlogRange } from "./graph-helpers"; -import { timeDay, timeYear, timeSunday, timeMonday, timeFriday, timeSaturday } from "d3-time"; +import { + timeDay, + timeYear, + timeSunday, + timeMonday, + timeFriday, + timeSaturday, +} from "d3-time"; import type { CountableTimeInterval } from "d3-time"; import type { I18n } from "anki/i18n"; export interface GraphData { // indexed by day, where day is relative to today reviewCount: Map; - timeFunction: CountableTimeInterval, + timeFunction: CountableTimeInterval; } interface DayDatum { @@ -47,13 +54,14 @@ export function gatherData(data: pb.BackendProto.GraphsOut): GraphData { reviewCount.set(day, count + 1); } - let timeFunction = data.firstWeekday === 1 - ? timeMonday - : data.firstWeekday === 5 - ? timeFriday - : data.firstWeekday === 6 - ? timeSaturday - : timeSunday; + const timeFunction = + data.firstWeekday === 1 + ? timeMonday + : data.firstWeekday === 5 + ? timeFriday + : data.firstWeekday === 6 + ? timeSaturday + : timeSunday; return { reviewCount, timeFunction }; }