mirror of
https://github.com/cjdenio/jia.git
synced 2024-11-22 15:43:39 +00:00
🚧 Start adding monthly progress report
This commit is contained in:
parent
ed06d3145d
commit
2f8d804628
6 changed files with 89 additions and 33 deletions
|
@ -15,6 +15,8 @@ func main() {
|
||||||
fmt.Println("Starting Jia…")
|
fmt.Println("Starting Jia…")
|
||||||
config := jia.NewConfig()
|
config := jia.NewConfig()
|
||||||
|
|
||||||
|
jia.InitCron(config)
|
||||||
|
|
||||||
// Start receiving messages
|
// Start receiving messages
|
||||||
fmt.Println(fmt.Sprintf("Listening on port %d", config.Port))
|
fmt.Println(fmt.Sprintf("Listening on port %d", config.Port))
|
||||||
jia.StartServer(config)
|
jia.StartServer(config)
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -6,5 +6,6 @@ require (
|
||||||
github.com/go-redis/redis v6.15.8+incompatible
|
github.com/go-redis/redis v6.15.8+incompatible
|
||||||
github.com/joho/godotenv v1.3.0
|
github.com/joho/godotenv v1.3.0
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/robfig/cron v1.2.0
|
||||||
github.com/slack-go/slack v0.6.5
|
github.com/slack-go/slack v0.6.5
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -13,6 +13,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
|
||||||
|
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
|
||||||
github.com/slack-go/slack v0.6.4 h1:cxOqFgM5RW6mdEyDqAJutFk3qiORK9oHRKi5bPqkY9o=
|
github.com/slack-go/slack v0.6.4 h1:cxOqFgM5RW6mdEyDqAJutFk3qiORK9oHRKi5bPqkY9o=
|
||||||
github.com/slack-go/slack v0.6.4/go.mod h1:sGRjv3w+ERAUMMMbldHObQPBcNSyVB7KLKYfnwUFBfw=
|
github.com/slack-go/slack v0.6.4/go.mod h1:sGRjv3w+ERAUMMMbldHObQPBcNSyVB7KLKYfnwUFBfw=
|
||||||
github.com/slack-go/slack v0.6.5 h1:IkDKtJ2IROJNoe3d6mW870/NRKvq2fhLB/Q5XmzWk00=
|
github.com/slack-go/slack v0.6.5 h1:IkDKtJ2IROJNoe3d6mW870/NRKvq2fhLB/Q5XmzWk00=
|
||||||
|
|
35
pkg/jia/cron.go
Normal file
35
pkg/jia/cron.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package jia
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/robfig/cron"
|
||||||
|
"github.com/slack-go/slack"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitCron(config *Config) {
|
||||||
|
c := cron.New()
|
||||||
|
|
||||||
|
c.AddFunc("0 * * * * *", func() {
|
||||||
|
slackClient := slack.New(config.BotToken)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
entries, _ := getLeaderboardEntries(now.Month(), now.Year())
|
||||||
|
|
||||||
|
_, _, err := slackClient.PostMessage(
|
||||||
|
config.ChannelID,
|
||||||
|
slack.MsgOptionText(
|
||||||
|
fmt.Sprintf("It is the start of a new minute! %v", entries[0].User),
|
||||||
|
false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Start()
|
||||||
|
}
|
46
pkg/jia/db.go
Normal file
46
pkg/jia/db.go
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package jia
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Entry struct {
|
||||||
|
Number int
|
||||||
|
User string
|
||||||
|
}
|
||||||
|
|
||||||
|
func getLeaderboardEntries(month time.Month, year int) ([]Entry, error) {
|
||||||
|
scan := redisClient.Scan(0, fmt.Sprintf("leaderboard:%d-%d:*", year, month), 10)
|
||||||
|
if scan.Err() != nil {
|
||||||
|
return nil, errors.New("Something went wrong while loading the leaderboard :cry: Please try again later!")
|
||||||
|
}
|
||||||
|
|
||||||
|
scanIterator := scan.Iterator()
|
||||||
|
|
||||||
|
entries := []Entry{}
|
||||||
|
|
||||||
|
for scanIterator.Next() {
|
||||||
|
entry := redisClient.Get(scanIterator.Val())
|
||||||
|
entryInt, err := entry.Int()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if user, ok := parseLeaderboardEntry(scanIterator.Val()); ok {
|
||||||
|
entries = append(entries, Entry{
|
||||||
|
Number: entryInt,
|
||||||
|
User: user,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort entries
|
||||||
|
sort.Slice(entries, func(i, j int) bool {
|
||||||
|
return entries[i].Number > entries[j].Number
|
||||||
|
})
|
||||||
|
|
||||||
|
return entries, nil
|
||||||
|
}
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -100,41 +99,12 @@ func HandleLeaderboardSlashCommand(w http.ResponseWriter, r *http.Request) {
|
||||||
year := now.Year()
|
year := now.Year()
|
||||||
month := now.Month()
|
month := now.Month()
|
||||||
|
|
||||||
scan := redisClient.Scan(0, fmt.Sprintf("leaderboard:%d-%d:*", year, month), 10)
|
entries, err := getLeaderboardEntries(month, year)
|
||||||
if scan.Err() != nil {
|
|
||||||
w.Write([]byte("Something went wrong while loading the leaderboard :cry: Please try again later!"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
scanIterator := scan.Iterator()
|
|
||||||
|
|
||||||
type Entry struct {
|
|
||||||
Number int
|
|
||||||
User string
|
|
||||||
}
|
|
||||||
|
|
||||||
entries := []Entry{}
|
|
||||||
|
|
||||||
for scanIterator.Next() {
|
|
||||||
entry := redisClient.Get(scanIterator.Val())
|
|
||||||
entryInt, err := entry.Int()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Write(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if user, ok := parseLeaderboardEntry(scanIterator.Val()); ok {
|
|
||||||
entries = append(entries, Entry{
|
|
||||||
Number: entryInt,
|
|
||||||
User: user,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort entries
|
|
||||||
sort.Slice(entries, func(i, j int) bool {
|
|
||||||
return entries[i].Number > entries[j].Number
|
|
||||||
})
|
|
||||||
|
|
||||||
blocks := []slack.Block{
|
blocks := []slack.Block{
|
||||||
slack.NewSectionBlock(
|
slack.NewSectionBlock(
|
||||||
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf(":chart_with_upwards_trend: Counting stats for *%s %d*:", month.String(), year), false, false),
|
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf(":chart_with_upwards_trend: Counting stats for *%s %d*:", month.String(), year), false, false),
|
||||||
|
|
Loading…
Reference in a new issue