diff --git a/plugins/countdown/countdown.go b/plugins/countdown/countdown.go index c2f7a30..d469e7b 100644 --- a/plugins/countdown/countdown.go +++ b/plugins/countdown/countdown.go @@ -4,6 +4,8 @@ package countdown import ( "fmt" bh "github.com/timshannon/bolthold" + "github.com/velour/catbase/plugins/babbler" + "github.com/velour/catbase/plugins/counter" "time" "github.com/rs/zerolog/log" @@ -48,35 +50,13 @@ func (p *CountdownPlugin) scheduleNYE(f func()) { func (p *CountdownPlugin) newYearRollover() { log.Debug().Msgf("Happy new year!") - // todo: something about rolling over - //tx, err := p.db.Beginx() - //if err != nil { - // logError(err, "error getting transaction") - // return - //} - //q := fmt.Sprintf(`create table counter_%d as select * from counter`, thisYear) - //_, err = tx.Exec(q) - //if err != nil { - // logError(err, "error running insert into") - // logError(tx.Rollback(), "error with insert rollback") - // return - //} - //q = `delete from counter` - //_, err = tx.Exec(q) - //if err != nil { - // logError(err, "error running delete") - // logError(tx.Rollback(), "error with delete rollback") - // return - //} - //err = tx.Commit() - //logError(err, "error committing transaction") -} - -func logError(err error, msg string) { - if err == nil { - return + if err := p.clearBabblers(); err != nil { + log.Error().Err(err).Msgf("error clearing babblers") + } + + if err := p.archiveCounters(); err != nil { + log.Error().Err(err).Msgf("error clearing babblers") } - log.Error().Err(err).Msgf(msg) } func (p *CountdownPlugin) nyeCountdown(i int) { @@ -86,3 +66,50 @@ func (p *CountdownPlugin) nyeCountdown(i int) { p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg, msg) } } + +func (p *CountdownPlugin) clearBabblers() error { + if err := p.store.DeleteMatching(babbler.Babbler{}, &bh.Query{}); err != nil { + return err + } + if err := p.store.DeleteMatching(babbler.BabblerArc{}, &bh.Query{}); err != nil { + return err + } + if err := p.store.DeleteMatching(babbler.BabblerNode{}, &bh.Query{}); err != nil { + return err + } + if err := p.store.DeleteMatching(babbler.BabblerWord{}, &bh.Query{}); err != nil { + return err + } + return nil +} + +type counterArchive struct { + counter.Item + Year int +} + +func (ca counterArchive) Key() string { + return fmt.Sprintf("%d-%d", ca.Year, ca.ID) +} + +func (p *CountdownPlugin) archiveCounters() error { + year := time.Now().Year() + allCounters := []counter.Item{} + + if err := p.store.Find(&allCounters, &bh.Query{}); err != nil { + return err + } + for _, c := range allCounters { + ca := counterArchive{c, year} + err := p.store.Insert(ca.Key(), ca) + if err != nil { + return err + } + } + + if err := p.store.DeleteMatching(counter.Item{}, &bh.Query{}); err != nil { + return err + } + + return nil +} diff --git a/plugins/countdown/countdown_test.go b/plugins/countdown/countdown_test.go index 3212848..6fe557b 100644 --- a/plugins/countdown/countdown_test.go +++ b/plugins/countdown/countdown_test.go @@ -1,6 +1,12 @@ package countdown import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + bh "github.com/timshannon/bolthold" + "github.com/velour/catbase/plugins/babbler" + "github.com/velour/catbase/plugins/counter" + "os" "testing" "time" @@ -9,6 +15,19 @@ import ( "github.com/velour/catbase/bot" ) +func init() { + output := zerolog.ConsoleWriter{Out: os.Stdout} + log.Logger = log.Output(output).With().Caller().Stack().Logger() +} + +func setup() (*bot.MockBot, *CountdownPlugin, func()) { + mb := bot.NewMockBot() + cd := New(mb) + return mb, cd, func() { + mb.TearDown() + } +} + func TestScheduleNewYears(t *testing.T) { mb := bot.NewMockBot() p := New(mb) @@ -24,3 +43,58 @@ func TestScheduleNewYears(t *testing.T) { assert.True(t, functionTripped) } + +func TestClearBabbles(t *testing.T) { + mb, cd, td := setup() + defer td() + b := babbler.Babbler{ + Name: "foo", + } + if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil { + t.Fail() + } + if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil { + t.Fail() + } + if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil { + t.Fail() + } + count, err := mb.Store().Count(babbler.Babbler{}, &bh.Query{}) + assert.Nil(t, err) + assert.Greater(t, count, 0) + err = cd.clearBabblers() + assert.Nil(t, err) + count, err = mb.Store().Count(babbler.Babbler{}, &bh.Query{}) + assert.Nil(t, err) + assert.Equal(t, count, 0) +} + +func TestArchiveCounters(t *testing.T) { + mb, cd, td := setup() + defer td() + i := counter.Item{ + Nick: "foo", + Item: "bar", + Count: 8, + UserID: "123", + } + if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil { + t.Fail() + } + if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil { + t.Fail() + } + if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil { + t.Fail() + } + count, err := mb.Store().Count(counter.Item{}, &bh.Query{}) + assert.Nil(t, err) + assert.Greater(t, count, 0) + + err = cd.archiveCounters() + assert.Nil(t, err) + + count, err = mb.Store().Count(counter.Item{}, &bh.Query{}) + assert.Nil(t, err) + assert.Equal(t, count, 0) +}