From ab25daf6c9953049832afee610545c00eb727a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sat, 19 Aug 2017 18:44:59 -0700 Subject: [PATCH] Implement logic for setting annotation visibility This allows to signal to the UI which annotations to hide and which to show by default, user still can view hidden ones --- internal/config/config.go | 33 +++++++++++++++++---------------- internal/models/annotation.go | 24 +++++++++++++++++++++--- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index ecf20783d..720e84186 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -24,22 +24,23 @@ func (mvd *spaceSeparatedList) Decode(value string) error { } type configEnvs struct { - AlertmanagerTimeout time.Duration `envconfig:"ALERTMANAGER_TIMEOUT" default:"40s" help:"Timeout for all request send to Alertmanager"` - AlertmanagerTTL time.Duration `envconfig:"ALERTMANAGER_TTL" default:"1m" help:"TTL for Alertmanager alerts and silences"` - AlertmanagerURIs spaceSeparatedList `envconfig:"ALERTMANAGER_URIS" required:"true" help:"List of Alertmanager URIs (name:uri)"` - AnnotationsHidden spaceSeparatedList `envconfig:"ANNOTATIONS_HIDDEN" help:"List of annotations that are hidden by default"` - AnnotationsVisible spaceSeparatedList `envconfig:"ANNOTATIONS_VISIBLE" help:"List of annotations that are visible by default"` - ColorLabelsStatic spaceSeparatedList `envconfig:"COLOR_LABELS_STATIC" help:"List of label names that should have the same (but distinct) color"` - ColorLabelsUnique spaceSeparatedList `envconfig:"COLOR_LABELS_UNIQUE" help:"List of label names that should have unique color"` - Debug bool `envconfig:"DEBUG" default:"false" help:"Enable debug mode"` - FilterDefault string `envconfig:"FILTER_DEFAULT" help:"Default filter string"` - JiraRegexp spaceSeparatedList `envconfig:"JIRA_REGEX" help:"List of JIRA regex rules"` - Port int `envconfig:"PORT" default:"8080" help:"HTTP port to listen on"` - SentryDSN string `envconfig:"SENTRY_DSN" help:"Sentry DSN for Go exceptions"` - SentryPublicDSN string `envconfig:"SENTRY_PUBLIC_DSN" help:"Sentry DSN for javascript exceptions"` - StripLabels spaceSeparatedList `envconfig:"STRIP_LABELS" help:"List of labels to ignore"` - KeepLabels spaceSeparatedList `envconfig:"KEEP_LABELS" help:"List of labels to keep, all other labels will be stripped"` - WebPrefix string `envconfig:"WEB_PREFIX" default:"/" help:"URL prefix"` + AlertmanagerTimeout time.Duration `envconfig:"ALERTMANAGER_TIMEOUT" default:"40s" help:"Timeout for all request send to Alertmanager"` + AlertmanagerTTL time.Duration `envconfig:"ALERTMANAGER_TTL" default:"1m" help:"TTL for Alertmanager alerts and silences"` + AlertmanagerURIs spaceSeparatedList `envconfig:"ALERTMANAGER_URIS" required:"true" help:"List of Alertmanager URIs (name:uri)"` + AnnotationsHidden spaceSeparatedList `envconfig:"ANNOTATIONS_HIDDEN" help:"List of annotations that are hidden by default"` + AnnotationsDefaultHidden bool `envconfig:"ANNOTATIONS_DEFAULT_HIDDEN" default:"false" help:"Hide all annotations by default unless listed in ANNOTATIONS_VISIBLE"` + AnnotationsVisible spaceSeparatedList `envconfig:"ANNOTATIONS_VISIBLE" help:"List of annotations that are visible by default"` + ColorLabelsStatic spaceSeparatedList `envconfig:"COLOR_LABELS_STATIC" help:"List of label names that should have the same (but distinct) color"` + ColorLabelsUnique spaceSeparatedList `envconfig:"COLOR_LABELS_UNIQUE" help:"List of label names that should have unique color"` + Debug bool `envconfig:"DEBUG" default:"false" help:"Enable debug mode"` + FilterDefault string `envconfig:"FILTER_DEFAULT" help:"Default filter string"` + JiraRegexp spaceSeparatedList `envconfig:"JIRA_REGEX" help:"List of JIRA regex rules"` + Port int `envconfig:"PORT" default:"8080" help:"HTTP port to listen on"` + SentryDSN string `envconfig:"SENTRY_DSN" help:"Sentry DSN for Go exceptions"` + SentryPublicDSN string `envconfig:"SENTRY_PUBLIC_DSN" help:"Sentry DSN for javascript exceptions"` + StripLabels spaceSeparatedList `envconfig:"STRIP_LABELS" help:"List of labels to ignore"` + KeepLabels spaceSeparatedList `envconfig:"KEEP_LABELS" help:"List of labels to keep, all other labels will be stripped"` + WebPrefix string `envconfig:"WEB_PREFIX" default:"/" help:"URL prefix"` } // Config exposes all options required to run diff --git a/internal/models/annotation.go b/internal/models/annotation.go index fc3d0e79a..573a40536 100644 --- a/internal/models/annotation.go +++ b/internal/models/annotation.go @@ -4,6 +4,7 @@ import ( "net/url" "sort" + "github.com/cloudflare/unsee/internal/config" "github.com/cloudflare/unsee/internal/slices" ) @@ -34,11 +35,11 @@ func (a Annotations) Less(i, j int) bool { // instances, it takes care of setting proper value for Visible attribute func AnnotationsFromMap(m map[string]string) Annotations { annotations := Annotations{} - for key, value := range m { + for name, value := range m { a := Annotation{ - Name: key, + Name: name, Value: value, - Visible: true, // FIXME needs implementing + Visible: isVisible(name), IsLink: isLink(value), } annotations = append(annotations, a) @@ -64,3 +65,20 @@ func isLink(s string) bool { } return false } + +func isVisible(name string) bool { + if slices.StringInSlice(config.Config.AnnotationsVisible, name) { + // annotation was explicitly marked as visible + return true + } + if slices.StringInSlice(config.Config.AnnotationsHidden, name) { + // annotation was explicitly marked as hidden + return false + } + if config.Config.AnnotationsDefaultHidden { + // user specified that default is to hide anything without explicit rules + return false + } + // default to show everything + return true +}