diff --git a/internal/server/activities/targets_filter.go b/internal/server/activities/targets_filter.go index 27d5f2a..71ca2fe 100644 --- a/internal/server/activities/targets_filter.go +++ b/internal/server/activities/targets_filter.go @@ -4,10 +4,10 @@ import ( "context" "github.com/dop251/goja" - "github.com/mentos1386/zdravko/database/models" "github.com/mentos1386/zdravko/internal/server/services" "github.com/mentos1386/zdravko/internal/temporal" "github.com/mentos1386/zdravko/pkg/script" + "gopkg.in/yaml.v3" ) func (a *Activities) TargetsFilter(ctx context.Context, param temporal.ActivityTargetsFilterParam) (*temporal.ActivityTargetsFilterResult, error) { @@ -17,22 +17,50 @@ func (a *Activities) TargetsFilter(ctx context.Context, param temporal.ActivityT if err != nil { return nil, err } - filteredTargets := make([]*models.Target, 0) + filteredTargets := make([]*temporal.Target, 0) + + program, err := goja.Compile("filter", script.UnescapeString(param.Filter), false) + if err != nil { + return nil, err + } for _, target := range allTargets { vm := goja.New() + vm.SetFieldNameMapper(goja.UncapFieldNameMapper()) - err = vm.Set("target", target) + var metadata map[string]interface{} + err := yaml.Unmarshal([]byte(target.Metadata), &metadata) if err != nil { return nil, err } - value, err := vm.RunString(script.UnescapeString(param.Filter)) + a.logger.Info("TargetsFilter", "target", target) + + targetWithMedatada := &struct { + Name string + Group string + Metadata map[string]interface{} + }{ + Name: target.Name, + Group: target.Group, + Metadata: metadata, + } + + err = vm.Set("target", targetWithMedatada) + if err != nil { + return nil, err + } + + value, err := vm.RunProgram(program) if err != nil { return nil, err } if value.Export().(bool) { - filteredTargets = append(filteredTargets, target) + filteredTargets = append(filteredTargets, &temporal.Target{ + Name: target.Name, + Group: target.Group, + Metadata: target.Metadata, + }) } } diff --git a/internal/temporal/activity_check.go b/internal/temporal/activity_check.go index 8bc5353..0dc276a 100644 --- a/internal/temporal/activity_check.go +++ b/internal/temporal/activity_check.go @@ -1,10 +1,8 @@ package temporal -import "github.com/mentos1386/zdravko/database/models" - type ActivityCheckParam struct { Script string - Target *models.Target + Target *Target } type ActivityCheckResult struct { diff --git a/internal/temporal/activity_targets_filter.go b/internal/temporal/activity_targets_filter.go index b204f7e..156f9b5 100644 --- a/internal/temporal/activity_targets_filter.go +++ b/internal/temporal/activity_targets_filter.go @@ -1,15 +1,11 @@ package temporal -import ( - "github.com/mentos1386/zdravko/database/models" -) - type ActivityTargetsFilterParam struct { Filter string } type ActivityTargetsFilterResult struct { - Targets []*models.Target + Targets []*Target } const ActivityTargetsFilterName = "TARGETS_FILTER" diff --git a/internal/temporal/temporal.go b/internal/temporal/temporal.go index 026e1f0..1867517 100644 --- a/internal/temporal/temporal.go +++ b/internal/temporal/temporal.go @@ -12,6 +12,12 @@ import ( "go.temporal.io/sdk/client" ) +type Target struct { + Name string + Group string + Metadata string +} + // Must be default, as we are also processing // some temporal things. const TEMPORAL_SERVER_QUEUE = "default" diff --git a/internal/worker/activities/check.go b/internal/worker/activities/check.go index 6dc09ab..d27f2f7 100644 --- a/internal/worker/activities/check.go +++ b/internal/worker/activities/check.go @@ -8,16 +8,23 @@ import ( "github.com/mentos1386/zdravko/pkg/k6" "github.com/mentos1386/zdravko/pkg/k6/zdravko" "github.com/mentos1386/zdravko/pkg/script" + "gopkg.in/yaml.v3" ) func (a *Activities) Check(ctx context.Context, param temporal.ActivityCheckParam) (*temporal.ActivityCheckResult, error) { execution := k6.NewExecution(slog.Default(), script.UnescapeString(param.Script)) + var metadata map[string]interface{} + err := yaml.Unmarshal([]byte(param.Target.Metadata), &metadata) + if err != nil { + return nil, err + } + ctx = zdravko.WithZdravkoContext(ctx, zdravko.Context{ Target: zdravko.Target{ - Name: param.Target.Name, - Group: param.Target.Group, - //Metadata: param.Target.Metadata, + Name: param.Target.Name, + Group: param.Target.Group, + Metadata: metadata, }, }) diff --git a/pkg/k6/zdravko/context.go b/pkg/k6/zdravko/context.go index e83fc8d..ef337e3 100644 --- a/pkg/k6/zdravko/context.go +++ b/pkg/k6/zdravko/context.go @@ -4,12 +4,6 @@ import "context" type zdravkoContextKey string -type Target struct { - Name string - Group string - Metadata map[string]interface{} -} - type Context struct { Target Target } diff --git a/pkg/k6/zdravko/zdravko.go b/pkg/k6/zdravko/zdravko.go index 45299c3..c1cdba3 100644 --- a/pkg/k6/zdravko/zdravko.go +++ b/pkg/k6/zdravko/zdravko.go @@ -1,6 +1,7 @@ package zdravko import ( + "github.com/dop251/goja" "go.k6.io/k6/js/modules" ) @@ -41,14 +42,20 @@ func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance { } } -type Zdravko struct { - vu modules.VU - Targets []string +type Target struct { + Name string + Group string + Metadata map[string]interface{} } -func (z *Zdravko) GetTarget() Target { +type Zdravko struct { + vu modules.VU + Targets []Target +} + +func (z *Zdravko) GetTarget() goja.Value { zdravkoContext := GetZdravkoContext(z.vu.Context()) - return zdravkoContext.Target + return z.vu.Runtime().ToValue(zdravkoContext.Target) } // Exports implements the modules.Instance interface and returns the exported types for the JS module.