mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-24 09:38:09 +01:00
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:
parent
b87373d6a9
commit
a1d8c01cea
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user