/
showlog.go
215 lines (180 loc) · 6.06 KB
/
showlog.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
package showlog
import (
"context"
"encoding/json"
"fmt"
"net/http"
"kong-logs-metrics/config"
"kong-logs-metrics/controller/common"
"kong-logs-metrics/model"
"github.com/gin-gonic/gin"
elastic "gopkg.in/olivere/elastic.v5"
)
//ShowLogs 展示日志
func ShowLogs(c *gin.Context) {
userInter, _ := c.Get("user")
user := userInter.(model.User)
page := new(model.Page)
SendErrJSON := common.SendErrJSON
query := elastic.NewBoolQuery().Must(elastic.NewMatchAllQuery())
ctx := context.Background()
if err := c.ShouldBindJSON(&page); err == nil {
if ok, _ := common.ES.IndexExists(page.DateValue).Do(ctx); ok {
if page.PageNumber >= 0 && page.PageSize > 0 {
page.Appid = user.AppID
if user.Name == "admin" {
AuthQuery(ctx, query, c, page)
} else {
phraseQuery := elastic.NewBoolQuery().Must(elastic.NewMatchPhraseQuery("request.headers.appid", page.Appid).Slop(0).Boost(1)).DisableCoord(false).AdjustPureNegative(true).Boost(1)
AuthQuery(ctx, phraseQuery, c, page)
}
}
} else {
SendErrJSON("当前日期没有数据", c)
}
} else {
fmt.Println(err.Error())
SendErrJSON("error", c)
return
}
}
//AuthQuery 根据不同的账号查询
func AuthQuery(ctx context.Context, query elastic.Query, c *gin.Context, page *model.Page) {
SendErrJSON := common.SendErrJSON
logs := new(model.Logs)
searchResult, err := common.ES.Search().Index(page.DateValue).Type(config.Conf.ElasticSearch.LogStashType).Query(query).From(page.PageNumber).Size(page.PageSize).Do(ctx)
if err != nil {
SendErrJSON("ES查询错误", c)
return
}
buf, err := json.Marshal(searchResult)
if err != nil {
SendErrJSON("error", c)
return
}
errCode := json.Unmarshal(buf, &logs)
if errCode != nil {
}
hits := logs.Hits
c.JSON(http.StatusOK, gin.H{
"errNo": model.ErrorCode.SUCCESS,
"msg": "success",
"data": hits,
})
}
//FindLogDetailByID 通过索引的ID 查找某个日志的详情
func FindLogDetailByID(c *gin.Context) {
id := new(model.ID)
SendErrJSON := common.SendErrJSON
if err := c.ShouldBindJSON(&id); err == nil {
if id.ID != "" && id.IndexName != "" {
query := elastic.NewIdsQuery().Ids(id.ID)
fmt.Println(query.Source())
ctx := context.Background()
searchResult, err := common.ES.Search().Index(id.IndexName).Type(config.Conf.ElasticSearch.LogStashType).Query(query).Do(ctx)
if err != nil {
SendErrJSON("error", c)
return
}
c.JSON(http.StatusOK, gin.H{
"errNo": model.ErrorCode.SUCCESS,
"msg": "success",
"data": searchResult.Hits,
})
} else {
SendErrJSON("无效的ID或者索引名称", c)
}
} else {
fmt.Println(err.Error())
SendErrJSON("error", c)
return
}
}
//FindLogByAPINameAndDate FindLogByAPINameAndDate
func FindLogByAPINameAndDate(c *gin.Context) {
api := new(model.API)
logs := new(model.Logs)
// userInter, _ := c.Get("user")
// user := userInter.(model.User)
SendErrJSON := common.SendErrJSON
ctx := context.Background()
if err := c.ShouldBindJSON(&api); err == nil {
if api.Name != "" && api.Data != "" {
res, _ := common.ES.IndexExists(api.Data).Do(ctx)
if res {
var searchResult interface{}
var err error
if api.Appid != "" {
boolQueryMatch := elastic.NewBoolQuery().Must(elastic.NewMatchPhraseQuery("request.headers.appid", api.Appid).Slop(0).Boost(1), elastic.NewMatchPhraseQuery("request.uri", api.Name).Slop(0).Boost(1)).DisableCoord(false).AdjustPureNegative(true).Boost(1)
boolQueryWrap := elastic.NewBoolQuery().Must(boolQueryMatch).DisableCoord(false).AdjustPureNegative(true).Boost(1)
filterQueryWrap := elastic.NewBoolQuery().Filter(boolQueryWrap).DisableCoord(false).AdjustPureNegative(true).Boost(1)
searchResult, err = common.ES.Search().Index(api.Data).Type(config.Conf.ElasticSearch.LogStashType).Query(filterQueryWrap).From(api.PageNumber).Size(api.PageSize).Do(ctx)
fmt.Println(searchResult)
fmt.Println(err)
} else {
boolQuery := elastic.NewBoolQuery().Must(elastic.NewMatchPhraseQuery("request.uri", api.Name).Slop(0).Boost(1)).DisableCoord(false).AdjustPureNegative(true).Boost(1)
macth := elastic.NewBoolQuery().Filter(boolQuery).DisableCoord(false).AdjustPureNegative(true).Boost(1)
searchResult, err = common.ES.Search().Index(api.Data).Type(config.Conf.ElasticSearch.LogStashType).Query(macth).From(api.PageNumber).Size(api.PageSize).Do(ctx)
}
if err != nil {
SendErrJSON("error", c)
return
}
buf, err := json.Marshal(searchResult)
if err != nil {
SendErrJSON("error", c)
return
}
errCode := json.Unmarshal(buf, &logs)
if errCode != nil {
}
c.JSON(http.StatusOK, gin.H{
"errNo": model.ErrorCode.SUCCESS,
"msg": "success",
"data": logs.Hits,
})
} else {
c.JSON(http.StatusOK, gin.H{
"errNo": model.ErrorCode.ERROR,
"msg": "当前日期没有数据,请选择其他日期",
"data": logs.Hits,
})
}
} else {
res, _ := common.ES.IndexExists(api.Data).Do(ctx)
if res {
boolQuery := elastic.NewBoolQuery().Must(elastic.NewMatchPhraseQuery("request.headers.appid", api.Appid).Slop(0).Boost(1)).DisableCoord(false).AdjustPureNegative(true).Boost(1)
macth := elastic.NewBoolQuery().Filter(boolQuery).DisableCoord(false).AdjustPureNegative(true).Boost(1)
searchResult, err := common.ES.Search().Index(api.Data).Type(config.Conf.ElasticSearch.LogStashType).Query(macth).From(api.PageNumber).Size(api.PageSize).Do(ctx)
if err != nil {
SendErrJSON("error", c)
return
}
buf, err := json.Marshal(searchResult)
if err != nil {
SendErrJSON("error", c)
return
}
errCode := json.Unmarshal(buf, &logs)
if errCode != nil {
SendErrJSON("error", c)
}
c.JSON(http.StatusOK, gin.H{
"errNo": model.ErrorCode.SUCCESS,
"msg": "success",
"data": logs.Hits,
})
} else {
c.JSON(http.StatusOK, gin.H{
"errNo": model.ErrorCode.ERROR,
"msg": "当前日期没有数据,请选择其他日期",
"data": logs.Hits,
})
}
}
} else {
fmt.Println(err.Error())
SendErrJSON("error", c)
return
}
}