Skip to content

Commit

Permalink
Merge pull request #1189 from flanksource/agent-id-filters
Browse files Browse the repository at this point in the history
chore: use agent_id filter in db queries
  • Loading branch information
moshloop authored Jul 31, 2023
2 parents afcd531 + 45ce705 commit dd6c220
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
17 changes: 7 additions & 10 deletions pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ func GetAllCanariesForSync() ([]pkg.Canary, error) {
return _canaries, nil
}

func GetAllChecks() ([]pkg.Check, error) {
var checks []pkg.Check
if err := Gorm.Find(&checks).Error; err != nil {
return nil, err
}
return checks, nil
}

func PersistCheck(check pkg.Check, canaryID uuid.UUID) (uuid.UUID, error) {
check.CanaryID = canaryID
name := check.GetName()
Expand Down Expand Up @@ -257,7 +249,10 @@ func GetCheck(id string) (*pkg.Check, error) {

func FindCanary(namespace, name string) (*pkg.Canary, error) {
var model pkg.Canary
if err := Gorm.Where("namespace = ? AND name = ?", namespace, name).First(&model).Error; err != nil {
if err := Gorm.
Where("namespace = ? AND name = ?", namespace, name).
Where("agent_id = '00000000-0000-0000-0000-000000000000'").
First(&model).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand All @@ -269,7 +264,9 @@ func FindCanary(namespace, name string) (*pkg.Canary, error) {

func FindCheck(canary pkg.Canary, name string) (*pkg.Check, error) {
var model pkg.Check
if err := Gorm.Where("canary_id = ? AND name = ?", canary.ID.String(), name).First(&model).Error; err != nil {
if err := Gorm.Where("canary_id = ? AND name = ?", canary.ID.String(), name).
Where("agent_id = '00000000-0000-0000-0000-000000000000'").
First(&model).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/db/canary_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ func GetChecksWithLabelSelector(labelSelector string) (selectedChecks pkg.Checks
}
}
var checks pkg.Checks
if err := Gorm.Table("checks").Where("labels @> ?", types.JSONStringMap(labels)).Find(&checks).Error; err != nil {
if err := Gorm.Table("checks").
Where("labels @> ?", types.JSONStringMap(labels)).
Where("agent_id = '00000000-0000-0000-0000-000000000000'").
Where("deleted_at IS NULL").
Find(&checks).Error; err != nil {
return nil, err
}
for _, c := range checks {
uninqueChecks[c.ID.String()] = c
}
for _, k := range onlyKeys {
var canaries pkg.Checks
if err := Gorm.Table("checks").Where("labels ?? ?", k).Find(&canaries).Error; err != nil {
if err := Gorm.Table("checks").
Where("labels ?? ?", k).
Where("agent_id = '00000000-0000-0000-0000-000000000000'").
Where("deleted_at IS NULL").
Find(&canaries).Error; err != nil {
continue
}
for _, c := range canaries {
Expand Down
7 changes: 4 additions & 3 deletions pkg/db/config_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"gorm.io/gorm/clause"

"github.com/flanksource/canary-checker/pkg"
//"github.com/flanksource/canary-checker/pkg/db/types"
"github.com/flanksource/commons/logger"
)

Expand All @@ -22,7 +21,7 @@ type ConfigComponentRelationship struct {
}

func configQuery(config pkg.Config) *gorm.DB {
query := Gorm.Table("config_items")
query := Gorm.Table("config_items").Where("agent_id = '00000000-0000-0000-0000-000000000000'")
if config.ConfigClass != "" {
query = query.Where("config_class = ?", config.ConfigClass)
}
Expand Down Expand Up @@ -67,7 +66,9 @@ func FindConfig(config pkg.Config) (*pkg.Config, error) {

func FindConfigForComponent(componentID, configType string) ([]pkg.Config, error) {
var dbConfigObjects []pkg.Config
relationshipQuery := Gorm.Table("config_component_relationships").Select("config_id").Where("component_id = ? AND deleted_at IS NULL", componentID)
relationshipQuery := Gorm.Table("config_component_relationships").
Select("config_id").
Where("component_id = ? AND deleted_at IS NULL", componentID)
query := Gorm.Table("config_items").Where("id IN (?)", relationshipQuery)
if configType != "" {
query = query.Where("type = @config_type OR config_class = @config_type", sql.Named("config_type", configType))
Expand Down

0 comments on commit dd6c220

Please sign in to comment.