component.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package models
  2. import (
  3. "encoding/json"
  4. "time"
  5. "gorm.io/gorm"
  6. )
  7. type Component struct {
  8. ID string `json:"id" gorm:"primaryKey"`
  9. Name string `json:"name" gorm:"not null"`
  10. Description string `json:"description"`
  11. Type ComponentType `json:"type" gorm:"index"` // frontend, backend, api, database, or microservice
  12. Language string `json:"language"` // Programming language or framework
  13. Version string `json:"version"` // Version number (e.g., 1.0.0)
  14. // Configuration details
  15. ConfigFile string `json:"configFile" gorm:"type:text"` // JSON configuration as a string
  16. EnvVariables string `json:"envVariables" gorm:"type:text"` // Environment variables as a string
  17. // Source code details
  18. Repository string `json:"repository"` // Git repository URL
  19. Branch string `json:"branch" gorm:"default:'main'"` // Git branch (default: main)
  20. BuildCommand string `json:"buildCommand"` // Command to build the app
  21. // Resource allocation - stored as JSON
  22. ResourcesJSON string `json:"-" gorm:"column:resources;type:text"`
  23. ScaleSettingsJSON string `json:"-" gorm:"column:scale_settings;type:text"`
  24. // Virtual fields for ORM serialization/deserialization
  25. Resources ResourceRequirements `json:"resources" gorm:"-"`
  26. ScaleSettings ScaleSettings `json:"scaleSettings" gorm:"-"`
  27. CreatedAt time.Time `json:"createdAt" gorm:"autoCreateTime"`
  28. UpdatedAt time.Time `json:"updatedAt" gorm:"autoUpdateTime"`
  29. CreatedBy string `json:"createdBy" gorm:"index"` // User ID who created the component
  30. DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // Soft delete support
  31. // Relationships
  32. Deployments []DeployedComponent `json:"deployments" gorm:"foreignKey:ComponentID"` // Components deployed in deployments
  33. }
  34. type ResourceRequirements struct {
  35. CPU string `json:"cpu"` // e.g., "0.5"
  36. Memory string `json:"memory"` // e.g., "512Mi"
  37. Storage string `json:"storage"` // e.g., "1Gi"
  38. }
  39. type ScaleSettings struct {
  40. MinInstances int `json:"minInstances"` // Minimum number of instances
  41. MaxInstances int `json:"maxInstances"` // Maximum number of instances
  42. CPUThreshold int `json:"cpuThreshold"` // CPU threshold for scaling (percentage)
  43. }
  44. // ComponentType represents the type of component
  45. type ComponentType string
  46. const (
  47. Frontend ComponentType = "frontend"
  48. Backend ComponentType = "backend"
  49. API ComponentType = "api"
  50. Database ComponentType = "database"
  51. Microservice ComponentType = "microservice"
  52. )
  53. // BeforeSave serializes the embedded JSON fields
  54. func (c *Component) BeforeSave(tx *gorm.DB) error {
  55. var err error
  56. // Marshal Resources to JSON
  57. resourcesJSON, err := json.Marshal(c.Resources)
  58. if err != nil {
  59. return err
  60. }
  61. c.ResourcesJSON = string(resourcesJSON)
  62. // Marshal ScaleSettings to JSON
  63. scaleSettingsJSON, err := json.Marshal(c.ScaleSettings)
  64. if err != nil {
  65. return err
  66. }
  67. c.ScaleSettingsJSON = string(scaleSettingsJSON)
  68. return nil
  69. }
  70. // AfterFind deserializes the JSON fields
  71. func (c *Component) AfterFind(tx *gorm.DB) error {
  72. // Unmarshal Resources from JSON
  73. if c.ResourcesJSON != "" {
  74. if err := json.Unmarshal([]byte(c.ResourcesJSON), &c.Resources); err != nil {
  75. return err
  76. }
  77. }
  78. // Unmarshal ScaleSettings from JSON
  79. if c.ScaleSettingsJSON != "" {
  80. if err := json.Unmarshal([]byte(c.ScaleSettingsJSON), &c.ScaleSettings); err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }