Skip to content

Commit

Permalink
Merge branch 'main' into fix-latency-check
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored Feb 3, 2025
2 parents 8f03c56 + 4ae473f commit 2412573
Show file tree
Hide file tree
Showing 16 changed files with 556 additions and 542 deletions.
2 changes: 1 addition & 1 deletion models/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c Check) GetLabelsMatcher() labels.Labels {
}

func (c Check) GetFieldsMatcher() fields.Fields {
return noopMatcher{}
return genericFieldMatcher{c.AsMap()}
}

type checkLabelsProvider struct {
Expand Down
10 changes: 8 additions & 2 deletions models/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import (
"encoding/json"
"fmt"

"github.com/flanksource/commons/logger"
"github.com/google/uuid"
Expand Down Expand Up @@ -149,7 +148,14 @@ type genericFieldMatcher struct {
}

func (c genericFieldMatcher) Get(key string) string {
return fmt.Sprintf("%v", c.Fields[key])
val := c.Fields[key]
switch v := val.(type) {
case string:
return v
default:
marshalled, _ := json.Marshal(v)
return string(marshalled)
}
}

func (c genericFieldMatcher) Has(key string) bool {
Expand Down
37 changes: 1 addition & 36 deletions models/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ import (
"k8s.io/apimachinery/pkg/labels"
)

var AllowedColumnFieldsInComponents = []string{
"owner",
"topology_type",
"topology_id",
"parent_id",
"type", // Deprecated. Use resource_selector.types instead
}

// Ensure interface compliance
var (
_ types.ResourceSelectable = Component{}
Expand Down Expand Up @@ -340,7 +332,7 @@ func (c Component) GetLabelsMatcher() labels.Labels {
}

func (c Component) GetFieldsMatcher() fields.Fields {
return componentFieldsProvider{c}
return genericFieldMatcher{c.AsMap()}
}

type componentLabelsProvider struct {
Expand All @@ -356,33 +348,6 @@ func (c componentLabelsProvider) Has(key string) bool {
return ok
}

type componentFieldsProvider struct {
Component
}

func (c componentFieldsProvider) Get(key string) string {
if lo.Contains(AllowedColumnFieldsInComponents, key) {
return fmt.Sprintf("%v", c.AsMap()[key])
}

v := c.Properties.Find(key)
if v == nil {
return ""
}

return fmt.Sprintf("%v", v.GetValue())
}

func (c componentFieldsProvider) Has(key string) bool {
if lo.Contains(AllowedColumnFieldsInComponents, key) {
_, ok := c.AsMap()[key]
return ok
}

v := c.Properties.Find(key)
return v != nil
}

var ComponentID = func(c Component) string {
return c.ID.String()
}
Expand Down
31 changes: 1 addition & 30 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ const (
AnalysisTypeTechDebt AnalysisType = "technical_debt"
)

var AllowedColumnFieldsInConfigs = []string{"config_class", "external_id"}

type RelatedConfigDirection string

const (
Expand Down Expand Up @@ -277,34 +275,7 @@ func (c ConfigItem) GetLabelsMatcher() labels.Labels {
}

func (c ConfigItem) GetFieldsMatcher() fields.Fields {
return configFields{c}
}

type configFields struct {
ConfigItem
}

func (c configFields) Get(key string) string {
if lo.Contains(AllowedColumnFieldsInConfigs, key) {
return fmt.Sprintf("%v", c.AsMap()[key])
}

v := c.Properties.Find(key)
if v == nil {
return ""
}

return fmt.Sprintf("%v", v.GetValue())
}

func (c configFields) Has(key string) bool {
if lo.Contains(AllowedColumnFieldsInConfigs, key) {
_, ok := c.AsMap()[key]
return ok
}

v := c.Properties.Find(key)
return v != nil
return genericFieldMatcher{c.AsMap()}
}

type configLabels struct {
Expand Down
80 changes: 3 additions & 77 deletions query/commons.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,31 @@
package query

import (
"fmt"
"net/url"
"strings"
"time"

"github.com/flanksource/duty/context"
"github.com/flanksource/duty/types"
"github.com/flanksource/duty/query/grammar"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

var LocalFilter = "deleted_at is NULL AND agent_id = '00000000-0000-0000-0000-000000000000' OR agent_id IS NULL"

type expressions struct {
In []interface{}
Prefix []string
Suffix []string
}

type Expressions []clause.Expression

var distinctTagsCache = cache.New(time.Minute*10, time.Hour)

// postgrestValues returns ["a", "b", "c"] as `"a","b","c"`
func postgrestValues(val []any) string {
return strings.Join(lo.Map(val, func(s any, i int) string {
return fmt.Sprintf(`"%s"`, s)
}), ",")
}

func (query FilteringQuery) AppendPostgrest(key string,
queryParam url.Values,
) {
if len(query.In) > 0 {
queryParam.Add(key, fmt.Sprintf("in.(%s)", postgrestValues(query.In)))
}

if len(query.Not.In) > 0 {
queryParam.Add(key, fmt.Sprintf("not.in.(%s)", postgrestValues(query.Not.In)))
}

for _, p := range query.Prefix {
queryParam.Add(key, fmt.Sprintf("like.%s*", p))
}

for _, p := range query.Suffix {
queryParam.Add(key, fmt.Sprintf("like.*%s", p))
}
}

func (e expressions) ToExpression(field string) []clause.Expression {
var clauses []clause.Expression
if len(e.In) > 0 {
clauses = append(clauses, clause.IN{Column: clause.Column{Name: field}, Values: e.In})
}

for _, p := range e.Prefix {
clauses = append(clauses, clause.Like{
Column: clause.Column{Name: field},
Value: p + "%",
})
}

for _, s := range e.Suffix {
clauses = append(clauses, clause.Like{
Column: clause.Column{Name: field},
Value: "%" + s,
})
}

return clauses
}

// ParseFilteringQuery parses a filtering query string.
// It returns four slices: 'in', 'notIN', 'prefix', and 'suffix'.
type FilteringQuery struct {
expressions
Not expressions
}

func (fq *FilteringQuery) ToExpression(field string) []clause.Expression {
exprs := fq.expressions.ToExpression(field)
not := clause.Not(fq.Not.ToExpression(field)...)
return append(exprs, not)
}

// ParseFilteringQuery parses a filtering query string.
// It returns four slices: 'in', 'notIN', 'prefix', and 'suffix'.
func ParseFilteringQuery(query string, decodeURL bool) (in []interface{}, notIN []interface{}, prefix, suffix []string, err error) {
if query == "" {
return
}

q, err := types.ParseFilteringQueryV2(query, decodeURL)
q, err := grammar.ParseFilteringQueryV2(query, decodeURL)
if err != nil {
return nil, nil, nil, nil, err
}

return q.In, q.Not.In, q.Prefix, q.Suffix, nil
}

Expand Down
3 changes: 2 additions & 1 deletion query/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/flanksource/duty/api"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query/grammar"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
"gorm.io/gorm"
Expand Down Expand Up @@ -228,7 +229,7 @@ func (t *ConfigSummaryRequest) filterClause(q *gorm.DB) *gorm.DB {
var excludeClause *gorm.DB

for k, v := range t.Filter {
query, _ := types.ParseFilteringQueryV2(v, true)
query, _ := grammar.ParseFilteringQueryV2(v, true)

if len(query.Not.In) > 0 {
if excludeClause == nil {
Expand Down
2 changes: 1 addition & 1 deletion types/filters.go → query/grammar/filters.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package grammar

import (
"fmt"
Expand Down
Loading

0 comments on commit 2412573

Please sign in to comment.