Skip to content

Commit

Permalink
Merge pull request #406 from theBeginner86/theBeginner86/chore/10
Browse files Browse the repository at this point in the history
[meshmodel] Add new `Registrant` filter for Models Construct
  • Loading branch information
theBeginner86 authored Nov 6, 2023
2 parents 92f26aa + c481d6b commit b33accc
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 for a given 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 b33accc

Please sign in to comment.