Skip to content

Commit

Permalink
add new filter for meshmodel GET request
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Singh <pranavsingh02@hotmail.com>
  • Loading branch information
theBeginner86 committed Nov 5, 2023
1 parent 92f26aa commit e2fca34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions models/meshmodel/core/v1alpha1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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
Registrant string //name of the registrant of the model
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 behavior 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
Expand Down
11 changes: 9 additions & 2 deletions models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1
type modelWithCategories struct {
v1alpha1.ModelDB
v1alpha1.CategoryDB
Registry
Host
}

countUniqueModels := func(models []modelWithCategories) int {
Expand All @@ -307,8 +309,10 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1

var modelWithCategoriess []modelWithCategories
finder := db.Model(&v1alpha1.ModelDB{}).
Select("model_dbs.*, category_dbs.*").
Joins("JOIN category_dbs ON model_dbs.category_id = category_dbs.id")
Select("model_dbs.*, category_dbs.*", "registries.*", "hosts.*").
Joins("JOIN category_dbs ON model_dbs.category_id = category_dbs.id").
Joins("JOIN registries ON registries.entity = model_dbs.id").
Joins("JOIN hosts ON hosts.id = registries.registrant_id")

// total count before pagination
var count int64
Expand Down Expand Up @@ -336,6 +340,9 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1
if mf.Category != "" {
finder = finder.Where("category_dbs.name = ?", mf.Category)
}
if mf.Registrant != "" {
finder = finder.Where("hosts.hostname = ?", mf.Registrant)
}
if mf.OrderOn != "" {
if mf.Sort == "desc" {
finder = finder.Order(clause.OrderByColumn{Column: clause.Column{Name: mf.OrderOn}, Desc: true})
Expand Down

0 comments on commit e2fca34

Please sign in to comment.