-
Notifications
You must be signed in to change notification settings - Fork 4
/
histogram_sets_read.go
42 lines (37 loc) · 1.27 KB
/
histogram_sets_read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package histogram
import (
"fmt"
"github.com/grokify/mogo/encoding/jsonutil"
"github.com/grokify/mogo/math/mathutil"
"github.com/grokify/mogo/type/stringsutil"
"github.com/grokify/gocharts/v2/data/table"
)
// NewHistogramSetsCSVs expects multiple files to have same columns.
func NewHistogramSetsCSVs(filenames []string, key1ColIdx, key2ColIdx, uidColIdx uint) (*HistogramSets, table.Table, error) {
hsets := NewHistogramSets("")
tbl, err := table.ReadFile(nil, filenames...)
if err != nil {
return hsets, tbl, err
}
hsets, err = NewHistogramSetsTable(tbl, key1ColIdx, key2ColIdx, uidColIdx)
return hsets, tbl, err
}
func NewHistogramSetsTable(tbl table.Table, key1ColIdx, key2ColIdx, uidColIdx uint) (*HistogramSets, error) {
hsets := NewHistogramSets(tbl.Name)
_, maxIdx := mathutil.MinMaxUint(key1ColIdx, key2ColIdx, uidColIdx)
for _, row := range tbl.Rows {
if len(stringsutil.SliceCondenseSpace(row, true, false)) == 0 {
continue
} else if len(row) <= int(maxIdx) {
return hsets, fmt.Errorf(
"NewHistogramSetsTable.E_ROW_LEN_ERROR NEED_ROW_LEN [%v] HAVE_ROW_LEN [%v] ROW_DATA [%s]",
maxIdx+1, len(row),
jsonutil.MustMarshalSimple(row, "", ""))
}
hsets.Add(
row[key1ColIdx],
row[key2ColIdx],
row[uidColIdx], 1, true)
}
return hsets, nil
}