fix tag retention api bug

Signed-off-by: Ziming Zhang <zziming@vmware.com>
Change-Id: I2c295090de34d133671c01422a6812fa426e9be7
This commit is contained in:
Ziming Zhang 2019-07-29 18:36:51 +08:00
parent b0f6000f16
commit 7cc29f0b90
3 changed files with 32 additions and 14 deletions

View File

@ -45,12 +45,6 @@ type RetentionExecution struct {
StartTime time.Time
EndTime time.Time `orm:"-"`
Status string `orm:"-"`
Total int `orm:"-"`
Succeed int `orm:"-"`
Failed int `orm:"-"`
InProgress int `orm:"-"`
Stopped int `orm:"-"`
Pending int `orm:"-"`
}
// RetentionTask ...

View File

@ -3,7 +3,9 @@ package dao
import (
"errors"
"fmt"
"github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/dao"
jobmodels "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/retention/dao/models"
"github.com/goharbor/harbor/src/pkg/retention/q"
)
@ -93,21 +95,45 @@ func GetExecution(id int64) (*models.RetentionExecution, error) {
// fillStatus the priority is InProgress Stopped Failed Succeed
func fillStatus(exec *models.RetentionExecution) error {
o := dao.GetOrmer()
var r orm.Params
if _, err := o.Raw("select status, count(*) num from retention_task where execution_id = ? group by status", exec.ID).
RowsToStruct(exec, "status", "num"); err != nil {
RowsToMap(&r, "status", "num"); err != nil {
return err
}
exec.Total = exec.Pending + exec.InProgress + exec.Succeed + exec.Failed + exec.Stopped
if exec.Total == 0 {
var (
total, running, succeed, failed, stopped int
)
for k, v := range r {
total += v.(int)
switch k {
case jobmodels.JobScheduled:
running += 1
case jobmodels.JobPending:
running += 1
case jobmodels.JobRunning:
running += 1
case jobmodels.JobRetrying:
running += 1
case jobmodels.JobFinished:
succeed += 1
case jobmodels.JobCanceled:
stopped += 1
case jobmodels.JobStopped:
stopped += 1
case jobmodels.JobError:
failed += 1
}
}
if total == 0 {
exec.Status = models.ExecutionStatusSucceed
exec.EndTime = exec.StartTime
return nil
}
if exec.Pending+exec.InProgress > 0 {
if running > 0 {
exec.Status = models.ExecutionStatusInProgress
} else if exec.Stopped > 0 {
} else if stopped > 0 {
exec.Status = models.ExecutionStatusStopped
} else if exec.Failed > 0 {
} else if failed > 0 {
exec.Status = models.ExecutionStatusFailed
} else {
exec.Status = models.ExecutionStatusSucceed

View File

@ -159,10 +159,8 @@ func TestExecution(t *testing.T) {
e := &models.RetentionExecution{
PolicyID: policyID,
Status: "Running",
DryRun: false,
Trigger: "manual",
Total: 10,
StartTime: time.Now(),
}
id, err := CreateExecution(e)