events log -> csv, say we're in devel mode

This commit is contained in:
Neale Pickett 2020-12-02 17:57:10 -07:00
parent 2297fc7fd7
commit 788ce7c3b6
3 changed files with 43 additions and 41 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"mime" "mime"
"os" "os"
"time" "time"
@ -53,15 +54,6 @@ func main() {
) )
flag.Parse() flag.Parse()
// Set random seed
if *seed == "" {
*seed = os.Getenv("SEED")
}
if *seed == "" {
*seed = fmt.Sprintf("%d%d", os.Getpid(), time.Now().Unix())
}
os.Setenv("SEED", *seed)
osfs := afero.NewOsFs() osfs := afero.NewOsFs()
theme := NewTheme(afero.NewBasePathFs(osfs, *themePath)) theme := NewTheme(afero.NewBasePathFs(osfs, *themePath))
state := NewState(afero.NewBasePathFs(osfs, *statePath)) state := NewState(afero.NewBasePathFs(osfs, *statePath))
@ -73,8 +65,19 @@ func main() {
if *puzzlePath != "" { if *puzzlePath != "" {
provider = NewTranspilerProvider(afero.NewBasePathFs(osfs, *puzzlePath)) provider = NewTranspilerProvider(afero.NewBasePathFs(osfs, *puzzlePath))
config.Devel = true config.Devel = true
log.Println("-=- You are in development mode, champ! -=-")
} }
// Set random seed
if *seed == "" {
*seed = os.Getenv("SEED")
}
if *seed == "" {
*seed = fmt.Sprintf("%d%d", os.Getpid(), time.Now().Unix())
}
os.Setenv("SEED", *seed)
log.Print("SEED=", *seed)
// Add some MIME extensions // Add some MIME extensions
// Doing this avoids decompressing a mothball entry twice per request // Doing this avoids decompressing a mothball entry twice per request
mime.AddExtensionType(".json", "application/json") mime.AddExtensionType(".json", "application/json")

View File

@ -2,12 +2,14 @@ package main
import ( import (
"bufio" "bufio"
"encoding/csv"
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"time" "time"
@ -37,8 +39,9 @@ type State struct {
Enabled bool Enabled bool
refreshNow chan bool refreshNow chan bool
eventStream chan string eventStream chan []string
eventWriter afero.File eventWriter *csv.Writer
eventWriterFile afero.File
} }
// NewState returns a new State struct backed by the given Fs // NewState returns a new State struct backed by the given Fs
@ -47,7 +50,7 @@ func NewState(fs afero.Fs) *State {
Fs: fs, Fs: fs,
Enabled: true, Enabled: true,
refreshNow: make(chan bool, 5), refreshNow: make(chan bool, 5),
eventStream: make(chan string, 80), eventStream: make(chan []string, 80),
} }
if err := s.reopenEventLog(); err != nil { if err := s.reopenEventLog(); err != nil {
log.Fatal(err) log.Fatal(err)
@ -100,12 +103,6 @@ func (s *State) updateEnabled() {
} }
if _, err := s.Stat("enabled"); os.IsNotExist(err) { if _, err := s.Stat("enabled"); os.IsNotExist(err) {
dirs, _ := afero.ReadDir(s, ".")
for _, dir := range dirs {
log.Println(dir.Name())
}
log.Print(s, err)
nextEnabled = false nextEnabled = false
why = "`state/enabled` missing" why = "`state/enabled` missing"
} }
@ -380,34 +377,35 @@ func logstr(s string) string {
// LogEvent writes to the event log // LogEvent writes to the event log
func (s *State) LogEvent(event, participantID, teamID, cat string, points int, extra ...string) { func (s *State) LogEvent(event, participantID, teamID, cat string, points int, extra ...string) {
event = strings.ReplaceAll(event, " ", "-") s.eventStream <- append(
[]string{
msg := fmt.Sprintf( strconv.FormatInt(time.Now().Unix(), 10),
"%s %s %s %s %d", event,
logstr(event), participantID,
logstr(participantID), teamID,
logstr(teamID), cat,
logstr(cat), strconv.Itoa(points),
points, },
extra...,
) )
for _, x := range extra {
msg = msg + " " + strings.ReplaceAll(x, " ", "-")
}
s.eventStream <- msg
} }
func (s *State) reopenEventLog() error { func (s *State) reopenEventLog() error {
if s.eventWriter != nil { if s.eventWriter != nil {
if err := s.eventWriter.Close(); err != nil { s.eventWriter.Flush()
}
if s.eventWriterFile != nil {
if err := s.eventWriterFile.Close(); err != nil {
// We're going to soldier on if Close returns error // We're going to soldier on if Close returns error
log.Print(err) log.Print(err)
} }
} }
eventWriter, err := s.OpenFile("events.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) eventWriterFile, err := s.OpenFile("events.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
return err return err
} }
s.eventWriter = eventWriter s.eventWriterFile = eventWriterFile
s.eventWriter = csv.NewWriter(s.eventWriterFile)
return nil return nil
} }
@ -426,8 +424,9 @@ func (s *State) Maintain(updateInterval time.Duration) {
for { for {
select { select {
case msg := <-s.eventStream: case msg := <-s.eventStream:
fmt.Fprintln(s.eventWriter, time.Now().Unix(), msg) s.eventWriter.Write(msg)
s.eventWriter.Sync() s.eventWriter.Flush()
s.eventWriterFile.Sync()
case <-ticker.C: case <-ticker.C:
s.refresh() s.refresh()
case <-s.refreshNow: case <-s.refreshNow:

View File

@ -127,13 +127,13 @@ func TestStateEvents(t *testing.T) {
s.LogEvent("moo", "", "", "", 0) s.LogEvent("moo", "", "", "", 0)
s.LogEvent("moo 2", "", "", "", 0) s.LogEvent("moo 2", "", "", "", 0)
if msg := <-s.eventStream; msg != "init - - - 0" { if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "init::::0" {
t.Error("Wrong message from event stream:", msg) t.Error("Wrong message from event stream:", msg)
} }
if msg := <-s.eventStream; msg != "moo - - - 0" { if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "moo::::0" {
t.Error("Wrong message from event stream:", msg) t.Error("Wrong message from event stream:", msg)
} }
if msg := <-s.eventStream; msg != "moo-2 - - - 0" { if msg := <-s.eventStream; strings.Join(msg[1:], ":") != "moo 2::::0" {
t.Error("Wrong message from event stream:", msg) t.Error("Wrong message from event stream:", msg)
} }
} }
@ -255,7 +255,7 @@ func TestStateMaintainer(t *testing.T) {
time.Sleep(updateInterval) time.Sleep(updateInterval)
eventLog, err := afero.ReadFile(s.Fs, "events.log") eventLog, err := afero.ReadFile(s.Fs, "events.csv")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else if events := strings.Split(string(eventLog), "\n"); len(events) != 3 { } else if events := strings.Split(string(eventLog), "\n"); len(events) != 3 {