2022-06-26 18:19:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-12-06 10:45:38 +00:00
|
|
|
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
"golang.org/x/text/message"
|
2022-06-26 18:19:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func task_main() {
|
|
|
|
log.Println("running per-minute scheduled tasks")
|
|
|
|
|
|
|
|
update_db_meta()
|
|
|
|
update_stats()
|
2022-12-06 10:45:38 +00:00
|
|
|
update_bot_status()
|
2022-06-26 18:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var table_stat_keys = []string{"system", "member", "group", "switch", "message"}
|
|
|
|
|
|
|
|
func plural(key string) string {
|
|
|
|
if key[len(key)-1] == 'h' {
|
|
|
|
return key + "es"
|
|
|
|
}
|
|
|
|
return key + "s"
|
|
|
|
}
|
|
|
|
|
|
|
|
func update_db_meta() {
|
|
|
|
log.Println("updating database stats")
|
|
|
|
for _, key := range table_stat_keys {
|
|
|
|
if key == "message" {
|
|
|
|
// updating message count from data db takes way too long, so we do it on a separate timer (every 10 minutes)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
q := fmt.Sprintf("update info set %s_count = (select count(*) from %s)", key, plural(key))
|
|
|
|
log.Println("data db query:", q)
|
|
|
|
run_simple_pg_query(data_db, q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func update_db_message_meta() {
|
2022-11-23 09:17:19 +00:00
|
|
|
count := get_message_count()
|
2022-06-26 18:19:46 +00:00
|
|
|
|
2022-11-23 09:17:19 +00:00
|
|
|
_, err := data_db.Exec(context.Background(), "update info set message_count = $1", count)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-06-26 18:19:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 10:45:38 +00:00
|
|
|
func get_discord_counts() (int, int) {
|
2022-06-26 18:19:46 +00:00
|
|
|
redisStats := run_redis_query()
|
|
|
|
|
|
|
|
guild_count := 0
|
|
|
|
channel_count := 0
|
|
|
|
|
|
|
|
for _, v := range redisStats {
|
|
|
|
log.Println(v.GuildCount, v.ChannelCount)
|
|
|
|
guild_count += v.GuildCount
|
|
|
|
channel_count += v.ChannelCount
|
|
|
|
}
|
|
|
|
|
2022-12-06 10:45:38 +00:00
|
|
|
return guild_count, channel_count
|
|
|
|
}
|
|
|
|
|
|
|
|
func update_stats() {
|
|
|
|
guild_count, channel_count := get_discord_counts()
|
|
|
|
|
2022-06-26 18:19:46 +00:00
|
|
|
do_stats_insert("guilds", guild_count)
|
|
|
|
do_stats_insert("channels", channel_count)
|
|
|
|
|
|
|
|
data_stats := run_data_stats_query()
|
|
|
|
for _, key := range table_stat_keys {
|
|
|
|
val := data_stats[key+"_count"].(int32)
|
|
|
|
do_stats_insert(plural(key), int(val))
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 10:45:38 +00:00
|
|
|
|
|
|
|
func update_bot_status() {
|
|
|
|
if !set_guild_count {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guild_count, _ := get_discord_counts()
|
|
|
|
p := message.NewPrinter(language.English)
|
2022-12-06 11:06:32 +00:00
|
|
|
s := p.Sprintf("%d", guild_count)
|
2022-12-06 10:45:38 +00:00
|
|
|
|
2022-12-06 11:06:32 +00:00
|
|
|
cmd := rdb.Set(context.Background(), "pluralkit:botstatus", "in "+s+" servers", 0)
|
2022-12-06 10:45:38 +00:00
|
|
|
if err := cmd.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|