Skip to content

Commit

Permalink
v.0.47 null data
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolainp committed Aug 9, 2024
1 parent 1400218 commit 313b1e3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
72 changes: 54 additions & 18 deletions datarecord/datarecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (

type ColumnStatistic struct {
Name string
Minimum float32
Maximum float32
Average float32
Minimum float64
Maximum float64
Average float64
}

type dataReaderData map[time.Time]map[string][]float32
type dataReaderData map[time.Time]map[string][]dataPoint

type columnStatistic struct {
minimum float32
maximum float32
sum float32
minimum float64
maximum float64
sum float64
count int
}

Expand All @@ -47,7 +47,12 @@ type dataReader struct {
type dataRecord struct {
dateTime time.Time
pivot string
points []float32
points []dataPoint
}

type dataPoint struct {
point float64
isNull bool
}

var checkErr = func(err error) {
Expand Down Expand Up @@ -122,7 +127,7 @@ func (obj *dataReader) ReadDataRecord(data string) {

_, ok := obj.data[record.dateTime]
if !ok {
obj.data[record.dateTime] = make(map[string][]float32)
obj.data[record.dateTime] = make(map[string][]dataPoint)
}
obj.data[record.dateTime][record.pivot] = record.points
}
Expand Down Expand Up @@ -157,7 +162,7 @@ func (obj *dataReader) GetDataRows() []string {

if ok {
for i := range points {
writer.WriteString(fmt.Sprintf(", %g", points[i]))
writer.WriteString(points[i].string())
}
} else {
writer.WriteString(blankPoints)
Expand Down Expand Up @@ -190,9 +195,8 @@ func (obj *dataColumns) addDataRecord(data dataRecord) {

element := obj.statistic[data.pivot]
for i, dataPoint := range data.points {
element[i].addDataPoint(dataPoint)
element[i].addDataPoint(dataPoint.float64())
}

}

func (obj *dataColumns) getPivotColumnNames() []string {
Expand All @@ -218,7 +222,7 @@ func (obj *dataColumns) getColumnStatistics() []ColumnStatistic {
Name: name,
Minimum: data.minimum,
Maximum: data.maximum,
Average: data.sum / float32(data.count),
Average: data.sum / float64(data.count),
}
}

Expand Down Expand Up @@ -254,7 +258,7 @@ func (obj *dataColumns) getColumnStatistics() []ColumnStatistic {
///////////////////////////////////////////////////////
// columnStatistic

func (obj *columnStatistic) addDataPoint(data float32) {
func (obj *columnStatistic) addDataPoint(data float64) {
if obj.count == 0 {
obj.minimum = data
obj.maximum = data
Expand All @@ -280,7 +284,7 @@ func (obj *dataReader) getDataRecord(data string) (record dataRecord) {
}
scan.Split(onDelimiter) //bufio.ScanWords

record.points = make([]float32, 0, 5)
record.points = make([]dataPoint, 0, 5)

for column := 1; scan.Scan(); column++ {
word := scan.Text()
Expand All @@ -299,10 +303,42 @@ func (obj *dataReader) getDataRecord(data string) (record dataRecord) {
continue
}

s, err := strconv.ParseFloat(word, 32)
checkErr(err)
record.points = append(record.points, float32(s))
record.points = append(record.points, newDataPoint(word))
}

return
}

///////////////////////////////////////////////////////
// datePoint

func newDataPoint(data string) (point dataPoint) {
point.setValue(data)
return
}

func (obj *dataPoint) setValue(data string) {

if len(strings.TrimSpace(data)) == 0 {
obj.isNull = true
return
}

s, err := strconv.ParseFloat(data, 32)
checkErr(err)
obj.point = s
}

func (obj *dataPoint) string() string {
if obj.isNull {
return ", null"
}
return fmt.Sprintf(", %g", obj.point)
}

func (obj *dataPoint) float64() float64 {
if obj.isNull {
return 0
}
return obj.point
}
16 changes: 8 additions & 8 deletions datarecord/datarecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func TestGetDataRecord(t *testing.T) {
want dataRecord
}{
{"test 1", args{"20121015100100 1 2 3"},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "", []float32{1, 2, 3}}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "", []dataPoint{{point: 1}, {point: 2}, {point: 3}}}},
{"test 2", args{"20121015100130 2 3 4"},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local), "", []float32{2, 3, 4}}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local), "", []dataPoint{{point: 2}, {point: 3}, {point: 4}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -52,9 +52,9 @@ func TestGetDataRecordPivot(t *testing.T) {
want dataRecord
}{
{"test 1", args{"20121015100100 first 1 2 3"},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "first", []float32{1, 2, 3}}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "first", []dataPoint{{point: 1}, {point: 2}, {point: 3}}}},
{"test 2", args{"20121015100130 second 2 3 4"},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local), "second", []float32{2, 3, 4}}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local), "second", []dataPoint{{point: 2}, {point: 3}, {point: 4}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -78,7 +78,7 @@ func Test_dataReader_ReadDataRecord(t *testing.T) {
delimiter: []byte{' '},

points: 3,
data: dataReaderData{time.Date(2012, time.October, 15, 10, 1, 30, 0, time.Local): {"": []float32{1, 2, 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 @@ -112,19 +112,19 @@ func Test_dataColumns_addDataRecord(t *testing.T) {
{
"test 1",
dataColumns{names: []string{}, statistic: map[string][]columnStatistic{}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "first", []float32{1, 2, 3}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "first", []dataPoint{{point: 1}, {point: 2}, {point: 3}}},
dataColumns{names: []string{}, statistic: map[string][]columnStatistic{"first": {{1, 1, 1, 1}, {2, 2, 2, 1}, {3, 3, 3, 1}}}},
},
{
"test 2",
dataColumns{names: []string{}, statistic: map[string][]columnStatistic{}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "", []float32{1, 2, 3}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "", []dataPoint{{point: 1}, {point: 2}, {point: 3}}},
dataColumns{names: []string{}, statistic: map[string][]columnStatistic{"": {{1, 1, 1, 1}, {2, 2, 2, 1}, {3, 3, 3, 1}}}},
},
{
"test 3",
dataColumns{names: []string{}, statistic: map[string][]columnStatistic{"first": {{1, 1, 1, 1}, {2, 2, 2, 1}, {3, 3, 3, 1}}}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "first", []float32{10, -2, 3}},
dataRecord{time.Date(2012, time.October, 15, 10, 1, 0, 0, time.Local), "first", []dataPoint{{point: 10}, {point: -2}, {point: 3}}},
dataColumns{names: []string{}, statistic: map[string][]columnStatistic{"first": {{1, 10, 11, 2}, {-2, 2, 0, 2}, {3, 3, 6, 2}}}},
},
}
Expand Down

0 comments on commit 313b1e3

Please sign in to comment.