mostly working
This commit is contained in:
parent
26eaa1d898
commit
5f42a580a9
47 changed files with 914 additions and 637 deletions
|
|
@ -3,118 +3,78 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
"strings"
|
||||
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
var (
|
||||
outputPath = flag.String("output", "", "The output file")
|
||||
inputFormat = flag.String("format", "csv", "The input format, either 'csv' or 'tsv' (defaults to 'csv')")
|
||||
inputPath = flag.String("f", "", "The input file")
|
||||
disableLinreg = flag.Bool("disable-linreg", false, "If we should omit linear regressions")
|
||||
disableLastValues = flag.Bool("disable-last-values", false, "If we should omit last values")
|
||||
)
|
||||
|
||||
// NewLogger returns a new logger.
|
||||
func NewLogger() *Logger {
|
||||
return &Logger{
|
||||
TimeFormat: time.RFC3339Nano,
|
||||
Stdout: os.Stdout,
|
||||
Stderr: os.Stderr,
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
flag.Parse()
|
||||
log := chart.NewLogger()
|
||||
|
||||
// Logger is a basic logger.
|
||||
type Logger struct {
|
||||
TimeFormat string
|
||||
Stdout io.Writer
|
||||
Stderr io.Writer
|
||||
}
|
||||
|
||||
// Info writes an info message.
|
||||
func (l *Logger) Info(arguments ...interface{}) {
|
||||
l.Println(append([]interface{}{"[INFO]"}, arguments...)...)
|
||||
}
|
||||
|
||||
// Infof writes an info message.
|
||||
func (l *Logger) Infof(format string, arguments ...interface{}) {
|
||||
l.Println(append([]interface{}{"[INFO]"}, fmt.Sprintf(format, arguments...))...)
|
||||
}
|
||||
|
||||
// Debug writes an debug message.
|
||||
func (l *Logger) Debug(arguments ...interface{}) {
|
||||
l.Println(append([]interface{}{"[DEBUG]"}, arguments...)...)
|
||||
}
|
||||
|
||||
// Debugf writes an debug message.
|
||||
func (l *Logger) Debugf(format string, arguments ...interface{}) {
|
||||
l.Println(append([]interface{}{"[DEBUG]"}, fmt.Sprintf(format, arguments...))...)
|
||||
}
|
||||
|
||||
// Error writes an error message.
|
||||
func (l *Logger) Error(arguments ...interface{}) {
|
||||
l.Println(append([]interface{}{"[ERROR]"}, arguments...)...)
|
||||
}
|
||||
|
||||
// Errorf writes an error message.
|
||||
func (l *Logger) Errorf(format string, arguments ...interface{}) {
|
||||
l.Println(append([]interface{}{"[ERROR]"}, fmt.Sprintf(format, arguments...))...)
|
||||
}
|
||||
|
||||
// Err writes an error message.
|
||||
func (l *Logger) Err(err error) {
|
||||
if err != nil {
|
||||
l.Println(append([]interface{}{"[ERROR]"}, err.Error())...)
|
||||
}
|
||||
}
|
||||
|
||||
// FatalErr writes an error message and exits.
|
||||
func (l *Logger) FatalErr(err error) {
|
||||
if err != nil {
|
||||
l.Println(append([]interface{}{"[FATAL]"}, err.Error())...)
|
||||
var rawData []byte
|
||||
var err error
|
||||
if *inputPath != "" {
|
||||
if *inputPath == "-" {
|
||||
rawData, err = ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
log.FatalErr(err)
|
||||
}
|
||||
} else {
|
||||
rawData, err = ioutil.ReadFile(*inputPath)
|
||||
if err != nil {
|
||||
log.FatalErr(err)
|
||||
}
|
||||
}
|
||||
} else if len(flag.Args()) > 0 {
|
||||
rawData = []byte(flag.Args()[0])
|
||||
} else {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Println prints a new message.
|
||||
func (l *Logger) Println(arguments ...interface{}) {
|
||||
fmt.Fprintln(l.Stdout, append([]interface{}{time.Now().UTC().Format(l.TimeFormat)}, arguments...)...)
|
||||
}
|
||||
var parts []string
|
||||
switch *inputFormat {
|
||||
case "csv":
|
||||
parts = chart.SplitCSV(string(rawData))
|
||||
case "tsv":
|
||||
parts = strings.Split(string(rawData), "\t")
|
||||
default:
|
||||
log.FatalErr(fmt.Errorf("invalid format; must be 'csv' or 'tsv'"))
|
||||
}
|
||||
|
||||
// Errorln prints a new message.
|
||||
func (l *Logger) Errorln(arguments ...interface{}) {
|
||||
fmt.Fprintln(l.Stderr, append([]interface{}{time.Now().UTC().Format(l.TimeFormat)}, arguments...)...)
|
||||
}
|
||||
|
||||
func main() {
|
||||
log := NewLogger()
|
||||
|
||||
rawData, err := ioutil.ReadAll(os.Stdin)
|
||||
yvalues, err := chart.ParseFloats(parts...)
|
||||
if err != nil {
|
||||
log.FatalErr(err)
|
||||
}
|
||||
|
||||
csvParts := chart.SplitCSV(string(rawData))
|
||||
|
||||
yvalues, err := chart.ParseFloats(csvParts...)
|
||||
|
||||
var series []chart.Series
|
||||
mainSeries := chart.ContinuousSeries{
|
||||
Name: "A test series",
|
||||
XValues: chart.SeqRange(0, float64(len(csvParts))), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
|
||||
Name: "Values",
|
||||
XValues: chart.LinearRange(1, float64(len(yvalues))),
|
||||
YValues: yvalues,
|
||||
}
|
||||
series = append(series, mainSeries)
|
||||
|
||||
linRegSeries := &chart.LinearRegressionSeries{
|
||||
InnerSeries: mainSeries,
|
||||
if !*disableLinreg {
|
||||
linRegSeries := &chart.LinearRegressionSeries{
|
||||
InnerSeries: mainSeries,
|
||||
}
|
||||
series = append(series, linRegSeries)
|
||||
}
|
||||
|
||||
graph := chart.Chart{
|
||||
Series: []chart.Series{
|
||||
mainSeries,
|
||||
linRegSeries,
|
||||
},
|
||||
Series: series,
|
||||
}
|
||||
|
||||
var output *os.File
|
||||
|
|
@ -130,8 +90,10 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
log.Info("rendering chart to", output.Name())
|
||||
if err := graph.Render(chart.PNG, output); err != nil {
|
||||
log.FatalErr(err)
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stdout, output.Name())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue