Skip to content

Commit

Permalink
Merge pull request #655 from flanksource/isLeaf
Browse files Browse the repository at this point in the history
feat: add isLeaf flag for the leaf nodes and fix updates logic for co…
  • Loading branch information
moshloop committed Oct 14, 2022
2 parents ff113a3 + 77bdb8e commit cda8df4
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/db/migrations/099_post_seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

INSERT INTO canary_checker_db_version(version_id, tstamp, is_applied) (
SELECT version_id, now() as tstamp, true as is_applied
FROM generate_series(100, 102) version_id
FROM generate_series(100, 103) version_id
);

11 changes: 11 additions & 0 deletions pkg/db/migrations/102_created_by.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@
ALTER TABLE canaries ADD COLUMN created_by UUID NULL;
ALTER TABLE templates ADD COLUMN created_by UUID NULL;
ALTER TABLE components ADD COLUMN created_by UUID NULL;

-- +goose Down
DROP TABLE config_component_relationships;
DROP TABLE check_component_relationships;
DROP TABLE component_relationships;
DROP FUNCTION lookup_component_by_property;
DROP TABLE components CASCADE;
DROP TABLE templates;
DROP TABLE check_statuses;
DROP TABLE checks CASCADE;
DROP TABLE canaries;
3 changes: 3 additions & 0 deletions pkg/db/migrations/103_is_leaf_components.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- +goose Up

ALTER TABLE components ADD COLUMN is_leaf BOOL DEFAULT false;
1 change: 1 addition & 0 deletions pkg/db/migrations/3_topology.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ CREATE TABLE components (
properties jsonb,
path text,
summary jsonb,
is_leaf BOOL DEFAULT false,
cost_per_minute numeric(16,4) NULL,
cost_total_1d numeric(16,4) NULL,
cost_total_7d numeric(16,4) NULL,
Expand Down
5 changes: 1 addition & 4 deletions pkg/db/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"

"time"

Expand Down Expand Up @@ -217,13 +216,12 @@ func PersistComponent(component *pkg.Component) ([]uuid.UUID, error) {
}
if existing.ID != uuid.Nil {
component.ID = existing.ID
component.DeletedAt = nil
tx = Gorm.Table("components").Clauses(
clause.OnConflict{
Columns: []clause.Column{{Name: "system_template_id"}, {Name: "name"}, {Name: "type"}, {Name: "parent_id"}},
UpdateAll: true,
},
).UpdateColumns(component)
).UpdateColumns(component).Update("deleted_at", nil) // explicitly set deleted_at to null; UpdateColumns doesn't set deleted_at to null. Needed in case a component is deleted but found again in the next sync
} else {
tx = Gorm.Table("components").Clauses(
clause.OnConflict{
Expand Down Expand Up @@ -318,7 +316,6 @@ func DeleteComponentsWithIDs(compIDs []string, deleteTime time.Time) error {
}
tx = Gorm.Table("component_relationships").Where("component_id in (?)", compIDs).UpdateColumn("deleted_at", deleteTime)
if tx.Error != nil {
fmt.Println("done from here")
return tx.Error
}
if err := Gorm.Table("check_component_relationships").Where("component_id in (?)", compIDs).UpdateColumn("deleted_at", deleteTime).Error; err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/system_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ type Component struct {
UpdatedAt time.Time `json:"updated_at,omitempty" time_format:"postgres_timestamp"`
DeletedAt *time.Time `json:"deleted_at,omitempty" time_format:"postgres_timestamp" swaggerignore:"true"`
ExternalId string `json:"external_id,omitempty"` //nolint
IsLeaf bool `json:"is_leaf"`
}

type ComponentRelationship struct {
Expand Down
12 changes: 12 additions & 0 deletions pkg/topology/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func Run(opts TopologyRunOptions, s v1.SystemTemplate) []*pkg.Component {
c.Namespace = ctx.SystemTemplate.GetNamespace()
c.Schedule = ctx.SystemTemplate.Spec.Schedule
}
results = updateLeafComponents(results)
return results
}

Expand Down Expand Up @@ -377,3 +378,14 @@ func ComponentStatusSummarySync() {
}
}
}

func updateLeafComponents(components pkg.Components) pkg.Components {
for _, component := range components {
if component.Components == nil {
component.IsLeaf = true
} else {
updateLeafComponents(component.Components)
}
}
return components
}

0 comments on commit cda8df4

Please sign in to comment.