Possibly parsing LAST_EVENT_ID

This commit is contained in:
Neale Pickett 2015-02-22 16:06:22 -07:00
parent c9a90d8ae0
commit 756d397431
3 changed files with 25 additions and 8 deletions

View File

@ -7,14 +7,17 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
) )
const eventIdSep = "/"
type Network struct { type Network struct {
running bool running bool
name string Name string
currentLog string currentLog string
lineno int64 lineno int64
@ -25,7 +28,7 @@ type Network struct {
func NewNetwork(basePath string) (*Network) { func NewNetwork(basePath string) (*Network) {
return &Network{ return &Network{
running: true, running: true,
name: path.Base(basePath), Name: path.Base(basePath),
basePath: basePath, basePath: basePath,
} }
} }
@ -34,13 +37,25 @@ func (nw *Network) Close() {
nw.running = false nw.running = false
} }
func (nw *Network) LastEventId() string { func (nw *Network) ReadLastEventId(lastEventId string) {
return fmt.Sprintf("%s/%s/%d", nw.name, nw.currentLog, nw.lineno) for _, eventId := range strings.Split(lastEventId, " ") {
parts := strings.Split(eventId, eventIdSep)
if len(parts) != 3 {
continue
} }
func (nw *Network) SetPosition(filename string, lineno int64) { if parts[0] != nw.Name {
nw.currentLog = filename continue
nw.lineno = lineno }
nw.currentLog = parts[1]
nw.lineno, _ = strconv.ParseInt(parts[2], 10, 64)
return
}
}
func (nw *Network) LastEventId() string {
parts := []string{nw.Name, nw.currentLog, strconv.FormatInt(nw.lineno, 10)}
return strings.Join(parts, eventIdSep)
} }
func (nw *Network) errmsg(err error) []string { func (nw *Network) errmsg(err error) []string {

Binary file not shown.

View File

@ -38,9 +38,11 @@ func (h Handler) handleTail(cfg *Config, w http.ResponseWriter, r *http.Request)
w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Content-Type", "text/event-stream")
nws := Networks(cfg.BaseDir) nws := Networks(cfg.BaseDir)
lastEventId := r.FormValue("HTTP_LAST_EVENT_ID")
updates := make(chan []string, 100) updates := make(chan []string, 100)
for _, nw := range nws { for _, nw := range nws {
nw.ReadLastEventId(lastEventId)
go nw.Tail(updates) go nw.Tail(updates)
defer nw.Close() defer nw.Close()
} }