/
event_query_action.go
228 lines (207 loc) · 7.17 KB
/
event_query_action.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
package falconPortal
import (
"fmt"
"time"
"strings"
"github.com/Cepave/open-falcon-backend/modules/fe/g"
"github.com/Cepave/open-falcon-backend/modules/fe/model/uic"
"github.com/astaxie/beego/orm"
log "github.com/sirupsen/logrus"
)
//generate status filter SQL templete
func genStatusQueryTemplete(status string, feildsName string) (filterTemplete string) {
statusList := strings.Split(status, ",")
var filterTempleteArr []string
for _, s := range statusList {
filterTempleteArr = append(filterTempleteArr, fmt.Sprintf("%s = '%s'", feildsName, s))
}
filterTemplete = strings.Join(filterTempleteArr, " OR ")
filterTemplete = fmt.Sprintf("( %s )", filterTemplete)
return
}
func genSqlFilterTemplete(whereConditions []string) string {
if len(whereConditions) == 0 {
return ""
}
conditions := strings.Join(whereConditions, " AND ")
return fmt.Sprintf("WHERE %s", conditions)
}
const SkipFilter = "ALL"
func GetEventCases(includeEvents bool, startTime int64, endTime int64, priority string, status string, progressStatus string, limit int, elimit int, username string, metrics string, caseId string) (result []EventCases, err error) {
config := g.Config()
q := orm.NewOrm()
q.Using("falcon_portal")
var whereConditions []string
if limit == 0 || limit > config.FalconPortal.Limit {
limit = config.FalconPortal.Limit
}
isadmin, tplids, err := GetCasePermission(username)
if tplids == "" {
tplids = "-1"
}
//fot generate sql filter
if startTime != 0 {
if endTime == 0 {
endTime = time.Now().Unix()
}
whereConditions = append(whereConditions, fmt.Sprintf("update_at BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", startTime, endTime))
}
if priority != "ALL" {
whereConditions = append(whereConditions, fmt.Sprintf("priority IN (%s) ", priority))
}
if status != SkipFilter {
log.Debug("statis ", status)
whereConditions = append(whereConditions, genStatusQueryTemplete(status, "status"))
}
if progressStatus != SkipFilter {
whereConditions = append(whereConditions, genStatusQueryTemplete(progressStatus, "process_status"))
}
if metrics != SkipFilter {
whereConditions = append(whereConditions, genStatusQueryTemplete(metrics, "metric"))
}
if caseId != "" {
whereConditions = append(whereConditions, fmt.Sprintf("id = '%s'", caseId))
}
//perpare ssql statement
if !isadmin {
whereConditions = append(whereConditions, fmt.Sprintf("(tpl_creator = '%s' OR template_id in (%s))", username, tplids))
}
_, err = q.Raw(fmt.Sprintf("SELECT * FROM `event_cases` %s ORDER BY update_at DESC limit %d", genSqlFilterTemplete(whereConditions), limit)).QueryRows(&result)
if len(result) == 0 {
result = []EventCases{}
return
}
if includeEvents {
//set default number of event
var eventLimit int
if eventLimit = elimit; elimit == 0 {
eventLimit = 10
}
for indx, event := range result {
var eventArr []*Events
q.Raw(fmt.Sprintf("SELECT * FROM `events` WHERE event_caseId = '%s' order by timestamp DESC Limit %d", event.Id, eventLimit)).QueryRows(&eventArr)
if len(eventArr) != 0 {
event.Events = eventArr
} else {
event.Events = []*Events{}
}
result[indx] = event
}
}
return
}
func GetEvents(startTime int64, endTime int64, status string, limit int, caseId string) (result []EventsRsp, err error) {
config := g.Config()
q := orm.NewOrm()
q.Using("falcon_portal")
var whereConditions []string
if status != SkipFilter {
if status == "OK" {
whereConditions = append(whereConditions, fmt.Sprintf("events.status = %d", 1))
} else if status == "PROBLEM" {
whereConditions = append(whereConditions, fmt.Sprintf("events.status = %d", 0))
}
}
if startTime != 0 && endTime != 0 {
whereConditions = append(whereConditions, fmt.Sprintf("events.timestamp BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", startTime, endTime))
}
if caseId != "" {
whereConditions = append(whereConditions, fmt.Sprintf("events.event_caseId = '%s'", caseId))
}
if limit == 0 {
limit = config.FalconPortal.Limit
}
_, err = q.Raw(fmt.Sprintf(`SELECT events.id as id,
events.step as step,
events.cond as cond,
events.timestamp as timestamp,
events.event_caseId as eid,
events.status as status,
event_cases.tpl_creator as tpl_creator,
event_cases.metric as metric,
event_cases.endpoint as endpoint
FROM events LEFT JOIN event_cases on event_cases.id = events.event_caseId
%s ORDER BY events.timestamp DESC limit %d`, genSqlFilterTemplete(whereConditions), limit)).QueryRows(&result)
if len(result) == 0 {
result = []EventsRsp{}
}
return
}
func GetAlarmTypeById(id int) AlarmType {
q := orm.NewOrm()
q.Using("falcon_portal")
aType := AlarmType{}
q.Raw("select * from alarm_types where id = ?", id).QueryRow(&aType)
return aType
}
func CountNumOfTlp() (c int, err error) {
var h []Tpl
q := orm.NewOrm()
q.Using("falcon_portal")
_, err = q.Raw("select * from `tpl`").QueryRows(&h)
c = len(h)
return
}
func GetNotes(eventCaseId string, limit int, startTime int64, endTime int64, filterIgnored bool) (enotes []EventNote, err error) {
q := orm.NewOrm()
q.Using("falcon_portal")
whereConditions := []string{}
if eventCaseId != "" {
whereConditions = append(whereConditions, fmt.Sprintf("event_note.event_caseId = '%s' ", eventCaseId))
}
switch {
//allow api only set the startTime and use the currentTime as the endTime
case startTime != 0 && endTime == 0:
endTime = time.Now().Unix()
case startTime != 0 && endTime != 0:
tempTime := ""
q.Raw("SELECT timestamp FROM event_cases WHERE id = ?", eventCaseId).QueryRow(&tempTime)
if tempTime != "" {
myzone, _ := time.Now().Zone()
parsedTime, err := time.Parse("2006-01-02 15:04:05 MST", fmt.Sprintf("%s %s", tempTime, myzone))
log.Debugf("got time: %v , convertedTime: %v, Unix: %v", fmt.Sprintf("%s %s", tempTime, myzone), parsedTime, parsedTime.Unix())
if err == nil {
startTime = parsedTime.Unix()
} else {
log.Debug(err.Error())
}
}
endTime = time.Now().Unix()
}
if startTime > 0 && endTime > 0 {
whereConditions = append(whereConditions, fmt.Sprintf("event_note.timestamp BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", startTime, endTime))
}
if filterIgnored {
whereConditions = append(whereConditions, "event_note.status != 'ignored' ")
}
limitTemplete := ""
if limit != 0 {
limitTemplete = fmt.Sprintf("LIMIT %d", limit)
}
sqlTemplete := fmt.Sprintf(`SELECT event_note.id as id,
event_note.event_caseId as event_caseId,
event_note.note as note,
event_note.case_id as case_id,
event_note.event_caseId as eid,
event_note.status as status,
event_note.timestamp as timestamp,
user.name as user_name
FROM falcon_portal.event_note as event_note LEFT JOIN uic.user as user on event_note.user_id = user.id
%s ORDER BY event_note.timestamp DESC %s`, genSqlFilterTemplete(whereConditions), limitTemplete)
_, err = q.Raw(sqlTemplete).QueryRows(&enotes)
if len(enotes) == 0 {
enotes = []EventNote{}
}
return
}
func GetNote(noteId int64) (EventNote, error) {
q := orm.NewOrm()
q.Using("falcon_portal")
var eventNote EventNote
err := q.Raw(`SELECT * from event_note WHERE event_note.id = ?`, noteId).QueryRow(&eventNote)
if err == nil {
user := uic.ReadUserById(eventNote.UserId)
eventNote.UserName = user.Name
}
return eventNote, err
}