feat: working targets and check integration

This commit is contained in:
Tine 2024-05-25 10:24:05 +02:00
parent c2eabedb91
commit b8c37cfd3c
No known key found for this signature in database
7 changed files with 63 additions and 27 deletions

View file

@ -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,
})
}
}

View file

@ -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 {

View file

@ -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"

View file

@ -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"

View file

@ -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,
},
})

View file

@ -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
}

View file

@ -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.