mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-21 22:36:39 +00:00
refactor: move report file reading
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user