1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00

feat(ui): Implement endpoints[].ui.hide-conditions

This commit is contained in:
TwiN 2024-04-18 20:57:07 -04:00
parent 7efe7429dd
commit e0ab35e86a
5 changed files with 80 additions and 0 deletions

View file

@ -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. <br />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` |

View file

@ -299,6 +299,9 @@ func (endpoint *Endpoint) EvaluateHealth() *Result {
}
result.Hostname = ""
}
if endpoint.UIConfig.HideConditions {
result.ConditionResults = nil
}
return result
}

View file

@ -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{

View file

@ -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},

53
core/ui/ui_test.go Normal file
View file

@ -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)
}
})
}
}