Skip to content

Commit

Permalink
serverstats: Don't use redis to store online members a second time
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Aug 3, 2018
1 parent 1309878 commit f4fbfce
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 69 deletions.
5 changes: 5 additions & 0 deletions bot/botrest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions bot/botrest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
66 changes: 0 additions & 66 deletions serverstats/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -85,33 +83,13 @@ 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()

err := common.RedisPool.Do(radix.FlatCmd(nil, "SET", "guild_stats_num_members:"+discordgo.StrID(g.ID), g.MemberCount))
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) {
Expand All @@ -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()

Expand Down Expand Up @@ -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
}
13 changes: 10 additions & 3 deletions serverstats/serverstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
)

Expand All @@ -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
Expand All @@ -265,7 +272,7 @@ func RetrieveRedisStats(guildID int64) (*FullStats, error) {
ChannelsHour: channelResult,
JoinedDay: joined,
LeftDay: left,
Online: online,
Online: int(online),
TotalMembers: members,
}

Expand Down

0 comments on commit f4fbfce

Please sign in to comment.