From a1d8c01ceac826d57a27ff7644eaff5f2aa36689 Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Thu, 7 Nov 2019 13:04:40 +0800 Subject: [PATCH] add status data in the scan all metrics Signed-off-by: Steven Zou Signed-off-by: Steven Zou --- src/core/api/admin_job.go | 24 +++++++----------------- src/core/api/scan_all.go | 23 ++++++++++++++++++++--- src/pkg/scan/all/stats.go | 1 + 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/core/api/admin_job.go b/src/core/api/admin_job.go index 8856566ae..e982a5c09 100644 --- a/src/core/api/admin_job.go +++ b/src/core/api/admin_job.go @@ -249,22 +249,13 @@ func (aj *AJAPI) submit(ajr *models.AdminJobReq) { // Only needs to care the 1st generic job. // Check if there are still ongoing scan jobs triggered by the previous admin job. // TODO: REPLACE WITH TASK MANAGER METHODS IN FUTURE - query := &common_models.AdminJobQuery{ - Name: ajr.Name, - Kind: common_job.JobKindGeneric, - } - query.Size = 1 - query.Page = 1 - - ajbs, err := dao.GetAdminJobs(query) + jb, err := aj.getLatestAdminJob(ajr.Name, common_job.JobKindGeneric) if err != nil { aj.SendInternalServerError(errors.Wrap(err, "AJAPI")) return } - if len(ajbs) > 0 { - jb := ajbs[0] - + if jb != nil { // With a reasonable timeout duration if jb.UpdateTime.Add(2 * time.Hour).After(time.Now()) { if isOnGoing(jb.Status) { @@ -284,7 +275,6 @@ func (aj *AJAPI) submit(ajr *models.AdminJobReq) { if stats.Total != stats.Completed { // Not all scan processes are completed - // In case status is hang, add outdated timeout err := errors.Errorf("scan processes started by %s job with ID %d is in progress: %s", jb.Name, jb.ID, progress(stats.Completed, stats.Total)) aj.SendPreconditionFailedError(errors.Wrap(err, "submit : AJAPI")) return @@ -322,9 +312,9 @@ func (aj *AJAPI) submit(ajr *models.AdminJobReq) { } } -func (aj *AJAPI) getLatestScanAllJobIDByKind(kind string) (int64, error) { +func (aj *AJAPI) getLatestAdminJob(name, kind string) (*common_models.AdminJob, error) { query := &common_models.AdminJobQuery{ - Name: common_job.ImageScanAllJob, + Name: name, Kind: kind, } query.Size = 1 @@ -333,16 +323,16 @@ func (aj *AJAPI) getLatestScanAllJobIDByKind(kind string) (int64, error) { jbs, err := dao.GetAdminJobs(query) if err != nil { - return 0, err + return nil, err } if len(jbs) == 0 { // Not exist - return 0, nil + return nil, nil } // Return the latest one (with biggest ID) - return jbs[0].ID, nil + return jbs[0], nil } func convertToAdminJobRep(job *common_models.AdminJob) (models.AdminJobRep, error) { diff --git a/src/core/api/scan_all.go b/src/core/api/scan_all.go index 01fc84b12..31d4d76fa 100644 --- a/src/core/api/scan_all.go +++ b/src/core/api/scan_all.go @@ -4,9 +4,12 @@ import ( "fmt" "net/http" "strconv" + "strings" common_job "github.com/goharbor/harbor/src/common/job" + cm "github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/core/api/models" + "github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/pkg/q" "github.com/goharbor/harbor/src/pkg/scan/all" "github.com/goharbor/harbor/src/pkg/scan/api/scan" @@ -110,19 +113,21 @@ func (sc *ScanAllAPI) GetScanAllMetrics() { } func (sc *ScanAllAPI) getMetrics(kind string) { - id, err := sc.getLatestScanAllJobIDByKind(kind) + aj, err := sc.getLatestAdminJob(common_job.ImageScanAllJob, kind) if err != nil { sc.SendInternalServerError(errors.Wrap(err, "get metrics: scan all API")) return } var sts *all.Stats - if id > 0 { - sts, err = scan.DefaultController.GetStats(fmt.Sprintf("%d", id)) + if aj != nil { + sts, err = scan.DefaultController.GetStats(fmt.Sprintf("%d", aj.ID)) if err != nil { sc.SendInternalServerError(errors.Wrap(err, "get metrics: scan all API")) return } + + setOngoing(sts, aj.Status) } // Return empty @@ -149,3 +154,15 @@ func isScanEnabled() (bool, error) { return len(l) > 0, nil } + +func setOngoing(stats *all.Stats, st string) { + status := job.PendingStatus + + if st == cm.JobFinished { + status = job.SuccessStatus + } else { + status = job.Status(strings.ToTitle(st)) + } + + stats.Ongoing = !status.Final() || stats.Total != stats.Completed +} diff --git a/src/pkg/scan/all/stats.go b/src/pkg/scan/all/stats.go index d678b9738..e2149a65b 100644 --- a/src/pkg/scan/all/stats.go +++ b/src/pkg/scan/all/stats.go @@ -22,6 +22,7 @@ type Stats struct { Completed uint `json:"completed"` Metrics StatusMetrics `json:"metrics"` Requester string `json:"requester"` + Ongoing bool `json:"ongoing"` // Indicate if the metrics data is stable now } // StatusMetrics contains the metrics of each status.