/
model_training.go
259 lines (216 loc) · 8.21 KB
/
model_training.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// Copyright 2019 EPAM Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package training
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/legion-platform/legion/legion/operator/pkg/apis/training"
"github.com/legion-platform/legion/legion/operator/pkg/repository/kubernetes"
mt_repository "github.com/legion-platform/legion/legion/operator/pkg/repository/training"
"github.com/legion-platform/legion/legion/operator/pkg/webserver/routes"
"net/http"
"reflect"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"strconv"
)
var logMT = logf.Log.WithName("training-controller")
const (
GetModelTrainingURL = "/model/training/:id"
GetAllModelTrainingURL = "/model/training"
GetModelTrainingLogsURL = "/model/training/:id/log"
CreateModelTrainingURL = "/model/training"
UpdateModelTrainingURL = "/model/training"
DeleteModelTrainingURL = "/model/training/:id"
IDMtURLParam = "id"
FollowURLParam = "follow"
)
var (
fieldsCache = map[string]int{}
)
func init() {
elem := reflect.TypeOf(&mt_repository.MTFilter{}).Elem()
for i := 0; i < elem.NumField(); i++ {
tagName := elem.Field(i).Tag.Get(mt_repository.TagKey)
fieldsCache[tagName] = i
}
}
type ModelTrainingController struct {
mtRepository mt_repository.Repository
validator *MtValidator
}
// @Summary Get a Model Training
// @Description Get a Model Training by id
// @Tags Training
// @Name id
// @Accept json
// @Produce json
// @Param id path string true "Model Training id"
// @Success 200 {object} training.ModelTraining
// @Failure 404 {object} routes.HTTPResult
// @Failure 400 {object} routes.HTTPResult
// @Router /api/v1/model/training/{id} [get]
func (mtc *ModelTrainingController) getMT(c *gin.Context) {
mtID := c.Param(IDMtURLParam)
mt, err := mtc.mtRepository.GetModelTraining(mtID)
if err != nil {
logMT.Error(err, fmt.Sprintf("Retrieving of %s model training", mtID))
c.AbortWithStatusJSON(routes.CalculateHTTPStatusCode(err), routes.HTTPResult{Message: err.Error()})
return
}
c.JSON(http.StatusOK, mt)
}
// @Summary Get list of Model Trainings
// @Description Get list of Model Trainings
// @Tags Training
// @Accept json
// @Produce json
// @Param size path int false "Number of entities in a response"
// @Param page path int false "Number of a page"
// @Param model_name path int false "Model name"
// @Param model_version path int false "Model version"
// @Param toolchain path int false "Toolchain name"
// @Success 200 {array} training.ModelTraining
// @Failure 400 {object} routes.HTTPResult
// @Router /api/v1/model/training [get]
func (mtc *ModelTrainingController) getAllMTs(c *gin.Context) {
filter := &mt_repository.MTFilter{}
size, page, err := routes.URLParamsToFilter(c, filter, fieldsCache)
if err != nil {
logMT.Error(err, "Malformed url parameters of model training request")
c.AbortWithStatusJSON(http.StatusBadRequest, routes.HTTPResult{Message: err.Error()})
return
}
mtList, err := mtc.mtRepository.GetModelTrainingList(
kubernetes.ListFilter(filter),
kubernetes.Size(size),
kubernetes.Page(page),
)
if err != nil {
logMT.Error(err, "Retrieving list of model trainings")
c.AbortWithStatusJSON(routes.CalculateHTTPStatusCode(err), routes.HTTPResult{Message: err.Error()})
return
}
c.JSON(http.StatusOK, &mtList)
}
// @Summary Create a Model Training
// @Description Create a Model Training. Results is created Model Training.
// @Param mt body training.ModelTraining true "Create a Model Training"
// @Tags Training
// @Accept json
// @Produce json
// @Success 201 {object} training.ModelTraining
// @Failure 400 {object} routes.HTTPResult
// @Router /api/v1/model/training [post]
func (mtc *ModelTrainingController) createMT(c *gin.Context) {
var mt training.ModelTraining
if err := c.ShouldBindJSON(&mt); err != nil {
logMT.Error(err, "JSON binding of the model training is failed")
c.AbortWithStatusJSON(http.StatusBadRequest, routes.HTTPResult{Message: err.Error()})
return
}
if err := mtc.validator.ValidatesAndSetDefaults(&mt); err != nil {
logMT.Error(err, fmt.Sprintf("Validation of the model training is failed: %v", mt))
c.AbortWithStatusJSON(http.StatusBadRequest, routes.HTTPResult{Message: err.Error()})
return
}
if err := mtc.mtRepository.CreateModelTraining(&mt); err != nil {
logMT.Error(err, fmt.Sprintf("Creation of the model training: %v", mt))
c.AbortWithStatusJSON(routes.CalculateHTTPStatusCode(err), routes.HTTPResult{Message: err.Error()})
return
}
c.JSON(http.StatusCreated, mt)
}
// @Summary Update a Model Training
// @Description Update a Model Training. Results is updated Model Training.
// @Param mt body training.ModelTraining true "Update a Model Training"
// @Tags Training
// @Accept json
// @Produce json
// @Success 200 {object} training.ModelTraining
// @Failure 404 {object} routes.HTTPResult
// @Failure 400 {object} routes.HTTPResult
// @Router /api/v1/model/training [put]
func (mtc *ModelTrainingController) updateMT(c *gin.Context) {
var mt training.ModelTraining
if err := c.ShouldBindJSON(&mt); err != nil {
logMT.Error(err, fmt.Sprintf("JSON binding of the model training is failed: %v", mt))
c.AbortWithStatusJSON(http.StatusBadRequest, routes.HTTPResult{Message: err.Error()})
return
}
if err := mtc.validator.ValidatesAndSetDefaults(&mt); err != nil {
logMT.Error(err, fmt.Sprintf("Creation of the model training: %v", mt))
c.AbortWithStatusJSON(http.StatusBadRequest, routes.HTTPResult{Message: err.Error()})
return
}
if err := mtc.mtRepository.UpdateModelTraining(&mt); err != nil {
logMT.Error(err, fmt.Sprintf("Creation of the model training: %v", mt))
c.AbortWithStatusJSON(routes.CalculateHTTPStatusCode(err), routes.HTTPResult{Message: err.Error()})
return
}
c.JSON(http.StatusOK, mt)
}
// @Summary Get a Model Training
// @Description Get a Model Training by id
// @Tags Training
// @Name id
// @Accept json
// @Produce json
// @Param id path string true "Model Training id"
// @Success 200 {object} routes.HTTPResult
// @Failure 404 {object} routes.HTTPResult
// @Failure 400 {object} routes.HTTPResult
// @Router /api/v1/model/training/{id} [delete]
func (mtc *ModelTrainingController) deleteMT(c *gin.Context) {
mtID := c.Param(IDMtURLParam)
if err := mtc.mtRepository.DeleteModelTraining(mtID); err != nil {
logMT.Error(err, fmt.Sprintf("Deletion of %s model training is failed", mtID))
c.AbortWithStatusJSON(routes.CalculateHTTPStatusCode(err), routes.HTTPResult{Message: err.Error()})
return
}
c.JSON(http.StatusOK, routes.HTTPResult{Message: fmt.Sprintf("Model training %s was deleted", mtID)})
}
// @Summary Stream logs from model training pod
// @Description Stream logs from model training pod
// @Tags Training
// @Name id
// @Produce plain
// @Accept plain
// @Param follow query bool false "follow logs"
// @Param id path string true "Model Training id"
// @Success 200 {string} string
// @Failure 400 {string} string
// @Router /api/v1/model/training/{id}/log [get]
func (mtc *ModelTrainingController) getModelTrainingLog(c *gin.Context) {
mtID := c.Param(IDMtURLParam)
follow := false
var err error
urlParameters := c.Request.URL.Query()
followParam := urlParameters.Get(FollowURLParam)
if len(followParam) != 0 {
follow, err = strconv.ParseBool(followParam)
if err != nil {
errMessage := fmt.Sprintf("Convert %s to bool", followParam)
logMT.Error(err, errMessage)
c.AbortWithStatusJSON(http.StatusBadRequest, routes.HTTPResult{Message: errMessage})
return
}
}
if err := mtc.mtRepository.GetModelTrainingLogs(mtID, c.Writer, follow); err != nil {
logMT.Error(err, fmt.Sprintf("Getting %s model training logs is failed", mtID))
c.AbortWithStatusJSON(routes.CalculateHTTPStatusCode(err), routes.HTTPResult{Message: err.Error()})
return
}
}