-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootcauses_handlers.go
244 lines (218 loc) · 7.48 KB
/
rootcauses_handlers.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package handlers
import (
"encoding/json"
"net/http"
"sort"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/myrteametrics/myrtea-engine-api/v4/internals/explainer/rootcause"
"github.com/myrteametrics/myrtea-engine-api/v4/internals/handlers/render"
"github.com/myrteametrics/myrtea-engine-api/v4/internals/models"
"go.uber.org/zap"
)
// GetRootCauses godoc
// @Summary Get all rootcause definitions
// @Description Get all rootcause definitions
// @Tags RootCauses
// @Produce json
// @Security Bearer
// @Success 200 {array} models.RootCause "list of rootcauses"
// @Failure 500 "internal server error"
// @Router /engine/rootcauses [get]
func GetRootCauses(w http.ResponseWriter, r *http.Request) {
rootcauses, err := rootcause.R().GetAll()
if err != nil {
zap.L().Error("Error getting rootcauses", zap.Error(err))
render.Error(w, r, render.ErrAPIDBSelectFailed, err)
return
}
rootcausesSlice := make([]models.RootCause, 0)
for _, rootcause := range rootcauses {
rootcausesSlice = append(rootcausesSlice, rootcause)
}
sort.SliceStable(rootcausesSlice, func(i, j int) bool {
return rootcausesSlice[i].ID < rootcausesSlice[j].ID
})
render.JSON(w, r, rootcausesSlice)
}
// GetRootCause godoc
// @Summary Get a rootcause definition
// @Description Get a rootcause definition
// @Tags RootCauses
// @Produce json
// @Param id path string true "RootCause ID"
// @Security Bearer
// @Success 200 {object} models.RootCause "rootcause"
// @Failure 400 "Status Bad Request"
// @Router /engine/rootcauses/{id} [get]
func GetRootCause(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
idRootCause, err := strconv.ParseInt(id, 10, 64)
if err != nil {
zap.L().Warn("Error on parsing rootcause id", zap.String("rootcauseID", id), zap.Error(err))
render.Error(w, r, render.ErrAPIParsingInteger, err)
return
}
rc, found, err := rootcause.R().Get(idRootCause)
if err != nil {
zap.L().Error("Get Rootcause by ID", zap.Int64("rootcauseid", idRootCause), zap.Error(err))
render.Error(w, r, render.ErrAPIDBSelectFailed, err)
return
}
if !found {
zap.L().Error("RootCause does not exists", zap.Int64("rootcauseid", idRootCause))
render.Error(w, r, render.ErrAPIDBResourceNotFound, err)
return
}
render.JSON(w, r, rc)
}
// ValidateRootCause godoc
// @Summary Validate a new rootcause definition
// @Description Validate a new rootcause definition
// @Tags RootCauses
// @Accept json
// @Produce json
// @Param rootcause body models.RootCause true "RootCause definition (json)"
// @Security Bearer
// @Success 200 {object} models.RootCause "rootcause"
// @Failure 400 "Status Bad Request"
// @Failure 500 "Status" internal server error"
// @Router /engine/rootcauses/validate [post]
func ValidateRootCause(w http.ResponseWriter, r *http.Request) {
var newRootCause models.RootCause
err := json.NewDecoder(r.Body).Decode(&newRootCause)
if err != nil {
zap.L().Warn("Rootcause json decoding", zap.Error(err))
render.Error(w, r, render.ErrAPIDecodeJSONBody, err)
return
}
if ok, err := newRootCause.IsValid(); !ok {
zap.L().Warn("Rootcause is not valid", zap.Error(err))
render.Error(w, r, render.ErrAPIResourceInvalid, err)
return
}
render.JSON(w, r, newRootCause)
}
// PostRootCause godoc
// @Summary Create a new rootcause definition
// @Description Create a new rootcause definition
// @Tags RootCauses
// @Accept json
// @Produce json
// @Param rootcause body models.RootCause true "RootCause definition (json)"
// @Security Bearer
// @Success 200 {object} models.RootCause "rootcause"
// @Failure 400 "Status Bad Request"
// @Failure 500 "Status" internal server error"
// @Router /engine/rootcauses [post]
func PostRootCause(w http.ResponseWriter, r *http.Request) {
var newRootCause models.RootCause
err := json.NewDecoder(r.Body).Decode(&newRootCause)
if err != nil {
zap.L().Warn("Rootcause json decoding", zap.Error(err))
render.Error(w, r, render.ErrAPIDecodeJSONBody, err)
return
}
if ok, err := newRootCause.IsValid(); !ok {
zap.L().Warn("Rootcause is not valid", zap.Error(err))
render.Error(w, r, render.ErrAPIResourceInvalid, err)
return
}
idRootCause, err := rootcause.R().Create(nil, newRootCause)
if err != nil {
zap.L().Error("Error while creating the RootCause", zap.Error(err))
render.Error(w, r, render.ErrAPIDBInsertFailed, err)
return
}
rc, found, err := rootcause.R().Get(idRootCause)
if err != nil {
zap.L().Error("Get Rootcause by ID", zap.Int64("rootcauseid", idRootCause), zap.Error(err))
render.Error(w, r, render.ErrAPIDBSelectFailed, err)
return
}
if !found {
zap.L().Error("RootCause does not exists", zap.Int64("rootcauseid", idRootCause))
render.Error(w, r, render.ErrAPIDBResourceNotFoundAfterInsert, err)
return
}
render.JSON(w, r, rc)
}
// PutRootCause godoc
// @Summary Create or remplace a rootcause definition
// @Description Create or remplace a rootcause definition
// @Tags RootCauses
// @Accept json
// @Produce json
// @Param id path string true "RootCause ID"
// @Param rootcause body models.RootCause true "RootCause definition (json)"
// @Security Bearer
// @Success 200 {object} models.RootCause "rootcause"
// @Failure 400 "Status Bad Request"
// @Failure 500 "Status" internal server error"
// @Router /engine/rootcauses/{id} [put]
func PutRootCause(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
idRootCause, err := strconv.ParseInt(id, 10, 64)
if err != nil {
zap.L().Warn("Error on parsing rootcause id", zap.String("rootcauseID", id), zap.Error(err))
render.Error(w, r, render.ErrAPIParsingInteger, err)
return
}
var newRootCause models.RootCause
err = json.NewDecoder(r.Body).Decode(&newRootCause)
if err != nil {
zap.L().Warn("Rootcause json decoding", zap.Error(err))
render.Error(w, r, render.ErrAPIDecodeJSONBody, err)
return
}
newRootCause.ID = idRootCause
if ok, err := newRootCause.IsValid(); !ok {
zap.L().Warn("Rootcause is not valid", zap.Error(err))
render.Error(w, r, render.ErrAPIResourceInvalid, err)
return
}
err = rootcause.R().Update(nil, idRootCause, newRootCause)
if err != nil {
zap.L().Error("Error while updating the RootCause", zap.Int64("idRootCause", idRootCause), zap.Error(err))
render.Error(w, r, render.ErrAPIDBUpdateFailed, err)
return
}
rc, found, err := rootcause.R().Get(idRootCause)
if err != nil {
zap.L().Error("Get Rootcause by ID", zap.Int64("rootcauseid", idRootCause), zap.Error(err))
render.Error(w, r, render.ErrAPIDBSelectFailed, err)
return
}
if !found {
zap.L().Error("RootCause does not exists", zap.Int64("rootcauseid", idRootCause))
render.Error(w, r, render.ErrAPIDBResourceNotFoundAfterInsert, err)
return
}
render.JSON(w, r, rc)
}
// DeleteRootCause godoc
// @Summary Delete a rootcause definition
// @Description Delete a rootcause definition
// @Tags RootCauses
// @Produce json
// @Param id path string true "RootCause ID"
// @Security Bearer
// @Success 200 "Status OK"
// @Failure 400 "Status Bad Request"
// @Router /engine/rootcauses/{id} [delete]
func DeleteRootCause(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
idRootCause, err := strconv.ParseInt(id, 10, 64)
if err != nil {
zap.L().Warn("Error on parsing rootcause id", zap.String("rootcauseID", id), zap.Error(err))
render.Error(w, r, render.ErrAPIParsingInteger, err)
return
}
err = rootcause.R().Delete(nil, idRootCause)
if err != nil {
zap.L().Error("Error while deleting the RootCause", zap.Int64("RootCause ID", idRootCause), zap.Error(err))
render.Error(w, r, render.ErrAPIDBDeleteFailed, err)
return
}
render.OK(w, r)
}