Expose fuzz delta for FSRS add-on

This commit is contained in:
Damien Elmes 2023-11-06 12:27:53 +10:00
parent e2ee6b7d44
commit 9268dce707
5 changed files with 46 additions and 1 deletions

View File

@ -53,6 +53,9 @@ service SchedulerService {
returns (ComputeOptimalRetentionResponse);
rpc EvaluateWeights(EvaluateWeightsRequest) returns (EvaluateWeightsResponse);
rpc ComputeMemoryState(cards.CardId) returns (ComputeMemoryStateResponse);
// The number of days the calculated interval will be fuzzed by. Utilized by
// the FSRS add-on.
rpc FuzzDelta(FuzzDeltaRequest) returns (FuzzDeltaResponse);
}
// Implicitly includes any of the above methods that are not listed in the
@ -406,3 +409,12 @@ message ComputeMemoryStateResponse {
optional cards.FsrsMemoryState state = 1;
float desired_retention = 2;
}
message FuzzDeltaRequest {
int64 card_id = 1;
uint32 interval = 2;
}
message FuzzDeltaResponse {
sint32 delta_days = 1;
}

View File

@ -1153,6 +1153,10 @@ class Collection(DeprecatedNamesMixin):
else:
return ComputedMemoryState(desired_retention=resp.desired_retention)
def fuzz_delta(self, card_id: CardId, interval: int) -> int:
"The delta days of fuzz applied if reviewing the card in v3."
return self._backend.fuzz_delta(card_id=card_id, interval=interval)
# Timeboxing
##########################################################################
# fixme: there doesn't seem to be a good reason why this code is in main.py

View File

@ -395,7 +395,7 @@ impl Collection {
})
}
fn home_deck_config(
pub(crate) fn home_deck_config(
&self,
config_id: Option<DeckConfigId>,
home_deck_id: DeckId,

View File

@ -11,6 +11,8 @@ use anki_proto::scheduler::ComputeFsrsWeightsResponse;
use anki_proto::scheduler::ComputeMemoryStateResponse;
use anki_proto::scheduler::ComputeOptimalRetentionRequest;
use anki_proto::scheduler::ComputeOptimalRetentionResponse;
use anki_proto::scheduler::FuzzDeltaRequest;
use anki_proto::scheduler::FuzzDeltaResponse;
use anki_proto::scheduler::GetOptimalRetentionParametersResponse;
use fsrs::FSRSItem;
use fsrs::FSRSReview;
@ -288,6 +290,12 @@ impl crate::services::SchedulerService for Collection {
fn compute_memory_state(&mut self, input: cards::CardId) -> Result<ComputeMemoryStateResponse> {
self.compute_memory_state(input.into())
}
fn fuzz_delta(&mut self, input: FuzzDeltaRequest) -> Result<FuzzDeltaResponse> {
Ok(FuzzDeltaResponse {
delta_days: self.get_fuzz_delta(input.card_id.into(), input.interval)?,
})
}
}
impl crate::services::BackendSchedulerService for Backend {

View File

@ -2,6 +2,8 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::StateContext;
use crate::collection::Collection;
use crate::prelude::*;
/// Describes a range of days for which a certain amount of fuzz is applied to
/// the new interval.
@ -37,6 +39,25 @@ impl<'a> StateContext<'a> {
}
}
impl Collection {
/// Used for FSRS add-on.
pub(crate) fn get_fuzz_delta(&self, card_id: CardId, interval: u32) -> Result<i32> {
let card = self.storage.get_card(card_id)?.or_not_found(card_id)?;
let deck = self
.storage
.get_deck(card.deck_id)?
.or_not_found(card.deck_id)?;
let config = self.home_deck_config(deck.config_id(), card.original_deck_id)?;
let fuzzed = with_review_fuzz(
card.get_fuzz_factor(),
interval as f32,
1,
config.inner.maximum_review_interval,
);
Ok((fuzzed as i32) - (interval as i32))
}
}
pub(crate) fn with_review_fuzz(
fuzz_factor: Option<f32>,
interval: f32,