Skip to content

Commit

Permalink
chore: revamp component health logic (#1004)
Browse files Browse the repository at this point in the history
* chore: revamp component health logic

* chore: add health comparison

* chore: simplify health logic
  • Loading branch information
yashmehrotra authored Aug 30, 2024
1 parent dadcbdd commit 5ec2692
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
12 changes: 12 additions & 0 deletions models/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ const (
HealthWarning Health = "warning"
)

func WorseHealth(healths ...Health) Health {
worst := HealthHealthy
for _, h := range healths {
if h == HealthUnhealthy {
return HealthUnhealthy
} else if h == HealthWarning {
worst = HealthWarning
}
}
return worst
}

func init() {
logger.SkipFrameContains = append(logger.SkipFrameContains, "duty/models")
}
Expand Down
25 changes: 12 additions & 13 deletions models/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c Component) GetStatus() (string, error) {
return string(c.Status), nil
}

func (c Component) GetHealth() (string, error) {
func (c Component) GetHealth() (Health, error) {
if c.HealthExpr != "" {
env := map[string]any{
"summary": c.Summary.AsEnv(),
Expand All @@ -188,24 +188,23 @@ func (c Component) GetHealth() (string, error) {
return "", fmt.Errorf("failed to evaluate health expression %s: %v", c.HealthExpr, err)
}

return out, nil
return Health(out), nil
}

// When HealthExpr is not defined, we take worse of checks, children and the component itself
var allHealths []Health
if h := lo.FromPtr(c.Health); h != "" {
return string(h), nil
allHealths = append(allHealths, h)
}

if c.Summary.Healthy > 0 && c.Summary.Unhealthy > 0 {
return string(types.ComponentStatusWarning), nil
} else if c.Summary.Unhealthy > 0 {
return string(types.ComponentStatusUnhealthy), nil
} else if c.Summary.Warning > 0 {
return string(types.ComponentStatusWarning), nil
} else if c.Summary.Healthy > 0 {
return string(types.ComponentStatusHealthy), nil
} else {
return string(types.ComponentStatusInfo), nil
for h, count := range c.Checks {
if count > 0 {
allHealths = append(allHealths, Health(h))
}
}

allHealths = append(allHealths, lo.Map(c.Components, func(item *Component, _ int) Health { return lo.FromPtr(item.Health) })...)
return WorseHealth(allHealths...), nil
}

func (c *Component) AsMap(removeFields ...string) map[string]any {
Expand Down
2 changes: 1 addition & 1 deletion query/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func generateTree(components models.Components, compChildrenMap map[string]model
if health, err := c.GetHealth(); err != nil {
return nil, err
} else {
c.Health = lo.ToPtr(models.Health(health))
c.Health = lo.ToPtr(health)
}

if status, err := c.GetStatus(); err != nil {
Expand Down

0 comments on commit 5ec2692

Please sign in to comment.