refactor: move report file reading

This commit is contained in:
Matthias Radestock
2017-05-26 12:19:07 +01:00
parent be0297d488
commit eb8695965a
2 changed files with 51 additions and 47 deletions

View File

@@ -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)

View File

@@ -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
}