feat(api): Configurable response time badge thresholds (#309)
* recreated all changes for setting thresholds on Uptime Badges * Suggestion accepted: Update core/ui/ui.go Co-authored-by: TwiN <twin@linux.com> * Suggestion accepted: Update core/ui/ui.go Co-authored-by: TwiN <twin@linux.com> * implemented final suggestions by Twin * Update controller/handler/badge.go * Update README.md * test: added the suggestons to set the UiConfig at another line Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
@ -145,6 +145,10 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error {
|
||||
}
|
||||
if endpoint.UIConfig == nil {
|
||||
endpoint.UIConfig = ui.GetDefaultConfig()
|
||||
} else {
|
||||
if err := endpoint.UIConfig.ValidateAndSetDefaults(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if endpoint.Interval == 0 {
|
||||
endpoint.Interval = 1 * time.Minute
|
||||
|
@ -1,5 +1,7 @@
|
||||
package ui
|
||||
|
||||
import "errors"
|
||||
|
||||
// Config is the UI configuration for core.Endpoint
|
||||
type Config struct {
|
||||
// HideHostname whether to hide the hostname in the Result
|
||||
@ -7,7 +9,36 @@ type Config struct {
|
||||
// HideURL whether to ensure the URL is not displayed in the results. Useful if the URL contains a token.
|
||||
HideURL bool `yaml:"hide-url"`
|
||||
// DontResolveFailedConditions whether to resolve failed conditions in the Result for display in the UI
|
||||
DontResolveFailedConditions bool `yaml:"dont-resolve-failed-conditions"`
|
||||
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")
|
||||
)
|
||||
|
||||
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
|
||||
@ -16,5 +47,10 @@ func GetDefaultConfig() *Config {
|
||||
HideHostname: false,
|
||||
HideURL: false,
|
||||
DontResolveFailedConditions: false,
|
||||
Badge: &Badge{
|
||||
ResponseTime: &ResponseTime{
|
||||
Thresholds: []int{50, 200, 300, 500, 750},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user