Skip to content

Commit

Permalink
Merge pull request #428 from VihasMakwana/api-enhancements
Browse files Browse the repository at this point in the history
[feat][chores] - Add enchanments for faster data retrieval.
  • Loading branch information
MUzairS15 authored Dec 9, 2023
2 parents dd2f363 + b6f1153 commit b741e2a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion models/meshmodel/core/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
27 changes: 18 additions & 9 deletions models/meshmodel/core/v1alpha1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion models/meshmodel/core/v1alpha1/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
27 changes: 27 additions & 0 deletions models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit b741e2a

Please sign in to comment.