Skip to content

Commit

Permalink
add sort by update time in list release plan
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ committed Jan 7, 2025
1 parent 707d72f commit febd0b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
15 changes: 9 additions & 6 deletions pkg/cli/upgradeassistant/cmd/migrate/321.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package migrate

import (
"fmt"
"time"

"github.com/koderover/zadig/v2/pkg/cli/upgradeassistant/internal/upgradepath"
Expand All @@ -35,10 +36,12 @@ func init() {
func V320ToV321() error {
ctx := handler.NewBackgroupContext()

ctx.Logger.Infof("start migrate release plan cronjob")
ctx.Logger.Infof("-------- start migrate release plan cronjob --------")
err := migrateReleasePlanCron(ctx)
if err != nil {
ctx.Logger.Errorf("failed to migrate release plan cronjob, error: %s", err)
err = fmt.Errorf("failed to migrate release plan cronjob, error: %w", err)
ctx.Logger.Error(err)
return err
}

return nil
Expand All @@ -54,15 +57,15 @@ func migrateReleasePlanCron(ctx *handler.Context) error {
bson.M{"type": "release_plan", "enabled": true},
)
if err != nil {
return err
return fmt.Errorf("failed to delete release plan cronjobs, error: %w", err)
}

releasePlans, _, err := commonrepo.NewReleasePlanColl().ListByOptions(&commonrepo.ListReleasePlanOption{})
if err != nil {
return err
return fmt.Errorf("failed to list release plans, error: %w", err)
}

// create new cronjob for release plan if schedule time is after now
// create new cronjob for release plan if schedule time is after now and status is executing
for _, releasePlan := range releasePlans {
if releasePlan.ScheduleExecuteTime != 0 && releasePlan.Status == config.StatusExecuting {
if time.Unix(releasePlan.ScheduleExecuteTime, 0).After(time.Now()) {
Expand All @@ -79,7 +82,7 @@ func migrateReleasePlanCron(ctx *handler.Context) error {
},
}
if err := commonrepo.NewCronjobColl().Upsert(cronjob); err != nil {
return err
return fmt.Errorf("failed to create new release plan schdule, error: %w", err)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (c *ReleasePlanColl) EnsureIndex(ctx context.Context) error {
Keys: bson.M{"success_time": 1},
Options: options.Index().SetUnique(false),
},
{
Keys: bson.M{"update_time": 1},
Options: options.Index().SetUnique(false),
},
}

_, err := c.Indexes().CreateMany(ctx, mod)
Expand Down Expand Up @@ -124,6 +128,13 @@ func (c *ReleasePlanColl) DeleteByID(ctx context.Context, idString string) error
return err
}

type SortReleasePlanBy string

const (
SortReleasePlanByIndex SortReleasePlanBy = "index"
SortReleasePlanByUpdateTime SortReleasePlanBy = "update_time"
)

type ListReleasePlanOption struct {
PageNum int64
PageSize int64
Expand All @@ -134,6 +145,7 @@ type ListReleasePlanOption struct {
UpdateTimeStart int64
UpdateTimeEnd int64
IsSort bool
SortBy SortReleasePlanBy
ExcludedFields []string
Status config.ReleasePlanStatus
}
Expand All @@ -149,8 +161,15 @@ func (c *ReleasePlanColl) ListByOptions(opt *ListReleasePlanOption) ([]*models.R
ctx := context.Background()
opts := options.Find()
if opt.IsSort {
opts.SetSort(bson.D{{"index", -1}})
if opt.SortBy == SortReleasePlanByIndex {
opts.SetSort(bson.D{{"index", -1}})
} else if opt.SortBy == SortReleasePlanByUpdateTime {
opts.SetSort(bson.D{{"update_time", -1}})
} else {
opts.SetSort(bson.D{{"index", -1}})
}
}

if opt.PageNum > 0 && opt.PageSize > 0 {
opts.SetSkip((opt.PageNum - 1) * opt.PageSize)
opts.SetLimit(opt.PageSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ func ListReleasePlans(opt *ListReleasePlanOption) (*ListReleasePlanResp, error)
SuccessTimeStart: timeStart,
SuccessTimeEnd: timeEnd,
IsSort: true,
SortBy: mongodb.SortReleasePlanByUpdateTime,
PageNum: opt.PageNum,
PageSize: opt.PageSize,
ExcludedFields: []string{"jobs", "logs"},
Expand All @@ -1040,6 +1041,7 @@ func ListReleasePlans(opt *ListReleasePlanOption) (*ListReleasePlanResp, error)
UpdateTimeStart: timeStart,
UpdateTimeEnd: timeEnd,
IsSort: true,
SortBy: mongodb.SortReleasePlanByUpdateTime,
PageNum: opt.PageNum,
PageSize: opt.PageSize,
ExcludedFields: []string{"jobs", "logs"},
Expand Down

0 comments on commit febd0b1

Please sign in to comment.