Skip to content

Commit

Permalink
v.0.48 pivot+null
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolainp committed Sep 21, 2024
1 parent 313b1e3 commit ea4a82d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 21 deletions.
26 changes: 9 additions & 17 deletions datarecord/datarecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ type dataReader struct {
isColumnNamesInFirstRow bool

columns dataColumns
points int
data dataReaderData
data dataReaderData
}

type dataRecord struct {
Expand Down Expand Up @@ -121,9 +120,6 @@ func (obj *dataReader) ReadDataRecord(data string) {
record := obj.getDataRecord(data)

obj.columns.addDataRecord(record)
if obj.points < len(record.points) {
obj.points = len(record.points)
}

_, ok := obj.data[record.dateTime]
if !ok {
Expand All @@ -145,27 +141,23 @@ func (obj *dataReader) GetDataRows() []string {
buffer := new(bytes.Buffer)
writer := bufio.NewWriter(buffer)

for i := 0; i < obj.points; i++ {
writer.WriteString(", null")
}
writer.Flush()
blankPoints := buffer.String()
buffer.Reset()

for dateTime, data := range obj.data {

writer.WriteString("[")
writer.WriteString(fmt.Sprintf("new Date(%s)", dateTime.Format("2006, 01, 02, 15, 04, 05")))

for _, columnName := range columns {
points, ok := data[columnName]

if ok {
if points, ok := data[columnName]; ok {
for i := range points {
writer.WriteString(points[i].string())
}
} else {
writer.WriteString(blankPoints)
continue
}
if points, ok := obj.columns.statistic[columnName]; ok {
for range points {
writer.WriteString(", null")
}
continue
}
}

Expand Down
71 changes: 67 additions & 4 deletions datarecord/datarecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package datarecord

import (
"reflect"
"sort"
"testing"
"time"

Expand Down Expand Up @@ -77,8 +78,7 @@ func Test_dataReader_ReadDataRecord(t *testing.T) {
pivotColumn: 0,
delimiter: []byte{' '},

points: 3,
data: dataReaderData{time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local): {"": []dataPoint{{point: 1}, {point: 2}, {point: 3}}}},
data: dataReaderData{time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local): {"": []dataPoint{{point: 1}, {point: 2}, {point: 3}}}},
},
"20121015100130 1 2 3",
},
Expand Down Expand Up @@ -186,12 +186,12 @@ func Test_dataColumns_getColumnStatistics(t *testing.T) {
},
{
"test 4",
dataColumns{names: []string{"A","B","C"}, statistic: map[string][]columnStatistic{"": {{1, 1, 1, 1}, {2, 2, 2, 1}, {3, 3, 3, 1}}}},
dataColumns{names: []string{"A", "B", "C"}, statistic: map[string][]columnStatistic{"": {{1, 1, 1, 1}, {2, 2, 2, 1}, {3, 3, 3, 1}}}},
[]ColumnStatistic{{"A", 1, 1, 1}, {"B", 2, 2, 2}, {"C", 3, 3, 3}},
},
{
"test 5",
dataColumns{names: []string{"A","B","C"}, statistic: map[string][]columnStatistic{"first": {{1, 1, 1, 1}, {2, 2, 2, 1}, {3, 3, 3, 1}}}},
dataColumns{names: []string{"A", "B", "C"}, statistic: map[string][]columnStatistic{"first": {{1, 1, 1, 1}, {2, 2, 2, 1}, {3, 3, 3, 1}}}},
[]ColumnStatistic{{"first A", 1, 1, 1}, {"first B", 2, 2, 2}, {"first C", 3, 3, 3}},
},
}
Expand All @@ -203,3 +203,66 @@ func Test_dataColumns_getColumnStatistics(t *testing.T) {
})
}
}

func Test_dataReader_GetDataRows(t *testing.T) {
tests := []struct {
name string
obj dataReader
want []string
}{
{"test 1", dataReader{
dateColumn: 1,
dateFormat: "20060102150405",
pivotColumn: 0,
delimiter: []byte{' '},

data: dataReaderData{
time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local): {
"": []dataPoint{{point: 1}, {point: 2}, {point: 3}}}},
},
[]string{"[new Date(2012, 10, 15, 10, 01, 30), 1, 2, 3]"},
},
{"test 2", dataReader{
dateColumn: 1,
dateFormat: "20060102150405",
pivotColumn: 0,
delimiter: []byte{' '},

columns: dataColumns{statistic: map[string][]columnStatistic{"a1": {}}},
data: dataReaderData{
time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local): {
"a1": []dataPoint{{point: 1}, {point: 2}, {point: 3}}}},
},
[]string{"[new Date(2012, 10, 15, 10, 01, 30), 1, 2, 3]"},
},
{"test 3", dataReader{
dateColumn: 1,
dateFormat: "20060102150405",
pivotColumn: 0,
delimiter: []byte{' '},

columns: dataColumns{statistic: map[string][]columnStatistic{
"a1": {{}, {}},
"b2": {{}, {}, {}}}},
data: dataReaderData{
time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local): {
"a1": []dataPoint{{point: 1}, {point: 2}}},
time.Date(2012, time.October, 15, 10, 1, 31, 0, time.Local): {
"b2": []dataPoint{{point: 4}, {point: 5}, {point: 6}}},
},
},
[]string{
"[new Date(2012, 10, 15, 10, 01, 30), 1, 2, null, null, null]",
"[new Date(2012, 10, 15, 10, 01, 31), null, null, 4, 5, 6]"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.obj.GetDataRows()
sort.Slice(got, func(i, j int) bool { return got[i] < got[j] })
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("dataReader.GetDataRows() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit ea4a82d

Please sign in to comment.