diff options
author | Mo Tarbin <mhed.t91@gmail.com> | 2024-07-07 00:26:08 -0400 |
---|---|---|
committer | Mo Tarbin <mhed.t91@gmail.com> | 2024-07-07 00:26:08 -0400 |
commit | 010db330a46ad32daf30400283971cd4e9835e1d (patch) | |
tree | 83d169e5a73345e8ce2492568366432997a1199b /internal/chore/scheduler.go | |
parent | c92cdb427d050456769f520cdcc4ee10db895ad9 (diff) | |
download | donetick-010db330a46ad32daf30400283971cd4e9835e1d.tar.gz donetick-010db330a46ad32daf30400283971cd4e9835e1d.tar.bz2 donetick-010db330a46ad32daf30400283971cd4e9835e1d.zip |
chore: Update chore detail query to include notes in result
Diffstat (limited to '')
-rw-r--r-- | internal/chore/scheduler.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/internal/chore/scheduler.go b/internal/chore/scheduler.go index 55cdf01..c1ff48d 100644 --- a/internal/chore/scheduler.go +++ b/internal/chore/scheduler.go @@ -47,6 +47,7 @@ func scheduleNextDueDate(chore *chModel.Chore, completedDate time.Time) (*time.T } else if chore.FrequencyType == "yearly" { nextDueDate = baseDate.AddDate(1, 0, 0) } else if chore.FrequencyType == "adaptive" { + // TODO: calculate next due date based on the history of the chore // calculate the difference between the due date and now in days: diff := completedDate.UTC().Sub(chore.NextDueDate.UTC()) @@ -129,6 +130,33 @@ func scheduleNextDueDate(chore *chModel.Chore, completedDate time.Time) (*time.T } +func scheduleAdaptiveNextDueDate(chore *chModel.Chore, completedDate time.Time, history []*chModel.ChoreHistory) (*time.Time, error) { + // will generate due date base on history and the different between the completed date and the due date + // the more recent the higher weight + if len(history) <= 1 { + if chore.NextDueDate != nil { + diff := completedDate.UTC().Sub(chore.NextDueDate.UTC()) + nextDueDate := completedDate.UTC().Add(diff) + return &nextDueDate, nil + } + return nil, nil + } + var weight float64 + var totalWeight float64 + var nextDueDate time.Time + for i := 0; i < len(history)-1; i++ { + delay := history[i].CompletedAt.UTC().Sub(history[i+1].CompletedAt.UTC()).Seconds() + weight = delay * float64(len(history)-i) + totalWeight += weight + } + // calculate the average delay + averageDelay := totalWeight / float64(len(history)-1) + // calculate the difference between the completed date and the due date + nextDueDate = completedDate.UTC().Add(time.Duration(averageDelay) * time.Second) + + return &nextDueDate, nil +} + func RemoveAssigneeAndReassign(chore *chModel.Chore, userID int) { for i, assignee := range chore.Assignees { if assignee.UserID == userID { |