Skip to content

Commit

Permalink
chore: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Sep 24, 2024
1 parent f629d4b commit 8c3da81
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
51 changes: 37 additions & 14 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/duration"
"github.com/flanksource/commons/logger"
"github.com/lib/pq"

"github.com/google/uuid"
"github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -225,21 +224,17 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
}
}

supportedFuncs := []string{"component_config_traverse"}
useFuncParsing := false
if resourceSelector.Search != "" {
if strings.HasPrefix(resourceSelector.Search, "component_config_traverse") && table == "components" {
// search: component_config_traverse=72143d48-da4a-477f-bac1-1e9decf188a6,outgoing,Kubernetes::Pod,Kubernetes::Node
items := strings.Split(resourceSelector.Search, "=")
if len(items) == 2 {
// Args should be componentID, direction and types (compID,direction,type1,type2,type3)
args := strings.SplitN(items[1], ",", 3)
if len(args) == 3 {
resourceSelector.Functions.ComponentConfigTraversal = &types.ComponentConfigTraversalArgs{
ComponentID: args[0],
Direction: args[1],
Types: pq.StringArray(strings.Split(args[2], ",")),
}
}
for _, sf := range supportedFuncs {
if strings.Contains(resourceSelector.Search, sf) {
useFuncParsing = true
break
}
}
if useFuncParsing {
setSearchQueryParams(&resourceSelector)
} else {
var prefixQueries []*gorm.DB
if resourceSelector.Name == "" {
Expand All @@ -266,6 +261,34 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
return query, nil
}

func setSearchQueryParams(rs *types.ResourceSelector) {
if rs.Search == "" {
return
}

queries := strings.Split(rs.Search, ";")
for _, q := range queries {
items := strings.Split(q, "=")
if len(items) != 2 {
continue
}

switch items[0] {
case "component_config_traverse":
// search: component_config_traverse=72143d48-da4a-477f-bac1-1e9decf188a6,outgoing,Kubernetes::Pod,Kubernetes::Node
// Args should be componentID, direction and types (compID,direction,type1,type2,type3)
args := strings.SplitN(items[1], ",", 3)
if len(args) == 3 {
rs.Functions.ComponentConfigTraversal = &types.ComponentConfigTraversalArgs{
ComponentID: args[0],
Direction: args[1],
Types: types.Items(strings.Split(args[2], ",")),
}
}
}
}
}

// queryResourceSelector runs the given resourceSelector and returns the resource ids
func queryResourceSelector(ctx context.Context, limit int, resourceSelector types.ResourceSelector, table string, allowedColumnsAsFields []string) ([]uuid.UUID, error) {
if resourceSelector.IsEmpty() {
Expand Down
3 changes: 2 additions & 1 deletion types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/flanksource/commons/collections"
"github.com/flanksource/gomplate/v3"
"github.com/lib/pq"
"github.com/samber/lo"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func asMap(t any, removeFields ...string) map[string]any {
return m
}

type Items []string
type Items pq.StringArray

func (items Items) String() string {
return strings.Join(items, ",")
Expand Down
7 changes: 3 additions & 4 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/hash"
"github.com/flanksource/commons/logger"
"github.com/lib/pq"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
Expand All @@ -18,9 +17,9 @@ import (
)

type ComponentConfigTraversalArgs struct {
ComponentID string `yaml:"component_id,omitempty" json:"component_id,omitempty"`
Direction string `yaml:"direction,omitempty" json:"direction,omitempty"`
Types pq.StringArray `yaml:"types,omitempty" json:"types,omitempty"`
ComponentID string `yaml:"component_id,omitempty" json:"component_id,omitempty"`
Direction string `yaml:"direction,omitempty" json:"direction,omitempty"`
Types Items `yaml:"types,omitempty" json:"types,omitempty"`
}

type Functions struct {
Expand Down

0 comments on commit 8c3da81

Please sign in to comment.