Merge pull request #11368 from heww/fix-ongoing-of-metrics

fix(scan): ongoing is true for schedule scan all only when job is running
This commit is contained in:
Wenkai Yin(尹文开) 2020-03-31 20:10:40 +08:00 committed by GitHub
commit c2c9fa28eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 5 deletions

View File

@ -127,7 +127,7 @@ func (sc *ScanAllAPI) getMetrics(kind string) {
return return
} }
setOngoing(sts, aj.Status) setOngoing(sts, aj.Status, kind)
} }
// Return empty // Return empty
@ -155,14 +155,18 @@ func isScanEnabled() (bool, error) {
return len(l) > 0, nil return len(l) > 0, nil
} }
func setOngoing(stats *all.Stats, st string) { func setOngoing(stats *all.Stats, jobStatus, jobKind string) {
status := job.PendingStatus status := job.PendingStatus
if st == cm.JobFinished { if jobStatus == cm.JobFinished {
status = job.SuccessStatus status = job.SuccessStatus
} else { } else {
status = job.Status(strings.ToTitle(st)) status = job.Status(strings.Title(jobStatus))
} }
stats.Ongoing = !status.Final() || stats.Total != stats.Completed if jobKind == common_job.JobKindPeriodic {
stats.Ongoing = (status == job.RunningStatus) || stats.Total != stats.Completed
} else if jobKind == common_job.JobKindGeneric {
stats.Ongoing = !status.Final() || stats.Total != stats.Completed
}
} }

View File

@ -17,6 +17,9 @@ package api
import ( import (
"testing" "testing"
common_job "github.com/goharbor/harbor/src/common/job"
cm "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/scan/all"
"github.com/goharbor/harbor/src/pkg/scan/dao/scanner" "github.com/goharbor/harbor/src/pkg/scan/dao/scanner"
sc "github.com/goharbor/harbor/src/pkg/scan/scanner" sc "github.com/goharbor/harbor/src/pkg/scan/scanner"
"github.com/goharbor/harbor/src/testing/apitests/apilib" "github.com/goharbor/harbor/src/testing/apitests/apilib"
@ -80,3 +83,53 @@ func (suite *ScanAllAPITestSuite) TestScanAllGet() {
require.NoError(suite.T(), err, "Error occurred while get a scan all job") require.NoError(suite.T(), err, "Error occurred while get a scan all job")
suite.Equal(200, code, "Get scan all status should be 200") suite.Equal(200, code, "Get scan all status should be 200")
} }
func (suite *ScanAllAPITestSuite) TestSetOngoing() {
{
stats := &all.Stats{}
setOngoing(stats, cm.JobPending, common_job.JobKindPeriodic)
suite.False(stats.Ongoing)
}
{
stats := &all.Stats{}
setOngoing(stats, cm.JobRunning, common_job.JobKindPeriodic)
suite.True(stats.Ongoing)
}
{
stats := &all.Stats{}
setOngoing(stats, cm.JobFinished, common_job.JobKindPeriodic)
suite.False(stats.Ongoing)
}
{
stats := &all.Stats{}
setOngoing(stats, cm.JobError, common_job.JobKindPeriodic)
suite.False(stats.Ongoing)
}
{
stats := &all.Stats{}
setOngoing(stats, cm.JobPending, common_job.JobKindGeneric)
suite.True(stats.Ongoing)
}
{
stats := &all.Stats{}
setOngoing(stats, cm.JobRunning, common_job.JobKindGeneric)
suite.True(stats.Ongoing)
}
{
stats := &all.Stats{}
setOngoing(stats, cm.JobFinished, common_job.JobKindGeneric)
suite.False(stats.Ongoing)
}
{
stats := &all.Stats{}
setOngoing(stats, cm.JobError, common_job.JobKindGeneric)
suite.False(stats.Ongoing)
}
}