templates

package
v0.11.0-rc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTemplateFuncs = template.FuncMap{
	"humanize": func(i any) (string, error) {
		v, err := convertToFloat(i)
		if err != nil {
			return "", err
		}
		if v == 0 || math.IsNaN(v) || math.IsInf(v, 0) {
			return fmt.Sprintf("%.4g", v), nil
		}
		if math.Abs(v) >= 1 {
			prefix := ""
			for _, p := range []string{"k", "M", "G", "T", "P", "E", "Z", "Y"} {
				if math.Abs(v) < 1000 {
					break
				}
				prefix = p
				v /= 1000
			}
			return fmt.Sprintf("%.4g%s", v, prefix), nil
		}
		prefix := ""
		for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
			if math.Abs(v) >= 1 {
				break
			}
			prefix = p
			v *= 1000
		}
		return fmt.Sprintf("%.4g%s", v, prefix), nil
	},
	"humanize1024": func(i any) (string, error) {
		v, err := convertToFloat(i)
		if err != nil {
			return "", err
		}
		if math.Abs(v) <= 1 || math.IsNaN(v) || math.IsInf(v, 0) {
			return fmt.Sprintf("%.4g", v), nil
		}
		prefix := ""
		for _, p := range []string{"ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"} {
			if math.Abs(v) < 1024 {
				break
			}
			prefix = p
			v /= 1024
		}
		return fmt.Sprintf("%.4g%s", v, prefix), nil
	},
	"humanizeDuration": func(i any) (string, error) {
		v, err := convertToFloat(i)
		if err != nil {
			return "", err
		}
		if math.IsNaN(v) || math.IsInf(v, 0) {
			return fmt.Sprintf("%.4g", v), nil
		}
		if v == 0 {
			return fmt.Sprintf("%.4gs", v), nil
		}
		if math.Abs(v) >= 1 {
			sign := ""
			if v < 0 {
				sign = "-"
				v = -v
			}
			duration := int64(v)
			seconds := duration % 60
			minutes := (duration / 60) % 60
			hours := (duration / 60 / 60) % 24
			days := duration / 60 / 60 / 24

			if days != 0 {
				return fmt.Sprintf("%s%dd %dh %dm %ds", sign, days, hours, minutes, seconds), nil
			}
			if hours != 0 {
				return fmt.Sprintf("%s%dh %dm %ds", sign, hours, minutes, seconds), nil
			}
			if minutes != 0 {
				return fmt.Sprintf("%s%dm %ds", sign, minutes, seconds), nil
			}

			return fmt.Sprintf("%s%.4gs", sign, v), nil
		}
		prefix := ""
		for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
			if math.Abs(v) >= 1 {
				break
			}
			prefix = p
			v *= 1000
		}
		return fmt.Sprintf("%.4g%ss", v, prefix), nil
	},
	"humanizePercentage": func(i any) (string, error) {
		v, err := convertToFloat(i)
		if err != nil {
			return "", err
		}
		return fmt.Sprintf("%.4g%%", v*100), nil
	},
	"humanizeTimestamp": func(i any) (string, error) {
		v, err := convertToFloat(i)
		if err != nil {
			return "", err
		}

		tm, err := floatToTime(v)
		switch {
		case errors.Is(err, errNaNOrInf):
			return fmt.Sprintf("%.4g", v), nil
		case err != nil:
			return "", err
		}

		return fmt.Sprint(tm), nil
	},
	"formatTime": func(i any) (string, error) {
		ts, ok := i.(time.Time)
		if !ok {
			return "", fmt.Errorf("formatTime: expected time.Time, got %T", i)
		}
		return fmt.Sprint(ts.Format(time.RFC822)), nil
	},
	"toTime": func(i any) (*time.Time, error) {
		v, err := convertToFloat(i)
		if err != nil {
			return nil, err
		}

		return floatToTime(v)
	},
}

Functions

func BodyTemplate added in v0.9.0

func BodyTemplate() string

func HeaderTemplate added in v0.9.0

func HeaderTemplate() string

func RegisterNewAlertManagerDefaults added in v0.9.0

func RegisterNewAlertManagerDefaults[T, U ~map[string]any](amTmplMap T, newTmplMap U)

func RegisterTemplateMap added in v0.9.0

func RegisterTemplateMap[T ~map[string]any](templateMap T, key string, templateFunc any) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL