2021-09-03 04:26:08 +02:00
package ui
2022-08-11 03:05:34 +02:00
import "errors"
2024-05-10 04:56:16 +02:00
// Config is the UI configuration for endpoint.Endpoint
2021-09-03 04:26:08 +02:00
type Config struct {
2024-04-19 02:57:07 +02:00
// HideConditions whether to hide the condition results on the UI
HideConditions bool ` yaml:"hide-conditions" `
2021-09-15 01:34:46 +02:00
// HideHostname whether to hide the hostname in the Result
HideHostname bool ` yaml:"hide-hostname" `
2022-08-11 03:09:22 +02:00
2022-06-16 23:53:03 +02:00
// HideURL whether to ensure the URL is not displayed in the results. Useful if the URL contains a token.
HideURL bool ` yaml:"hide-url" `
2022-08-11 03:09:22 +02:00
2021-09-15 01:34:46 +02:00
// DontResolveFailedConditions whether to resolve failed conditions in the Result for display in the UI
2022-08-11 03:09:22 +02:00
DontResolveFailedConditions bool ` yaml:"dont-resolve-failed-conditions" `
2022-08-11 03:05:34 +02:00
// Badge is the configuration for the badges generated
Badge * Badge ` yaml:"badge" `
}
type Badge struct {
ResponseTime * ResponseTime ` yaml:"response-time" `
}
2022-08-11 03:09:22 +02:00
2022-08-11 03:05:34 +02:00
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" )
)
2024-04-10 00:56:33 +02:00
// ValidateAndSetDefaults validates the UI configuration and sets the default values
2022-08-11 03:05:34 +02:00
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
2021-09-03 04:26:08 +02:00
}
// GetDefaultConfig retrieves the default UI configuration
func GetDefaultConfig ( ) * Config {
return & Config {
2021-09-15 01:34:46 +02:00
HideHostname : false ,
2022-06-16 23:53:03 +02:00
HideURL : false ,
2021-09-15 01:34:46 +02:00
DontResolveFailedConditions : false ,
2024-04-19 02:57:07 +02:00
HideConditions : false ,
2022-08-11 03:05:34 +02:00
Badge : & Badge {
ResponseTime : & ResponseTime {
Thresholds : [ ] int { 50 , 200 , 300 , 500 , 750 } ,
} ,
} ,
2021-09-03 04:26:08 +02:00
}
}