diff --git a/models/meshmodel/core/v1alpha1/component.go b/models/meshmodel/core/v1alpha1/component.go index 8daeb923..8137f635 100644 --- a/models/meshmodel/core/v1alpha1/component.go +++ b/models/meshmodel/core/v1alpha1/component.go @@ -41,7 +41,7 @@ type ComponentDefinition struct { } type ComponentDefinitionDB struct { ID uuid.UUID `json:"-"` - ModelID uuid.UUID `json:"-" gorm:"modelID"` + ModelID uuid.UUID `json:"-" gorm:"index:idx_component_definition_dbs_model_id,column:modelID"` TypeMeta DisplayName string `json:"displayName" gorm:"displayName"` Format ComponentFormat `json:"format" yaml:"format"` diff --git a/models/meshmodel/core/v1alpha1/models.go b/models/meshmodel/core/v1alpha1/models.go index b9c92455..e11734a7 100644 --- a/models/meshmodel/core/v1alpha1/models.go +++ b/models/meshmodel/core/v1alpha1/models.go @@ -24,6 +24,10 @@ type ModelFilter struct { Limit int //If 0 or unspecified then all records are returned and limit is not used Offset int Annotations string //When this query parameter is "true", only models with the "isAnnotation" property set to true are returned. When this query parameter is "false", all models except those considered to be annotation models are returned. Any other value of the query parameter results in both annoations as well as non-annotation models being returned. + + // When these are set to true, we also retrieve components/relationships associated with the model. + Components bool + Relationships bool } // Create the filter from map[string]interface{} @@ -36,16 +40,19 @@ func (cf *ModelFilter) Create(m map[string]interface{}) { // swagger:response Model type Model struct { - ID uuid.UUID `json:"-" yaml:"-"` - Name string `json:"name"` - Version string `json:"version"` - DisplayName string `json:"displayName" gorm:"modelDisplayName"` - HostName string `json:"hostname"` - HostID uuid.UUID `json:"hostID"` - DisplayHostName string `json:"displayhostname"` - Category Category `json:"category"` - Metadata map[string]interface{} `json:"metadata" yaml:"modelMetadata"` + ID uuid.UUID `json:"-" yaml:"-"` + Name string `json:"name"` + Version string `json:"version"` + DisplayName string `json:"displayName" gorm:"modelDisplayName"` + HostName string `json:"hostname"` + HostID uuid.UUID `json:"hostID"` + DisplayHostName string `json:"displayhostname"` + Category Category `json:"category"` + Metadata map[string]interface{} `json:"metadata" yaml:"modelMetadata"` + Components []ComponentDefinitionDB `json:"components"` + Relationships []RelationshipDefinitionDB `json:"relationships"` } + type ModelDB struct { ID uuid.UUID `json:"-"` CategoryID uuid.UUID `json:"-" gorm:"categoryID"` @@ -101,6 +108,8 @@ func (cmd *ModelDB) GetModel(cat Category) (c Model) { c.DisplayName = cmd.DisplayName c.Name = cmd.Name c.Version = cmd.Version + c.Components = make([]ComponentDefinitionDB, 0) + c.Relationships = make([]RelationshipDefinitionDB, 0) _ = json.Unmarshal(cmd.Metadata, &c.Metadata) return } diff --git a/models/meshmodel/core/v1alpha1/relationship.go b/models/meshmodel/core/v1alpha1/relationship.go index 61b23409..c69577d9 100644 --- a/models/meshmodel/core/v1alpha1/relationship.go +++ b/models/meshmodel/core/v1alpha1/relationship.go @@ -32,7 +32,7 @@ type RelationshipDefinition struct { type RelationshipDefinitionDB struct { ID uuid.UUID `json:"-"` - ModelID uuid.UUID `json:"-" gorm:"modelID"` + ModelID uuid.UUID `json:"-" gorm:"index:idx_relationship_definition_dbs_model_id,column:modelID"` TypeMeta Metadata []byte `json:"metadata" yaml:"metadata"` SubType string `json:"subType" yaml:"subType"` diff --git a/models/meshmodel/registry/registry.go b/models/meshmodel/registry/registry.go index 9be106d5..98e2c87d 100644 --- a/models/meshmodel/registry/registry.go +++ b/models/meshmodel/registry/registry.go @@ -317,6 +317,9 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1 // total count before pagination var count int64 + // include components and relationships in response body + var includeComponents, includeRelationships bool + if mf, ok := f.(*v1alpha1.ModelFilter); ok { if mf.Greedy { if mf.Name != "" && mf.DisplayName != "" { @@ -369,6 +372,8 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1 if mf.Offset != 0 { finder = finder.Offset(mf.Offset) } + includeComponents = mf.Components + includeRelationships = mf.Relationships } err := finder. Scan(&modelWithCategoriess).Error @@ -383,6 +388,28 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1 model.HostID = host.ID model.HostName = host.Hostname model.DisplayHostName = host.Hostname + + if includeComponents { + var components []v1alpha1.ComponentDefinitionDB + finder := db.Model(&v1alpha1.ComponentDefinitionDB{}). + Select("component_definition_dbs.kind, component_definition_dbs.display_name, component_definition_dbs.api_version, component_definition_dbs.metadata"). + Where("component_definition_dbs.model_id = ?", model.ID) + if err := finder.Scan(&components).Error; err != nil { + fmt.Println(err) + } + model.Components = components + } + if includeRelationships { + var relationships []v1alpha1.RelationshipDefinitionDB + finder := db.Model(&v1alpha1.RelationshipDefinitionDB{}). + Select("relationship_definition_dbs.*"). + Where("relationship_definition_dbs.model_id = ?", model.ID) + if err := finder.Scan(&relationships).Error; err != nil { + fmt.Println(err) + } + model.Relationships = relationships + } + m = append(m, model) } return m, count, countUniqueModels(modelWithCategoriess)