/
mock.go
173 lines (149 loc) · 3.78 KB
/
mock.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 api
import (
"encoding/json"
"fmt"
"math/rand"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type searchMeta struct {
query string
ExecAt time.Time
}
type MockHandler struct {
mapSearchID map[searchID]*searchMeta
LogTotal int
LogLimit int
}
func NewMockHandler() *MockHandler {
return &MockHandler{
mapSearchID: make(map[searchID]*searchMeta),
}
}
func (x *MockHandler) ExecSearch(c *gin.Context) (*Response, Error) {
id := searchID(uuid.New().String())
x.mapSearchID[id] = &searchMeta{
ExecAt: time.Now(),
}
return &Response{201, &ExecSearchResponse{
SearchID: searchID(id),
}}, nil
}
type logSampleGenerator struct {
seq int
}
func (x *logSampleGenerator) New() *logData {
sampleColor := []string{"blue", "orange", "red"}
x.seq++
return &logData{
Tag: "test.1",
Timestamp: time.Now().Unix(),
Log: map[string]string{
"seq": fmt.Sprintf("%d", x.seq),
"steps": fmt.Sprintf("%d", rand.Intn(64)),
"port": fmt.Sprintf("%d", rand.Intn(65655)),
"color": sampleColor[rand.Intn(len(sampleColor))],
},
}
}
type actionLog struct {
Name string `json:"name"`
Target string `json:"target"`
}
func mustToJSON(v interface{}) string {
raw, err := json.Marshal(v)
if err != nil {
Logger.Fatal("Fail to marshal JSON:", err)
}
return string(raw)
}
func newLogStream(max int) chan *logQueue {
logs := [][]string{
[]string{"test.user", "1580000000", mustToJSON(map[string]interface{}{
"name": "Ao",
"color": "blue",
"rank": 0,
})},
[]string{"test.user", "1580000002", mustToJSON(map[string]interface{}{
"name": "Tou",
"color": "orange",
"rank": 1,
})},
[]string{"test.user", "1580000004", mustToJSON(map[string]interface{}{
"name": "Alice",
"rank": 100,
})},
[]string{"test.user", "1580000005", mustToJSON(map[string]interface{}{
"name": "Barth",
"rank": 1,
})},
[]string{"test.user", "1580000009", mustToJSON(map[string]interface{}{
"name": "Chris",
"rank": 200,
})},
[]string{"test.user", "1580000010", mustToJSON(map[string]interface{}{
"name": "Dymos",
"rank": 300,
})},
[]string{"test.action", "1580000012", mustToJSON(actionLog{
Name: "attack",
Target: "rock",
})},
[]string{"test.action", "1580000012", mustToJSON(actionLog{
Name: "Ao",
Target: "paper",
})},
}
ch := make(chan *logQueue, 1)
go func() {
defer close(ch)
for i := 0; i < max; i++ {
ch <- &logQueue{
Seq: int64(i),
Record: logs[rand.Intn(len(logs))],
}
}
}()
return ch
}
func (x *MockHandler) GetSearchLogs(c *gin.Context) (*Response, Error) {
id := searchID(c.Param("search_id"))
resp := GetSearchLogsResponse{
ID: id,
}
q, ok := x.mapSearchID[id]
if !ok {
Logger.Error("invalid query ID")
return &Response{404, "query not found"}, nil
}
now := time.Now()
resp.MetaData.ElapsedSeconds = float64(now.Sub(q.ExecAt))
resp.MetaData.Status = statusRunning
if resp.MetaData.ElapsedSeconds >= 3 {
filter, apiErr := buildLogFilter(c)
if apiErr != nil {
return nil, apiErr
}
Logger.WithField("filter", filter).Debug("Built filter")
logSet, err := extractLogs(newLogStream(x.LogTotal), *filter)
if err != nil {
return nil, wrapSystemError(err, 500, "Fail to load logs")
}
resp.Logs = logSet.Logs
resp.MetaData.Total = logSet.Total
resp.MetaData.SubTotal = logSet.SubTotal
resp.MetaData.Tags = logSet.Tags
resp.MetaData.Offset = filter.Offset
resp.MetaData.Limit = filter.Limit
resp.MetaData.Status = statusSuccess
Logger.WithField("len(logs)", len(resp.Logs)).WithField("meta", resp.MetaData).Info("response")
}
return &Response{200, &resp}, nil
}
func (x *MockHandler) GetSearchTimeSeries(c *gin.Context) (*Response, Error) {
return nil, nil
}
func (x *MockHandler) GetSearch(c *gin.Context) (*Response, Error) {
return nil, nil
}