Skip to content

Commit 05d562e

Browse files
Merge pull request #607 from Yashsharma1911/yash/updateModelFilter
Add components and relationships count in model response
2 parents 1f09f47 + f45ae45 commit 05d562e

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/google/uuid v1.6.0
2525
github.com/kubernetes/kompose v1.31.1
2626
github.com/layer5io/meshery-operator v0.7.0
27-
github.com/meshery/schemas v0.7.31
27+
github.com/meshery/schemas v0.7.36
2828
github.com/nats-io/nats.go v1.31.0
2929
github.com/open-policy-agent/opa v0.67.1
3030
github.com/opencontainers/image-spec v1.1.0
@@ -39,7 +39,7 @@ require (
3939
gopkg.in/yaml.v3 v3.0.1
4040
gorm.io/driver/postgres v1.5.3
4141
gorm.io/driver/sqlite v1.5.4
42-
gorm.io/gorm v1.25.11
42+
gorm.io/gorm v1.25.12
4343
helm.sh/helm/v3 v3.13.2
4444
k8s.io/api v0.28.4
4545
k8s.io/apimachinery v0.28.4

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S
608608
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
609609
github.com/meshery/kompose v1.0.1 h1:lg8B/pkLh6762jeFsQATD8UJZZwXZf/aviC3/dzw78A=
610610
github.com/meshery/kompose v1.0.1/go.mod h1:TWhWTEMbJBUzENf4JTEtBmZRFm/r8n0nS6v4/nSD2vA=
611-
github.com/meshery/schemas v0.7.31 h1:FfVR+oErAiiEomt6sTZI5uKhoyU26QXawT6UDZHbthI=
612-
github.com/meshery/schemas v0.7.31/go.mod h1:UfiO+zm92yLkaJP0aroNwVnjuozoh793AWDXrKDYmT0=
611+
github.com/meshery/schemas v0.7.36 h1:KQOz/SODr+T6fDS2lDSrNo/Eu3LCaEUGIRauQtvXkjg=
612+
github.com/meshery/schemas v0.7.36/go.mod h1:wOh519/EDxiYlC4aeGv74ru+t9h9VJ4P2JYIvSfdPWQ=
613613
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
614614
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
615615
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
@@ -1198,8 +1198,8 @@ gorm.io/driver/postgres v1.5.3 h1:qKGY5CPHOuj47K/VxbCXJfFvIUeqMSXXadqdCY+MbBU=
11981198
gorm.io/driver/postgres v1.5.3/go.mod h1:F+LtvlFhZT7UBiA81mC9W6Su3D4WUhSboc/36QZU0gk=
11991199
gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0=
12001200
gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
1201-
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
1202-
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
1201+
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
1202+
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
12031203
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
12041204
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
12051205
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=

models/meshmodel/registry/v1beta1/model_filter.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ type ModelFilter struct {
2929
Components bool
3030
Relationships bool
3131
Status string
32+
// When Trim is true it will only send necessary models data
33+
// like: component count, relationship count, id and name of model
34+
Trim bool
3235
}
3336

3437
// Create the filter from map[string]interface{}
@@ -182,27 +185,48 @@ func (mf *ModelFilter) Get(db *database.Handler) ([]entity.Entity, int64, int, e
182185
for _, modelDB := range modelWithCategories {
183186
// resolve for loop scope
184187
_modelDB := modelDB
185-
if includeComponents {
186-
var components []component.ComponentDefinition
187-
finder := db.Model(&component.ComponentDefinition{}).
188-
Select("component_definition_dbs.id, component_definition_dbs.component, component_definition_dbs.display_name, component_definition_dbs.metadata, component_definition_dbs.schema_version, component_definition_dbs.version,component_definition_dbs.styles,component_definition_dbs.capabilities").
189-
Where("component_definition_dbs.model_id = ?", _modelDB.Id)
190-
if err := finder.Scan(&components).Error; err != nil {
191-
return nil, 0, 0, err
188+
var componentCount int64
189+
db.Model(&component.ComponentDefinition{}).Where("component_definition_dbs.model_id = ?", _modelDB.Id).Count(&componentCount)
190+
var relationshipCount int64
191+
db.Model(&relationship.RelationshipDefinition{}).Where("relationship_definition_dbs.model_id = ?", _modelDB.Id).Count(&relationshipCount)
192+
_modelDB.ComponentsCount = int(componentCount)
193+
_modelDB.RelationshipsCount = int(relationshipCount)
194+
195+
// If Trim is true, only include the id, name, counts and metadata
196+
if mf.Trim {
197+
trimmedModel := &model.ModelDefinition{
198+
Id: _modelDB.Id,
199+
Name: _modelDB.Name,
200+
DisplayName: _modelDB.DisplayName,
201+
Metadata: _modelDB.Metadata,
202+
ComponentsCount: int(componentCount),
203+
RelationshipsCount: int(relationshipCount),
192204
}
193-
_modelDB.Components = components
194-
}
195-
if includeRelationships {
196-
var relationships []relationship.RelationshipDefinition
197-
finder := db.Model(&relationship.RelationshipDefinition{}).
198-
Select("relationship_definition_dbs.*").
199-
Where("relationship_definition_dbs.model_id = ?", _modelDB.Id)
200-
if err := finder.Scan(&relationships).Error; err != nil {
201-
return nil, 0, 0, err
205+
defs = append(defs, trimmedModel)
206+
207+
} else {
208+
if includeComponents {
209+
var components []component.ComponentDefinition
210+
finder := db.Model(&component.ComponentDefinition{}).
211+
Select("component_definition_dbs.*").
212+
Where("component_definition_dbs.model_id = ?", _modelDB.Id)
213+
if err := finder.Scan(&components).Error; err != nil {
214+
return nil, 0, 0, err
215+
}
216+
_modelDB.Components = components
217+
}
218+
if includeRelationships {
219+
var relationships []relationship.RelationshipDefinition
220+
finder := db.Model(&relationship.RelationshipDefinition{}).
221+
Select("relationship_definition_dbs.*").
222+
Where("relationship_definition_dbs.model_id = ?", _modelDB.Id)
223+
if err := finder.Scan(&relationships).Error; err != nil {
224+
return nil, 0, 0, err
225+
}
226+
_modelDB.Relationships = relationships
202227
}
203-
_modelDB.Relationships = relationships
228+
defs = append(defs, &_modelDB)
204229
}
205-
defs = append(defs, &_modelDB)
206230
}
207231
return defs, count, countUniqueModels(modelWithCategories), nil
208232
}

0 commit comments

Comments
 (0)