Skip to content

Commit

Permalink
Merge pull request #553 from MUzairS15/model-svg
Browse files Browse the repository at this point in the history
Write SVG to file system for entites.
  • Loading branch information
Mohd Uzair authored Aug 7, 2024
2 parents 4aad0da + e706e48 commit f0c4a9a
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 31 deletions.
1 change: 0 additions & 1 deletion models/meshmodel/core/v1beta1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (m *Model) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error
m.ID = modelID
m.CategoryID = id
m.RegistrantID = hostID
m.Status = entity.Enabled
err = db.Omit(clause.Associations).Create(&m).Error
if err != nil {
return uuid.UUID{}, err
Expand Down
4 changes: 0 additions & 4 deletions models/meshmodel/registry/v1beta1/model_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ func (mf *ModelFilter) Get(db *database.Handler) ([]entity.Entity, int64, int, e

finder = finder.Where("model_dbs.status = ?", status)

if mf.Status != "" {
finder = finder.Where("model_dbs.status = ?", mf.Status)
}

includeComponents = mf.Components
includeRelationships = mf.Relationships

Expand Down
54 changes: 28 additions & 26 deletions models/registration/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,60 @@ import (

// packaingUnit is the representation of the atomic unit that can be registered into the capabilities registry
type packagingUnit struct {
model v1beta1.Model
components []v1beta1.ComponentDefinition
model v1beta1.Model
components []v1beta1.ComponentDefinition
relationships []v1alpha2.RelationshipDefinition
_ []v1beta1.PolicyDefinition
_ []v1beta1.PolicyDefinition
}

type RegistrationHelper struct {
regManager *meshmodel.RegistryManager
regErrStore RegistrationErrorStore
regManager *meshmodel.RegistryManager
regErrStore RegistrationErrorStore
svgBaseDir string
}

func NewRegistrationHelper(regm *meshmodel.RegistryManager, regErrStore RegistrationErrorStore) RegistrationHelper {
return RegistrationHelper{ regManager: regm, regErrStore: regErrStore}
func NewRegistrationHelper(svgBaseDir string, regm *meshmodel.RegistryManager, regErrStore RegistrationErrorStore) RegistrationHelper {
return RegistrationHelper{svgBaseDir: svgBaseDir, regManager: regm, regErrStore: regErrStore}
}

/*
Register will accept a RegisterableEntity (dir, tar or oci for now).
Register will accept a RegisterableEntity (dir, tar or oci for now).
*/
func (rh *RegistrationHelper) Register(entity RegisterableEntity) error {
// get the packaging units
pu, err := entity.PkgUnit(rh.regErrStore)
if(err != nil){
if err != nil {
// given input is not a valid model, or could not walk the directory
return err
}
// fmt.Printf("Packaging Unit: Model name: %s, comps: %d, rels: %d\n", pu.model.Name, len(pu.components), len(pu.relationships))
return rh.register(pu)
}


/*
register will return an error if it is not able to register the `model`.
If there are errors when registering other entities, they are handled properly but does not stop the registration process.
register will return an error if it is not able to register the `model`.
If there are errors when registering other entities, they are handled properly but does not stop the registration process.
*/
func (rh *RegistrationHelper)register(pkg packagingUnit) error {
func (rh *RegistrationHelper) register(pkg packagingUnit) error {
// 1. Register the model
model := pkg.model

// Dont register anything else if registrant is not there
if(model.Registrant.Hostname == ""){
if model.Registrant.Hostname == "" {
err := ErrMissingRegistrant(model.Name)
rh.regErrStore.InsertEntityRegError(model.Registrant.Hostname, "",entity.Model, model.Name, err)
rh.regErrStore.InsertEntityRegError(model.Registrant.Hostname, "", entity.Model, model.Name, err)
return err
}
writeAndReplaceSVGWithFileSystemPath(model.Metadata, rh.svgBaseDir, model.Name, model.Name) //Write SVG for models
_, _, err := rh.regManager.RegisterEntity(
v1beta1.Host{Hostname: model.Registrant.Hostname,},
v1beta1.Host{Hostname: model.Registrant.Hostname},
&model,
)
)

// If model cannot be registered, don't register anything else
if err != nil {
err = ErrRegisterEntity(err, string(model.Type()), model.DisplayName)
rh.regErrStore.InsertEntityRegError(model.Registrant.Hostname, "",entity.Model, model.Name, err)
rh.regErrStore.InsertEntityRegError(model.Registrant.Hostname, "", entity.Model, model.Name, err)
return err
}

Expand All @@ -70,14 +71,15 @@ func (rh *RegistrationHelper)register(pkg packagingUnit) error {
// 2. Register components
for _, comp := range pkg.components {
comp.Model = model
writeAndReplaceSVGWithFileSystemPath(comp.Metadata, rh.svgBaseDir, comp.Model.Name, comp.Component.Kind) //Write SVG on components
_, _, err := rh.regManager.RegisterEntity(
v1beta1.Host{Hostname: hostname,},
&comp,
v1beta1.Host{Hostname: hostname},
&comp,
)
if err != nil {
err = ErrRegisterEntity(err, string(comp.Type()), comp.DisplayName)
rh.regErrStore.InsertEntityRegError(hostname, modelName ,entity.ComponentDefinition, comp.DisplayName, err)
}
if err != nil {
err = ErrRegisterEntity(err, string(comp.Type()), comp.DisplayName)
rh.regErrStore.InsertEntityRegError(hostname, modelName, entity.ComponentDefinition, comp.DisplayName, err)
}
}

// 3. Register relationships
Expand All @@ -88,8 +90,8 @@ func (rh *RegistrationHelper)register(pkg packagingUnit) error {
}, &rel)
if err != nil {
err = ErrRegisterEntity(err, string(rel.Type()), rel.Kind)
rh.regErrStore.InsertEntityRegError(hostname, modelName ,entity.RelationshipDefinition, rel.ID.String(), err)
rh.regErrStore.InsertEntityRegError(hostname, modelName, entity.RelationshipDefinition, rel.ID.String(), err)
}
}
return nil
return nil
}
134 changes: 134 additions & 0 deletions models/registration/svg_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package registration

import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
)

var hashCheckSVG = make(map[string]string)
var mx sync.Mutex
var UISVGPaths = make([]string, 1)

func writeHashCheckSVG(key string, val string) {
mx.Lock()
hashCheckSVG[key] = val
mx.Unlock()
}

func writeAndReplaceSVGWithFileSystemPath(metadata map[string]interface{}, baseDir, dirname, filename string) {
filename = strings.ToLower(filename)
successCreatingDirectory := false
defer func() {
if successCreatingDirectory {
UISVGPaths = append(UISVGPaths, filepath.Join(baseDir, dirname))
}
}()
if metadata["svgColor"] != "" {
path := filepath.Join(baseDir, dirname, "color")
err := os.MkdirAll(path, 0777)
if err != nil {
fmt.Println(err)
return
}
successCreatingDirectory = true

x, ok := metadata["svgColor"].(string)
if ok {
hash := md5.Sum([]byte(x))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
metadata["svgColor"] = pathsvg
goto White
}
f, err := os.Create(filepath.Join(path, filename+"-color.svg"))
if err != nil {
fmt.Println(err)
return
}
_, err = f.WriteString(x)
if err != nil {
fmt.Println(err)
return
}
metadata["svgColor"] = getRelativePathForAPI(baseDir, filepath.Join(dirname, "color", filename+"-color.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, metadata["svgColor"].(string))
}
}
White:
if metadata["svgWhite"] != "" {
path := filepath.Join(baseDir, dirname, "white")
err := os.MkdirAll(path, 0777)
if err != nil {
fmt.Println(err)
return
}
successCreatingDirectory = true

x, ok := metadata["svgWhite"].(string)
if ok {
hash := md5.Sum([]byte(x))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
metadata["svgWhite"] = pathsvg
goto Complete
}
f, err := os.Create(filepath.Join(path, filename+"-white.svg"))
if err != nil {
fmt.Println(err)
return
}
_, err = f.WriteString(x)
if err != nil {
fmt.Println(err)
return
}
metadata["svgWhite"] = getRelativePathForAPI(baseDir, filepath.Join(dirname, "white", filename+"-white.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, metadata["svgWhite"].(string))
}
}
Complete:
if metadata["svgComplete"] != "" {
path := filepath.Join(baseDir, dirname, "complete")
err := os.MkdirAll(path, 0777)
if err != nil {
fmt.Println(err)
return
}
successCreatingDirectory = true

x, ok := metadata["svgComplete"].(string)
if ok {
hash := md5.Sum([]byte(x))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
metadata["svgComplete"] = pathsvg
return
}
f, err := os.Create(filepath.Join(path, filename+"-complete.svg"))
if err != nil {
fmt.Println(err)
return
}
_, err = f.WriteString(x)
if err != nil {
fmt.Println(err)
return
}
metadata["svgComplete"] = getRelativePathForAPI(baseDir, filepath.Join(dirname, "complete", filename+"-complete.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, metadata["svgComplete"].(string))
}
}
}

func getRelativePathForAPI(baseDir, path string) string {
ui := strings.TrimPrefix(baseDir, "../../")
return filepath.Join(ui, path)
}

0 comments on commit f0c4a9a

Please sign in to comment.