From e0ab35e86afe9162a02b238363df43dba7be8a56 Mon Sep 17 00:00:00 2001 From: TwiN Date: Thu, 18 Apr 2024 20:57:07 -0400 Subject: [PATCH] feat(ui): Implement endpoints[].ui.hide-conditions --- README.md | 1 + core/endpoint.go | 3 +++ core/endpoint_test.go | 19 ++++++++++++++++ core/ui/ui.go | 4 ++++ core/ui/ui_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 core/ui/ui_test.go diff --git a/README.md b/README.md index ddc0df65..3b198e7a 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,7 @@ You can then configure alerts to be triggered when an endpoint is unhealthy once | `endpoints[].alerts` | List of all alerts for a given endpoint.
See [Alerting](#alerting). | `[]` | | `endpoints[].client` | [Client configuration](#client-configuration). | `{}` | | `endpoints[].ui` | UI configuration at the endpoint level. | `{}` | +| `endpoints[].ui.hide-conditions` | Whether to hide conditions from the results. Note that this only hides conditions from results evaluated from the moment this was enabled. | `false` | | `endpoints[].ui.hide-hostname` | Whether to hide the hostname in the result. | `false` | | `endpoints[].ui.hide-url` | Whether to ensure the URL is not displayed in the results. Useful if the URL contains a token. | `false` | | `endpoints[].ui.dont-resolve-failed-conditions` | Whether to resolve failed conditions for the UI. | `false` | diff --git a/core/endpoint.go b/core/endpoint.go index 4e8e1592..749b1d3d 100644 --- a/core/endpoint.go +++ b/core/endpoint.go @@ -299,6 +299,9 @@ func (endpoint *Endpoint) EvaluateHealth() *Result { } result.Hostname = "" } + if endpoint.UIConfig.HideConditions { + result.ConditionResults = nil + } return result } diff --git a/core/endpoint_test.go b/core/endpoint_test.go index 3c54f5a5..82c620b7 100644 --- a/core/endpoint_test.go +++ b/core/endpoint_test.go @@ -92,6 +92,25 @@ func TestEndpoint(t *testing.T) { return &http.Response{StatusCode: http.StatusBadGateway, Body: http.NoBody} }), }, + { + Name: "failed-status-condition-with-hidden-conditions", + Endpoint: Endpoint{ + Name: "website-health", + URL: "https://twin.sh/health", + Conditions: []Condition{"[STATUS] == 200"}, + UIConfig: &ui.Config{HideConditions: true}, + }, + ExpectedResult: &Result{ + Success: false, + Connected: true, + Hostname: "twin.sh", + ConditionResults: []*ConditionResult{}, // Because UIConfig.HideConditions is true, the condition results should not be shown. + DomainExpiration: 0, // Because there's no [DOMAIN_EXPIRATION] condition, this is not resolved, so it should be 0. + }, + MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response { + return &http.Response{StatusCode: http.StatusBadGateway, Body: http.NoBody} + }), + }, { Name: "condition-with-failed-certificate-expiration", Endpoint: Endpoint{ diff --git a/core/ui/ui.go b/core/ui/ui.go index c7635ad2..7ea6eeec 100644 --- a/core/ui/ui.go +++ b/core/ui/ui.go @@ -4,6 +4,9 @@ import "errors" // Config is the UI configuration for core.Endpoint type Config struct { + // HideConditions whether to hide the condition results on the UI + HideConditions bool `yaml:"hide-conditions"` + // HideHostname whether to hide the hostname in the Result HideHostname bool `yaml:"hide-hostname"` @@ -52,6 +55,7 @@ func GetDefaultConfig() *Config { HideHostname: false, HideURL: false, DontResolveFailedConditions: false, + HideConditions: false, Badge: &Badge{ ResponseTime: &ResponseTime{ Thresholds: []int{50, 200, 300, 500, 750}, diff --git a/core/ui/ui_test.go b/core/ui/ui_test.go new file mode 100644 index 00000000..2bd0e770 --- /dev/null +++ b/core/ui/ui_test.go @@ -0,0 +1,53 @@ +package ui + +import ( + "errors" + "testing" +) + +func TestValidateAndSetDefaults(t *testing.T) { + tests := []struct { + name string + config *Config + wantErr error + }{ + { + name: "with-valid-config", + config: &Config{ + Badge: &Badge{ + ResponseTime: &ResponseTime{Thresholds: []int{50, 200, 300, 500, 750}}, + }, + }, + wantErr: nil, + }, + { + name: "with-invalid-threshold-length", + config: &Config{ + Badge: &Badge{ + ResponseTime: &ResponseTime{Thresholds: []int{50, 200, 300, 500}}, + }, + }, + wantErr: ErrInvalidBadgeResponseTimeConfig, + }, + { + name: "with-invalid-thresholds-order", + config: &Config{ + Badge: &Badge{ResponseTime: &ResponseTime{Thresholds: []int{50, 200, 500, 300, 750}}}, + }, + wantErr: ErrInvalidBadgeResponseTimeConfig, + }, + { + name: "with-no-badge-configured", // should give default badge cfg + config: &Config{}, + wantErr: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.config.ValidateAndSetDefaults(); !errors.Is(err, tt.wantErr) { + t.Errorf("Expected error %v, got %v", tt.wantErr, err) + } + }) + } +}