-
Notifications
You must be signed in to change notification settings - Fork 330
/
dsl.go
173 lines (144 loc) · 4.08 KB
/
dsl.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
package search
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/didi/sharingan/replayer-agent/idl"
"github.com/didi/sharingan/replayer-agent/model/nuwaplt"
)
type DSL struct {
Size int `json:"size"`
From int `json:"from"`
Query map[string]interface{} `json:"query"`
Source []string `json:"_source,omitempty"`
Sort map[string]interface{} `json:"sort"`
}
func toDSL(req *idl.SearchReq) ([]byte, error) {
dsl := new(DSL)
// page limit: from, size
if req.Page <= 0 {
req.Page = 1
}
dsl.Size = req.Size
if req.Size <= 0 {
dsl.Size = 20
}
dsl.From = (req.Page - 1) * dsl.Size
if len(req.Field) != 0 {
dsl.Source = req.Field
}
dsl.Sort = newCond("CallFromInbound.OccurredAt", "order", "desc")
must := make([]interface{}, 0)
mustNot := make([]interface{}, 0)
// filter out sessions without ReturnInbound
must = append(must, newCond("exists", "field", "ReturnInbound"))
if p, ok := nuwaplt.Module2Host[req.Project]; ok {
var conds []interface{}
for _, s := range strings.Split(p, ",") {
conds = append(conds, newCond("match_phrase", "Context", s))
}
must = append(must, map[string]interface{}{
"bool": map[string]interface{}{
"should": conds,
"minimum_should_match": 1,
},
})
}
if req.InboundRequest != "" {
must, mustNot = unionMulti(must, mustNot, "CallFromInbound.Request", req.InboundRequest, parseCond)
}
if req.InboundResponse != "" {
must, mustNot = unionMulti(must, mustNot, "ReturnInbound.Response", req.InboundResponse, parseCond)
}
if req.OutboundRequest != "" {
must, mustNot = unionMulti(must, mustNot, "Actions.Request", req.OutboundRequest, parseCond)
}
if req.OutboundResponse != "" {
must, mustNot = unionMulti(must, mustNot, "Actions.Response", req.OutboundResponse, parseCond)
}
if req.Apollo != "" {
must, mustNot = unionMulti(must, mustNot, "Actions.Content", req.Apollo, parseApollo)
}
if req.SessionId != "" {
must = append(must, newCond("term", "SessionId", req.SessionId))
}
must = unionTime(must, req.Start, req.End)
dsl.Query = map[string]interface{}{
"bool": map[string]interface{}{
"must": must,
"must_not": mustNot,
},
}
return json.Marshal(dsl)
}
func unionMulti(must []interface{}, mustNot []interface{}, field string, words string,
parseFunc func(field string, word string) map[string]interface{}) ([]interface{}, []interface{}) {
for _, word := range strings.Fields(words) {
word = strings.ToLower(word)
if word[0] == '!' {
if len(word) > 1 {
mustNot = append(mustNot, parseFunc(field, word[1:]))
}
continue
}
must = append(must, parseFunc(field, word))
}
return must, mustNot
}
func unionTime(must []interface{}, start, end string) []interface{} {
if start == "" && end == "" {
return must
}
timeRange := map[string]int64{}
if start != "" {
st, _ := parseTime(start)
timeRange["gte"] = st.UnixNano()
}
if end != "" {
ed, hasTime := parseTime(end)
if !hasTime {
ed = ed.Add(24 * time.Hour)
}
timeRange["lt"] = ed.UnixNano()
}
must = append(must, map[string]interface{}{
"range": map[string]interface{}{
"CallFromInbound.OccurredAt": timeRange,
},
})
return must
}
func parseTime(str string) (time.Time, bool) {
hasTime := true
if strings.Index(str, "T") < 0 {
str += "T00:00:00"
hasTime = false
}
layout := "2006-01-02T15:04:05"
t, err := time.ParseInLocation(layout, str, time.Local)
if err != nil {
return time.Now(), hasTime
}
return t, hasTime
}
func parseCond(field string, word string) map[string]interface{} {
if strings.ContainsAny(word, "?*") {
return newCond("wildcard", field, word)
}
return newCond("match_phrase", field, word)
}
func parseApollo(field string, word string) map[string]interface{} {
sp := strings.Split(word, "=")
if len(sp) == 2 {
return newCond("match_phrase", field, fmt.Sprintf("%s %s %s", sp[0], sp[1], sp[0]))
}
return newCond("match_phrase", field, word)
}
func newCond(keyword, field, word string) map[string]interface{} {
return map[string]interface{}{
keyword: map[string]interface{}{
field: word,
},
}
}