root moves fixed future schedules to shceduled on automove
parent
e913c9cd3c
commit
ac3dc241f1
|
|
@ -22,19 +22,20 @@ func (root Root) Equals(other Root) bool {
|
||||||
|
|
||||||
func (root *Root) AutoMove() {
|
func (root *Root) AutoMove() {
|
||||||
root.MoveScheduledToTodo()
|
root.MoveScheduledToTodo()
|
||||||
//root.MoveTodoToScheduled()
|
root.MoveTodoToScheduled()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (root *Root) MoveTodoToScheduled() {
|
func (root *Root) MoveTodoToScheduled() {
|
||||||
for i := len(root.Todo) - 1; i >= 0; i-- {
|
for i := len(root.Todo) - 1; i >= 0; i-- {
|
||||||
if root.Todo[i].Schedule != "" && !root.Todo[i].Triggered() {
|
if !root.Todo[i].Schedule.isFixedFuture() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
root.Scheduled = append(root.Scheduled, root.Todo[i])
|
root.Scheduled = append(root.Scheduled, root.Todo[i])
|
||||||
for j := i; j < len(root.Todo)-1; j++ {
|
for j := i; j < len(root.Todo)-1; j++ {
|
||||||
root.Todo[j] = root.Todo[j+1]
|
root.Todo[j] = root.Todo[j+1]
|
||||||
}
|
}
|
||||||
root.Todo = root.Todo[:len(root.Todo)-1]
|
root.Todo = root.Todo[:len(root.Todo)-1]
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (root *Root) MoveScheduledToTodo() {
|
func (root *Root) MoveScheduledToTodo() {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,42 @@ func (schedule Schedule) Next(t time.Time) (time.Time, error) {
|
||||||
return scheduler.next(t)
|
return scheduler.next(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (schedule Schedule) isFixedFuture() bool {
|
||||||
|
if !schedule.isFixed() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return schedule.isFuture()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (schedule Schedule) isFixed() bool {
|
||||||
|
if schedule.empty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
scheduler := schedulerFactory(string(schedule))
|
||||||
|
switch scheduler.(type) {
|
||||||
|
case scheduleEZDate, scheduleDue:
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (schedule Schedule) isFuture() bool {
|
||||||
|
if schedule.empty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
scheduler := schedulerFactory(string(schedule))
|
||||||
|
t, err := scheduler.next(time.Now())
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return time.Now().Before(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (schedule Schedule) empty() bool {
|
||||||
|
return string(schedule) == ""
|
||||||
|
}
|
||||||
|
|
||||||
type scheduler interface {
|
type scheduler interface {
|
||||||
next(time.Time) (time.Time, error)
|
next(time.Time) (time.Time, error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -130,3 +130,60 @@ func TestSchedulerFactory(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestScheduleIsFixedFuture(t *testing.T) {
|
||||||
|
cases := map[string]struct {
|
||||||
|
input string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
"empty": {
|
||||||
|
input: "",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
"cron": {
|
||||||
|
input: "* * * * *",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
"duration": {
|
||||||
|
input: "1m",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
"due past": {
|
||||||
|
input: "123",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
"due future": {
|
||||||
|
input: "9648154541",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
"ez date short past": {
|
||||||
|
input: "2000-01-02",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
"ez date short future": {
|
||||||
|
input: "2100-01-02",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
"ez date long past": {
|
||||||
|
input: "2000-01-02T01",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
"ez date long future": {
|
||||||
|
input: "2100-01-02T02",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, d := range cases {
|
||||||
|
c := d
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
schedule := Schedule(c.input)
|
||||||
|
got := schedule.isFixedFuture()
|
||||||
|
if got != c.want {
|
||||||
|
gotFuture := schedule.isFuture()
|
||||||
|
gotFixed := schedule.isFixed()
|
||||||
|
t.Errorf("want %v, got %v = %v && %v", c.want, got, gotFuture, gotFixed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue