Skip to content

Commit

Permalink
feat: add topology/component db functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Jun 20, 2024
1 parent c94f795 commit 729ec86
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type HTTPSuccess struct {
Payload any `json:"payload,omitempty"`
}

func WriteSuccess(c echo.Context, payload any) error {
return c.JSON(http.StatusOK, HTTPSuccess{Message: "success", Payload: payload})
}

func WriteError(c echo.Context, err error) error {
code, message, data := ErrorCode(err), ErrorMessage(err), ErrorData(err)

Expand Down
20 changes: 20 additions & 0 deletions models/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,23 @@ func (c Canary) TableName() string {
func (c Canary) AsMap(removeFields ...string) map[string]any {
return asMap(c, removeFields...)
}

func DeleteChecksForCanary(db *gorm.DB, id string) ([]string, error) {
var checkIDs []string
var checks []Check
err := db.Model(&checks).
Table("checks").
Clauses(clause.Returning{Columns: []clause.Column{{Name: "id"}}}).
Where("canary_id = ? and deleted_at IS NULL", id).
UpdateColumn("deleted_at", Now()).
Error

for _, c := range checks {
checkIDs = append(checkIDs, c.ID.String())
}
return checkIDs, err
}

func DeleteCheckComponentRelationshipsForCanary(db *gorm.DB, id string) error {
return db.Table("check_component_relationships").Where("canary_id = ?", id).UpdateColumn("deleted_at", Now()).Error
}
99 changes: 99 additions & 0 deletions models/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,41 @@ func (c Component) GetType() string {
return c.Type
}

func (c *Component) Save(db *gorm.DB) error {
if c.ParentId != nil && !strings.Contains(c.Path, c.ParentId.String()) {
if c.Path == "" {
c.Path = c.ParentId.String()
} else {
c.Path += "." + c.ParentId.String()
}
}
err := db.Clauses(
clause.OnConflict{
Columns: []clause.Column{{Name: "topology_id"}, {Name: "name"}, {Name: "type"}, {Name: "parent_id"}},
UpdateAll: true,
},
clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
UpdateAll: true,
},
).Create(c).Error

if err != nil {
return err
}

if len(c.Components) > 0 {
for _, child := range c.Components {
child.TopologyID = c.TopologyID
child.ParentId = &c.ID
if err := child.Save(db); err != nil {
return err
}
}
}
return err
}

func (c Component) GetLabelsMatcher() labels.Labels {
return componentLabelsProvider{c}
}
Expand Down Expand Up @@ -376,6 +411,70 @@ func (components Components) Find(name string) *Component {
return nil
}

func (components Components) Save(db *gorm.DB) error {
for _, component := range components {
if err := component.Save(db); err != nil {
return err
}
}
return nil
}

// DeleteComponentsWithID deletes all components with specified ids.
func DeleteComponentsWithIDs(db *gorm.DB, compIDs []string) error {
if err := db.Table("components").Where("id in (?)", compIDs).UpdateColumn("deleted_at", Now()).Error; err != nil {
return err
}
if err := db.Table("component_relationships").Where("component_id in (?)", compIDs).UpdateColumn("deleted_at", Now()).Error; err != nil {
return err
}
if err := db.Table("check_component_relationships").Where("component_id in (?)", compIDs).UpdateColumn("deleted_at", Now()).Error; err != nil {
return err
}
for _, compID := range compIDs {
if err := DeleteInlineCanariesForComponent(db, compID); err != nil {
logger.Errorf("Error deleting component[%s] relationship: %v", compID, err)
}

if err := DeleteComponentChildren(db, compID); err != nil {
logger.Errorf("Error deleting component[%s] children: %v", compID, err)
}
}
return nil
}

func DeleteComponentChildren(db *gorm.DB, componentID string) error {
return db.Table("components").
Where("path LIKE ?", "%"+componentID+"%").
Update("deleted_at", Now()).
Error
}

func DeleteInlineCanariesForComponent(db *gorm.DB, componentID string) error {
var rows []struct {
ID string
}
source := "component/" + componentID
if err := db.
Model(&rows).
Table("canaries").
Where("source = ?", source).
Clauses(clause.Returning{Columns: []clause.Column{{Name: "id"}}}).
UpdateColumn("deleted_at", Now()).Error; err != nil {
return err
}

for _, r := range rows {
if _, err := DeleteChecksForCanary(db, r.ID); err != nil {
logger.Errorf("Error deleting checks for canary[%s]: %v", r.ID, err)
}
if err := DeleteCheckComponentRelationshipsForCanary(db, r.ID); err != nil {
logger.Errorf("Error deleting check component relationships for canary[%s]: %v", r.ID, err)
}
}
return nil
}

type Text struct {
Tooltip string `json:"tooltip,omitempty"`
Icon string `json:"icon,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions models/local_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package models
import (
"database/sql/driver"
"time"

"gorm.io/gorm"
"gorm.io/gorm/clause"
)

const TimeFormat = "2006-01-02T15:04:05"

type LocalTime time.Time

func Now() clause.Expr {
return gorm.Expr("NOW()")
}

func (t *LocalTime) UnmarshalJSON(data []byte) (err error) {
if len(data) == 2 {
*t = LocalTime(time.Time{})
Expand Down
10 changes: 10 additions & 0 deletions models/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ func (Topology) TableName() string {
func (t *Topology) AsMap(removeFields ...string) map[string]any {
return asMap(t, removeFields...)
}

func (t *Topology) Save(db *gorm.DB) error {
err := db.Clauses(Topology{}.OnConflictClause(),
clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{"labels", "spec"}),
},
).Create(t).Error
return err
}

0 comments on commit 729ec86

Please sign in to comment.