From f4fbfcee0c2c38112721f6a84255fa5d2445501f Mon Sep 17 00:00:00 2001 From: jonas747 Date: Fri, 3 Aug 2018 18:08:41 +0200 Subject: [PATCH] serverstats: Don't use redis to store online members a second time --- bot/botrest/client.go | 5 +++ bot/botrest/server.go | 23 +++++++++++++ serverstats/plugin_bot.go | 66 -------------------------------------- serverstats/serverstats.go | 13 ++++++-- 4 files changed, 38 insertions(+), 69 deletions(-) diff --git a/bot/botrest/client.go b/bot/botrest/client.go index 6b1bfa4f2a..f41be43a42 100644 --- a/bot/botrest/client.go +++ b/bot/botrest/client.go @@ -80,6 +80,11 @@ func GetBotMember(guildID int64) (m *discordgo.Member, err error) { return } +func GetOnlineCount(guildID int64) (c int64, err error) { + err = get(discordgo.StrID(guildID)+"/onlinecount", &c) + return +} + func GetMembers(guildID int64, members ...int64) (m []*discordgo.Member, err error) { stringed := make([]string, 0, len(members)) for _, v := range members { diff --git a/bot/botrest/server.go b/bot/botrest/server.go index ee3b2b5b18..ddc40e7eb0 100644 --- a/bot/botrest/server.go +++ b/bot/botrest/server.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "github.com/jonas747/discordgo" + "github.com/jonas747/dutil/dstate" "github.com/jonas747/yagpdb/bot" "github.com/jonas747/yagpdb/common" "github.com/pkg/errors" @@ -45,6 +46,7 @@ func (p *Plugin) BotInit() { muxer.HandleFunc(pat.Get("/:guild/guild"), HandleGuild) muxer.HandleFunc(pat.Get("/:guild/botmember"), HandleBotMember) muxer.HandleFunc(pat.Get("/:guild/members"), HandleGetMembers) + muxer.HandleFunc(pat.Get("/:guild/onlinecount"), HandleGetOnlineCount) muxer.HandleFunc(pat.Get("/:guild/channelperms/:channel"), HandleChannelPermissions) muxer.HandleFunc(pat.Get("/gw_status"), HandleGWStatus) muxer.HandleFunc(pat.Post("/shard/:shard/reconnect"), HandleReconnectShard) @@ -188,6 +190,27 @@ func HandleGetMembers(w http.ResponseWriter, r *http.Request) { ServeJson(w, r, members) } +func HandleGetOnlineCount(w http.ResponseWriter, r *http.Request) { + gId, _ := strconv.ParseInt(pat.Param(r, "guild"), 10, 64) + + guild := bot.State.Guild(true, gId) + if guild == nil { + ServerError(w, r, errors.New("Guild not found")) + return + } + + count := 0 + guild.RLock() + for _, ms := range guild.Members { + if ms.PresenceSet && ms.PresenceStatus != dstate.StatusNotSet && ms.PresenceStatus != dstate.StatusOffline { + count++ + } + } + guild.RUnlock() + + ServeJson(w, r, count) +} + func HandleChannelPermissions(w http.ResponseWriter, r *http.Request) { gId, _ := strconv.ParseInt(pat.Param(r, "guild"), 10, 64) cId, _ := strconv.ParseInt(pat.Param(r, "channel"), 10, 64) diff --git a/serverstats/plugin_bot.go b/serverstats/plugin_bot.go index da90617006..a8537b454b 100644 --- a/serverstats/plugin_bot.go +++ b/serverstats/plugin_bot.go @@ -29,9 +29,7 @@ func (p *Plugin) BotInit() { eventsystem.AddHandler(HandleMemberRemove, eventsystem.EventGuildMemberRemove) eventsystem.AddHandler(HandleMessageCreate, eventsystem.EventMessageCreate) - eventsystem.AddHandler(HandlePresenceUpdate, eventsystem.EventPresenceUpdate) eventsystem.AddHandler(HandleGuildCreate, eventsystem.EventGuildCreate) - eventsystem.AddHandler(HandleReady, eventsystem.EventReady) go UpdateStatsLoop() } @@ -85,21 +83,6 @@ func (p *Plugin) AddCommands() { } -func HandleReady(evt *eventsystem.EventData) { - r := evt.Ready() - - for _, guild := range r.Guilds { - if guild.Unavailable { - continue - } - - err := ApplyPresences(guild.ID, guild.Presences) - if err != nil { - log.WithError(err).Error("Failed applying presences") - } - } -} - func HandleGuildCreate(evt *eventsystem.EventData) { g := evt.GuildCreate() @@ -107,11 +90,6 @@ func HandleGuildCreate(evt *eventsystem.EventData) { if err != nil { log.WithError(err).Error("Failed Settings member count") } - - err = ApplyPresences(g.ID, g.Presences) - if err != nil { - log.WithError(err).Error("Failed applying presences") - } } func HandleMemberAdd(evt *eventsystem.EventData) { @@ -128,28 +106,6 @@ func HandleMemberAdd(evt *eventsystem.EventData) { } } -func HandlePresenceUpdate(evt *eventsystem.EventData) { - p := evt.PresenceUpdate() - - if p.Status == "" { // Not a status update - return - } - - key := "guild_stats_online:" + discordgo.StrID(p.GuildID) - uID := discordgo.StrID(p.User.ID) - - var err error - if p.Status == "offline" { - err = common.RedisPool.Do(radix.Cmd(nil, "SREM", key, uID)) - } else { - err = common.RedisPool.Do(radix.Cmd(nil, "SADD", key, uID)) - } - - if err != nil { - log.WithError(err).Error("Failed updating a presence") - } -} - func HandleMemberRemove(evt *eventsystem.EventData) { g := evt.GuildMemberRemove() @@ -196,25 +152,3 @@ func HandleMessageCreate(evt *eventsystem.EventData) { MarkGuildAsToBeChecked(channel.Guild.ID) } - -func ApplyPresences(guildID int64, presences []*discordgo.Presence) error { - key := "guild_stats_online:" + discordgo.StrID(guildID) - - err := common.RedisPool.Do(radix.WithConn(key, func(c radix.Conn) error { - c.Do(radix.Cmd(nil, "DEL", key)) - - values := make([]string, 1, len(presences)+1) - values[0] = key - for _, p := range presences { - if p.Status == "offline" { - continue - } - - values = append(values, discordgo.StrID(p.User.ID)) - } - - return c.Do(radix.Cmd(nil, "SADD", values...)) - })) - - return err -} diff --git a/serverstats/serverstats.go b/serverstats/serverstats.go index 7db1567157..842bb82b0f 100644 --- a/serverstats/serverstats.go +++ b/serverstats/serverstats.go @@ -3,6 +3,7 @@ package serverstats import ( "context" "github.com/jonas747/discordgo" + "github.com/jonas747/yagpdb/bot/botrest" "github.com/jonas747/yagpdb/common" "github.com/jonas747/yagpdb/common/configstore" "github.com/jonas747/yagpdb/serverstats/models" @@ -240,14 +241,13 @@ func RetrieveRedisStats(guildID int64) (*FullStats, error) { var messageStatsRaw []string var joined int var left int - var online int + var online int64 var members int pipeCmd := radix.Pipeline( radix.Cmd(&messageStatsRaw, "ZRANGEBYSCORE", "guild_stats_msg_channel_day:"+strGID, unixYesterday, "+inf"), radix.Cmd(&joined, "ZCOUNT", "guild_stats_members_joined_day:"+strGID, unixYesterday, "+inf"), radix.Cmd(&left, "ZCOUNT", "guild_stats_members_left_day:"+strGID, unixYesterday, "+inf"), - radix.Cmd(&online, "SCARD", "guild_stats_online:"+strGID), radix.Cmd(&members, "GET", "guild_stats_num_members:"+strGID), ) @@ -256,6 +256,13 @@ func RetrieveRedisStats(guildID int64) (*FullStats, error) { return nil, err } + online, err = botrest.GetOnlineCount(guildID) + if err != nil { + if botrest.BotIsRunning() { + log.WithError(err).Error("Failed fetching online count") + } + } + channelResult, err := GetChannelMessageStats(messageStatsRaw, guildID) if err != nil { return nil, err @@ -265,7 +272,7 @@ func RetrieveRedisStats(guildID int64) (*FullStats, error) { ChannelsHour: channelResult, JoinedDay: joined, LeftDay: left, - Online: online, + Online: int(online), TotalMembers: members, }