add status data in the scan all metrics

Signed-off-by: Steven Zou <szou@vmware.com>

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2019-11-07 13:04:40 +08:00
parent b87373d6a9
commit a1d8c01cea
3 changed files with 28 additions and 20 deletions

View File

@ -249,22 +249,13 @@ func (aj *AJAPI) submit(ajr *models.AdminJobReq) {
// Only needs to care the 1st generic job. // Only needs to care the 1st generic job.
// Check if there are still ongoing scan jobs triggered by the previous admin job. // Check if there are still ongoing scan jobs triggered by the previous admin job.
// TODO: REPLACE WITH TASK MANAGER METHODS IN FUTURE // TODO: REPLACE WITH TASK MANAGER METHODS IN FUTURE
query := &common_models.AdminJobQuery{ jb, err := aj.getLatestAdminJob(ajr.Name, common_job.JobKindGeneric)
Name: ajr.Name,
Kind: common_job.JobKindGeneric,
}
query.Size = 1
query.Page = 1
ajbs, err := dao.GetAdminJobs(query)
if err != nil { if err != nil {
aj.SendInternalServerError(errors.Wrap(err, "AJAPI")) aj.SendInternalServerError(errors.Wrap(err, "AJAPI"))
return return
} }
if len(ajbs) > 0 { if jb != nil {
jb := ajbs[0]
// With a reasonable timeout duration // With a reasonable timeout duration
if jb.UpdateTime.Add(2 * time.Hour).After(time.Now()) { if jb.UpdateTime.Add(2 * time.Hour).After(time.Now()) {
if isOnGoing(jb.Status) { if isOnGoing(jb.Status) {
@ -284,7 +275,6 @@ func (aj *AJAPI) submit(ajr *models.AdminJobReq) {
if stats.Total != stats.Completed { if stats.Total != stats.Completed {
// Not all scan processes are 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)) 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")) aj.SendPreconditionFailedError(errors.Wrap(err, "submit : AJAPI"))
return 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{ query := &common_models.AdminJobQuery{
Name: common_job.ImageScanAllJob, Name: name,
Kind: kind, Kind: kind,
} }
query.Size = 1 query.Size = 1
@ -333,16 +323,16 @@ func (aj *AJAPI) getLatestScanAllJobIDByKind(kind string) (int64, error) {
jbs, err := dao.GetAdminJobs(query) jbs, err := dao.GetAdminJobs(query)
if err != nil { if err != nil {
return 0, err return nil, err
} }
if len(jbs) == 0 { if len(jbs) == 0 {
// Not exist // Not exist
return 0, nil return nil, nil
} }
// Return the latest one (with biggest ID) // 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) { func convertToAdminJobRep(job *common_models.AdminJob) (models.AdminJobRep, error) {

View File

@ -4,9 +4,12 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings"
common_job "github.com/goharbor/harbor/src/common/job" 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/core/api/models"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/pkg/q" "github.com/goharbor/harbor/src/pkg/q"
"github.com/goharbor/harbor/src/pkg/scan/all" "github.com/goharbor/harbor/src/pkg/scan/all"
"github.com/goharbor/harbor/src/pkg/scan/api/scan" "github.com/goharbor/harbor/src/pkg/scan/api/scan"
@ -110,19 +113,21 @@ func (sc *ScanAllAPI) GetScanAllMetrics() {
} }
func (sc *ScanAllAPI) getMetrics(kind string) { func (sc *ScanAllAPI) getMetrics(kind string) {
id, err := sc.getLatestScanAllJobIDByKind(kind) aj, err := sc.getLatestAdminJob(common_job.ImageScanAllJob, kind)
if err != nil { if err != nil {
sc.SendInternalServerError(errors.Wrap(err, "get metrics: scan all API")) sc.SendInternalServerError(errors.Wrap(err, "get metrics: scan all API"))
return return
} }
var sts *all.Stats var sts *all.Stats
if id > 0 { if aj != nil {
sts, err = scan.DefaultController.GetStats(fmt.Sprintf("%d", id)) sts, err = scan.DefaultController.GetStats(fmt.Sprintf("%d", aj.ID))
if err != nil { if err != nil {
sc.SendInternalServerError(errors.Wrap(err, "get metrics: scan all API")) sc.SendInternalServerError(errors.Wrap(err, "get metrics: scan all API"))
return return
} }
setOngoing(sts, aj.Status)
} }
// Return empty // Return empty
@ -149,3 +154,15 @@ func isScanEnabled() (bool, error) {
return len(l) > 0, nil 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
}

View File

@ -22,6 +22,7 @@ type Stats struct {
Completed uint `json:"completed"` Completed uint `json:"completed"`
Metrics StatusMetrics `json:"metrics"` Metrics StatusMetrics `json:"metrics"`
Requester string `json:"requester"` Requester string `json:"requester"`
Ongoing bool `json:"ongoing"` // Indicate if the metrics data is stable now
} }
// StatusMetrics contains the metrics of each status. // StatusMetrics contains the metrics of each status.