swirl/biz/event.go

181 lines
5.7 KiB
Go
Raw Permalink Normal View History

2017-09-26 12:50:09 +00:00
package biz
import (
2021-12-06 12:24:22 +00:00
"context"
2022-01-04 08:44:03 +00:00
"time"
2017-09-26 12:50:09 +00:00
2021-12-24 07:04:15 +00:00
"github.com/cuigh/auxo/data"
2022-01-04 08:44:03 +00:00
"github.com/cuigh/auxo/ext/times"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/log"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/dao"
2021-12-06 12:24:22 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2017-09-26 12:50:09 +00:00
)
2021-12-06 12:24:22 +00:00
type EventType string
const (
2021-12-24 07:04:15 +00:00
EventTypeRegistry EventType = "Registry"
EventTypeNode EventType = "Node"
EventTypeNetwork EventType = "Network"
EventTypeService EventType = "Service"
EventTypeStack EventType = "Stack"
EventTypeConfig EventType = "Config"
EventTypeSecret EventType = "Secret"
EventTypeImage EventType = "Image"
EventTypeContainer EventType = "Container"
EventTypeVolume EventType = "Volume"
EventTypeUser EventType = "User"
EventTypeRole EventType = "Role"
EventTypeChart EventType = "Chart"
EventTypeSetting EventType = "Setting"
2021-12-06 12:24:22 +00:00
)
2017-09-26 12:50:09 +00:00
2021-12-06 12:24:22 +00:00
type EventAction string
const (
EventActionLogin EventAction = "Login"
EventActionCreate EventAction = "Create"
EventActionDelete EventAction = "Delete"
EventActionUpdate EventAction = "Update"
EventActionScale EventAction = "Scale"
EventActionRollback EventAction = "Rollback"
EventActionRestart EventAction = "Restart"
EventActionDisconnect EventAction = "Disconnect"
EventActionDeploy EventAction = "Deploy"
EventActionShutdown EventAction = "Shutdown"
EventActionPrune EventAction = "Prune"
)
type EventBiz interface {
2022-01-06 08:54:14 +00:00
Search(ctx context.Context, args *dao.EventSearchArgs) (events []*dao.Event, total int, err error)
Prune(ctx context.Context, days int32) (err error)
2021-12-06 12:24:22 +00:00
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)
CreateService(action EventAction, name string, user web.User)
CreateConfig(action EventAction, id, name string, user web.User)
CreateSecret(action EventAction, id, name string, user web.User)
CreateStack(action EventAction, name string, user web.User)
2021-12-24 07:04:15 +00:00
CreateImage(action EventAction, node, id string, user web.User)
CreateContainer(action EventAction, node, id, name string, user web.User)
CreateVolume(action EventAction, node, name string, user web.User)
2021-12-06 12:24:22 +00:00
CreateUser(action EventAction, id, name string, user web.User)
CreateRole(action EventAction, id, name string, user web.User)
CreateChart(action EventAction, id, title string, user web.User)
CreateSetting(action EventAction, user web.User)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
func NewEvent(d dao.Interface) EventBiz {
return &eventBiz{d: d}
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
type eventBiz struct {
d dao.Interface
2017-09-26 12:50:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *eventBiz) Search(ctx context.Context, args *dao.EventSearchArgs) (events []*dao.Event, total int, err error) {
return b.d.EventSearch(ctx, args)
2017-10-09 13:02:41 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *eventBiz) Prune(ctx context.Context, days int32) (err error) {
return b.d.EventPrune(ctx, time.Now().Add(-times.Days(days)))
2022-01-04 08:44:03 +00:00
}
2021-12-24 07:04:15 +00:00
func (b *eventBiz) create(et EventType, ea EventAction, args data.Map, user web.User) {
2021-12-23 11:28:31 +00:00
event := &dao.Event{
2021-12-06 12:24:22 +00:00
ID: primitive.NewObjectID(),
Type: string(et),
Action: string(ea),
2021-12-24 07:04:15 +00:00
Args: args,
2017-09-26 12:50:09 +00:00
UserID: user.ID(),
Username: user.Name(),
2021-12-16 12:23:08 +00:00
Time: now(),
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
err := b.d.EventCreate(context.TODO(), event)
if err != nil {
2021-12-24 07:04:15 +00:00
log.Get("event").Errorf("failed to create event `%+v`: %s", event, err)
2017-09-26 12:50:09 +00:00
}
}
2021-12-06 12:24:22 +00:00
func (b *eventBiz) CreateRegistry(action EventAction, id, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": name}
b.create(EventTypeRegistry, action, args, user)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
func (b *eventBiz) CreateService(action EventAction, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"name": name}
b.create(EventTypeService, action, args, user)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
func (b *eventBiz) CreateNetwork(action EventAction, id, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": name}
b.create(EventTypeNetwork, action, args, user)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
func (b *eventBiz) CreateNode(action EventAction, id, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": name}
b.create(EventTypeNode, action, args, user)
2017-09-26 12:50:09 +00:00
}
2021-12-24 07:04:15 +00:00
func (b *eventBiz) CreateImage(action EventAction, node, id string, user web.User) {
args := data.Map{"node": node}
if id != "" {
args["id"] = id
}
b.create(EventTypeImage, action, args, user)
}
2021-12-24 07:04:15 +00:00
func (b *eventBiz) CreateContainer(action EventAction, node, id, name string, user web.User) {
args := data.Map{"node": node}
if id != "" {
args["id"] = id
}
if name != "" {
args["name"] = name
}
b.create(EventTypeContainer, action, args, user)
}
2021-12-24 07:04:15 +00:00
func (b *eventBiz) CreateVolume(action EventAction, node, name string, user web.User) {
args := data.Map{"node": node}
if name != "" {
args["name"] = name
}
b.create(EventTypeVolume, action, args, user)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
func (b *eventBiz) CreateStack(action EventAction, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"name": name}
b.create(EventTypeStack, action, args, user)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
func (b *eventBiz) CreateSecret(action EventAction, id, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": name}
b.create(EventTypeSecret, action, args, user)
2021-12-06 12:24:22 +00:00
}
func (b *eventBiz) CreateConfig(action EventAction, id, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": name}
b.create(EventTypeConfig, action, args, user)
2021-12-06 12:24:22 +00:00
}
func (b *eventBiz) CreateRole(action EventAction, id, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": name}
b.create(EventTypeRole, action, args, user)
2021-12-06 12:24:22 +00:00
}
func (b *eventBiz) CreateSetting(action EventAction, user web.User) {
2021-12-24 07:04:15 +00:00
b.create(EventTypeSetting, action, nil, user)
2021-12-06 12:24:22 +00:00
}
func (b *eventBiz) CreateUser(action EventAction, id, name string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": name}
b.create(EventTypeUser, action, args, user)
2021-12-06 12:24:22 +00:00
}
func (b *eventBiz) CreateChart(action EventAction, id, title string, user web.User) {
2021-12-24 07:04:15 +00:00
args := data.Map{"id": id, "name": title}
b.create(EventTypeChart, action, args, user)
2017-09-26 12:50:09 +00:00
}