forked from cloudflare/go-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.go
184 lines (157 loc) · 4.61 KB
/
cube.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package pg
import "github.com/cloudflare/go-stream/cube"
import (
"log"
"reflect"
)
func VisitWrapper(wrapper reflect.Value, visitor func(fieldValue reflect.Value, fieldDescription reflect.StructField)) {
for i := 0; i < wrapper.NumField(); i++ {
if wrapper.Field(i).Kind() == reflect.Struct && wrapper.Type().Field(i).Anonymous == true {
VisitDimensions(wrapper.Field(i), visitor)
} else {
visitor(wrapper.Field(i), wrapper.Type().Field(i))
}
}
}
func VisitDimensions(wrapper reflect.Value, visitor func(fieldValue reflect.Value, fieldDescription reflect.StructField)) {
VisitWrapper(wrapper, visitor)
}
func VisitAggregates(wrapper reflect.Value, visitor func(fieldValue reflect.Value, fieldDescription reflect.StructField)) {
VisitWrapper(wrapper, visitor)
}
func AddDimensions(table *Table, rDims reflect.Value) {
i := 0
visitor := func(fieldValue reflect.Value, fieldDescription reflect.StructField) {
col := getDimensionPgTypeVisit(fieldValue, fieldDescription)
if 0 == i {
_, ok := col.(*TimeCol)
if ok {
table.SetTimeCol(col)
} else {
log.Fatal("Expecting the primary time col as first dimension")
}
}
i += 1
table.AddDim(col)
}
VisitDimensions(rDims, visitor)
}
func AddAggregates(table *Table, rAggs reflect.Value) {
visitor := func(fieldValue reflect.Value, fieldDescription reflect.StructField) {
col := getAggregatePgTypeVisit(fieldValue, fieldDescription)
table.AddAgg(col)
}
VisitAggregates(rAggs, visitor)
}
/*
func AddDimensions(table *Table, rDims reflect.Value) {
for i := 0; i < rDims.NumField(); i++ {
if rDims.Field(i).Kind() == reflect.Struct && rDims.Type().Field(i).Anonymous == true {
AddDimensions(table, rDims.Field(i))
} else {
col := getDimensionPgType(i, rDims)
if 0 == i {
_, ok := col.(*TimeCol)
if ok {
table.SetTimeCol(col)
} else {
log.Fatal("Expecting the primary time col as first dimension")
}
}
table.AddDim(col)
}
}
}*/
func MakeTable(name string, cd cube.CubeDescriber) *Table {
rDims := reflect.ValueOf(cd.GetDimensions())
rAggs := reflect.ValueOf(cd.GetAggregates())
table := NewTable(name)
AddDimensions(table, rDims)
AddAggregates(table, rAggs)
return table
}
func getDimensionPgTypeVisit(fieldValue reflect.Value, fieldDescription reflect.StructField) Column {
name := fieldDescription.Name
tagName := fieldDescription.Tag.Get("db")
if tagName != "" {
name = tagName
}
switch dt := fieldValue.Interface().(type) {
default:
log.Fatal("Unknown Dimension type ", reflect.TypeOf(dt), " for field ", name)
case cube.TimeDimension:
return &TimeCol{NewDefaultCol(name)}
case cube.IntDimension:
tn := "INT"
tagtype := fieldDescription.Tag.Get("dbtype")
if tagtype != "" {
tn = tagtype
}
return &IntCol{NewDefaultCol(name), tn}
case cube.StringDimension:
return &StringCol{NewDefaultCol(name)}
}
return nil
}
func getAggregatePgTypeVisit(fieldValue reflect.Value, fieldDescription reflect.StructField) AggregateColumn {
name := fieldDescription.Name
tagName := fieldDescription.Tag.Get("db")
if tagName != "" {
name = tagName
}
var ca *cube.CountAggregate
var hlla *cube.HllAggregate
switch da := fieldValue.Type(); da {
default:
log.Fatal("Unknown Aggregate type", da, "for field", name)
case reflect.TypeOf(ca):
tn := "INT"
tagtype := fieldDescription.Tag.Get("dbtype")
if tagtype != "" {
tn = tagtype
}
return &CountCol{&IntCol{NewDefaultCol(name), tn}}
case reflect.TypeOf(hlla):
tn := "HLL"
tagtype := fieldDescription.Tag.Get("dbtype")
if tagtype != "" {
tn = tagtype
}
return &HllCol{NewDefaultCol(name), tn}
}
return nil
}
/*
func getDimensionPgType(i int, dims reflect.Value) Column {
name := dims.Type().Field(i).Name
tagName := dims.Type().Field(i).Tag.Get("db")
if tagName != "" {
name = tagName
}
switch dt := dims.Field(i).Interface().(type) {
default:
log.Fatal("Unknown Dimension type ", reflect.TypeOf(dt), " for field ", dims.Type().Field(i).Name)
case cube.TimeDimension:
return &TimeCol{NewDefaultCol(name, i)}
case cube.IntDimension:
return &IntCol{NewDefaultCol(name, i)}
case cube.StringDimension:
return &IntCol{NewDefaultCol(name, i)}
}
return nil
}
func getAggregatePgType(index int, aggs reflect.Value) AggregateColumn {
name := aggs.Type().Field(index).Name
tagName := aggs.Type().Field(index).Tag.Get("db")
if tagName != "" {
name = tagName
}
var ca *cube.CountAggregate
switch da := aggs.Field(index).Type(); da {
default:
log.Fatal("Unknown Aggregate type", da, "for field", aggs.Type().Field(index).Name)
case reflect.TypeOf(ca):
return &CountCol{&IntCol{NewDefaultCol(name, index)}}
}
return nil
}*/