Skip to content

Commit

Permalink
Merge pull request #383 from Jougan-0/GetHostsFromDatabase
Browse files Browse the repository at this point in the history
feat:retrieve registrants and number of models components relationshi…
  • Loading branch information
theBeginner86 authored Oct 16, 2023
2 parents 2139daa + 870c5fd commit 94baa5b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
37 changes: 37 additions & 0 deletions models/meshmodel/core/v1alpha1/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package v1alpha1

import "github.com/google/uuid"

type MeshModelHostsWithEntitySummary struct {
ID uuid.UUID `json:"id"`
Hostname string `json:"hostname"`
Port int `json:"port"`
Summary EntitySummary `json:"summary"`
}
type EntitySummary struct {
Models int64 `json:"models"`
Components int64 `json:"components"`
Relationships int64 `json:"relationships"`
Policies int64 `json:"policies"`
}
type MesheryHostSummaryDB struct {
HostID uuid.UUID `json:"-" gorm:"id"`
Hostname string `json:"-" gorm:"hostname"`
Port int `json:"-" gorm:"port"`
Models int64 `json:"-" gorm:"models"`
Components int64 `json:"-" gorm:"components"`
Relationships int64 `json:"-" gorm:"relationships"`
Policies int64 `json:"-" gorm:"policies"`
}

type HostFilter struct {
Name string
Greedy bool //when set to true - instead of an exact match, name will be prefix matched
Trim bool //when set to true - the schema is not returned
DisplayName string
Version string
Sort string //asc or desc. Default behavior is asc
OrderOn string
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
}
58 changes: 58 additions & 0 deletions models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,64 @@ func (rm *RegistryManager) RegisterEntity(h Host, en Entity) error {
}
}

func (rm *RegistryManager) GetRegistrants(f *v1alpha1.HostFilter) ([]v1alpha1.MeshModelHostsWithEntitySummary, int64, error) {
var result []v1alpha1.MesheryHostSummaryDB
var totalcount int64
db := rm.db

query := db.Table("hosts h").
Count(&totalcount).
Select("h.id AS host_id, h.hostname, h.port, " +
"COUNT(CASE WHEN r.type = 'component' THEN 1 END) AS components, " +
"COUNT(CASE WHEN r.type = 'model' THEN 1 END) AS models," +
"COUNT(CASE WHEN r.type = 'relationship' THEN 1 END) AS relationships, " +
"COUNT(CASE WHEN r.type = 'policy' THEN 1 END) AS policies").
Joins("LEFT JOIN registries r ON h.id = r.registrant_id").
Group("h.id, h.hostname, h.port")

if f.DisplayName != "" {
query = query.Where("hostname LIKE ?", "%"+f.DisplayName+"%")
}

if f.OrderOn != "" {
if f.Sort == "desc" {
query = query.Order(clause.OrderByColumn{Column: clause.Column{Name: f.OrderOn}, Desc: true})
} else {
query = query.Order(f.OrderOn)
}
} else {
query = query.Order("hostname")
}

query = query.Offset(f.Offset)
if f.Limit != 0 {
query = query.Limit(f.Limit)
}

err := query.Scan(&result).Error

if err != nil {
return nil, 0, err
}

var response []v1alpha1.MeshModelHostsWithEntitySummary

for _, r := range result {
res := v1alpha1.MeshModelHostsWithEntitySummary{
ID: r.HostID,
Hostname: HostnameToPascalCase(r.Hostname),
Port: r.Port,
Summary: v1alpha1.EntitySummary{
Models: r.Models,
Components: r.Components,
Relationships: r.Relationships,
},
}
response = append(response, res)
}

return response, totalcount, nil
}
func (rm *RegistryManager) GetEntities(f types.Filter) ([]Entity, *int64, *int) {
switch filter := f.(type) {
case *v1alpha1.ComponentFilter:
Expand Down

0 comments on commit 94baa5b

Please sign in to comment.