diff --git a/api/event.go b/api/event.go index 6eb3490..7742968 100644 --- a/api/event.go +++ b/api/event.go @@ -2,7 +2,6 @@ package api import ( "github.com/cuigh/auxo/data" - "github.com/cuigh/auxo/errors" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" "github.com/cuigh/swirl/dao" @@ -47,15 +46,13 @@ func eventSearch(b biz.EventBiz) web.HandlerFunc { func eventPrune(b biz.EventBiz) web.HandlerFunc { type Args struct { - Date string `json:"date"` + Days int32 `json:"days"` } return func(ctx web.Context) (err error) { var args = &Args{} if err = ctx.Bind(args); err == nil { - // TODO - //err = b.Prune(args.Date) - err = errors.NotImplemented + err = b.Prune(args.Days) } return ajax(ctx, err) } diff --git a/biz/event.go b/biz/event.go index c844e8f..9f22527 100644 --- a/biz/event.go +++ b/biz/event.go @@ -2,8 +2,10 @@ package biz import ( "context" + "time" "github.com/cuigh/auxo/data" + "github.com/cuigh/auxo/ext/times" "github.com/cuigh/auxo/log" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/dao" @@ -47,6 +49,7 @@ const ( type EventBiz interface { Search(args *dao.EventSearchArgs) (events []*dao.Event, total int, err error) + Prune(days int32) (err error) CreateRegistry(action EventAction, id, name string, user web.User) CreateNode(action EventAction, id, name string, user web.User) CreateNetwork(action EventAction, id, name string, user web.User) @@ -75,6 +78,10 @@ func (b *eventBiz) Search(args *dao.EventSearchArgs) (events []*dao.Event, total return b.d.EventSearch(context.TODO(), args) } +func (b *eventBiz) Prune(days int32) (err error) { + return b.d.EventPrune(context.TODO(), time.Now().Add(-times.Days(days))) +} + func (b *eventBiz) create(et EventType, ea EventAction, args data.Map, user web.User) { event := &dao.Event{ ID: primitive.NewObjectID(), diff --git a/dao/bolt/event.go b/dao/bolt/event.go index c267154..20283e6 100644 --- a/dao/bolt/event.go +++ b/dao/bolt/event.go @@ -5,6 +5,7 @@ import ( "sort" "time" + "github.com/boltdb/bolt" "github.com/cuigh/auxo/util/cast" "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/misc" @@ -47,3 +48,16 @@ func (d *Dao) EventSearch(ctx context.Context, args *dao.EventSearchArgs) (event func (d *Dao) EventCreate(ctx context.Context, event *dao.Event) (err error) { return d.replace(Event, event.ID.Hex(), event) } + +func (d *Dao) EventPrune(ctx context.Context, end time.Time) (err error) { + return d.db.Update(func(tx *bolt.Tx) (err error) { + b := tx.Bucket([]byte(Event)) + return b.ForEach(func(k, v []byte) error { + event := &dao.Event{} + if err = decode(v, event); err == nil && time.Time(event.Time).Before(end) { + err = b.Delete(k) + } + return err + }) + }) +} diff --git a/dao/dao.go b/dao/dao.go index 729c636..46b4599 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -61,6 +61,7 @@ type Interface interface { EventSearch(ctx context.Context, args *EventSearchArgs) (events []*Event, count int, err error) EventCreate(ctx context.Context, event *Event) error + EventPrune(ctx context.Context, end time.Time) (err error) SettingGet(ctx context.Context, id string) (*Setting, error) SettingGetAll(ctx context.Context) (settings []*Setting, err error) diff --git a/dao/mongo/event.go b/dao/mongo/event.go index ff20089..cd56461 100644 --- a/dao/mongo/event.go +++ b/dao/mongo/event.go @@ -2,6 +2,7 @@ package mongo import ( "context" + "time" "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" @@ -26,3 +27,9 @@ func (d *Dao) EventSearch(ctx context.Context, args *dao.EventSearchArgs) (event func (d *Dao) EventCreate(ctx context.Context, event *dao.Event) (err error) { return d.create(ctx, Event, event) } + +func (d *Dao) EventPrune(ctx context.Context, end time.Time) (err error) { + filter := bson.M{"time": bson.M{"$lt": end}} + _, err = d.db.Collection(Event).DeleteMany(ctx, filter) + return +} diff --git a/ui/src/api/event.ts b/ui/src/api/event.ts index a1b4412..f66a3ca 100644 --- a/ui/src/api/event.ts +++ b/ui/src/api/event.ts @@ -28,6 +28,10 @@ export class EventApi { search(args: SearchArgs) { return ajax.get('/event/search', args) } + + prune(days: number) { + return ajax.post>('/event/prune', { days }) + } } export default new EventApi diff --git a/ui/src/locales/en.ts b/ui/src/locales/en.ts index 3b4db18..782fd2d 100644 --- a/ui/src/locales/en.ts +++ b/ui/src/locales/en.ts @@ -247,6 +247,10 @@ export default { "title": "Prune volume", "body": "Are you sure you want to clean up unused data volumes?", }, + "prune_event": { + "title": "Prune event", + "label": "Retention days", + }, }, "objects": { "registry": "Registry | Registries", diff --git a/ui/src/locales/zh.ts b/ui/src/locales/zh.ts index 4162c7c..e353cf2 100644 --- a/ui/src/locales/zh.ts +++ b/ui/src/locales/zh.ts @@ -247,6 +247,10 @@ export default { "title": "清理数据卷", "body": "是否确实要清理未使用的数据卷?", }, + "prune_event": { + "title": "清理事件", + "label": "保留天数", + }, }, "objects": { "registry": "镜像仓库", diff --git a/ui/src/pages/event/List.vue b/ui/src/pages/event/List.vue index 8d1b94d..84a83bf 100644 --- a/ui/src/pages/event/List.vue +++ b/ui/src/pages/event/List.vue @@ -45,7 +45,7 @@ \ No newline at end of file