@@ -21,16 +21,17 @@ import (
21
21
22
22
// Routes
23
23
const (
24
- TasksRoot = "/tasks"
25
- TasksReportRoot = TasksRoot + "/report"
26
- TasksReportQueueRoot = TasksReportRoot + "/queue"
27
- TaskRoot = TasksRoot + "/:" + ID
28
- TaskReportRoot = TaskRoot + "/report"
29
- TaskAttachedRoot = TaskRoot + "/attached"
30
- TaskBucketRoot = TaskRoot + "/bucket"
31
- TaskBucketContentRoot = TaskBucketRoot + "/*" + Wildcard
32
- TaskSubmitRoot = TaskRoot + "/submit"
33
- TaskCancelRoot = TaskRoot + "/cancel"
24
+ TasksRoot = "/tasks"
25
+ TasksReportRoot = TasksRoot + "/report"
26
+ TasksReportQueueRoot = TasksReportRoot + "/queue"
27
+ TasksReportDashboardRoot = TasksReportRoot + "/dashboard"
28
+ TaskRoot = TasksRoot + "/:" + ID
29
+ TaskReportRoot = TaskRoot + "/report"
30
+ TaskAttachedRoot = TaskRoot + "/attached"
31
+ TaskBucketRoot = TaskRoot + "/bucket"
32
+ TaskBucketContentRoot = TaskBucketRoot + "/*" + Wildcard
33
+ TaskSubmitRoot = TaskRoot + "/submit"
34
+ TaskCancelRoot = TaskRoot + "/cancel"
34
35
)
35
36
36
37
const (
@@ -54,6 +55,7 @@ func (h TaskHandler) AddRoutes(e *gin.Engine) {
54
55
routeGroup .PATCH (TaskRoot , Transaction , h .Update )
55
56
routeGroup .DELETE (TaskRoot , h .Delete )
56
57
routeGroup .GET (TasksReportQueueRoot , h .Queued )
58
+ routeGroup .GET (TasksReportDashboardRoot , h .Dashboard )
57
59
// Actions
58
60
routeGroup .PUT (TaskSubmitRoot , Transaction , h .Submit )
59
61
routeGroup .PUT (TaskCancelRoot , h .Cancel )
@@ -181,6 +183,7 @@ func (h TaskHandler) List(ctx *gin.Context) {
181
183
db := h .DB (ctx )
182
184
db = db .Model (& model.Task {})
183
185
db = db .Joins ("Application" )
186
+ db = db .Joins ("Report" )
184
187
db = sort .Sorted (db )
185
188
db = filter .Where (db )
186
189
var m model.Task
@@ -272,6 +275,83 @@ func (h TaskHandler) Queued(ctx *gin.Context) {
272
275
h .Respond (ctx , http .StatusOK , r )
273
276
}
274
277
278
+ // Dashboard godoc
279
+ // @summary List all task dashboard resources.
280
+ // @description List all task dashboard resources.
281
+ // @description Filters:
282
+ // @description - kind
283
+ // @description - createUser
284
+ // @description - addon
285
+ // @description - name
286
+ // @description - locator
287
+ // @description - state
288
+ // @description - application.id
289
+ // @description - application.name
290
+ // @tags tasks
291
+ // @produce json
292
+ // @success 200 {object} []api.TaskDashboard
293
+ // @router /tasks [get]
294
+ func (h TaskHandler ) Dashboard (ctx * gin.Context ) {
295
+ resources := []TaskDashboard {}
296
+ // filter
297
+ filter , err := qf .New (ctx ,
298
+ []qf.Assert {
299
+ {Field : "id" , Kind : qf .LITERAL },
300
+ {Field : "createUser" , Kind : qf .STRING },
301
+ {Field : "kind" , Kind : qf .STRING },
302
+ {Field : "addon" , Kind : qf .STRING },
303
+ {Field : "name" , Kind : qf .STRING },
304
+ {Field : "locator" , Kind : qf .STRING },
305
+ {Field : "state" , Kind : qf .STRING },
306
+ {Field : "application.id" , Kind : qf .STRING },
307
+ {Field : "application.name" , Kind : qf .STRING },
308
+ })
309
+ if err != nil {
310
+ _ = ctx .Error (err )
311
+ return
312
+ }
313
+ filter = filter .Renamed ("application.id" , "application__id" )
314
+ filter = filter .Renamed ("application.name" , "application__name" )
315
+ filter = filter .Renamed ("createUser" , "task\\ .createUser" )
316
+ filter = filter .Renamed ("id" , "task\\ .id" )
317
+ filter = filter .Renamed ("name" , "task\\ .name" )
318
+ // sort
319
+ sort := Sort {}
320
+ sort .Add ("task.id" , "id" )
321
+ sort .Add ("task.createUser" , "createUser" )
322
+ sort .Add ("task.name" , "name" )
323
+ sort .Add ("application__id" , "application.id" )
324
+ sort .Add ("application__name" , "application.name" )
325
+ err = sort .With (ctx , & model.Task {})
326
+ if err != nil {
327
+ _ = ctx .Error (err )
328
+ return
329
+ }
330
+ // Fetch
331
+ db := h .DB (ctx )
332
+ db = db .Model (& model.Task {})
333
+ db = db .Joins ("Application" )
334
+ db = db .Joins ("Report" )
335
+ db = sort .Sorted (db )
336
+ db = filter .Where (db )
337
+ var list []model.Task
338
+ page := Page {}
339
+ page .With (ctx )
340
+ err = db .Find (& list ).Error
341
+ if err != nil {
342
+ _ = ctx .Error (err )
343
+ return
344
+ }
345
+ for i := range list {
346
+ m := & list [i ]
347
+ r := TaskDashboard {}
348
+ r .With (m )
349
+ resources = append (resources , r )
350
+ }
351
+
352
+ h .Respond (ctx , http .StatusOK , resources )
353
+ }
354
+
275
355
// Create godoc
276
356
// @summary Create a task.
277
357
// @description Create a task.
@@ -496,6 +576,7 @@ func (h TaskHandler) CreateReport(ctx *gin.Context) {
496
576
report := & TaskReport {}
497
577
err := h .Bind (ctx , report )
498
578
if err != nil {
579
+ _ = ctx .Error (err )
499
580
return
500
581
}
501
582
report .TaskID = id
@@ -827,3 +908,30 @@ type TaskQueue struct {
827
908
Pending int `json:"pending"`
828
909
Running int `json:"running"`
829
910
}
911
+
912
+ // TaskDashboard report.
913
+ type TaskDashboard struct {
914
+ Resource `yaml:",inline"`
915
+ Name string `json:"name,omitempty" yaml:",omitempty"`
916
+ Kind string `json:"kind,omitempty" yaml:",omitempty"`
917
+ Addon string `json:"addon,omitempty" yaml:",omitempty"`
918
+ State string `json:"state,omitempty" yaml:",omitempty"`
919
+ Locator string `json:"locator,omitempty" yaml:",omitempty"`
920
+ Application * Ref `json:"application,omitempty" yaml:",omitempty"`
921
+ Started * time.Time `json:"started,omitempty" yaml:",omitempty"`
922
+ Terminated * time.Time `json:"terminated,omitempty" yaml:",omitempty"`
923
+ Errors int `json:"errors,omitempty" yaml:",omitempty"`
924
+ }
925
+
926
+ func (r * TaskDashboard ) With (m * model.Task ) {
927
+ r .Resource .With (& m .Model )
928
+ r .Name = m .Name
929
+ r .Kind = m .Kind
930
+ r .Addon = m .Addon
931
+ r .State = m .State
932
+ r .Locator = m .Locator
933
+ r .Application = r .refPtr (m .ApplicationID , m .Application )
934
+ r .Started = m .Started
935
+ r .Terminated = m .Terminated
936
+ r .Errors = len (m .Errors )
937
+ }
0 commit comments