Skip to content

Commit

Permalink
feat: add support for resource selector cache type
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Feb 2, 2024
1 parent c4c2d80 commit bd40fdc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 84 deletions.
28 changes: 4 additions & 24 deletions query/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package query

import (
"fmt"
"time"

"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
Expand All @@ -28,31 +27,12 @@ func FindCheckIDs(ctx context.Context, resourceSelectors ...types.ResourceSelect

var allChecks []uuid.UUID
for _, resourceSelector := range resourceSelectors {
hash := "FindChecks-CachePrefix" + resourceSelector.Hash()
cacheToUse := getterCache
if resourceSelector.Immutable() {
cacheToUse = immutableCache
items, err := queryResourceSelector(ctx, resourceSelector, "checks", "labels", nil)
if err != nil {
return nil, err
}

if val, ok := cacheToUse.Get(hash); ok {
allChecks = append(allChecks, val.([]uuid.UUID)...)
continue
}

if query := resourceSelectorQuery(ctx, resourceSelector, "labels", nil); query != nil {
var ids []uuid.UUID
if err := query.Model(&models.Check{}).Find(&ids).Error; err != nil {
return nil, fmt.Errorf("error getting checks with selectors[%v]: %w", resourceSelector, err)
}

if len(ids) == 0 {
cacheToUse.Set(hash, ids, time.Minute) // if results weren't found cache it shortly even on the immutable cache
} else {
cacheToUse.SetDefault(hash, ids)
}

allChecks = append(allChecks, ids...)
}
allChecks = append(allChecks, items...)
}

return allChecks, nil
Expand Down
30 changes: 4 additions & 26 deletions query/components.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package query

import (
"fmt"
"time"

"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
Expand Down Expand Up @@ -40,31 +37,12 @@ func FindComponents(ctx context.Context, resourceSelectors types.ResourceSelecto
func FindComponentIDs(ctx context.Context, resourceSelectors ...types.ResourceSelector) ([]uuid.UUID, error) {
var allComponents []uuid.UUID
for _, resourceSelector := range resourceSelectors {
hash := "FindComponents-CachePrefix" + resourceSelector.Hash()
cacheToUse := getterCache
if resourceSelector.Immutable() {
cacheToUse = immutableCache
}

if val, ok := cacheToUse.Get(hash); ok {
allComponents = append(allComponents, val.([]uuid.UUID)...)
continue
items, err := queryResourceSelector(ctx, resourceSelector, "components", "labels", allowedColumnFieldsInComponents)
if err != nil {
return nil, err
}

if query := resourceSelectorQuery(ctx, resourceSelector, "labels", allowedColumnFieldsInComponents); query != nil {
var ids []uuid.UUID
if err := query.Model(&models.Component{}).Find(&ids).Error; err != nil {
return nil, fmt.Errorf("error getting components with selectors[%v]: %w", resourceSelector, err)
}

if len(ids) == 0 {
cacheToUse.Set(hash, ids, time.Minute) // if results weren't found cache it shortly even on the immutable cache
} else {
cacheToUse.SetDefault(hash, ids)
}

allComponents = append(allComponents, ids...)
}
allComponents = append(allComponents, items...)
}

return allComponents, nil
Expand Down
28 changes: 4 additions & 24 deletions query/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
gocontext "context"
"database/sql"
"fmt"
"time"

"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
Expand Down Expand Up @@ -51,31 +50,12 @@ func FindConfigIDsByResourceSelector(ctx context.Context, resourceSelectors ...t
var allConfigs []uuid.UUID

for _, resourceSelector := range resourceSelectors {
hash := "FindConfigs-CachePrefix" + resourceSelector.Hash()
cacheToUse := getterCache
if resourceSelector.Immutable() {
cacheToUse = immutableCache
}

if val, ok := cacheToUse.Get(hash); ok {
allConfigs = append(allConfigs, val.([]uuid.UUID)...)
continue
items, err := queryResourceSelector(ctx, resourceSelector, "config_items", "tags", allowedColumnFieldsInConfigs)
if err != nil {
return nil, err
}

if query := resourceSelectorQuery(ctx, resourceSelector, "tags", allowedColumnFieldsInConfigs); query != nil {
var ids []uuid.UUID
if err := query.Model(&models.ConfigItem{}).Find(&ids).Error; err != nil {
return nil, fmt.Errorf("error getting configs with selectors[%v]: %w", resourceSelector, err)
}

if len(ids) == 0 {
cacheToUse.Set(hash, ids, time.Minute) // if results weren't found cache it shortly even on the immutable cache
} else {
cacheToUse.SetDefault(hash, ids)
}

allConfigs = append(allConfigs, ids...)
}
allConfigs = append(allConfigs, items...)
}

return allConfigs, nil
Expand Down
52 changes: 45 additions & 7 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@ package query

import (
"fmt"
"strings"
"time"

"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/duration"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
"gorm.io/gorm"
"github.com/patrickmn/go-cache"
)

// resourceSelectorQuery returns an ANDed query from all the fields
func resourceSelectorQuery(ctx context.Context, resourceSelector types.ResourceSelector, labelsColumn string, allowedColumnsAsFields []string) *gorm.DB {
// queryResourceSelector runs the given resourceSelector and returns the resource ids
func queryResourceSelector(ctx context.Context, resourceSelector types.ResourceSelector, table, labelsColumn string, allowedColumnsAsFields []string) ([]uuid.UUID, error) {
if resourceSelector.IsEmpty() {
return nil
return nil, nil
}

query := ctx.DB().Debug().Select("id").Where("deleted_at IS NULL")
hash := fmt.Sprintf("%s-%s", table, resourceSelector.Hash())
cacheToUse := getterCache
if resourceSelector.Immutable() {
cacheToUse = immutableCache
}

if resourceSelector.Cache != "no-cache" {
if val, ok := cacheToUse.Get(hash); ok {
return val.([]uuid.UUID), nil
}
}

query := ctx.DB().Select("id").Where("deleted_at IS NULL").Table(table)

if resourceSelector.ID != "" {
query = query.Where("id = ?", resourceSelector.ID)
Expand All @@ -43,7 +58,7 @@ func resourceSelectorQuery(ctx context.Context, resourceSelector types.ResourceS
} else { // assume it's an agent name
agent, err := FindCachedAgent(ctx, resourceSelector.Agent)
if err != nil {
return nil
return nil, err
}
query = query.Where("agent_id = ?", agent.ID)
}
Expand Down Expand Up @@ -86,5 +101,28 @@ func resourceSelectorQuery(ctx context.Context, resourceSelector types.ResourceS
}
}

return query
var output []uuid.UUID
if err := query.Find(&output).Error; err != nil {
return nil, err
}

if resourceSelector.Cache != "no-store" {
cacheDuration := cache.NoExpiration
if len(output) == 0 {
cacheDuration = time.Minute // if results weren't found, cache it shortly even on the immutable cache
}

if strings.HasPrefix(resourceSelector.Cache, "max-age=") {
d, err := duration.ParseDuration(strings.TrimPrefix(resourceSelector.Cache, "max-age="))
if err != nil {
return nil, err
}

cacheDuration = time.Duration(d)
}

cacheToUse.Set(hash, output, cacheDuration)
}

return output, nil
}
6 changes: 3 additions & 3 deletions tests/getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/samber/lo"
)

var _ = ginkgo.Describe("FindChecks", func() {
var _ = ginkgo.Describe("FindChecks", ginkgo.Focus, func() {
type testRecord struct {
Name string
Selectors []types.ResourceSelector
Expand Down Expand Up @@ -75,7 +75,7 @@ var _ = ginkgo.Describe("FindChecks", func() {
}
})

var _ = ginkgo.Describe("FindConfigs", func() {
var _ = ginkgo.Describe("FindConfigs", ginkgo.Focus, func() {
type testRecord struct {
Name string
Selectors []types.ResourceSelector
Expand Down Expand Up @@ -131,7 +131,7 @@ var _ = ginkgo.Describe("FindConfigs", func() {
}
})

var _ = ginkgo.Describe("FindComponent", func() {
var _ = ginkgo.Describe("FindComponent", ginkgo.Focus, func() {
type testRecord struct {
Name string
Selectors []types.ResourceSelector
Expand Down

0 comments on commit bd40fdc

Please sign in to comment.