Files
upstream-corteza/server/system/automation/actionlog_handler.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

155 lines
3.5 KiB
Go

package automation
import (
"context"
"github.com/cortezaproject/corteza/server/pkg/actionlog"
. "github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/wfexec"
)
type (
actionlogHandler struct {
reg actionlogHandlerRegistry
svc actionlog.Recorder
}
actionSetIterator struct {
// Item buffer, current item pointer, and total items traversed
ptr uint
buffer actionlog.ActionSet
total uint
// When filter limit is set, this constraints it
iterLimit uint
useIterLimit bool
// Item loader for additional chunks
filter actionlog.Filter
loader func() error
}
)
func ActionlogHandler(reg actionlogHandlerRegistry, svc actionlog.Recorder) *actionlogHandler {
h := &actionlogHandler{
reg: reg,
svc: svc,
}
h.register()
return h
}
func (h actionlogHandler) search(ctx context.Context, args *actionlogSearchArgs) (results *actionlogSearchResults, err error) {
results = &actionlogSearchResults{}
var (
f = actionlog.Filter{
FromTimestamp: args.FromTimestamp,
ToTimestamp: args.ToTimestamp,
BeforeActionID: args.BeforeActionID,
Origin: args.Origin,
Resource: args.Resource,
Action: args.Action,
Limit: uint(args.Limit),
}
)
if args.ActorID > 0 {
f.ActorID = id.Strings(args.ActorID)
}
results.Actions, _, err = h.svc.Find(ctx, f)
return
}
func (h actionlogHandler) each(ctx context.Context, args *actionlogEachArgs) (out wfexec.IteratorHandler, err error) {
var (
i = &actionSetIterator{}
f = actionlog.Filter{
FromTimestamp: args.FromTimestamp,
ToTimestamp: args.ToTimestamp,
BeforeActionID: args.BeforeActionID,
Origin: args.Origin,
Resource: args.Resource,
Action: args.Action,
}
)
if args.ActorID > 0 {
f.ActorID = id.Strings(args.ActorID)
}
if args.hasLimit {
i.useIterLimit = true
i.iterLimit = uint(args.Limit)
f.Limit = uint(args.Limit)
if args.Limit > uint64(wfexec.MaxIteratorBufferSize) {
f.Limit = wfexec.MaxIteratorBufferSize
}
i.iterLimit = uint(args.Limit)
} else {
f.Limit = wfexec.MaxIteratorBufferSize
}
i.filter = f
i.loader = func() (err error) {
i.total += i.ptr
i.ptr = 0
if len(i.buffer) > 0 {
i.filter.BeforeActionID = i.buffer[len(i.buffer)-1].ID
}
i.buffer, i.filter, err = h.svc.Find(ctx, i.filter)
return
}
// Initial load
return i, i.loader()
}
func (h actionlogHandler) record(ctx context.Context, args *actionlogRecordArgs) (err error) {
a := &actionlog.Action{
Resource: args.Resource,
Action: args.Action,
Error: args.Error,
Severity: actionlog.NewSeverity(args.Severity),
Description: args.Description,
}
if args.Meta != nil {
a.Meta = args.Meta.Dict()
}
ctx = actionlog.RequestOriginToContext(ctx, actionlog.RequestOrigin_Automation)
h.svc.Record(ctx, a)
return nil
}
func (i *actionSetIterator) More(context.Context, *Vars) (bool, error) {
return wfexec.GenericResourceNextCheck(i.useIterLimit, i.ptr, uint(len(i.buffer)), i.total, i.iterLimit, i.iterLimit > uint(len(i.buffer))), nil
}
func (i *actionSetIterator) Start(context.Context, *Vars) error { i.ptr = 0; return nil }
func (i *actionSetIterator) Next(context.Context, *Vars) (out *Vars, err error) {
if len(i.buffer)-int(i.ptr) <= 0 {
if err = i.loader(); err != nil {
panic(err)
}
}
out = &Vars{}
out.Set("user", Must(NewUser(i.buffer[i.ptr])))
out.Set("index", Must(NewInteger(i.total+i.ptr)))
i.ptr++
return out, nil
}