Add test for silence deletion

This commit is contained in:
Fabian Reinartz 2015-10-12 08:48:56 +02:00
parent 2d3f0ecd84
commit f9f32c7136
1 changed files with 106 additions and 7 deletions

View File

@ -15,11 +15,16 @@ package template
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"math"
"strings" "strings"
"time"
text_tmpl "html/template" html_tmpl "html/template"
html_tmpl "text/template" text_tmpl "text/template"
"github.com/prometheus/common/model"
) )
type Template struct { type Template struct {
@ -49,11 +54,6 @@ func FromGlobs(paths ...string) (*Template, error) {
type FuncMap map[string]interface{} type FuncMap map[string]interface{}
var DefaultFuncs = FuncMap{
"upper": strings.ToUpper,
"lower": strings.ToLower,
}
func (t *Template) funcs(fm FuncMap) *Template { func (t *Template) funcs(fm FuncMap) *Template {
t.text.Funcs(text_tmpl.FuncMap(fm)) t.text.Funcs(text_tmpl.FuncMap(fm))
t.html.Funcs(html_tmpl.FuncMap(fm)) t.html.Funcs(html_tmpl.FuncMap(fm))
@ -79,3 +79,102 @@ func (t *Template) ExecuteHTMLString(name string, data interface{}) (string, err
func (t *Template) ExecuteHTML(w io.Writer, name string, data interface{}) error { func (t *Template) ExecuteHTML(w io.Writer, name string, data interface{}) error {
return t.html.ExecuteTemplate(w, name, data) return t.html.ExecuteTemplate(w, name, data)
} }
var DefaultFuncs = FuncMap{
"upper": strings.ToUpper,
"lower": strings.ToLower,
// safe marks a string as safe to be not HTML-escaped.
"safe": func(s string) html_tmpl.HTML {
return html_tmpl.HTML(s)
},
// commonLabels returns the labels that are equal across
// all given alerts.
"commonLabels": func(alerts model.Alerts) model.LabelSet {
if len(alerts) < 1 {
return model.LabelSet{}
}
common := alerts[0].Labels.Clone()
for _, a := range alerts[1:] {
for ln, lv := range common {
if a.Labels[ln] != lv {
delete(common, ln)
}
}
}
return common
},
// commonAnnotations returns the annotations that are equal across
// all given alerts.
"commonAnnotations": func(alerts model.Alerts) model.LabelSet {
if len(alerts) < 1 {
return model.LabelSet{}
}
common := alerts[0].Annotations.Clone()
for _, a := range alerts[1:] {
for ln, lv := range common {
if a.Annotations[ln] != lv {
delete(common, ln)
}
}
}
return common
},
// humanize returns a human-readable string representation of the value.
"humanize": func(v float64) string {
if v == 0 || math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v)
}
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)
}
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)
},
"humanize1024": func(v float64) string {
if math.Abs(v) <= 1 || math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v)
}
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)
},
// duration returns a duration for the second value.
"duration": func(v float64) time.Duration {
if math.IsNaN(v) || math.IsInf(v, 0) {
return 0
}
return time.Duration(v) * time.Nanosecond
},
// time returns a time representation of the second value.
"time": func(v float64) time.Time {
if math.IsNaN(v) || math.IsInf(v, 0) {
return time.Time{}
}
return time.Unix(int64(v), 0)
},
}