Skip to content

Commit

Permalink
Add pipehealth command
Browse files Browse the repository at this point in the history
  • Loading branch information
rcy committed Oct 2, 2023
1 parent 94bb26b commit 5b19e80
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
41 changes: 36 additions & 5 deletions handlers/feed_me.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,34 @@ import (
"time"
)

func FeedMe(params bot.HandlerParams) error {
func candidateLinks(age time.Duration) ([]notes.Note, error) {
notes := []notes.Note{}

query := `select id, created_at, nick, text, kind from notes where created_at <= ? and nick = target order by random() limit 5`
query := `select id, created_at, nick, text, kind from notes where created_at <= ? and nick = target order by random() limit 69`

t := time.Now().Add(-time.Hour * 24).UTC().Format("2006-01-02T15:04:05Z")
t := time.Now().Add(-age).UTC().Format("2006-01-02T15:04:05Z")
err := model.DB.Select(&notes, query, t)
if err != nil {
if err == sql.ErrNoRows {
return nil
return nil, nil
}
return nil, err
}
return notes, nil
}

const (
MINAGE = time.Hour * 24
THRESHOLD = 5
)

func FeedMe(params bot.HandlerParams) error {
notes, err := candidateLinks(MINAGE)
if err != nil {
return err
}

if len(notes) < 5 {
if len(notes) < THRESHOLD {
params.Privmsgf(params.Target, "not enough links to feed the channel")
return nil
}
Expand All @@ -39,3 +52,21 @@ func FeedMe(params bot.HandlerParams) error {

return nil
}

func PipeHealth(params bot.HandlerParams) error {
readyNotes, err := candidateLinks(MINAGE)
if err != nil {
return err
}
totalNotes, err := candidateLinks(0)
if err != nil {
return err
}

ready := len(readyNotes)
fermenting := len(totalNotes) - ready

params.Privmsgf(params.Target, "%d links ready to serve (%d fermenting)", ready, fermenting)

return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func addHandlers(b *bot.Bot) {
b.Handle(`^,(.+)$`, handlers.CreateNote)
b.Handle(`^([^\s:]+): (.+)$`, handlers.DeferredDelivery)
b.Handle(`^!feedme`, handlers.FeedMe)
b.Handle(`^!pipehealth`, handlers.PipeHealth)
b.Handle(`(https?://\S+)`, handlers.Link)
b.Handle(`^!day`, handlers.NationalDay)
b.Handle(`\b69[^0-9]*\b`, handlers.Nice)
Expand Down

0 comments on commit 5b19e80

Please sign in to comment.