/
inference_data.go
250 lines (202 loc) · 5.89 KB
/
inference_data.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/go-playground/validator/v10"
)
type User struct {
ID string `json:"id"`
Metadata map[string]json.RawMessage `json:"metadata"`
}
type SourceConf struct {
Name string `json:"name"`
Source string `json:"source"`
Config json.RawMessage `json:"config"`
}
type InferenceDataEvent struct {
User User `json:"user"`
Study string `json:"study"`
SourceConf *SourceConf `json:"source_conf"`
Timestamp time.Time `json:"timestamp"`
Variable string `json:"variable"`
Value json.RawMessage `json:"value"`
}
type InferenceDataValue struct {
Timestamp time.Time `json:"timestamp"`
Variable string `json:"variable"`
Value json.RawMessage `json:"value"`
ValueType string `json:"value_type"`
}
type InferenceDataRow struct {
User string `json:"user"`
Data map[string]*InferenceDataValue `json:"data"`
}
type InferenceData map[string]*InferenceDataRow
// TODO: validate not empty fields...?
type ExtractionConf struct {
Name string `json:"name"`
Function string `json:"function"`
Params json.RawMessage `json:"params"`
Type string `json:"type"`
Aggregate string `json:"aggregate"` // first, last, max, min
fn func(json.RawMessage) ([]byte, error)
}
type InferenceDataSource struct {
VariableExtractionMapping map[string]*ExtractionConf `json:"variable_extraction"` // source variable name -> extraction conf
MetadataExtractionMapping map[string]*ExtractionConf `json:"metadata_extraction"` // source variable name -> extraction conf
}
type InferenceDataConf struct {
DataSources map[string]*InferenceDataSource `json:"data_sources"`
}
func (c InferenceDataConf) Sources() []string {
m := c.DataSources
i := 0
keys := make([]string, len(m))
for k := range m {
keys[i] = k
i++
}
return keys
}
func (conf *ExtractionConf) Extract(dat json.RawMessage) ([]byte, error) {
if conf.fn == nil {
var p ExtractionFunctionParams
switch conf.Function {
case "select":
p = new(SelectFunctionParams)
}
err := json.Unmarshal(conf.Params, p)
if err != nil {
return nil, fmt.Errorf(
"Could not parse function params for function %s. Param json: %s. Parsing error: %s.",
conf.Function,
string(conf.Params),
err)
}
v := validator.New()
err = v.Struct(p)
if err != nil {
return nil, err
}
conf.fn = p.GetValue
}
return conf.fn(dat)
}
// TODO: this casting is pretty silly...
// We were happily transparently flowing raw json through as value
// until all the sudden we needed to compare...
func getNumericValues(oldVal, val *InferenceDataValue) (float64, float64, error) {
o, err := CastContinuous(oldVal.Value)
if err != nil {
return 0, 0, err
}
n, err := CastContinuous(val.Value)
if err != nil {
return o, 0, err
}
return o, n, nil
}
func addValue(conf *ExtractionConf, id InferenceData, user string, val *InferenceDataValue) (InferenceData, error) {
_, ok := id[user]
if !ok {
id[user] = &InferenceDataRow{user, make(map[string]*InferenceDataValue)}
}
oldVal, ok := id[user].Data[val.Variable]
if !ok {
id[user].Data[val.Variable] = val
return id, nil
}
if bytes.Equal(oldVal.Value, val.Value) {
return id, nil
}
switch conf.Aggregate {
case "last":
if oldVal.Timestamp.Before(val.Timestamp) {
id[user].Data[val.Variable] = val
}
return id, nil
case "first":
if oldVal.Timestamp.After(val.Timestamp) {
id[user].Data[val.Variable] = val
}
return id, nil
case "max":
// TODO: this casting is pretty silly...
o, n, err := getNumericValues(oldVal, val)
if err != nil {
return id, err
}
if o < n {
id[user].Data[val.Variable] = val
}
return id, nil
case "min":
o, n, err := getNumericValues(oldVal, val)
if err != nil {
return id, err
}
if o > n {
id[user].Data[val.Variable] = val
}
return id, nil
default:
return id, fmt.Errorf("Could not find match for aggregate function: %s", conf.Aggregate)
}
}
func extractValue(id InferenceData, e *InferenceDataEvent, extractionConfs map[string]*ExtractionConf) (InferenceData, error) {
// Now deal with the main value from the event
conf, ok := extractionConfs[e.Variable]
if !ok {
return nil, nil
}
val, err := conf.Extract(e.Value)
if err != nil {
return nil, err
}
v := &InferenceDataValue{e.Timestamp, conf.Name, val, conf.Type}
return addValue(conf, id, e.User.ID, v)
}
func extractMetadata(id InferenceData, e *InferenceDataEvent, extractionConfs map[string]*ExtractionConf) (InferenceData, error) {
// Every event might have user metadata of interest, so we need to look at all of it
for k, conf := range extractionConfs {
val, ok := e.User.Metadata[k]
if !ok {
continue
}
v := &InferenceDataValue{e.Timestamp, conf.Name, val, conf.Type}
id, err := addValue(conf, id, e.User.ID, v)
if err != nil {
return id, err
}
}
return id, nil
}
func Reduce(events []*InferenceDataEvent, c *InferenceDataConf) (InferenceData, error) {
id := make(InferenceData)
for _, e := range events {
sourceConf, ok := c.DataSources[e.SourceConf.Name]
if !ok {
return nil, fmt.Errorf("Attempted to process event from data source not in SourceVariableMapping. "+
"Data source: %s. Sources in mapping: %s",
e.SourceConf.Name,
c.Sources())
}
// attempt to extract the values from the user metadata, according to config
// TODO: should this have a nil check? Preferably not!
// add from metadata
id, err := extractMetadata(id, e, sourceConf.MetadataExtractionMapping)
if err != nil {
return id, err
}
// attempt to extract the values from the event itself, according to config
id, err = extractValue(id, e, sourceConf.VariableExtractionMapping)
if err != nil {
bb, _ := json.Marshal(e)
fmt.Println(string(bb))
return id, err
}
}
return id, nil
}