From ea4a82d9a96347c25144bc07b87522331a075ef2 Mon Sep 17 00:00:00 2001 From: NN Date: Sat, 21 Sep 2024 11:51:55 +0300 Subject: [PATCH] v.0.48 pivot+null --- datarecord/datarecord.go | 26 +++++-------- datarecord/datarecord_test.go | 71 +++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/datarecord/datarecord.go b/datarecord/datarecord.go index f911661..dedaf44 100644 --- a/datarecord/datarecord.go +++ b/datarecord/datarecord.go @@ -40,8 +40,7 @@ type dataReader struct { isColumnNamesInFirstRow bool columns dataColumns - points int - data dataReaderData + data dataReaderData } type dataRecord struct { @@ -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 { @@ -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 } } diff --git a/datarecord/datarecord_test.go b/datarecord/datarecord_test.go index 453aebe..94f7daf 100644 --- a/datarecord/datarecord_test.go +++ b/datarecord/datarecord_test.go @@ -2,6 +2,7 @@ package datarecord import ( "reflect" + "sort" "testing" "time" @@ -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", }, @@ -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}}, }, } @@ -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) + } + }) + } +}