Skip to content

Commit

Permalink
[stats] rewrites voice session
Browse files Browse the repository at this point in the history
stores voice sessions in elastic searches
uses voice Voice State Update event instead of polling all channels
  • Loading branch information
Seklfreak committed Dec 25, 2017
1 parent 5c71c99 commit dc87017
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 198 deletions.
2 changes: 1 addition & 1 deletion _assets/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@
"stats": {
"voicestats-toplist-no-entries": "No sessions saved yet. Sessions get saved after someone leaves a voice chat.",
"voicestats-toplist-embed-title": "Voice Channel Duration Leaderboard for this server",
"voicestats-embed-footer": "Total durations exclude the currently active sessions. Stats are refreshed every 30 seconds.",
"voicestats-embed-footer": "Total durations exclude the currently active sessions.",
"no-emotes": "No custom emotes on this server yet. <a:ablobshocked:394026914076950539>",
"reaction-embed-title": "@%s: Custom Emotes on %s",
"reaction-embed-footer": "There are %d custom emotes on this server.",
Expand Down
34 changes: 34 additions & 0 deletions helpers/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,40 @@ func ElasticAddVanityInviteClick(vanityInvite models.VanityInviteEntry, referer
return err
}

func ElasticAddVoiceSession(guildID, channelID, userID string, joinTime, leaveTime time.Time) (err error) {
if !cache.HasElastic() {
return errors.New("no elastic client")
}

if guildID == "" || channelID == "" || userID == "" ||
joinTime.IsZero() || leaveTime.IsZero() || leaveTime.Before(joinTime) {
return errors.New("invalid voice session entry submitted")
}

if IsBlacklistedGuild(guildID) || IsLimitedGuild(guildID) || IsBlacklisted(userID) {
return nil
}

duration := leaveTime.Sub(joinTime)

elasticVoiceSessionData := models.ElasticVoiceSession{
CreatedAt: time.Now(),
GuildID: guildID,
ChannelID: channelID,
UserID: userID,
JoinTime: joinTime,
LeaveTime: leaveTime,
DurationSeconds: int64(duration.Seconds()),
}

_, err = cache.GetElastic().Index().
Index(models.ElasticIndexVoiceSessions).
Type("doc").
BodyJson(elasticVoiceSessionData).
Do(context.Background())
return err
}

func GetMinTimeForInterval(interval string, count int) (minTime time.Time) {
switch interval {
case "second":
Expand Down
70 changes: 70 additions & 0 deletions migrations/52_create_elastic_index_voice_sessions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package migrations

import (
"context"

"github.com/Seklfreak/Robyul2/cache"
)

func m52_create_elastic_index_voice_sessions() {
if !cache.HasElastic() {
return
}

elastic := cache.GetElastic()
exists, err := elastic.IndexExists("robyul-voice_session").Do(context.Background())
if err != nil {
panic(err)
}
if exists {
return
}

messageMapping := map[string]interface{}{
"mappings": map[string]interface{}{
"doc": map[string]interface{}{
"properties": map[string]interface{}{
"CreatedAt": map[string]interface{}{
"type": "date",
},
"GuildID": map[string]interface{}{
"type": "text",
},
"ChannelID": map[string]interface{}{
"type": "text",
"fields": map[string]interface{}{
"keyword": map[string]interface{}{
"type": "keyword",
},
},
},
"UserID": map[string]interface{}{
"type": "text",
"fields": map[string]interface{}{
"keyword": map[string]interface{}{
"type": "keyword",
},
},
},
"JoinTime": map[string]interface{}{
"type": "date",
},
"LeaveTime": map[string]interface{}{
"type": "date",
},
"DurationSeconds": map[string]interface{}{
"type": "long",
},
},
},
},
}

index, err := elastic.CreateIndex("robyul-voice_session").BodyJson(messageMapping).Do(context.Background())
if err != nil {
panic(err)
}
if !index.Acknowledged {
cache.GetLogger().WithField("module", "migrations").Error("ElasticSearch index not acknowledged")
}
}
1 change: 1 addition & 0 deletions migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var migrations = []helpers.Callback{
m49_create_elastic_index_presence_updates,
m50_create_elastic_vanity_invite_clicks,
m51_reindex_elasticv5_to_v6,
m52_create_elastic_index_voice_sessions,
}

// Run executes all registered migrations
Expand Down
11 changes: 11 additions & 0 deletions models/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
ElasticIndexReactions = "robyul-reactions"
ElasticIndexPresenceUpdates = "robyul-presence_updates"
ElasticIndexVanityInviteClicks = "robyul-vanity_invite_clicks"
ElasticIndexVoiceSessions = "robyul-voice_session"
)

type ElasticLegacyMessage struct {
Expand Down Expand Up @@ -76,3 +77,13 @@ type ElasticVanityInviteClick struct {
GuildID string
Referer string
}

type ElasticVoiceSession struct {
CreatedAt time.Time
GuildID string
ChannelID string
UserID string
JoinTime time.Time
LeaveTime time.Time
DurationSeconds int64
}
Loading

0 comments on commit dc87017

Please sign in to comment.