2019-09-02 18:13:37 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-02 19:47:24 -06:00
|
|
|
"bufio"
|
2020-12-02 17:57:10 -07:00
|
|
|
"encoding/csv"
|
2020-10-13 18:33:12 -06:00
|
|
|
"errors"
|
2019-09-02 18:13:37 -06:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-09-02 19:47:24 -06:00
|
|
|
"math/rand"
|
2019-09-02 18:13:37 -06:00
|
|
|
"os"
|
2019-12-01 18:58:09 -07:00
|
|
|
"path/filepath"
|
2020-12-02 17:57:10 -07:00
|
|
|
"strconv"
|
2019-09-02 19:47:24 -06:00
|
|
|
"strings"
|
2021-10-26 12:48:23 -06:00
|
|
|
"sync"
|
2019-09-02 18:13:37 -06:00
|
|
|
"time"
|
2020-08-14 20:26:04 -06:00
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
"github.com/dirtbags/moth/pkg/award"
|
2020-08-14 20:26:04 -06:00
|
|
|
"github.com/spf13/afero"
|
2019-09-02 18:13:37 -06:00
|
|
|
)
|
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
// DistinguishableChars are visually unambiguous glyphs.
|
|
|
|
// People with mediocre handwriting could write these down unambiguously,
|
|
|
|
// and they can be entered without holding down shift.
|
2020-09-11 20:16:58 -06:00
|
|
|
const DistinguishableChars = "34678abcdefhikmnpqrtwxy="
|
|
|
|
|
|
|
|
// RFC3339Space is a time layout which replaces 'T' with a space.
|
|
|
|
// This is also a valid RFC3339 format.
|
|
|
|
const RFC3339Space = "2006-01-02 15:04:05Z07:00"
|
2019-09-02 18:13:37 -06:00
|
|
|
|
2020-10-13 18:33:12 -06:00
|
|
|
// ErrAlreadyRegistered means a team cannot be registered because it was registered previously.
|
2021-10-13 18:25:27 -06:00
|
|
|
var ErrAlreadyRegistered = errors.New("team ID has already been registered")
|
2022-05-18 08:44:01 -06:00
|
|
|
var NoMatchingPointEntry = errors.New("Unable to find matching point entry")
|
2020-10-13 18:33:12 -06:00
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
// State defines the current state of a MOTH instance.
|
2019-09-02 18:13:37 -06:00
|
|
|
// We use the filesystem for synchronization between threads.
|
|
|
|
// The only thing State methods need to know is the path to the state directory.
|
|
|
|
type State struct {
|
2020-02-29 16:51:32 -07:00
|
|
|
afero.Fs
|
2020-08-18 17:04:23 -06:00
|
|
|
|
|
|
|
// Enabled tracks whether the current State system is processing updates
|
2020-02-29 22:37:22 -07:00
|
|
|
Enabled bool
|
2020-08-18 17:04:23 -06:00
|
|
|
|
2020-12-02 17:57:10 -07:00
|
|
|
refreshNow chan bool
|
|
|
|
eventStream chan []string
|
|
|
|
eventWriter *csv.Writer
|
|
|
|
eventWriterFile afero.File
|
2021-10-26 12:48:23 -06:00
|
|
|
|
|
|
|
// Caches, so we're not hammering NFS with metadata operations
|
|
|
|
teamNames map[string]string
|
|
|
|
pointsLog award.List
|
|
|
|
messages string
|
2021-12-30 18:00:14 -07:00
|
|
|
teamIDLock sync.RWMutex
|
|
|
|
teamIDFileLock sync.RWMutex
|
|
|
|
teamNameLock sync.RWMutex
|
|
|
|
teamNameFileLock sync.RWMutex
|
|
|
|
pointsLock sync.RWMutex
|
|
|
|
pointsLogFileLock sync.RWMutex // Sometimes, we need to fiddle with the file, while leaving the internal state alone
|
|
|
|
messageFileLock sync.RWMutex
|
2022-05-12 18:03:26 -06:00
|
|
|
teamNamesLastChange time.Time
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
// NewState returns a new State struct backed by the given Fs
|
2019-12-01 18:58:09 -07:00
|
|
|
func NewState(fs afero.Fs) *State {
|
2020-08-18 17:04:23 -06:00
|
|
|
s := &State{
|
|
|
|
Fs: fs,
|
|
|
|
Enabled: true,
|
|
|
|
refreshNow: make(chan bool, 5),
|
2020-12-02 17:57:10 -07:00
|
|
|
eventStream: make(chan []string, 80),
|
2021-10-26 12:48:23 -06:00
|
|
|
|
|
|
|
teamNames: make(map[string]string),
|
2020-08-18 17:04:23 -06:00
|
|
|
}
|
|
|
|
if err := s.reopenEventLog(); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2020-08-18 17:04:23 -06:00
|
|
|
return s
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2020-08-18 17:04:23 -06:00
|
|
|
// updateEnabled checks a few things to see if this state directory is "enabled".
|
|
|
|
func (s *State) updateEnabled() {
|
2019-11-24 15:58:43 -07:00
|
|
|
nextEnabled := true
|
2020-09-11 20:16:58 -06:00
|
|
|
why := "`state/enabled` present, `state/hours.txt` missing"
|
2020-08-21 17:02:38 -06:00
|
|
|
|
2020-09-11 20:16:58 -06:00
|
|
|
if untilFile, err := s.Open("hours.txt"); err == nil {
|
2020-08-21 17:02:38 -06:00
|
|
|
defer untilFile.Close()
|
2020-09-11 20:16:58 -06:00
|
|
|
why = "`state/hours.txt` present"
|
2020-08-21 17:02:38 -06:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(untilFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if len(line) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
thisEnabled := true
|
|
|
|
switch line[0] {
|
|
|
|
case '+':
|
|
|
|
thisEnabled = true
|
|
|
|
line = line[1:]
|
|
|
|
case '-':
|
|
|
|
thisEnabled = false
|
|
|
|
line = line[1:]
|
|
|
|
case '#':
|
|
|
|
continue
|
|
|
|
default:
|
2020-09-11 20:16:58 -06:00
|
|
|
log.Println("Misformatted line in hours.txt file")
|
2020-08-21 17:02:38 -06:00
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
until, err := time.Parse(time.RFC3339, line)
|
2020-09-11 20:16:58 -06:00
|
|
|
if err != nil {
|
|
|
|
until, err = time.Parse(RFC3339Space, line)
|
|
|
|
}
|
2020-08-21 17:02:38 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Suspended: Unparseable until date:", line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if until.Before(time.Now()) {
|
|
|
|
nextEnabled = thisEnabled
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2020-08-21 17:02:38 -06:00
|
|
|
if _, err := s.Stat("enabled"); os.IsNotExist(err) {
|
|
|
|
nextEnabled = false
|
|
|
|
why = "`state/enabled` missing"
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2020-08-21 17:02:38 -06:00
|
|
|
|
2019-11-24 15:58:43 -07:00
|
|
|
if nextEnabled != s.Enabled {
|
|
|
|
s.Enabled = nextEnabled
|
2020-08-21 17:02:38 -06:00
|
|
|
log.Printf("Setting enabled=%v: %s", s.Enabled, why)
|
2020-10-14 18:20:49 -06:00
|
|
|
if s.Enabled {
|
|
|
|
s.LogEvent("enabled", "", "", "", 0, why)
|
|
|
|
} else {
|
|
|
|
s.LogEvent("disabled", "", "", "", 0, why)
|
|
|
|
}
|
2019-11-24 15:58:43 -07:00
|
|
|
}
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
/* ****************** Team ID functions ****************** */
|
|
|
|
|
|
|
|
func (s *State) TeamIDs() ([]string, error) {
|
|
|
|
var teamIDs []string
|
|
|
|
|
|
|
|
s.teamIDFileLock.RLock()
|
|
|
|
defer s.teamIDFileLock.RUnlock()
|
|
|
|
|
|
|
|
idsFile, err := s.Open("teamids.txt")
|
|
|
|
if err != nil {
|
|
|
|
return teamIDs, fmt.Errorf("team IDs file does not exist")
|
|
|
|
}
|
|
|
|
defer idsFile.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(idsFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
teamIDs = append(teamIDs, scanner.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
return teamIDs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) writeTeamIDs(teamIDs []string) error {
|
|
|
|
s.teamIDFileLock.Lock()
|
|
|
|
defer s.teamIDFileLock.Unlock()
|
|
|
|
|
2022-05-18 11:19:53 -06:00
|
|
|
if f, err := s.OpenFile("teamids.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err == nil {
|
2021-12-30 18:00:14 -07:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
for _, teamID := range teamIDs {
|
|
|
|
fmt.Fprintln(f, string(teamID))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetTeamIDs(teamIDs []string) error {
|
|
|
|
s.teamIDLock.Lock()
|
|
|
|
defer s.teamIDLock.Unlock()
|
|
|
|
|
|
|
|
return s.writeTeamIDs(teamIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) AddTeamID(newTeamID string) error {
|
|
|
|
s.teamIDLock.Lock()
|
|
|
|
defer s.teamIDLock.Unlock()
|
|
|
|
|
|
|
|
teamIDs, err := s.TeamIDs()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, teamID := range teamIDs {
|
|
|
|
if newTeamID == teamID {
|
|
|
|
return fmt.Errorf("Team ID already exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
teamIDs = append(teamIDs, newTeamID)
|
|
|
|
|
|
|
|
return s.writeTeamIDs(teamIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) RemoveTeamID(removeTeamID string) error {
|
|
|
|
s.teamIDLock.Lock()
|
|
|
|
defer s.teamIDLock.Unlock()
|
|
|
|
|
2022-05-18 11:19:53 -06:00
|
|
|
var newTeamIDs []string
|
|
|
|
|
|
|
|
oldTeamIDs, err := s.TeamIDs()
|
2021-12-30 18:00:14 -07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-18 11:19:53 -06:00
|
|
|
for _, teamID := range oldTeamIDs {
|
2021-12-30 18:00:14 -07:00
|
|
|
if removeTeamID != teamID {
|
2022-05-18 11:19:53 -06:00
|
|
|
newTeamIDs = append(newTeamIDs, teamID)
|
2021-12-30 18:00:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 11:19:53 -06:00
|
|
|
return s.writeTeamIDs(newTeamIDs)
|
2021-12-30 18:00:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) TeamIDExists(teamID string) (bool, error) {
|
|
|
|
s.teamIDLock.RLock()
|
|
|
|
defer s.teamIDLock.RUnlock()
|
|
|
|
|
|
|
|
teamIDs, err := s.TeamIDs()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, candidateTeamID := range teamIDs {
|
|
|
|
if teamID == candidateTeamID {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ********************* Team Name functions ********* */
|
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
// TeamName returns team name given a team ID.
|
2020-08-14 20:26:04 -06:00
|
|
|
func (s *State) TeamName(teamID string) (string, error) {
|
2021-12-30 18:00:14 -07:00
|
|
|
s.teamNameLock.RLock()
|
|
|
|
defer s.teamNameLock.RUnlock()
|
|
|
|
|
2021-10-26 12:48:23 -06:00
|
|
|
name, ok := s.teamNames[teamID]
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2021-10-26 12:48:23 -06:00
|
|
|
if !ok {
|
2021-10-13 18:25:27 -06:00
|
|
|
return "", fmt.Errorf("unregistered team ID: %s", teamID)
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2021-10-26 12:48:23 -06:00
|
|
|
return name, nil
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
func (s *State) TeamNames() map[string]string {
|
2022-05-17 13:41:25 -06:00
|
|
|
s.teamNameLock.RLock()
|
|
|
|
defer s.teamNameLock.RUnlock()
|
|
|
|
return s.teamNames
|
2021-12-30 18:00:14 -07:00
|
|
|
}
|
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
// SetTeamName writes out team name.
|
2020-10-13 18:33:12 -06:00
|
|
|
// This can only be done once per team.
|
2020-08-14 20:26:04 -06:00
|
|
|
func (s *State) SetTeamName(teamID, teamName string) error {
|
2022-05-17 13:41:25 -06:00
|
|
|
s.teamNameFileLock.Lock()
|
|
|
|
defer s.teamNameFileLock.Unlock()
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2022-05-17 13:41:25 -06:00
|
|
|
teamIDs, err := s.TeamIDs()
|
2020-08-17 17:43:57 -06:00
|
|
|
if err != nil {
|
2021-12-30 18:00:14 -07:00
|
|
|
return err
|
2020-08-17 17:43:57 -06:00
|
|
|
}
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
found := false
|
2021-12-30 18:00:14 -07:00
|
|
|
for _, validTeamID := range teamIDs {
|
|
|
|
if validTeamID == teamID {
|
2020-08-17 17:43:57 -06:00
|
|
|
found = true
|
|
|
|
break
|
2019-12-01 20:53:13 -07:00
|
|
|
}
|
2019-12-01 20:47:46 -07:00
|
|
|
}
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2020-08-17 17:43:57 -06:00
|
|
|
if !found {
|
2021-10-13 18:25:27 -06:00
|
|
|
return fmt.Errorf("team ID not found in list of valid team IDs")
|
2020-08-17 17:43:57 -06:00
|
|
|
}
|
2019-12-01 20:47:46 -07:00
|
|
|
|
2022-05-17 13:41:25 -06:00
|
|
|
s.teamNameLock.RLock()
|
|
|
|
_, ok := s.teamNames[teamID]
|
|
|
|
s.teamNameLock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return ErrAlreadyRegistered
|
|
|
|
}
|
|
|
|
|
2020-08-21 17:02:38 -06:00
|
|
|
teamFilename := filepath.Join("teams", teamID)
|
2020-10-13 19:48:37 -06:00
|
|
|
teamFile, err := s.Fs.OpenFile(teamFilename, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644)
|
2022-05-17 13:41:25 -06:00
|
|
|
if os.IsExist(err) { // This shouldn't ever hit, since we just checked, but strange things happen
|
2020-10-13 18:33:12 -06:00
|
|
|
return ErrAlreadyRegistered
|
2020-08-21 17:02:38 -06:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2019-12-12 20:02:57 -07:00
|
|
|
}
|
2020-08-21 17:02:38 -06:00
|
|
|
defer teamFile.Close()
|
2020-12-02 19:31:34 -07:00
|
|
|
log.Printf("Setting team name [%s] in file %s", teamName, teamFilename)
|
2020-08-21 17:02:38 -06:00
|
|
|
fmt.Fprintln(teamFile, teamName)
|
2020-10-13 19:48:37 -06:00
|
|
|
teamFile.Close()
|
2021-10-26 12:48:23 -06:00
|
|
|
|
|
|
|
s.refreshNow <- true
|
|
|
|
|
2020-08-21 17:02:38 -06:00
|
|
|
return nil
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
func (s *State) SetTeamNames(teams map[string]string) error {
|
|
|
|
return s.writeTeamNames(teams)
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
func (s *State) TeamIDFromName(teamName string) (string, error) {
|
|
|
|
for name, id := range s.TeamNames() {
|
|
|
|
if name == teamName {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("team name not found")
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
func (s *State) DeleteTeamName(teamID string) error {
|
|
|
|
newTeams := s.TeamNames()
|
2021-12-30 13:17:17 -07:00
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
_, ok := newTeams[teamID];
|
|
|
|
if ok {
|
|
|
|
delete(newTeams, teamID)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("team not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.writeTeamNames(newTeams)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) writeTeamNames(teams map[string]string) error {
|
|
|
|
s.teamNameFileLock.Lock()
|
|
|
|
defer s.teamNameFileLock.Unlock()
|
|
|
|
|
|
|
|
s.RemoveAll("teams")
|
|
|
|
s.Mkdir("teams", 0755)
|
|
|
|
|
|
|
|
// Write out all of the new team names
|
|
|
|
for teamID, teamName := range teams {
|
|
|
|
teamFilename := filepath.Join("teams", teamID)
|
|
|
|
teamFile, err := s.Fs.OpenFile(teamFilename, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer teamFile.Close()
|
|
|
|
|
|
|
|
log.Printf("Setting team name [%s] in file %s", teamName, teamFilename)
|
|
|
|
fmt.Fprintln(teamFile, teamName)
|
|
|
|
teamFile.Close()
|
|
|
|
}
|
2021-12-30 13:17:17 -07:00
|
|
|
|
|
|
|
s.refreshNow <- true
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/* **************** Point log functions ************ */
|
|
|
|
|
|
|
|
// PointsLog retrieves the current points log.
|
|
|
|
func (s *State) PointsLog() award.List {
|
|
|
|
s.pointsLock.RLock()
|
|
|
|
ret := make(award.List, len(s.pointsLog))
|
|
|
|
copy(ret, s.pointsLog)
|
|
|
|
s.pointsLock.RUnlock()
|
|
|
|
return ret
|
2021-12-30 13:17:17 -07:00
|
|
|
}
|
|
|
|
|
2020-08-14 20:26:04 -06:00
|
|
|
// AwardPoints gives points to teamID in category.
|
2020-12-02 19:31:34 -07:00
|
|
|
// This doesn't attempt to ensure the teamID has been registered.
|
2019-09-02 18:13:37 -06:00
|
|
|
// It first checks to make sure these are not duplicate points.
|
|
|
|
// This is not a perfect check, you can trigger a race condition here.
|
|
|
|
// It's just a courtesy to the user.
|
|
|
|
// The update task makes sure we never have duplicate points in the log.
|
2020-08-14 20:26:04 -06:00
|
|
|
func (s *State) AwardPoints(teamID, category string, points int) error {
|
2021-10-14 19:01:12 -06:00
|
|
|
return s.awardPointsAtTime(time.Now().Unix(), teamID, category, points)
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
func (s *State) AwardPointsAtTime(teamID, category string, points int, when int64) error {
|
|
|
|
return s.awardPointsAtTime(when, teamID, category, points)
|
|
|
|
}
|
|
|
|
|
2021-10-14 19:01:12 -06:00
|
|
|
func (s *State) awardPointsAtTime(when int64, teamID string, category string, points int) error {
|
2020-08-17 17:43:57 -06:00
|
|
|
a := award.T{
|
2021-10-14 19:01:12 -06:00
|
|
|
When: when,
|
2020-08-14 20:26:04 -06:00
|
|
|
TeamID: teamID,
|
2019-09-02 18:13:37 -06:00
|
|
|
Category: category,
|
|
|
|
Points: points,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range s.PointsLog() {
|
2020-08-17 17:43:57 -06:00
|
|
|
if a.Equal(e) {
|
2021-10-13 18:25:27 -06:00
|
|
|
return fmt.Errorf("points already awarded to this team in this category")
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 19:01:12 -06:00
|
|
|
//fn := fmt.Sprintf("%s-%s-%d", a.TeamID, a.Category, a.Points)
|
|
|
|
fn := a.Filename()
|
2019-12-01 18:58:09 -07:00
|
|
|
tmpfn := filepath.Join("points.tmp", fn)
|
|
|
|
newfn := filepath.Join("points.new", fn)
|
2019-09-02 18:13:37 -06:00
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
if err := afero.WriteFile(s, tmpfn, []byte(a.String()), 0644); err != nil {
|
2019-09-02 18:13:37 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
if err := s.Rename(tmpfn, newfn); err != nil {
|
2019-09-02 18:13:37 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:01:21 -06:00
|
|
|
// State should be updated immediately
|
|
|
|
s.refreshNow <- true
|
|
|
|
|
2019-09-02 18:13:37 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
func (s *State) PointExists(teamID string, cat string, points int) bool {
|
2022-05-17 14:30:33 -06:00
|
|
|
s.pointsLock.RLock()
|
|
|
|
defer s.pointsLock.RUnlock()
|
2021-12-30 18:00:14 -07:00
|
|
|
for _, pointEntry := range s.pointsLog {
|
|
|
|
if (pointEntry.TeamID == teamID) && (pointEntry.Category == cat) && (pointEntry.Points == points) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) PointExistsAtTime(teamID string, cat string, points int, when int64) bool {
|
2022-05-17 14:30:33 -06:00
|
|
|
s.pointsLock.RLock()
|
|
|
|
defer s.pointsLock.RUnlock()
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
for _, pointEntry := range s.pointsLog {
|
|
|
|
if (pointEntry.TeamID == teamID) && (pointEntry.Category == cat) && (pointEntry.Points == points) && (pointEntry.When == when) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pointEntry.When > when) { // Since the points log is sorted, we can bail out earlier, if we see that current points are from later than our event
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) flushPointsLog(newPoints award.List) error {
|
|
|
|
s.pointsLogFileLock.Lock()
|
|
|
|
defer s.pointsLogFileLock.Unlock()
|
|
|
|
|
2022-05-18 08:44:01 -06:00
|
|
|
logf, err := s.OpenFile("points.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
2021-12-30 18:00:14 -07:00
|
|
|
defer logf.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-17 14:14:09 -06:00
|
|
|
return fmt.Errorf("Can't write to points log: %s", err)
|
2021-12-30 18:00:14 -07:00
|
|
|
}
|
2022-05-18 08:44:01 -06:00
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
for _, pointEntry := range newPoints {
|
|
|
|
fmt.Fprintln(logf, pointEntry.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) RemovePoints(teamID string, cat string, points int) error {
|
|
|
|
s.pointsLock.Lock()
|
|
|
|
defer s.pointsLock.Unlock()
|
|
|
|
|
|
|
|
var newPoints award.List
|
|
|
|
removed := false
|
|
|
|
|
|
|
|
for _, pointEntry := range s.pointsLog {
|
|
|
|
if (pointEntry.TeamID == teamID) && (pointEntry.Category == cat) && (pointEntry.Points == points) {
|
|
|
|
removed = true
|
|
|
|
} else {
|
|
|
|
newPoints = append(newPoints, pointEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! removed) {
|
2022-05-18 08:44:01 -06:00
|
|
|
return NoMatchingPointEntry
|
2021-12-30 18:00:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err := s.flushPointsLog(newPoints)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.refreshNow <- true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) RemovePointsAtTime(teamID string, cat string, points int, when int64) error {
|
|
|
|
s.pointsLock.Lock()
|
|
|
|
defer s.pointsLock.Unlock()
|
|
|
|
|
|
|
|
var newPoints award.List
|
|
|
|
removed := false
|
|
|
|
|
|
|
|
for _, pointEntry := range s.pointsLog {
|
|
|
|
if (pointEntry.TeamID == teamID) && (pointEntry.Category == cat) && (pointEntry.Points == points) && (pointEntry.When == when) {
|
|
|
|
removed = true
|
|
|
|
} else {
|
|
|
|
newPoints = append(newPoints, pointEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! removed) {
|
2022-05-18 08:44:01 -06:00
|
|
|
return NoMatchingPointEntry
|
2021-12-30 18:00:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err := s.flushPointsLog(newPoints)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.refreshNow <- true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetPoints(newPoints award.List) error {
|
|
|
|
err := s.flushPointsLog(newPoints)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.refreshNow <- true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:13:37 -06:00
|
|
|
// collectPoints gathers up files in points.new/ and appends their contents to points.log,
|
|
|
|
// removing each points.new/ file as it goes.
|
|
|
|
func (s *State) collectPoints() {
|
2020-02-29 16:51:32 -07:00
|
|
|
files, err := afero.ReadDir(s, "points.new")
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2019-12-01 18:58:09 -07:00
|
|
|
filename := filepath.Join("points.new", f.Name())
|
2020-02-29 16:51:32 -07:00
|
|
|
awardstr, err := afero.ReadFile(s, filename)
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print("Opening new points: ", err)
|
|
|
|
continue
|
|
|
|
}
|
2020-08-17 17:43:57 -06:00
|
|
|
awd, err := award.Parse(string(awardstr))
|
2019-09-02 18:13:37 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Print("Can't parse award file ", filename, ": ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicate := false
|
2021-12-30 18:00:14 -07:00
|
|
|
s.pointsLock.RLock()
|
2021-10-26 12:48:23 -06:00
|
|
|
for _, e := range s.pointsLog {
|
2020-08-17 17:43:57 -06:00
|
|
|
if awd.Equal(e) {
|
2019-09-02 18:13:37 -06:00
|
|
|
duplicate = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-12-30 18:00:14 -07:00
|
|
|
s.pointsLock.RUnlock()
|
2019-09-02 18:13:37 -06:00
|
|
|
|
|
|
|
if duplicate {
|
2020-08-17 17:43:57 -06:00
|
|
|
log.Print("Skipping duplicate points: ", awd.String())
|
2019-09-02 18:13:37 -06:00
|
|
|
} else {
|
2020-08-17 17:43:57 -06:00
|
|
|
log.Print("Award: ", awd.String())
|
2019-09-02 18:13:37 -06:00
|
|
|
|
2022-05-17 14:14:09 -06:00
|
|
|
{
|
|
|
|
s.pointsLogFileLock.Lock()
|
|
|
|
|
|
|
|
logf, err := s.OpenFile("points.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Can't append to points log: ", err)
|
|
|
|
s.pointsLogFileLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(logf, awd.String())
|
|
|
|
logf.Close()
|
|
|
|
s.pointsLogFileLock.Unlock()
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2021-10-26 12:48:23 -06:00
|
|
|
|
|
|
|
// Stick this on the cache too
|
2021-12-30 18:00:14 -07:00
|
|
|
s.pointsLock.Lock()
|
2021-10-26 12:48:23 -06:00
|
|
|
s.pointsLog = append(s.pointsLog, awd)
|
2022-05-17 14:14:09 -06:00
|
|
|
s.pointsLock.Unlock()
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
if err := s.Remove(filename); err != nil {
|
2019-09-02 18:13:37 -06:00
|
|
|
log.Print("Unable to remove new points file: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
/* ******************* Message functions *********** */
|
|
|
|
|
|
|
|
// Messages retrieves the current messages.
|
|
|
|
func (s *State) Messages() string {
|
|
|
|
return s.messages
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMessages sets the current message
|
|
|
|
func (s *State) SetMessages(message string) error {
|
|
|
|
s.messageFileLock.Lock()
|
|
|
|
defer s.messageFileLock.Unlock()
|
|
|
|
|
|
|
|
err := afero.WriteFile(s, "messages.html", []byte(message), 0600)
|
|
|
|
|
|
|
|
s.refreshNow <- true
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ***************** Other utilitity functions ******* */
|
|
|
|
|
2019-09-02 18:13:37 -06:00
|
|
|
func (s *State) maybeInitialize() {
|
|
|
|
// Are we supposed to re-initialize?
|
2020-02-29 16:51:32 -07:00
|
|
|
if _, err := s.Stat("initialized"); !os.IsNotExist(err) {
|
2019-09-02 18:13:37 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2020-02-29 22:37:22 -07:00
|
|
|
now := time.Now().UTC().Format(time.RFC3339)
|
2019-09-02 18:13:37 -06:00
|
|
|
log.Print("initialized file missing, re-initializing")
|
|
|
|
|
|
|
|
// Remove any extant control and state files
|
2020-02-29 16:51:32 -07:00
|
|
|
s.Remove("enabled")
|
2020-09-11 20:16:58 -06:00
|
|
|
s.Remove("hours.txt")
|
2021-12-30 18:00:14 -07:00
|
|
|
|
|
|
|
s.pointsLogFileLock.Lock()
|
2020-02-29 16:51:32 -07:00
|
|
|
s.Remove("points.log")
|
2021-12-30 18:00:14 -07:00
|
|
|
s.pointsLogFileLock.Unlock()
|
|
|
|
|
2022-05-10 13:30:44 -06:00
|
|
|
s.Remove("events.csv")
|
2022-05-17 13:41:25 -06:00
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
s.messageFileLock.Lock()
|
2020-02-29 22:37:22 -07:00
|
|
|
s.Remove("messages.html")
|
2021-12-30 18:00:14 -07:00
|
|
|
s.messageFileLock.Unlock()
|
|
|
|
|
2020-08-18 17:04:23 -06:00
|
|
|
s.Remove("mothd.log")
|
2020-02-29 16:51:32 -07:00
|
|
|
s.RemoveAll("points.tmp")
|
|
|
|
s.RemoveAll("points.new")
|
2021-12-30 18:00:14 -07:00
|
|
|
|
|
|
|
s.teamNameFileLock.Lock()
|
2020-02-29 16:51:32 -07:00
|
|
|
s.RemoveAll("teams")
|
2021-12-30 18:00:14 -07:00
|
|
|
s.Mkdir("teams", 0755)
|
|
|
|
s.teamNameFileLock.Unlock()
|
2019-09-02 18:13:37 -06:00
|
|
|
|
2020-08-18 17:04:23 -06:00
|
|
|
// Open log file
|
|
|
|
if err := s.reopenEventLog(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-14 18:20:49 -06:00
|
|
|
s.LogEvent("init", "", "", "", 0)
|
2020-08-18 17:04:23 -06:00
|
|
|
|
2019-09-02 18:13:37 -06:00
|
|
|
// Make sure various subdirectories exist
|
2020-02-29 16:51:32 -07:00
|
|
|
s.Mkdir("points.tmp", 0755)
|
|
|
|
s.Mkdir("points.new", 0755)
|
2019-09-02 18:13:37 -06:00
|
|
|
|
|
|
|
// Preseed available team ids if file doesn't exist
|
2021-12-30 18:00:14 -07:00
|
|
|
var teamIDs []string
|
|
|
|
id := make([]byte, 8)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
for i := range id {
|
|
|
|
char := rand.Intn(len(DistinguishableChars))
|
|
|
|
id[i] = DistinguishableChars[char]
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2021-12-30 18:00:14 -07:00
|
|
|
teamIDs = append(teamIDs, string(id))
|
2019-09-02 18:13:37 -06:00
|
|
|
}
|
2021-12-30 18:00:14 -07:00
|
|
|
s.SetTeamIDs(teamIDs)
|
2019-09-02 18:13:37 -06:00
|
|
|
|
|
|
|
// Create some files
|
2020-02-29 22:37:22 -07:00
|
|
|
if f, err := s.Create("initialized"); err == nil {
|
|
|
|
fmt.Fprintln(f, "initialized: remove to re-initialize the contest.")
|
|
|
|
fmt.Fprintln(f)
|
2020-10-12 10:46:03 -06:00
|
|
|
fmt.Fprintln(f, "This instance was initialized at", now)
|
2020-02-29 22:37:22 -07:00
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, err := s.Create("enabled"); err == nil {
|
|
|
|
fmt.Fprintln(f, "enabled: remove or rename to suspend the contest.")
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2020-09-11 20:16:58 -06:00
|
|
|
if f, err := s.Create("hours.txt"); err == nil {
|
|
|
|
fmt.Fprintln(f, "# hours.txt: when the contest is enabled")
|
2020-02-29 22:37:22 -07:00
|
|
|
fmt.Fprintln(f, "#")
|
|
|
|
fmt.Fprintln(f, "# Enable: + timestamp")
|
|
|
|
fmt.Fprintln(f, "# Disable: - timestamp")
|
|
|
|
fmt.Fprintln(f, "#")
|
|
|
|
fmt.Fprintln(f, "# You can have multiple start/stop times.")
|
|
|
|
fmt.Fprintln(f, "# Whatever time is the most recent, wins.")
|
|
|
|
fmt.Fprintln(f, "# Times in the future are ignored.")
|
|
|
|
fmt.Fprintln(f)
|
|
|
|
fmt.Fprintln(f, "+", now)
|
2020-10-14 18:20:49 -06:00
|
|
|
fmt.Fprintln(f, "- 2519-10-31T00:00:00Z")
|
2020-02-29 22:37:22 -07:00
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
s.messageFileLock.Lock()
|
2020-02-29 22:37:22 -07:00
|
|
|
if f, err := s.Create("messages.html"); err == nil {
|
|
|
|
fmt.Fprintln(f, "<!-- messages.html: put client broadcast messages here. -->")
|
|
|
|
f.Close()
|
|
|
|
}
|
2021-12-30 18:00:14 -07:00
|
|
|
s.messageFileLock.Unlock()
|
2020-02-29 22:37:22 -07:00
|
|
|
|
|
|
|
if f, err := s.Create("points.log"); err == nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
2020-08-18 17:04:23 -06:00
|
|
|
}
|
|
|
|
|
2020-10-14 18:20:49 -06:00
|
|
|
// LogEvent writes to the event log
|
|
|
|
func (s *State) LogEvent(event, participantID, teamID, cat string, points int, extra ...string) {
|
2020-12-02 17:57:10 -07:00
|
|
|
s.eventStream <- append(
|
|
|
|
[]string{
|
|
|
|
strconv.FormatInt(time.Now().Unix(), 10),
|
|
|
|
event,
|
|
|
|
participantID,
|
|
|
|
teamID,
|
|
|
|
cat,
|
|
|
|
strconv.Itoa(points),
|
|
|
|
},
|
|
|
|
extra...,
|
2020-10-14 18:20:49 -06:00
|
|
|
)
|
2020-08-18 17:04:23 -06:00
|
|
|
}
|
2020-02-29 22:37:22 -07:00
|
|
|
|
2020-08-18 17:04:23 -06:00
|
|
|
func (s *State) reopenEventLog() error {
|
|
|
|
if s.eventWriter != nil {
|
2020-12-02 17:57:10 -07:00
|
|
|
s.eventWriter.Flush()
|
|
|
|
}
|
|
|
|
if s.eventWriterFile != nil {
|
|
|
|
if err := s.eventWriterFile.Close(); err != nil {
|
2020-08-18 17:04:23 -06:00
|
|
|
// We're going to soldier on if Close returns error
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 17:57:10 -07:00
|
|
|
eventWriterFile, err := s.OpenFile("events.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
2020-08-18 17:04:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 17:57:10 -07:00
|
|
|
s.eventWriterFile = eventWriterFile
|
|
|
|
s.eventWriter = csv.NewWriter(s.eventWriterFile)
|
2020-08-18 17:04:23 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:48:23 -06:00
|
|
|
func (s *State) updateCaches() {
|
2022-05-17 14:30:33 -06:00
|
|
|
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2022-05-17 14:14:09 -06:00
|
|
|
// Re-read the points log
|
|
|
|
{
|
|
|
|
s.pointsLogFileLock.RLock()
|
|
|
|
defer s.pointsLogFileLock.RUnlock()
|
2021-10-26 12:48:23 -06:00
|
|
|
|
2022-05-17 14:14:09 -06:00
|
|
|
if f, err := s.Open("points.log"); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
} else {
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
pointsLog := make(award.List, 0, 200)
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
cur, err := award.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Skipping malformed award line %s: %s", line, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pointsLog = append(pointsLog, cur)
|
2021-10-26 12:48:23 -06:00
|
|
|
}
|
2022-05-17 14:30:33 -06:00
|
|
|
s.pointsLock.Lock()
|
2022-05-17 14:14:09 -06:00
|
|
|
s.pointsLog = pointsLog
|
2022-05-17 14:30:33 -06:00
|
|
|
s.pointsLock.Unlock()
|
2022-05-17 14:14:09 -06:00
|
|
|
}
|
2021-10-26 12:48:23 -06:00
|
|
|
}
|
|
|
|
|
2022-05-12 18:03:26 -06:00
|
|
|
// Only do this if the teams directory has a newer mtime; directories with
|
|
|
|
// hundreds of team names can cause NFS I/O storms
|
2021-10-26 12:48:23 -06:00
|
|
|
{
|
2021-12-30 18:00:14 -07:00
|
|
|
s.teamNameLock.Lock()
|
|
|
|
defer s.teamNameLock.Unlock()
|
2022-05-17 13:41:25 -06:00
|
|
|
s.teamNameFileLock.RLock()
|
|
|
|
defer s.teamNameFileLock.RUnlock()
|
|
|
|
|
2022-05-12 18:03:26 -06:00
|
|
|
_, ismmfs := s.Fs.(*afero.MemMapFs) // Tests run so quickly that the time check isn't precise enough
|
|
|
|
if fi, err := s.Fs.Stat("teams"); err != nil {
|
|
|
|
log.Printf("Getting modification time of teams directory: %v", err)
|
|
|
|
} else if ismmfs || s.teamNamesLastChange.Before(fi.ModTime()) {
|
|
|
|
s.teamNamesLastChange = fi.ModTime()
|
|
|
|
|
|
|
|
// The compiler recognizes this as an optimization case
|
|
|
|
for k := range s.teamNames {
|
|
|
|
delete(s.teamNames, k)
|
|
|
|
}
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2022-05-12 18:03:26 -06:00
|
|
|
teamsFs := afero.NewBasePathFs(s.Fs, "teams")
|
|
|
|
if dirents, err := afero.ReadDir(teamsFs, "."); err != nil {
|
|
|
|
log.Printf("Reading team ids: %v", err)
|
|
|
|
} else {
|
|
|
|
for _, dirent := range dirents {
|
|
|
|
teamID := dirent.Name()
|
|
|
|
if teamNameBytes, err := afero.ReadFile(teamsFs, teamID); err != nil {
|
|
|
|
log.Printf("Reading team %s: %v", teamID, err)
|
|
|
|
} else {
|
|
|
|
teamName := strings.TrimSpace(string(teamNameBytes))
|
|
|
|
s.teamNames[teamID] = teamName
|
|
|
|
}
|
2021-10-26 12:48:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:14:09 -06:00
|
|
|
// Re-read the messages file
|
|
|
|
{
|
|
|
|
s.messageFileLock.RLock()
|
|
|
|
defer s.messageFileLock.RUnlock()
|
2021-12-30 18:00:14 -07:00
|
|
|
|
2022-05-17 14:14:09 -06:00
|
|
|
if bMessages, err := afero.ReadFile(s, "messages.html"); err == nil {
|
|
|
|
s.messages = string(bMessages)
|
|
|
|
}
|
2021-10-26 12:48:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:04:23 -06:00
|
|
|
func (s *State) refresh() {
|
2019-12-01 18:58:09 -07:00
|
|
|
s.maybeInitialize()
|
2020-08-18 17:04:23 -06:00
|
|
|
s.updateEnabled()
|
2019-12-01 18:58:09 -07:00
|
|
|
if s.Enabled {
|
|
|
|
s.collectPoints()
|
|
|
|
}
|
2021-10-26 12:48:23 -06:00
|
|
|
s.updateCaches()
|
2019-12-01 18:58:09 -07:00
|
|
|
}
|
2020-08-18 17:04:23 -06:00
|
|
|
|
|
|
|
// Maintain performs housekeeping on a State struct.
|
|
|
|
func (s *State) Maintain(updateInterval time.Duration) {
|
|
|
|
ticker := time.NewTicker(updateInterval)
|
|
|
|
s.refresh()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-s.eventStream:
|
2020-12-02 17:57:10 -07:00
|
|
|
s.eventWriter.Write(msg)
|
|
|
|
s.eventWriter.Flush()
|
|
|
|
s.eventWriterFile.Sync()
|
2020-08-18 17:04:23 -06:00
|
|
|
case <-ticker.C:
|
|
|
|
s.refresh()
|
|
|
|
case <-s.refreshNow:
|
|
|
|
s.refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 19:31:34 -07:00
|
|
|
|
|
|
|
// DevelState is a StateProvider for use by development servers
|
|
|
|
type DevelState struct {
|
|
|
|
StateProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDevelState returns a new state object that can be used by the development server.
|
|
|
|
//
|
2021-02-01 16:53:38 -07:00
|
|
|
// The main thing this provides is the ability to register a team with any team ID.
|
|
|
|
// If a team ID is provided that wasn't recognized by the underlying StateProvider,
|
|
|
|
// it is associated with a team named "<devel:$ID>".
|
|
|
|
//
|
2020-12-02 19:31:34 -07:00
|
|
|
// This makes it possible to use the server without having to register a team.
|
|
|
|
func NewDevelState(sp StateProvider) *DevelState {
|
|
|
|
return &DevelState{sp}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TeamName returns a valid team name for any teamID
|
|
|
|
//
|
|
|
|
// If one's registered, it will use it.
|
2021-02-01 16:53:38 -07:00
|
|
|
// Otherwise, it returns "<devel:$ID>"
|
2020-12-02 19:31:34 -07:00
|
|
|
func (ds *DevelState) TeamName(teamID string) (string, error) {
|
|
|
|
if name, err := ds.StateProvider.TeamName(teamID); err == nil {
|
|
|
|
return name, nil
|
|
|
|
}
|
2020-12-02 19:49:06 -07:00
|
|
|
return fmt.Sprintf("«devel:%s»", teamID), nil
|
2020-12-02 19:31:34 -07:00
|
|
|
}
|
2021-02-01 16:53:38 -07:00
|
|
|
|
|
|
|
// SetTeamName associates a team name with any teamID
|
|
|
|
//
|
|
|
|
// If the underlying StateProvider returns any sort of error,
|
|
|
|
// this returns ErrAlreadyRegistered,
|
|
|
|
// so the user can join a pre-existing team for whatever ID the provide.
|
|
|
|
func (ds *DevelState) SetTeamName(teamID, teamName string) error {
|
|
|
|
if err := ds.StateProvider.SetTeamName(teamID, teamName); err != nil {
|
|
|
|
return ErrAlreadyRegistered
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|