-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathevaluation.go
More file actions
272 lines (253 loc) · 8.18 KB
/
Copy pathevaluation.go
File metadata and controls
272 lines (253 loc) · 8.18 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
package handler
import (
"errors"
"fmt"
"log/slog"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"opencsg.com/csghub-server/api/httpbase"
"opencsg.com/csghub-server/common/config"
"opencsg.com/csghub-server/common/errorx"
"opencsg.com/csghub-server/common/types"
"opencsg.com/csghub-server/component"
)
func NewEvaluationHandler(config *config.Config) (*EvaluationHandler, error) {
wkf, err := component.NewEvaluationComponent(config)
if err != nil {
return nil, err
}
sc, err := component.NewSensitiveComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
return &EvaluationHandler{
evaluation: wkf,
sensitive: sc,
}, nil
}
type EvaluationHandler struct {
evaluation component.EvaluationComponent
sensitive component.SensitiveComponent
}
// create evaluation godoc
// @Security ApiKey
// @Summary run model evaluation
// @Tags Evaluation
// @Accept json
// @Produce json
// @Param namespace path string true "namespace"
// @Param name path string true "name"
// @Param body body types.EvaluationReq true "body setting of evaluation"
// @Success 200 {object} string "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /evaluations [post]
func (h *EvaluationHandler) RunEvaluation(ctx *gin.Context) {
currentUser := httpbase.GetCurrentUser(ctx)
var req types.EvaluationReq
if err := ctx.ShouldBindJSON(&req); err != nil {
slog.ErrorContext(ctx.Request.Context(), "Bad request format", "error", err)
httpbase.BadRequest(ctx, err.Error())
return
}
_, err := h.sensitive.CheckRequestV2(ctx.Request.Context(), &req)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "failed to check sensitive request", slog.Any("error", err))
httpbase.BadRequestWithExt(ctx, errorx.ErrSensitiveInfoNotAllowed)
return
}
req.Username = currentUser
if req.OwnerNamespace == "" {
req.OwnerNamespace = currentUser
}
evaluation, err := h.evaluation.CreateEvaluation(ctx.Request.Context(), req)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "Failed to create evaluation job", slog.Any("error", err))
httpbase.ServerError(ctx, err)
return
}
httpbase.OK(ctx, evaluation)
}
// get evaluation godoc
// @Security ApiKey
// @Summary get model evaluation
// @Tags Evaluation
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {object} types.EvaluationRes "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /evaluations/{id} [get]
func (h *EvaluationHandler) GetEvaluation(ctx *gin.Context) {
currentUser := httpbase.GetCurrentUser(ctx)
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "Bad request format", "error", err)
httpbase.BadRequest(ctx, err.Error())
return
}
var req = &types.EvaluationGetReq{}
req.ID = id
req.Username = currentUser
evaluation, err := h.evaluation.GetEvaluation(ctx.Request.Context(), *req)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "Failed to get evaluation job", slog.Any("error", err))
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
return
}
httpbase.ServerError(ctx, err)
return
}
httpbase.OK(ctx, evaluation)
}
func (h *EvaluationHandler) GetEvaluationByTaskID(ctx *gin.Context) {
currentUser := httpbase.GetCurrentUser(ctx)
taskID := ctx.Param("task_id")
if taskID == "" {
httpbase.BadRequest(ctx, "task_id is required")
return
}
var req = &types.EvaluationGetReq{}
req.TaskID = taskID
req.Username = currentUser
evaluation, err := h.evaluation.GetEvaluation(ctx.Request.Context(), *req)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "Failed to get evaluation job by task id", slog.Any("error", err))
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
return
}
httpbase.ServerError(ctx, err)
return
}
httpbase.OK(ctx, evaluation)
}
// deleteEvaluation godoc
// @Security ApiKey
// @Summary delete model evaluation
// @Tags Evaluation
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {object} string "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /evaluations/{id} [delete]
func (h *EvaluationHandler) DeleteEvaluation(ctx *gin.Context) {
currentUser := httpbase.GetCurrentUser(ctx)
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "Bad request format", "error", err)
httpbase.BadRequest(ctx, err.Error())
return
}
var req = &types.EvaluationDelReq{}
req.ID = id
req.Username = currentUser
err = h.evaluation.DeleteEvaluation(ctx.Request.Context(), *req)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "Failed to delete evaluation job", slog.Any("error", err))
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
return
}
httpbase.ServerError(ctx, err)
return
}
httpbase.OK(ctx, nil)
}
// GetEvaluationLogs godoc
// @Security ApiKey
// @Summary get evaluation job logs
// @Tags Evaluation
// @Accept json
// @Produce json
// @Param id path string true "evaluation job id or task id"
// @Param since query string false "since time. Optional values: 10mins, 30mins, 1hour, 6hours, 1day, 2days, 1week"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /evaluations/{id}/logs [get]
func (h *EvaluationHandler) GetLogs(ctx *gin.Context) {
since := ctx.Query("since")
currentUser := httpbase.GetCurrentUser(ctx)
stream := ctx.Query("stream")
idStr := ctx.Param("id")
if len(idStr) < 1 {
httpbase.BadRequest(ctx, "id is required")
return
}
req := types.EvaluationLogReq{
CurrentUser: currentUser,
Since: since,
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
req.TaskID = idStr
} else {
req.ID = id
}
if strings.Trim(stream, " ") == "true" {
h.readLogInStream(ctx, req)
return
}
h.readLogNonStream(ctx, req)
}
func (h *EvaluationHandler) readLogNonStream(ctx *gin.Context, req types.EvaluationLogReq) {
logs, err := h.evaluation.ReadJobLogsNonStream(ctx.Request.Context(), req)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "failed to get evaluation job non-stream logs", slog.Any("error", err), slog.Any("req", req))
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
return
}
httpbase.ServerError(ctx, err)
return
}
httpbase.OK(ctx, logs)
}
func (h *EvaluationHandler) readLogInStream(ctx *gin.Context, req types.EvaluationLogReq) {
logReader, err := h.evaluation.ReadJobLogsInStream(ctx.Request.Context(), req)
if err != nil {
slog.ErrorContext(ctx.Request.Context(), "failed to get evaluation job in-stream logs", slog.Any("error", err), slog.Any("req", req))
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
return
}
httpbase.ServerError(ctx, err)
return
}
if logReader.RunLog() == nil {
httpbase.ServerError(ctx, errors.New("don't find any evaluation job log"))
return
}
ctx.Writer.Header().Set("Content-Type", "text/event-stream")
ctx.Writer.Header().Set("Cache-Control", "no-cache")
ctx.Writer.Header().Set("Connection", "keep-alive")
ctx.Writer.Header().Set("Transfer-Encoding", "chunked")
ctx.Writer.WriteHeader(http.StatusOK)
ctx.Writer.Flush()
heartbeatTicker := time.NewTicker(30 * time.Second)
defer heartbeatTicker.Stop()
for {
select {
case <-ctx.Request.Context().Done():
return
case data, ok := <-logReader.RunLog():
if !ok {
return
}
ctx.SSEvent("Container", string(data))
ctx.Writer.Flush()
case <-heartbeatTicker.C:
ctx.SSEvent("Heartbeat", "keep-alive")
ctx.Writer.Flush()
default:
time.Sleep(time.Second)
}
}
}