Skip to content

Commit

Permalink
chore: bring back status expression
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Aug 16, 2024
1 parent 36c4524 commit 34d9c96
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
2 changes: 0 additions & 2 deletions functions/drop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ DROP VIEW IF EXISTS configs CASCADE;
DROP VIEW IF EXISTS config_detail CASCADE;

DROP VIEW IF EXISTS config_tags CASCADE;

DROP VIEW IF EXISTS topology CASCADE;
25 changes: 21 additions & 4 deletions models/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ type Component struct {
// ConfigID is the id of the config from which this component is derived
ConfigID *uuid.UUID `json:"config_id,omitempty"`

// StatusExpr allows defining a cel expression to evaluate the status of a component
// based on the summary.
StatusExpr string `json:"status_expr,omitempty" gorm:"column:status_expr;default:null"`

// HealthExpr allows defining a cel expression to evaluate the health of a component
// based on the summary.
HealthExpr string `json:"health_expr,omitempty" gorm:"column:health_expr;default:null"`
Expand Down Expand Up @@ -158,6 +162,22 @@ func (c *Component) ObjectMeta() metav1.ObjectMeta {
}
}

func (c Component) GetStatus() (string, error) {
if c.StatusExpr != "" {
env := map[string]any{
"summary": c.Summary.AsEnv(),
}
out, err := gomplate.RunTemplate(env, gomplate.Template{Expression: c.StatusExpr})
if err != nil {
return "", fmt.Errorf("failed to evaluate status expression %s: %v", c.StatusExpr, err)
}

return out, nil
}

return string(c.Status), nil
}

func (c Component) GetHealth() (string, error) {
if c.HealthExpr != "" {
env := map[string]any{
Expand All @@ -184,10 +204,6 @@ func (c Component) GetHealth() (string, error) {
}
}

func (c Component) GetStatus() (string, error) {
return string(c.Status), nil
}

func (c *Component) AsMap(removeFields ...string) map[string]any {
return asMap(c, removeFields...)
}
Expand Down Expand Up @@ -258,6 +274,7 @@ func (component Component) Clone() Component {
ExternalId: component.ExternalId,
Schedule: component.Schedule,
Health: component.Health,
StatusExpr: component.StatusExpr,
HealthExpr: component.HealthExpr,
}

Expand Down
9 changes: 8 additions & 1 deletion query/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/flanksource/commons/collections"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/jackc/pgx/v5"
gocache "github.com/patrickmn/go-cache"
"github.com/samber/lo"
Expand All @@ -32,7 +33,7 @@ func (opt TopologyOptions) selectClause() string {
}

// parents & (incidents, analysis, checks) columns need to fetched to create the topology tree even though they may not be essential to the UI.
return "name, namespace, id, is_leaf, status, health_expr, status_reason, icon, summary, topology_type, labels, team_names, type, parent_id, parents, incidents, analysis, checks"
return "name, namespace, id, is_leaf, status, status_expr, health_expr, status_reason, icon, summary, topology_type, labels, team_names, type, parent_id, parents, incidents, analysis, checks"
}

func (opt TopologyOptions) componentWhereClause() string {
Expand Down Expand Up @@ -397,6 +398,12 @@ func generateTree(components models.Components, compChildrenMap map[string]model
c.Health = lo.ToPtr(models.Health(health))
}

if status, err := c.GetStatus(); err != nil {
return nil, err
} else {
c.Status = types.ComponentStatus(status)
}

nodes = append(nodes, c)
}

Expand Down
8 changes: 6 additions & 2 deletions schema/components.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ table "components" {
null = false
type = text
}
column "status_expr" {
null = true
type = text
}
column "health_expr" {
null = true
type = text
Expand Down Expand Up @@ -366,9 +370,9 @@ table "components" {
}
index "components_path_is_pushed_idx" {
on {
expr = "length(path)"
expr = "length(path)"
}
where = "is_pushed IS FALSE"
where = "is_pushed IS FALSE"
}
index "idx_components_deleted_at" {
columns = [column.deleted_at]
Expand Down
1 change: 1 addition & 0 deletions views/006_config_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ END;
$$ LANGUAGE plpgsql;



CREATE OR REPLACE FUNCTION drop_config_items(ids text[]) RETURNS void as $$
BEGIN
ALTER TABLE config_items
Expand Down
3 changes: 2 additions & 1 deletion views/010_topology.sql
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ CREATE OR REPLACE VIEW incident_summary_by_component AS
SELECT id, jsonb_object_agg(key, value) as incidents FROM (select id, json_object_agg(type,json) incidents from type_summary group by id, type) i, json_each(incidents) group by id;

-- Topology view
CREATE OR REPLACE VIEW topology AS
CREATE OR REPLACE VIEW
topology AS
WITH
children AS (
SELECT
Expand Down

0 comments on commit 34d9c96

Please sign in to comment.