/
processor.go
237 lines (221 loc) · 6.79 KB
/
processor.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
package mongo
import (
//"fmt"
"context"
"strconv"
"strings"
"github.com/bmeg/grip/gdbi"
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/log"
"github.com/bmeg/grip/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Processor stores the information for a mongo aggregation pipeline
type Processor struct {
db *Graph
startCollection string
query mongo.Pipeline
dataType gdbi.DataType
markTypes map[string]gdbi.DataType
aggTypes map[string]*gripql.Aggregate
}
func getDataElement(result map[string]interface{}) *gdbi.DataElement {
de := &gdbi.DataElement{}
if x, ok := result["_id"]; ok {
de.ID = x.(string)
}
if x, ok := result["label"]; ok {
de.Label = x.(string)
}
if x, ok := result["data"]; ok {
de.Data = removePrimatives(x).(map[string]interface{})
de.Loaded = true
}
if x, ok := result["to"]; ok {
de.To = x.(string)
}
if x, ok := result["from"]; ok {
de.From = x.(string)
}
return de
}
// Process runs the mongo aggregation pipeline
func (proc *Processor) Process(ctx context.Context, man gdbi.Manager, in gdbi.InPipe, out gdbi.OutPipe) context.Context {
plog := log.WithFields(log.Fields{"query_id": util.UUID(), "query": proc.query, "query_collection": proc.startCollection})
plog.Debug("Running Mongo Processor")
go func() {
defer close(out)
initCol := proc.db.ar.client.Database(proc.db.ar.database).Collection(proc.startCollection)
for t := range in {
nResults := 0
//plog.Infof("Running: %#v", proc.query)
trueVal := true
cursor, err := initCol.Aggregate(ctx, proc.query, &options.AggregateOptions{AllowDiskUse: &trueVal})
if err != nil {
plog.Errorf("Query Error (%s) : %s", proc.query, err)
continue
}
//defer cursor.Close(context.TODO())
result := map[string]interface{}{}
for cursor.Next(ctx) {
nResults++
select {
case <-ctx.Done():
return
default:
}
if nil != cursor.Decode(&result) {
plog.Errorf("Result Error : %s", err)
continue
}
//fmt.Printf("Data: %s\n", result)
switch proc.dataType {
case gdbi.CountData:
eo := &gdbi.BaseTraveler{}
if x, ok := result["count"]; ok {
eo.Count = uint32(x.(int32))
}
out <- eo
case gdbi.SelectionData:
selections := map[string]*gdbi.DataElement{}
if marks, ok := result["marks"]; ok {
if marks, ok := marks.(map[string]interface{}); ok {
for k, v := range marks {
if v, ok := v.(map[string]interface{}); ok {
de := getDataElement(v)
selections[k] = de
}
}
}
}
out <- &gdbi.BaseTraveler{Selections: selections}
case gdbi.AggregationData:
for k, v := range result {
buckets, ok := v.(bson.A)
if !ok {
plog.Errorf("Failed to convert Mongo aggregation result (%s): %+v", k, v)
continue
}
var lastBucket float64
for i, bucket := range buckets {
bucket, ok := bucket.(map[string]interface{})
if !ok {
plog.Errorf("Failed to convert Mongo aggregation result bucket: %+v", bucket)
continue
}
var term interface{}
switch proc.aggTypes[k].GetAggregation().(type) {
case *gripql.Aggregate_Term:
term = bucket["_id"]
case *gripql.Aggregate_Histogram:
term = bucket["_id"]
curPos := bucket["_id"].(float64)
stepSize := float64(proc.aggTypes[k].GetHistogram().Interval)
if i != 0 {
for nv := lastBucket + stepSize; nv < curPos; nv += stepSize {
out <- &gdbi.BaseTraveler{Aggregation: &gdbi.Aggregate{Name: k, Key: nv, Value: float64(0.0)}}
}
}
lastBucket = curPos
case *gripql.Aggregate_Percentile:
bid := strings.Replace(bucket["_id"].(string), "_", ".", -1)
f, err := strconv.ParseFloat(bid, 64)
if err != nil {
plog.Errorf("failed to parse percentile aggregation result key: %v", err)
continue
}
term = f
case *gripql.Aggregate_Field:
term = bucket["_id"]
case *gripql.Aggregate_Count:
term = bucket["_id"]
case *gripql.Aggregate_Type:
switch bucket["_id"] {
case "double":
term = "NUMERIC"
case "null":
term = "UNKNOWN"
case "string":
term = "STRING"
}
default:
plog.Errorf("unknown aggregation result type")
}
//fmt.Printf("term: %s %s", term, count)
switch bucket["count"].(type) {
case int:
count := bucket["count"].(int)
out <- &gdbi.BaseTraveler{Aggregation: &gdbi.Aggregate{Name: k, Key: term, Value: float64(count)}}
case int32:
count := bucket["count"].(int32)
out <- &gdbi.BaseTraveler{Aggregation: &gdbi.Aggregate{Name: k, Key: term, Value: float64(count)}}
case float64:
count := bucket["count"].(float64)
out <- &gdbi.BaseTraveler{Aggregation: &gdbi.Aggregate{Name: k, Key: term, Value: float64(count)}}
default:
plog.Errorf("unexpected aggregation result type: %T", bucket["count"])
continue
}
}
}
default:
if path, ok := result["path"]; ok {
if pathA, ok := path.(bson.A); ok {
o := make([]gdbi.DataElementID, len(pathA))
for i := range pathA {
if elem, ok := pathA[i].(map[string]interface{}); ok {
if v, ok := elem["vertex"]; ok {
o[i] = gdbi.DataElementID{Vertex: removePrimatives(v).(string)}
} else if v, ok := elem["edge"]; ok {
o[i] = gdbi.DataElementID{Edge: removePrimatives(v).(string)}
}
}
}
//t.Path = o
t = &gdbi.BaseTraveler{Path: o}
}
}
if marks, ok := result["marks"]; ok {
if marks, ok := marks.(map[string]interface{}); ok {
for k, v := range marks {
if v, ok := v.(map[string]interface{}); ok {
de := getDataElement(v)
t = t.AddMark(k, de)
}
}
}
}
de := &gdbi.DataElement{}
if x, ok := result["_id"]; ok {
de.ID = removePrimatives(x).(string)
}
if x, ok := result["label"]; ok {
de.Label = x.(string)
}
if x, ok := result["data"]; ok {
de.Data = removePrimatives(x).(map[string]interface{})
de.Loaded = true
}
if x, ok := result["to"]; ok {
de.To = x.(string)
}
if x, ok := result["from"]; ok {
de.From = x.(string)
}
out <- t.AddCurrent(de)
}
}
if err := cursor.Close(context.TODO()); err != nil {
plog.WithFields(log.Fields{"error": err}).Error("MongoDb: iterating results")
continue
}
if nResults == 0 && proc.dataType == gdbi.CountData {
out <- &gdbi.BaseTraveler{Count: 0}
}
}
plog.Debug("Mongo Processor Finished")
}()
return ctx
}