mirror of
https://github.com/goharbor/harbor.git
synced 2024-09-13 06:28:19 +02:00
Merge pull request #8457 from bitsf/tag_retention_api_fix
fix tag retention api bug
This commit is contained in:
commit
b90874b06a
@ -45,12 +45,6 @@ type RetentionExecution struct {
|
|||||||
StartTime time.Time
|
StartTime time.Time
|
||||||
EndTime time.Time `orm:"-"`
|
EndTime time.Time `orm:"-"`
|
||||||
Status string `orm:"-"`
|
Status string `orm:"-"`
|
||||||
Total int `orm:"-"`
|
|
||||||
Succeed int `orm:"-"`
|
|
||||||
Failed int `orm:"-"`
|
|
||||||
InProgress int `orm:"-"`
|
|
||||||
Stopped int `orm:"-"`
|
|
||||||
Pending int `orm:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RetentionTask ...
|
// RetentionTask ...
|
||||||
|
@ -3,7 +3,9 @@ package dao
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/astaxie/beego/orm"
|
||||||
"github.com/goharbor/harbor/src/common/dao"
|
"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/dao/models"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/q"
|
"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
|
// fillStatus the priority is InProgress Stopped Failed Succeed
|
||||||
func fillStatus(exec *models.RetentionExecution) error {
|
func fillStatus(exec *models.RetentionExecution) error {
|
||||||
o := dao.GetOrmer()
|
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).
|
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
|
return err
|
||||||
}
|
}
|
||||||
exec.Total = exec.Pending + exec.InProgress + exec.Succeed + exec.Failed + exec.Stopped
|
var (
|
||||||
if exec.Total == 0 {
|
total, running, succeed, failed, stopped int
|
||||||
|
)
|
||||||
|
for k, v := range r {
|
||||||
|
total += v.(int)
|
||||||
|
switch k {
|
||||||
|
case jobmodels.JobScheduled:
|
||||||
|
running += v.(int)
|
||||||
|
case jobmodels.JobPending:
|
||||||
|
running += v.(int)
|
||||||
|
case jobmodels.JobRunning:
|
||||||
|
running += v.(int)
|
||||||
|
case jobmodels.JobRetrying:
|
||||||
|
running += v.(int)
|
||||||
|
case jobmodels.JobFinished:
|
||||||
|
succeed += v.(int)
|
||||||
|
case jobmodels.JobCanceled:
|
||||||
|
stopped += v.(int)
|
||||||
|
case jobmodels.JobStopped:
|
||||||
|
stopped += v.(int)
|
||||||
|
case jobmodels.JobError:
|
||||||
|
failed += v.(int)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if total == 0 {
|
||||||
exec.Status = models.ExecutionStatusSucceed
|
exec.Status = models.ExecutionStatusSucceed
|
||||||
exec.EndTime = exec.StartTime
|
exec.EndTime = exec.StartTime
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if exec.Pending+exec.InProgress > 0 {
|
if running > 0 {
|
||||||
exec.Status = models.ExecutionStatusInProgress
|
exec.Status = models.ExecutionStatusInProgress
|
||||||
} else if exec.Stopped > 0 {
|
} else if stopped > 0 {
|
||||||
exec.Status = models.ExecutionStatusStopped
|
exec.Status = models.ExecutionStatusStopped
|
||||||
} else if exec.Failed > 0 {
|
} else if failed > 0 {
|
||||||
exec.Status = models.ExecutionStatusFailed
|
exec.Status = models.ExecutionStatusFailed
|
||||||
} else {
|
} else {
|
||||||
exec.Status = models.ExecutionStatusSucceed
|
exec.Status = models.ExecutionStatusSucceed
|
||||||
@ -127,6 +153,7 @@ func ListExecutions(policyID int64, query *q.Query) ([]*models.RetentionExecutio
|
|||||||
qs := o.QueryTable(new(models.RetentionExecution))
|
qs := o.QueryTable(new(models.RetentionExecution))
|
||||||
|
|
||||||
qs = qs.Filter("policy_id", policyID)
|
qs = qs.Filter("policy_id", policyID)
|
||||||
|
qs = qs.OrderBy("-id")
|
||||||
if query != nil {
|
if query != nil {
|
||||||
qs = qs.Limit(query.PageSize, (query.PageNumber-1)*query.PageSize)
|
qs = qs.Limit(query.PageSize, (query.PageNumber-1)*query.PageSize)
|
||||||
}
|
}
|
||||||
|
@ -159,10 +159,8 @@ func TestExecution(t *testing.T) {
|
|||||||
|
|
||||||
e := &models.RetentionExecution{
|
e := &models.RetentionExecution{
|
||||||
PolicyID: policyID,
|
PolicyID: policyID,
|
||||||
Status: "Running",
|
|
||||||
DryRun: false,
|
DryRun: false,
|
||||||
Trigger: "manual",
|
Trigger: "manual",
|
||||||
Total: 10,
|
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
}
|
}
|
||||||
id, err := CreateExecution(e)
|
id, err := CreateExecution(e)
|
||||||
|
@ -66,6 +66,7 @@ type DefaultManager struct {
|
|||||||
func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
|
func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
|
||||||
p1 := &models.RetentionPolicy{}
|
p1 := &models.RetentionPolicy{}
|
||||||
p1.ScopeLevel = p.Scope.Level
|
p1.ScopeLevel = p.Scope.Level
|
||||||
|
p1.ScopeReference = p.Scope.Reference
|
||||||
p1.TriggerKind = p.Trigger.Kind
|
p1.TriggerKind = p.Trigger.Kind
|
||||||
data, _ := json.Marshal(p)
|
data, _ := json.Marshal(p)
|
||||||
p1.Data = string(data)
|
p1.Data = string(data)
|
||||||
@ -79,6 +80,7 @@ func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
|
|||||||
p1 := &models.RetentionPolicy{}
|
p1 := &models.RetentionPolicy{}
|
||||||
p1.ID = p.ID
|
p1.ID = p.ID
|
||||||
p1.ScopeLevel = p.Scope.Level
|
p1.ScopeLevel = p.Scope.Level
|
||||||
|
p1.ScopeReference = p.Scope.Reference
|
||||||
p1.TriggerKind = p.Trigger.Kind
|
p1.TriggerKind = p.Trigger.Kind
|
||||||
p.ID = 0
|
p.ID = 0
|
||||||
data, _ := json.Marshal(p)
|
data, _ := json.Marshal(p)
|
||||||
@ -142,6 +144,7 @@ func (d *DefaultManager) ListExecutions(policyID int64, query *q.Query) ([]*Exec
|
|||||||
e1.Status = e.Status
|
e1.Status = e.Status
|
||||||
e1.StartTime = e.StartTime
|
e1.StartTime = e.StartTime
|
||||||
e1.EndTime = e.EndTime
|
e1.EndTime = e.EndTime
|
||||||
|
e1.DryRun = e.DryRun
|
||||||
execs1 = append(execs1, e1)
|
execs1 = append(execs1, e1)
|
||||||
}
|
}
|
||||||
return execs1, nil
|
return execs1, nil
|
||||||
@ -159,6 +162,7 @@ func (d *DefaultManager) GetExecution(eid int64) (*Execution, error) {
|
|||||||
e1.Status = e.Status
|
e1.Status = e.Status
|
||||||
e1.StartTime = e.StartTime
|
e1.StartTime = e.StartTime
|
||||||
e1.EndTime = e.EndTime
|
e1.EndTime = e.EndTime
|
||||||
|
e1.DryRun = e.DryRun
|
||||||
return e1, nil
|
return e1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ type Metadata struct {
|
|||||||
|
|
||||||
// Valid Valid
|
// Valid Valid
|
||||||
func (m *Metadata) Valid(v *validation.Validation) {
|
func (m *Metadata) Valid(v *validation.Validation) {
|
||||||
if m.Trigger.Kind == TriggerKindSchedule {
|
if m.Trigger != nil && m.Trigger.Kind == TriggerKindSchedule {
|
||||||
if m.Trigger.Settings == nil {
|
if m.Trigger.Settings == nil {
|
||||||
_ = v.SetError("Trigger.Settings", "Trigger.Settings is required")
|
_ = v.SetError("Trigger.Settings", "Trigger.Settings is required")
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user