This commit is contained in:
2025-04-04 19:06:29 -07:00
parent ebb45b13bb
commit 393381d456
275 changed files with 56094 additions and 2 deletions

69
config/endpoint/ui/ui.go Normal file
View File

@ -0,0 +1,69 @@
package ui
import "errors"
// Config is the UI configuration for endpoint.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"`
// HideURL whether to ensure the URL is not displayed in the results. Useful if the URL contains a token.
HideURL bool `yaml:"hide-url"`
// HidePort whether to hide the port in the Result
HidePort bool `yaml:"hide-port"`
// DontResolveFailedConditions whether to resolve failed conditions in the Result for display in the UI
DontResolveFailedConditions bool `yaml:"dont-resolve-failed-conditions"`
// Badge is the configuration for the badges generated
Badge *Badge `yaml:"badge"`
}
type Badge struct {
ResponseTime *ResponseTime `yaml:"response-time"`
}
type ResponseTime struct {
Thresholds []int `yaml:"thresholds"`
}
var (
ErrInvalidBadgeResponseTimeConfig = errors.New("invalid response time badge configuration: expected parameter 'response-time' to have 5 ascending numerical values")
)
// ValidateAndSetDefaults validates the UI configuration and sets the default values
func (config *Config) ValidateAndSetDefaults() error {
if config.Badge != nil {
if len(config.Badge.ResponseTime.Thresholds) != 5 {
return ErrInvalidBadgeResponseTimeConfig
}
for i := 4; i > 0; i-- {
if config.Badge.ResponseTime.Thresholds[i] < config.Badge.ResponseTime.Thresholds[i-1] {
return ErrInvalidBadgeResponseTimeConfig
}
}
} else {
config.Badge = GetDefaultConfig().Badge
}
return nil
}
// GetDefaultConfig retrieves the default UI configuration
func GetDefaultConfig() *Config {
return &Config{
HideHostname: false,
HideURL: false,
HidePort: false,
DontResolveFailedConditions: false,
HideConditions: false,
Badge: &Badge{
ResponseTime: &ResponseTime{
Thresholds: []int{50, 200, 300, 500, 750},
},
},
}
}

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