From eb8695965a8a09e80c033bff4de6f37587566dc1 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 26 May 2017 12:19:07 +0100 Subject: [PATCH] refactor: move report file reading --- app/collector.go | 48 +-------------------------------------------- report/marshal.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/app/collector.go b/app/collector.go index 51cce7092..659fe242a 100644 --- a/app/collector.go +++ b/app/collector.go @@ -1,9 +1,7 @@ package app import ( - "compress/gzip" "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -11,7 +9,6 @@ import ( "sync" "time" - "github.com/ugorji/go/codec" "golang.org/x/net/context" "github.com/weaveworks/common/mtime" @@ -234,7 +231,7 @@ func NewFileCollector(path string, window time.Duration) (Collector, error) { } timestamps = append(timestamps, t) - rpt, err := readReport(p) + rpt, err := report.MakeFromFile(p) if err != nil { return err } @@ -267,49 +264,6 @@ func timestampFromFilepath(path string) (time.Time, error) { return time.Unix(0, nanosecondsSinceEpoch), nil } -func readReport(path string) (rpt report.Report, _ error) { - f, err := os.Open(path) - if err != nil { - return rpt, err - } - defer f.Close() - - var ( - handle codec.Handle - gzipped bool - ) - fileType := filepath.Ext(path) - if fileType == ".gz" { - gzipped = true - fileType = filepath.Ext(strings.TrimSuffix(path, fileType)) - } - switch fileType { - case ".json": - handle = &codec.JsonHandle{} - case ".msgpack": - handle = &codec.MsgpackHandle{} - default: - return rpt, fmt.Errorf("Unsupported file extension: %v", fileType) - } - - var buf []byte - if gzipped { - r, err := gzip.NewReader(f) - if err != nil { - return rpt, err - } - buf, err = ioutil.ReadAll(r) - } else { - buf, err = ioutil.ReadAll(f) - } - if err != nil { - return rpt, err - } - err = rpt.ReadBytes(buf, handle) - - return rpt, err -} - func replay(a Adder, timestamps []time.Time, reports []report.Report) { // calculate delays between report n and n+1 l := len(timestamps) diff --git a/report/marshal.go b/report/marshal.go index f5e2e776a..6ebe044f1 100644 --- a/report/marshal.go +++ b/report/marshal.go @@ -3,8 +3,12 @@ package report import ( "bytes" "compress/gzip" + "fmt" "io" "io/ioutil" + "os" + "path/filepath" + "strings" log "github.com/Sirupsen/logrus" "github.com/ugorji/go/codec" @@ -118,3 +122,49 @@ func MakeFromBytes(buf []byte) (*Report, error) { } return &rep, nil } + +// MakeFromFile construct a Report from a file, with the encoding +// determined by the extension (".msgpack" or ".json", with an +// optional ".gz"). +func MakeFromFile(path string) (rpt Report, _ error) { + f, err := os.Open(path) + if err != nil { + return rpt, err + } + defer f.Close() + + var ( + handle codec.Handle + gzipped bool + ) + fileType := filepath.Ext(path) + if fileType == ".gz" { + gzipped = true + fileType = filepath.Ext(strings.TrimSuffix(path, fileType)) + } + switch fileType { + case ".json": + handle = &codec.JsonHandle{} + case ".msgpack": + handle = &codec.MsgpackHandle{} + default: + return rpt, fmt.Errorf("Unsupported file extension: %v", fileType) + } + + var buf []byte + if gzipped { + r, err := gzip.NewReader(f) + if err != nil { + return rpt, err + } + buf, err = ioutil.ReadAll(r) + } else { + buf, err = ioutil.ReadAll(f) + } + if err != nil { + return rpt, err + } + err = rpt.ReadBytes(buf, handle) + + return rpt, err +}