forked from meshery/meshery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_persister.go
173 lines (140 loc) · 4.61 KB
/
events_persister.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 models
import (
"github.com/gofrs/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/events"
"gorm.io/gorm/clause"
)
// EventsPersister assists with persisting events in local SQLite DB
type EventsPersister struct {
DB *database.Handler
}
// swagger:response EventsResponse
type EventsResponse struct {
Events []*events.Event `json:"events"`
Page int `json:"page"`
PageSize int `json:"page_size"`
CountBySeverityLevel []*CountBySeverityLevel `json:"count_by_severity_level"`
TotalCount int64 `json:"total_count"`
}
type CountBySeverityLevel struct {
Severity string `json:"severity"`
Count int `json:"count"`
}
func (e *EventsPersister) GetEventTypes(userID uuid.UUID) (map[string]interface{}, error) {
eventTypes := make(map[string]interface{}, 2)
var categories, actions []string
err := e.DB.Table("events").Distinct("category").Where("user_id = ?", userID).Find(&categories).Error
if err != nil {
return nil, err
}
eventTypes["category"] = categories
err = e.DB.Table("events").Distinct("action").Where("user_id = ?", userID).Find(&actions).Error
if err != nil {
return nil, err
}
eventTypes["action"] = actions
return eventTypes, err
}
func (e *EventsPersister) GetAllEvents(eventsFilter *events.EventsFilter, userID uuid.UUID) (*EventsResponse, error) {
eventsDB := []*events.Event{}
finder := e.DB.Model(&events.Event{}).Where("user_id = ?", userID)
if len(eventsFilter.Category) != 0 {
finder = finder.Where("category IN ?", eventsFilter.Category)
}
if len(eventsFilter.Action) != 0 {
finder = finder.Where("action IN ?", eventsFilter.Action)
}
if len(eventsFilter.Severity) != 0 {
finder = finder.Where("severity IN ?", eventsFilter.Severity)
}
if eventsFilter.Search != "" {
finder = finder.Where("description LIKE ?", "%"+eventsFilter.Search+"%")
}
if eventsFilter.Status != "" {
finder = finder.Where("status = ?", eventsFilter.Status)
}
if eventsFilter.Order == "asc" {
finder = finder.Order(eventsFilter.SortOn)
} else {
finder = finder.Order(clause.OrderByColumn{Column: clause.Column{Name: eventsFilter.SortOn}, Desc: true})
}
var count int64
finder.Count(&count)
if eventsFilter.Offset != 0 {
finder = finder.Offset(eventsFilter.Offset)
}
if eventsFilter.Limit != 0 {
finder = finder.Limit(eventsFilter.Limit)
}
err := finder.Scan(&eventsDB).Error
if err != nil {
return nil, err
}
countBySeverity, err := e.getCountBySeverity(userID, eventsFilter.Status)
if err != nil {
return nil, err
}
return &EventsResponse{
Events: eventsDB,
PageSize: eventsFilter.Limit,
TotalCount: count,
CountBySeverityLevel: countBySeverity,
}, nil
}
func (e *EventsPersister) UpdateEventStatus(eventID uuid.UUID, status string) (*events.Event, error) {
err := e.DB.Model(&events.Event{ID: eventID, Status: events.EventStatus(status)}).Update("status", status).Error
if err != nil {
return nil, err
}
updatedEvent := &events.Event{}
err = e.DB.Find(updatedEvent, "id = ?", eventID).Error
if err != nil {
return nil, err
}
return updatedEvent, nil
}
func (e *EventsPersister) BulkUpdateEventStatus(eventIDs []*uuid.UUID, status string) ([]*events.Event, error) {
err := e.DB.Model(&events.Event{Status: events.EventStatus(status)}).Where("id IN ?", eventIDs).Update("status", status).Error
if err != nil {
return nil, err
}
updatedEvent := &[]*events.Event{}
err = e.DB.Find(updatedEvent, "id IN ?", eventIDs).Error
if err != nil {
return nil, err
}
return *updatedEvent, nil
}
func (e *EventsPersister) DeleteEvent(eventID uuid.UUID) error {
err := e.DB.Delete(&events.Event{ID: eventID}).Error
if err != nil {
return err
}
return nil
}
func (e *EventsPersister) BulkDeleteEvent(eventIDs []*uuid.UUID) error {
err := e.DB.Where("id IN ?", eventIDs).Delete(&events.Event{}).Error
if err != nil {
return err
}
return nil
}
func (e *EventsPersister) PersistEvent(event *events.Event) error {
err := e.DB.Save(event).Error
if err != nil {
return ErrPersistEvent(err)
}
return nil
}
func (e *EventsPersister) getCountBySeverity(userID uuid.UUID, eventStatus events.EventStatus) ([]*CountBySeverityLevel, error) {
if eventStatus == "" {
eventStatus = events.Unread
}
eventsBySeverity := []*CountBySeverityLevel{}
err := e.DB.Model(&events.Event{}).Select("severity, count(severity) as count").Where("status = ? and user_id = ?", eventStatus, userID).Group("severity").Find(&eventsBySeverity).Error
if err != nil {
return nil, err
}
return eventsBySeverity, nil
}