From 48e74e33d6d4597488e848ac82fbd4a62df025a4 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 2 May 2018 23:00:53 +0000 Subject: [PATCH] still working on maintenance functions --- maintenance.go | 16 ++++++++++++++-- mothd.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/maintenance.go b/maintenance.go index 1406f38..8afe8af 100644 --- a/maintenance.go +++ b/maintenance.go @@ -42,7 +42,7 @@ func tidy() { newCategories := []string{} for f := range(allfiles(mothPath("packages"))) { filename := f.Name() - filepath := mothPath(path.Join("packages", filename)) + filepath := mothPath("packages", filename) if ! strings.HasSuffix(filename, ".mb") { continue } @@ -58,9 +58,21 @@ func tidy() { // // Collect new points // + pointsLog = os.OpenFile(statePath("points.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) for f := range allfiles(statePath("points.new")) { - + filename := statePath("points.new", f.Name()) + s := ioutil.ReadFile(filename) + award, err := ParseAward(s) + if (err != nil) { + log.Printf("Can't parse award file %s: %s", filename, err) + continue + } + fmt.Fprintf(pointsLog, "%s\n", award.String()) + log.Print(award.String()) + pointsLog.Sync() + os.Remove(filename) } + pointsLog.Close() } // maintenance is the goroutine that runs a periodic maintenance task diff --git a/mothd.go b/mothd.go index 8d6c08c..437214d 100644 --- a/mothd.go +++ b/mothd.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path" + "strconv" "strings" "time" ) @@ -16,6 +17,47 @@ var basePath = "." var maintenanceInterval = 20 * time.Second var categories = []string{} +type Award struct { + when time.Time, + team string, + category string, + points int, + comment string +} + +func ParseAward(s string) (*Award, error) { + ret := Award{} + + parts := strings.SplitN(s, " ", 5) + if len(parts) < 4 { + return nil, Error("Malformed award string") + } + + whenEpoch, err = strconv.Atoi(parts[0]) + if (err != nil) { + return nil, Errorf("Malformed timestamp: %s", parts[0]) + } + ret.when = time.Unix(whenEpoch, 0) + + ret.team = parts[1] + ret.category = parts[2] + + points, err = strconv.Atoi(parts[3]) + if (err != nil) { + return nil, Errorf("Malformed points: %s", parts[3]) + } + + if len(parts) == 5 { + ret.comment = parts[4] + } + + return &ret +} + +func (a *Award) String() string { + return fmt.Sprintf("%d %s %s %d %s", a.when.Unix(), a.team, a.category, a.points, a.comment) +} + func mooHandler(w http.ResponseWriter, req *http.Request) { moo := req.FormValue("moo") fmt.Fprintf(w, "Hello, %q. %s", html.EscapeString(req.URL.Path), html.EscapeString(moo)) @@ -28,12 +70,12 @@ func rootHandler(w http.ResponseWriter, req *http.Request) { } } -func mothPath(filename string) string { - return path.Join(basePath, filename) +func mothPath(parts ...string) string { + return path.Join(basePath, parts...) } -func statePath(filename string) string { - return path.Join(basePath, "state", filename) +func statePath(parts ...string) string { + return path.Join(basePath, "state", parts...) } func exists(filename string) bool {