feat(#2): адаптивный шедулер — сдвигает напоминания на «хорошие» часы из истории

This commit is contained in:
AnRil
2026-05-22 13:52:38 +07:00
parent 81481f2131
commit a0b89ddf71
7 changed files with 152 additions and 9 deletions

View File

@@ -6,6 +6,7 @@ import { getExercises, getHistory, getSettings, updateExercise } from './store'
import { fireReminder } from './notifications'
import { broadcastState } from './state-actions'
import { isMeetingActiveSync, refreshMeetingState } from './meeting-detect'
import { adjustNextFireAt } from './adaptive'
/**
* Сколько reps пользователь сделал по упражнению `ex` за сегодня (local day).
@@ -57,10 +58,12 @@ function checkDueExercises(): void {
const now = Date.now()
const exercises = getExercises()
// history запрашивается только если хотя бы у одного упражнения есть
// dailyGoal — для большинства pure-interval упражнений не нужна.
const anyGoal = exercises.some((e) => e.dailyGoal !== undefined)
const history = anyGoal ? getHistory() : []
// history запрашивается если у какого-нибудь упражнения есть
// dailyGoal или adaptive: false — иначе экономим IPC-нагрузку.
const needsHistory = exercises.some(
(e) => e.dailyGoal !== undefined || e.adaptive
)
const history = needsHistory ? getHistory() : []
let anyFired = false
for (const ex of exercises) {
if (!ex.enabled) continue
@@ -77,9 +80,13 @@ function checkDueExercises(): void {
continue
}
}
const updated = updateExercise(ex.id, {
nextFireAt: now + ex.intervalMinutes * 60_000
})
// Базовый candidate. Если adaptive — сдвигаем на «хороший» час
// по исторической статистике успеха/скипов.
let nextFireAt = now + ex.intervalMinutes * 60_000
if (ex.adaptive) {
nextFireAt = adjustNextFireAt(ex, nextFireAt, history)
}
const updated = updateExercise(ex.id, { nextFireAt })
if (updated) {
anyFired = true
fireReminder(updated, settings.notificationMode)