diff --git a/src/common/dao/admin_job.go b/src/common/dao/admin_job.go index 59c2a1d3dc..305a2feca3 100644 --- a/src/common/dao/admin_job.go +++ b/src/common/dao/admin_job.go @@ -90,7 +90,7 @@ func SetAdminJobUUID(id int64, uuid string) error { // GetTop10AdminJobs ... func GetTop10AdminJobs() ([]*models.AdminJob, error) { sql := `select * from admin_job - where deleted = false order by update_time limit 10` + where deleted = false order by update_time desc limit 10` jobs := []*models.AdminJob{} _, err := GetOrmer().Raw(sql).QueryRows(&jobs) return jobs, err diff --git a/src/ui/api/models/reg_gc.go b/src/ui/api/models/reg_gc.go index b2d1051f67..f50d3577ff 100644 --- a/src/ui/api/models/reg_gc.go +++ b/src/ui/api/models/reg_gc.go @@ -24,6 +24,7 @@ import ( "github.com/goharbor/harbor/src/common/utils" "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/ui/config" + "time" ) const ( @@ -50,9 +51,22 @@ type ScheduleParam struct { // Daily, Weekly, Manual, None Type string `json:"type"` // Optional, only used when type is 'weekly' - Weekday int8 `json:"Weekday"` + Weekday int8 `json:"weekday"` // The time offset with the UTC 00:00 in seconds - Offtime int64 `json:"Offtime"` + Offtime int64 `json:"offtime"` +} + +// GCRep holds the response of query gc +type GCRep struct { + ID int64 `json:"id"` + Name string `json:"job_name"` + Kind string `json:"job_kind"` + Schedule *ScheduleParam `json:"schedule"` + Status string `json:"job_status"` + UUID string `json:"-"` + Deleted bool `json:"deleted"` + CreationTime time.Time `json:"creation_time"` + UpdateTime time.Time `json:"update_time"` } // Valid validates the gc request diff --git a/src/ui/api/reg_gc.go b/src/ui/api/reg_gc.go index 052d98c43b..4d323fb9a4 100644 --- a/src/ui/api/reg_gc.go +++ b/src/ui/api/reg_gc.go @@ -20,6 +20,7 @@ import ( "os" "strconv" + "encoding/json" "github.com/goharbor/harbor/src/common/dao" common_http "github.com/goharbor/harbor/src/common/http" common_job "github.com/goharbor/harbor/src/common/job" @@ -109,11 +110,22 @@ func (gc *GCAPI) GetGC() { jobs, err := dao.GetAdminJobs(&common_models.AdminJobQuery{ ID: id, }) + + gcreps := []*models.GCRep{} + for _, job := range jobs { + gcrep, err := convertToGCRep(job) + if err != nil { + gc.HandleInternalServerError(fmt.Sprintf("failed to convert gc response: %v", err)) + return + } + gcreps = append(gcreps, &gcrep) + } + if err != nil { gc.HandleInternalServerError(fmt.Sprintf("failed to get admin jobs: %v", err)) return } - gc.Data["json"] = jobs + gc.Data["json"] = gcreps gc.ServeJSON() } @@ -124,7 +136,16 @@ func (gc *GCAPI) List() { gc.HandleInternalServerError(fmt.Sprintf("failed to get admin jobs: %v", err)) return } - gc.Data["json"] = jobs + gcreps := []*models.GCRep{} + for _, job := range jobs { + gcrep, err := convertToGCRep(job) + if err != nil { + gc.HandleInternalServerError(fmt.Sprintf("failed to convert gc response: %v", err)) + return + } + gcreps = append(gcreps, &gcrep) + } + gc.Data["json"] = gcreps gc.ServeJSON() } @@ -142,7 +163,16 @@ func (gc *GCAPI) Get() { gc.HandleInternalServerError("Get more than one GC scheduled job, make sure there has only one.") return } - gc.Data["json"] = jobs + gcreps := []*models.GCRep{} + for _, job := range jobs { + gcrep, err := convertToGCRep(job) + if err != nil { + gc.HandleInternalServerError(fmt.Sprintf("failed to convert gc response: %v", err)) + return + } + gcreps = append(gcreps, &gcrep) + } + gc.Data["json"] = gcreps gc.ServeJSON() } @@ -230,3 +260,27 @@ func (gc *GCAPI) submitJob(gr *models.GCReq) { return } } + +func convertToGCRep(job *common_models.AdminJob) (models.GCRep, error) { + if job == nil { + return models.GCRep{}, nil + } + + gcrep := models.GCRep{ + ID: job.ID, + Name: job.Name, + Kind: job.Kind, + Status: job.Status, + Deleted: job.Deleted, + CreationTime: job.CreationTime, + UpdateTime: job.UpdateTime, + } + if len(job.Cron) > 0 { + schedule := &models.ScheduleParam{} + if err := json.Unmarshal([]byte(job.Cron), &schedule); err != nil { + return models.GCRep{}, err + } + gcrep.Schedule = schedule + } + return gcrep, nil +} diff --git a/src/ui/api/reg_gc_test.go b/src/ui/api/reg_gc_test.go index 97b1d74982..da3b51b549 100644 --- a/src/ui/api/reg_gc_test.go +++ b/src/ui/api/reg_gc_test.go @@ -3,6 +3,8 @@ package api import ( "testing" + common_models "github.com/goharbor/harbor/src/common/models" + api_modes "github.com/goharbor/harbor/src/ui/api/models" "github.com/goharbor/harbor/tests/apitests/apilib" "github.com/stretchr/testify/assert" ) @@ -37,3 +39,42 @@ func TestAdminJobGet(t *testing.T) { assert.Equal(200, code, "Get adminjob status should be 200") } } + +func TestConvertToGCRep(t *testing.T) { + cases := []struct { + input *common_models.AdminJob + expected api_modes.GCRep + }{ + { + input: nil, + expected: api_modes.GCRep{}, + }, + { + input: &common_models.AdminJob{ + ID: 1, + Name: "IMAGE_GC", + Kind: "Generic", + Cron: "{\"Type\":\"Manual\",\"Weekday\":0,\"Offtime\":0}", + Status: "pending", + Deleted: false, + }, + expected: api_modes.GCRep{ + ID: 1, + Name: "IMAGE_GC", + Kind: "Generic", + Schedule: &api_modes.ScheduleParam{ + Type: "Manual", + Weekday: 0, + Offtime: 0, + }, + Status: "pending", + Deleted: false, + }, + }, + } + + for _, c := range cases { + actual, _ := convertToGCRep(c.input) + assert.EqualValues(t, c.expected, actual) + } +}