Skip to content

Commit

Permalink
Isolates dataset in order to package it. #39
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariano Gappa committed Aug 11, 2018
1 parent b4124f5 commit 280b8c0
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 46 deletions.
9 changes: 6 additions & 3 deletions chart_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/marianogappa/chart/format"
)

func resolveChartType(ct chartType, lf format.LineFormat) (chartType, error) {
func resolveChartType(ct chartType, lf format.LineFormat, datasetLength int) (chartType, error) {
ct = _resolveChartType(ct, lf)
return ct, assertChartable(ct, lf)
return ct, assertChartable(ct, lf, datasetLength)
}

func _resolveChartType(ct chartType, f format.LineFormat) chartType {
Expand All @@ -27,7 +27,10 @@ func _resolveChartType(ct chartType, f format.LineFormat) chartType {
}
}

func assertChartable(ct chartType, f format.LineFormat) error {
func assertChartable(ct chartType, f format.LineFormat, datasetLength int) error {
if datasetLength == 0 {
return fmt.Errorf("empty dataset; nothing to plot here")
}
var errIncompatibleFormat = fmt.Errorf("I don't know how to plot a dataset with this line format")
switch ct {
case pie, bar:
Expand Down
2 changes: 1 addition & 1 deletion chart_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestResolveChartType(t *testing.T) {

for _, ts := range tests {
lf, _ := format.NewLineFormat(ts.lf, ' ', "") // ignoring errors as we're not testing the format package here
result, err := resolveChartType(ts.t, lf)
result, err := resolveChartType(ts.t, lf, 123)
if err != nil { // TODO test cases where there's an error
t.Errorf("%v: there was an error resolving the chart type", ts.name)
}
Expand Down
9 changes: 6 additions & 3 deletions cheatsheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ func TestCheatsheet(t *testing.T) {

var rdr io.Reader
rdr, o.lineFormat = format.Parse(rd, o.separator, o.dateFormat)
d := mustNewDataset(rdr, o)
o.chartType, err = resolveChartType(o.chartType, d.lineFormat)
d := mustNewDataset(rdr, o.lineFormat)
if !o.lineFormat.HasFloats && !o.lineFormat.HasDateTimes && o.lineFormat.HasStrings {
d.fss, d.sss, o.lineFormat = preprocessFreq(d.sss, o.lineFormat)
}
o.chartType, err = resolveChartType(o.chartType, o.lineFormat, d.Len())
if err != nil {
t.Errorf("[%v] error resolving chart type when o.chartType=%v and d.lineFormat=%v: [%v]", f, o.chartType, d.lineFormat, err)
t.Errorf("[%v] error resolving chart type when o.chartType=%v and d.lineFormat=%v: [%v]", f, o.chartType, o.lineFormat, err)
t.FailNow()
}
b, err := chartjs.New(
Expand Down
37 changes: 12 additions & 25 deletions dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"fmt"
"io"
"time"

Expand All @@ -11,13 +10,12 @@ import (
)

type dataset struct {
fss [][]float64
sss [][]string
tss [][]time.Time
minFSS []float64
maxFSS []float64
lineFormat format.LineFormat
stdinLen int
fss [][]float64
sss [][]string
tss [][]time.Time
minFSS []float64
maxFSS []float64
stdinLen int
}

func (d dataset) Len() int {
Expand All @@ -27,36 +25,34 @@ func (d dataset) Len() int {
return len(d.fss)
}

func mustNewDataset(r io.Reader, o options) *dataset {
d, err := newDataset(r, o)
func mustNewDataset(r io.Reader, f format.LineFormat) *dataset {
d, err := newDataset(r, f)
if err != nil {
log.WithError(err).Fatal("Could not build dataset.")
}
return d
}

func newDataset(r io.Reader, o options) (*dataset, error) {
func newDataset(r io.Reader, f format.LineFormat) (*dataset, error) {
d := &dataset{
fss: make([][]float64, 0, 500),
sss: make([][]string, 0, 500),
tss: make([][]time.Time, 0, 500),
minFSS: make([]float64, 0, 500),
maxFSS: make([]float64, 0, 500),
}
return d, d.read(r, o)
return d, d.read(r, f)
}

func (d *dataset) read(r io.Reader, o options) error {
func (d *dataset) read(r io.Reader, f format.LineFormat) error {
var (
nilSSS, nilFSS, nilTSS = true, true, true
scanner = bufio.NewScanner(r)
stdinLen = 0
)
d.lineFormat = o.lineFormat

for scanner.Scan() {
stdinLen++
fs, ss, ts, err := d.lineFormat.ParseLine(scanner.Text())
fs, ss, ts, err := f.ParseLine(scanner.Text())
if err != nil {
continue
}
Expand Down Expand Up @@ -104,15 +100,6 @@ func (d *dataset) read(r io.Reader, o options) error {
if nilTSS {
d.tss = nil
}
if !d.lineFormat.HasFloats && len(d.sss) > 0 {
d.fss, d.sss = preprocessFreq(d.sss)
d.lineFormat.ColTypes = append(d.lineFormat.ColTypes, format.Float)
d.lineFormat.FloatCount++
d.lineFormat.HasFloats = true
}
if d.Len() == 0 {
return fmt.Errorf("empty dataset; nothing to plot here")
}

return nil
}
10 changes: 3 additions & 7 deletions dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ func TestDataset(t *testing.T) {
c
a
`,
o: options{separator: '\t', scaleType: linear, chartType: pie},
fss: [][]float64{
{2}, {1}, {1},
},
sss: [][]string{{"a"}, {"b"}, {"c"}},
o: options{separator: '\t', scaleType: linear, chartType: pie},
sss: [][]string{{"a"}, {"b"}, {"c"}, {"a"}},
tss: nil,
minFSS: nil,
maxFSS: nil,
Expand All @@ -91,8 +88,7 @@ func TestDataset(t *testing.T) {

for _, ts := range tests {
rd, lf := format.Parse(strings.NewReader(ts.i), ts.o.separator, ts.o.dateFormat)
ts.o.lineFormat = lf
d, err := newDataset(rd, ts.o)
d, err := newDataset(rd, lf)

if err != nil {
t.Errorf("'%v' failed: error reading dataset %v", ts.name, err)
Expand Down
2 changes: 1 addition & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func renderDebug(d dataset, o options, err error) string {
tcn = len(d.tss[0])
}
buffer.WriteString(fmt.Sprintf("Lines read\t%v\n", d.stdinLen))
buffer.WriteString(fmt.Sprintf("Line format inferred\t%v\n", d.lineFormat.String()))
buffer.WriteString(fmt.Sprintf("Line format inferred\t%v\n", o.lineFormat.String()))
buffer.WriteString(fmt.Sprintf("Lines used\t%v\n", rn))
buffer.WriteString(fmt.Sprintf("Float column count\t%v\n", fcn))
buffer.WriteString(fmt.Sprintf("String column count\t%v\n", scn))
Expand Down
11 changes: 9 additions & 2 deletions freq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"sort"

"github.com/marianogappa/chart/format"
)

func preprocessFreq(isss [][]string) ([][]float64, [][]string) {
func preprocessFreq(isss [][]string, lineFormat format.LineFormat) ([][]float64, [][]string, format.LineFormat) {
fss := [][]float64{}
sss := [][]string{}

Expand Down Expand Up @@ -44,7 +46,12 @@ func preprocessFreq(isss [][]string) ([][]float64, [][]string) {
sss = append(sss, []string{fqs.fs[9].s})
}

return fss, sss
// Updates lineFormat
lineFormat.ColTypes = append(lineFormat.ColTypes, format.Float)
lineFormat.FloatCount++
lineFormat.HasFloats = true

return fss, sss, lineFormat
}

type freq struct {
Expand Down
8 changes: 6 additions & 2 deletions freq_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "testing"
import (
"testing"

"github.com/marianogappa/chart/format"
)

func TestFreq(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -119,7 +123,7 @@ func TestFreq(t *testing.T) {
}

for _, ts := range tests {
fss, sss := preprocessFreq(ts.isss)
fss, sss, _ := preprocessFreq(ts.isss, format.LineFormat{})

if !equalMap(fss, ts.expectedFSS, sss, ts.expectedSSS) {
t.Errorf("[%v] case failed: %v, %v were not equal to %v, %v", ts.name, fss, sss, ts.expectedFSS, ts.expectedSSS)
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ func main() {
if opts.rawLineFormat == "" {
rd, opts.lineFormat = format.Parse(rd, opts.separator, opts.dateFormat)
}
dataset := mustNewDataset(rd, opts)
if opts.chartType, err = resolveChartType(opts.chartType, dataset.lineFormat); opts.debug || err != nil {
dataset := mustNewDataset(rd, opts.lineFormat)
if !opts.lineFormat.HasFloats && !opts.lineFormat.HasDateTimes && opts.lineFormat.HasStrings {
dataset.fss, dataset.sss, opts.lineFormat = preprocessFreq(dataset.sss, opts.lineFormat)
}
if opts.chartType, err = resolveChartType(opts.chartType, opts.lineFormat, dataset.Len()); opts.debug || err != nil {
fmt.Println(renderDebug(*dataset, opts, err))
os.Exit(0)
}
Expand Down

0 comments on commit 280b8c0

Please sign in to comment.