Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add components and relationships count in model response #607

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions models/meshmodel/registry/v1beta1/model_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
Components bool
Relationships bool
Status string
// When Trim is true it will only send necessary models data
// like: component count, relationship count, id and name of model
Trim bool
}

// Create the filter from map[string]interface{}
Expand Down Expand Up @@ -182,27 +185,48 @@
for _, modelDB := range modelWithCategories {
// resolve for loop scope
_modelDB := modelDB
if includeComponents {
var components []component.ComponentDefinition
finder := db.Model(&component.ComponentDefinition{}).
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").
Where("component_definition_dbs.model_id = ?", _modelDB.Id)
if err := finder.Scan(&components).Error; err != nil {
return nil, 0, 0, err
var componentCount int64
db.Model(&component.ComponentDefinition{}).Where("component_definition_dbs.model_id = ?", _modelDB.Id).Count(&componentCount)
var relationshipCount int64
db.Model(&relationship.RelationshipDefinition{}).Where("relationship_definition_dbs.model_id = ?", _modelDB.Id).Count(&relationshipCount)
_modelDB.ComponentsCount = int(componentCount)

Check failure on line 192 in models/meshmodel/registry/v1beta1/model_filter.go

View workflow job for this annotation

GitHub Actions / codecov

_modelDB.ComponentsCount undefined (type "github.com/meshery/schemas/models/v1beta1/model".ModelDefinition has no field or method ComponentsCount)
_modelDB.RelationshipsCount = int(relationshipCount)

Check failure on line 193 in models/meshmodel/registry/v1beta1/model_filter.go

View workflow job for this annotation

GitHub Actions / codecov

_modelDB.RelationshipsCount undefined (type "github.com/meshery/schemas/models/v1beta1/model".ModelDefinition has no field or method RelationshipsCount)

// If Trim is true, only include the id, name, counts and metadata
if mf.Trim {
trimmedModel := &model.ModelDefinition{
Id: _modelDB.Id,
Name: _modelDB.Name,
DisplayName: _modelDB.DisplayName,
Metadata: _modelDB.Metadata,
ComponentsCount: int(componentCount),

Check failure on line 202 in models/meshmodel/registry/v1beta1/model_filter.go

View workflow job for this annotation

GitHub Actions / codecov

unknown field ComponentsCount in struct literal of type "github.com/meshery/schemas/models/v1beta1/model".ModelDefinition
RelationshipsCount: int(relationshipCount),

Check failure on line 203 in models/meshmodel/registry/v1beta1/model_filter.go

View workflow job for this annotation

GitHub Actions / codecov

unknown field RelationshipsCount in struct literal of type "github.com/meshery/schemas/models/v1beta1/model".ModelDefinition
}
_modelDB.Components = components
}
if includeRelationships {
var relationships []relationship.RelationshipDefinition
finder := db.Model(&relationship.RelationshipDefinition{}).
Select("relationship_definition_dbs.*").
Where("relationship_definition_dbs.model_id = ?", _modelDB.Id)
if err := finder.Scan(&relationships).Error; err != nil {
return nil, 0, 0, err
defs = append(defs, trimmedModel)

} else {
if includeComponents {
var components []component.ComponentDefinition
finder := db.Model(&component.ComponentDefinition{}).
Select("component_definition_dbs.*").
Where("component_definition_dbs.model_id = ?", _modelDB.Id)
if err := finder.Scan(&components).Error; err != nil {
return nil, 0, 0, err
}
_modelDB.Components = components
}
if includeRelationships {
var relationships []relationship.RelationshipDefinition
finder := db.Model(&relationship.RelationshipDefinition{}).
Select("relationship_definition_dbs.*").
Where("relationship_definition_dbs.model_id = ?", _modelDB.Id)
if err := finder.Scan(&relationships).Error; err != nil {
return nil, 0, 0, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jougan-0 look good?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

}
_modelDB.Relationships = relationships
}
_modelDB.Relationships = relationships
defs = append(defs, &_modelDB)
}
defs = append(defs, &_modelDB)
}
return defs, count, countUniqueModels(modelWithCategories), nil
}
Loading