moth/cmd/mothd/redis_state.go

280 lines
6.5 KiB
Go
Raw Normal View History

2021-10-28 16:36:07 -06:00
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/go-redis/redis/v8"
"github.com/dirtbags/moth/pkg/award"
)
const (
REDIS_KEY_PREFIX = "moth"
REDIS_KEY_MESSAGE = "message"
REDIS_KEY_TEAMS = "teams"
REDIS_KEY_TEAM_IDS = "team_ids"
REDIS_KEY_POINT_LOG = "points"
REDIS_KEY_EVENT_LOG = "events"
REDIS_KEY_ENABLED = "enabled"
2021-10-28 16:36:07 -06:00
)
type RedisState struct {
ctx context.Context
redis_client *redis.Client
// Enabled tracks whether the current State system is processing updates
Enabled bool
instance_id string
2021-10-28 16:36:07 -06:00
eventStream chan map[string]interface{}
2021-10-28 16:36:07 -06:00
}
type RedisAward struct {
when int64
teamID string
category string
points int
2021-10-28 16:36:07 -06:00
}
// NewRedisState returns a new State struct backed by the given Fs
func NewRedisState(redis_addr string, redis_db int, instance_id string) *RedisState {
2021-10-28 16:36:07 -06:00
rdb := redis.NewClient(&redis.Options{
Addr: redis_addr,
Password: "", // no password set
DB: redis_db, // use default DB
})
s := &RedisState{
ctx: context.Background(),
redis_client: rdb,
Enabled: true,
instance_id: instance_id,
eventStream: make(chan map[string]interface{}, 80),
2021-10-28 16:36:07 -06:00
}
//s.initialize()
2021-10-28 16:36:07 -06:00
return s
}
func (s *RedisState) formatRedisKey(key string) string {
return fmt.Sprintf("%s:%s:%s", REDIS_KEY_PREFIX, s.instance_id, key)
}
func (s *RedisState) initialize() {
s.SetMessagesOverride("", false)
}
// ************ Message-related operations ****************
2021-10-28 16:36:07 -06:00
// Messages retrieves the current messages.
func (s *RedisState) Messages() string {
val, err := s.redis_client.Get(s.ctx, s.formatRedisKey(REDIS_KEY_MESSAGE)).Result()
2021-10-28 16:36:07 -06:00
if err != nil {
return ""
}
return val
}
func (s *RedisState) SetMessages(message string) error {
return s.SetMessagesOverride(message, true)
}
func (s *RedisState) SetMessagesOverride(message string, override bool) error {
if override {
return s.redis_client.Set(s.ctx, s.formatRedisKey(REDIS_KEY_MESSAGE), message, 0).Err()
} else {
return s.redis_client.SetNX(s.ctx, s.formatRedisKey(REDIS_KEY_MESSAGE), message, 0).Err()
}
}
// ******************** Team operations ******************
func (s *RedisState) TeamIDs() ([]string, error) {
return s.redis_client.SMembers(s.ctx, s.formatRedisKey(REDIS_KEY_TEAM_IDS)).Result()
}
func (s *RedisState) AddTeamID(teamID string) error {
return s.redis_client.SAdd(s.ctx, s.formatRedisKey(REDIS_KEY_TEAM_IDS), teamID).Err()
}
func (s *RedisState) TeamNames() (map[string]string, error) {
return s.redis_client.HGetAll(s.ctx, s.formatRedisKey(REDIS_KEY_TEAMS)).Result()
}
2021-10-28 16:36:07 -06:00
// TeamName returns team name given a team ID.
func (s *RedisState) TeamName(teamID string) (string, error) {
team_name, err := s.redis_client.HGet(s.ctx, s.formatRedisKey(REDIS_KEY_TEAMS), teamID).Result()
2021-10-28 16:36:07 -06:00
if err != nil {
return "", fmt.Errorf("unregistered team ID: %s", teamID)
}
return team_name, nil
}
// SetTeamName writes out team name.
// This can only be done once per team.
func (s *RedisState) SetTeamName(teamID, teamName string) error {
valid_id, err := s.redis_client.SIsMember(s.ctx, s.formatRedisKey(REDIS_KEY_TEAM_IDS), teamID).Result()
2021-10-28 16:36:07 -06:00
if err != nil {
return fmt.Errorf("Unexpected error while validating team ID: %s", teamID)
} else if (!valid_id) {
return fmt.Errorf("team ID: (%s) not found in list of valid team IDs", teamID)
}
2021-12-10 17:13:34 -07:00
exists, err := s.redis_client.HExists(s.ctx, s.formatRedisKey(REDIS_KEY_TEAMS), teamID).Result()
if exists {
return nil
}
success, err := s.redis_client.HSetNX(s.ctx, s.formatRedisKey(REDIS_KEY_TEAMS), teamID, teamName).Result()
2021-10-28 16:36:07 -06:00
if err != nil {
2021-12-10 17:13:34 -07:00
fmt.Println(err)
2021-10-28 16:36:07 -06:00
return fmt.Errorf("Unexpected error while setting team ID: %s and team Name: %s", teamID, teamName)
}
if (success) {
return nil
}
return fmt.Errorf("Team ID: %s is already set", teamID)
}
2021-10-28 16:36:07 -06:00
// PointsLog retrieves the current points log.
func (s *RedisState) PointsLog() award.List {
redis_args := &redis.ZRangeBy{
2021-12-10 17:13:34 -07:00
Min: "-inf",
Max: "+inf",
2021-10-28 16:36:07 -06:00
}
scores, err := s.redis_client.ZRangeByScoreWithScores(s.ctx, s.formatRedisKey(REDIS_KEY_POINT_LOG), redis_args).Result()
2021-10-28 16:36:07 -06:00
if err != nil {
2021-12-10 17:13:34 -07:00
fmt.Println("Encountered an error processing points")
2021-10-28 16:36:07 -06:00
return make(award.List, 0)
}
point_log := make(award.List, len(scores))
for _, item := range scores {
point_entry := award.T{}
point_string := strings.TrimSpace(item.Member.(string))
point_entry.When = int64(item.Score)
n, err := fmt.Sscanf(point_string, "%s %s %d", &point_entry.TeamID, &point_entry.Category, &point_entry.Points)
if err != nil {
// Do nothing
} else if n != 3 {
// Wrong number of fields, do nothing
} else {
point_log = append(point_log, point_entry)
}
}
return point_log
}
func (s *RedisState) AwardPoints(teamID, category string, points int) error {
redis_args := redis.ZAddArgs {
LT: true,
}
awardTime := time.Now().Unix()
2021-12-10 15:15:45 -07:00
point_string := fmt.Sprintf("%s %s %d", teamID, category, points)
2021-10-28 16:36:07 -06:00
new_member := redis.Z{
Score: float64(awardTime),
Member: point_string,
}
redis_args.Members = append(redis_args.Members, new_member)
_, err := s.redis_client.ZAddArgs(s.ctx, s.formatRedisKey(REDIS_KEY_POINT_LOG), redis_args).Result()
2021-10-28 16:36:07 -06:00
if err != nil {
return err
}
return nil
}
// LogEvent writes to the event log
func (s *RedisState) LogEvent(event, participantID, teamID, cat string, points int, extra ...string) {
/*
2021-10-28 16:36:07 -06:00
new_event := RedisEventEntry {
When: time.Now().Unix(),
Event: event,
ParticipantID: participantID,
TeamID: teamID,
Category: cat,
Points: points,
Extra: extra,
}
message, _ := new_event.MarshalJSON()
*/
2021-10-28 16:36:07 -06:00
/*
redis_args := redis.XAddArgs {
Stream: s.redisKeyEventLog(),
Values: map[string]interface{}{
"When": time.Now().Unix(),
"Event": event,
"ParticipantID": participantID,
"TeamID": teamID,
"Category": cat,
"Points": points,
"Extra": extra,
2021-10-28 16:36:07 -06:00
},
}
2021-10-28 16:36:07 -06:00
s.redis_client.XAdd(s.ctx, &redis_args)
*/
2021-10-28 16:36:07 -06:00
s.eventStream <-
map[string]interface{}{
"When": time.Now().Unix(),
"Event": event,
"ParticipantID": participantID,
"TeamID": teamID,
"Category": cat,
"Points": points,
"Extra": extra,
}
}
func (s *RedisState) writeEvent(event map[string]interface{}) {
redis_args := redis.XAddArgs {
Stream: s.formatRedisKey(REDIS_KEY_EVENT_LOG),
Values: event,
}
s.redis_client.XAdd(s.ctx, &redis_args)
2021-10-28 16:36:07 -06:00
}
func (s *RedisState) Maintain(updateInterval time.Duration) {
ticker := time.NewTicker(updateInterval)
2021-10-28 16:36:07 -06:00
for {
select {
case msg := <-s.eventStream:
s.writeEvent(msg)
2021-10-28 16:36:07 -06:00
case <-ticker.C:
/* There are no maintanance tasks for this provider, currently. Maybe some state-saving mechanism, at some point? */
2021-10-28 16:36:07 -06:00
}
}
}