Skip to content

Commit

Permalink
Merge pull request #275 from Revolyssup/master
Browse files Browse the repository at this point in the history
Add search on display name in Meshmodels
  • Loading branch information
Revolyssup authored Mar 7, 2023
2 parents 500374c + 37e4ed7 commit 3777ce5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
41 changes: 26 additions & 15 deletions models/meshmodel/core/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,29 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon
finder := db.Model(&ComponentDefinitionDB{}).
Select("component_definition_dbs.*, models.*").
Joins("JOIN models ON component_definition_dbs.model_id = models.id") //
if f.Name != "" {
if f.Greedy {
if f.Greedy {
if f.Name != "" && f.DisplayName != "" {
finder = finder.Where("component_definition_dbs.kind LIKE ? OR component_definition_dbs.display_name LIKE ?", f.Name+"%", f.DisplayName+"%")
} else if f.Name != "" {
finder = finder.Where("component_definition_dbs.kind LIKE ?", f.Name+"%")
} else {
} else if f.DisplayName != "" {
finder = finder.Where("component_definition_dbs.display_name LIKE ?", f.DisplayName+"%")
}
} else {
if f.Name != "" {
finder = finder.Where("component_definition_dbs.kind = ?", f.Name)
}
if f.DisplayName != "" {
finder = finder.Where("component_definition_dbs.display_name = ?", f.DisplayName)
}
}
if f.ModelName != "" && f.ModelName != "all" {
finder = finder.Where("models.name = ?", f.ModelName)
}
if f.APIVersion != "" {
finder = finder.Where("component_definition_dbs.api_version = ?", f.APIVersion)
}
if f.ModelName != "" {
finder = finder.Where("models.name = ?", f.ModelName)
}

if f.Version != "" {
finder = finder.Where("models.version = ?", f.Version)
}
Expand All @@ -134,15 +144,16 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon
}

type ComponentFilter struct {
Name string
APIVersion string
Greedy bool //when set to true - instead of an exact match, name will be prefix matched
ModelName string
Version string
Sort string //asc or desc. Default behavior is asc
OrderOn string
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
Name string
APIVersion string
Greedy bool //when set to true - instead of an exact match, name will be prefix matched
DisplayName string
ModelName string
Version string
Sort string //asc or desc. Default behavior is asc
OrderOn string
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
}

// Create the filter from map[string]interface{}
Expand Down
17 changes: 9 additions & 8 deletions models/meshmodel/core/v1alpha1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

var modelCreationLock sync.Mutex //Each component/relationship will perform a check and if the model already doesn't exist, it will create a model. This lock will make sure that there are no race conditions.
type ModelFilter struct {
Name string
Greedy bool //when set to true - instead of an exact match, name will be prefix matched
Version string
Category string
OrderOn string
Sort string //asc or desc. Default behavior is asc
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
Name string
DisplayName string //If Name is already passed, avoid passing Display name unless greedy=true, else the filter will translate to an AND returning only the models where name and display name match exactly. Ignore, if this behaviour is expected.
Greedy bool //when set to true - instead of an exact match, name will be prefix matched. Also an OR will be performed of name and display_name
Version string
Category string
OrderOn string
Sort string //asc or desc. Default behavior is asc
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
}

// swagger:response Model
Expand Down
15 changes: 12 additions & 3 deletions models/meshmodel/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,21 @@ func (rm *RegistryManager) GetModels(f types.Filter) []v1alpha1.Model {
var mod []v1alpha1.Model
finder := rm.db.Model(&mod)
if mf, ok := f.(*v1alpha1.ModelFilter); ok {
if mf.Name != "" {
if mf.Greedy {
if mf.Greedy {
if mf.Name != "" && mf.DisplayName != "" {
finder = finder.Where("name LIKE ? OR display_name LIKE ?", mf.Name+"%", mf.DisplayName+"%")
} else if mf.Name != "" {
finder = finder.Where("name LIKE ?", mf.Name+"%")
} else {
} else if mf.DisplayName != "" {
finder = finder.Where("display_name LIKE ?", mf.DisplayName+"%")
}
} else {
if mf.Name != "" {
finder = finder.Where("name = ?", mf.Name)
}
if mf.DisplayName != "" {
finder = finder.Where("display_name = ?", mf.DisplayName)
}
}
if mf.Version != "" {
finder = finder.Where("version = ?", mf.Version)
Expand Down

0 comments on commit 3777ce5

Please sign in to comment.