add countdown back

This commit is contained in:
Chris Sexton 2021-12-22 13:52:16 -05:00
parent 8b38429a31
commit b1822efe18
2 changed files with 129 additions and 28 deletions

View File

@ -4,6 +4,8 @@ package countdown
import ( import (
"fmt" "fmt"
bh "github.com/timshannon/bolthold" bh "github.com/timshannon/bolthold"
"github.com/velour/catbase/plugins/babbler"
"github.com/velour/catbase/plugins/counter"
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -48,35 +50,13 @@ func (p *CountdownPlugin) scheduleNYE(f func()) {
func (p *CountdownPlugin) newYearRollover() { func (p *CountdownPlugin) newYearRollover() {
log.Debug().Msgf("Happy new year!") log.Debug().Msgf("Happy new year!")
// todo: something about rolling over if err := p.clearBabblers(); err != nil {
//tx, err := p.db.Beginx() log.Error().Err(err).Msgf("error clearing babblers")
//if err != nil { }
// logError(err, "error getting transaction")
// return if err := p.archiveCounters(); err != nil {
//} log.Error().Err(err).Msgf("error clearing babblers")
//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
} }
log.Error().Err(err).Msgf(msg)
} }
func (p *CountdownPlugin) nyeCountdown(i int) { 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) 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
}

View File

@ -1,6 +1,12 @@
package countdown package countdown
import ( 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" "testing"
"time" "time"
@ -9,6 +15,19 @@ import (
"github.com/velour/catbase/bot" "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) { func TestScheduleNewYears(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
p := New(mb) p := New(mb)
@ -24,3 +43,58 @@ func TestScheduleNewYears(t *testing.T) {
assert.True(t, functionTripped) 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)
}