Skip to content

Commit

Permalink
chore: enhance resource selector
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Feb 13, 2024
1 parent 926b20b commit a3e4ba6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
1 change: 1 addition & 0 deletions query/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
allowedColumnFieldsInComponents = []string{
"owner",
"topology_type",
"topology_id",
"type", // Deprecated. Use resource_selector.types instead
}
)
Expand Down
21 changes: 0 additions & 21 deletions query/components_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,10 @@ const cacheJobBatchSize = 1000
// <id> -> models.Component
var componentCache = cache.New[models.Component](gocache_store.NewGoCache(gocache.New(10*time.Minute, 10*time.Minute)))

// <name/type/parentID/topologyID> -> models.Component
var componentNameTypeParentTopologyCache = cache.New[models.Component](gocache_store.NewGoCache(gocache.New(30*time.Minute, 30*time.Minute)))

func componentCacheKey(id string) string {
return "componentID:" + id
}

func componentNameTypeParentTopologyCacheKey(name, typ string, parentID *uuid.UUID, topologyID, agentID string) string {
pID := uuid.Nil.String()
if parentID != nil {
pID = parentID.String()
}
return name + typ + pID + topologyID + agentID
}

func SyncComponentCache(ctx context.Context) error {
next := uuid.Nil.String()
for {
Expand All @@ -56,11 +45,6 @@ func SyncComponentCache(ctx context.Context) error {
if err := componentCache.Set(ctx, componentCacheKey(comp.ID.String()), comp); err != nil {
return fmt.Errorf("error caching component(%s): %w", comp.ID, err)
}

cacheKey := componentNameTypeParentTopologyCacheKey(comp.Name, comp.Type, comp.ParentId, comp.TopologyID.String(), comp.AgentID.String())
if err := componentNameTypeParentTopologyCache.Set(ctx, cacheKey, comp); err != nil {
return fmt.Errorf("error caching component(%s): %w", comp.ID, err)
}
}

next = components[len(components)-1].ID.String()
Expand Down Expand Up @@ -88,11 +72,6 @@ func ComponentFromCache(ctx context.Context, id string) (models.Component, error
return c, nil
}

func ComponentFromByNameTypeParentTopology(ctx context.Context, name, typ string, parentID *uuid.UUID, topologyID, agentID string) (models.Component, error) {
cacheKey := componentNameTypeParentTopologyCacheKey(name, typ, parentID, topologyID, agentID)
return componentNameTypeParentTopologyCache.Get(ctx, cacheKey)
}

var SyncComponentCacheJob = &job.Job{
Name: "SyncComponentCache",
Schedule: "@every 5m",
Expand Down
8 changes: 8 additions & 0 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func queryResourceSelector(ctx context.Context, resourceSelector types.ResourceS
query = query.Where("agent_id = ?", agent.ID)
}

if resourceSelector.ParentID != "" {
if resourceSelector.ParentID == "nil" {
query = query.Where("parent_id IS NULL")
} else {
query = query.Where("parent_id = ?", resourceSelector.ParentID)
}
}

if len(resourceSelector.LabelSelector) > 0 {
labels := collections.SelectorToMap(resourceSelector.LabelSelector)
var onlyKeys []string
Expand Down
3 changes: 3 additions & 0 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type ResourceSelector struct {
// Additionally, the special "self" value can be used to select resources without an agent.
Agent string `yaml:"agent,omitempty" json:"agent,omitempty"`

// Set this to "nil" to query it as NULL or give a uuid
ParentID string `yaml:"parent_id,omitempty" json:"parent_id,omitempty"`

// Cache directives
// 'no-cache' (should not fetch from cache but can be cached)
// 'no-store' (should not cache)
Expand Down

0 comments on commit a3e4ba6

Please sign in to comment.