From 77079a9629b237d8fa5cb02347ab1fbbde8a64fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 19 Jul 2018 18:35:45 +0200 Subject: [PATCH] feat(api): export annotation settings in the API --- internal/mock/mock.py | 50 ++++++++++++++++++++++++++++++++++++++++++ internal/models/api.go | 5 ++++- views.go | 5 ++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 internal/mock/mock.py diff --git a/internal/mock/mock.py b/internal/mock/mock.py new file mode 100644 index 000000000..187a5b293 --- /dev/null +++ b/internal/mock/mock.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + + +import random +import time + + +def send_alert(annotations, labels): + data = { + "annotations": annotations, + "labels": data, + "generatorURL": "http://localhost:9093" + } + req = urllib2.Request('http://localhost:9093/api/v1/alerts') + req.add_header('Content-Type', 'application/json') + response = urllib2.urlopen(req, json.dumps(data)) + print(response) + + +class FlappingAlert(object): + + annotations = {} + labels = {} + + def __init__(self, active_for, idle_for, splay): + self._active_for = active_for + self._idle_for = idle_for + self._max_splay = splay + + self.active = True + self.last_change = time.time() + self.splay = self.random_splay() + + def random_splay(self): + return random.randrange(0, self._max_splay) + + def tick(): + if time.time() >= self.last_change + self.splay: + self.active = !self.active + self.last_change = time.time() + self.splay = self.random_splay() + + if self.active: + send_alert(self.annotations, self.labels) + + +def main(): + alerts = [ + + ] diff --git a/internal/models/api.go b/internal/models/api.go index a6c3f69d1..1df40ef4a 100644 --- a/internal/models/api.go +++ b/internal/models/api.go @@ -160,7 +160,10 @@ func (ag *APIAlertGroup) DedupSharedMaps() { // Settings is used to export unsee configuration that is used by UI type Settings struct { - StaticColorLabels []string `json:"staticColorLabels"` + StaticColorLabels []string `json:"staticColorLabels"` + AnnotationsDefaultHidden bool `json:"annotationsDefaultHidden"` + AnnotationsHidden []string `json:"annotationsHidden"` + AnnotationsVisible []string `json:"annotationsVisible"` } // AlertsResponse is the structure of JSON response UI will use to get alert data diff --git a/views.go b/views.go index e84f2c084..b625163a3 100644 --- a/views.go +++ b/views.go @@ -63,7 +63,10 @@ func alerts(c *gin.Context) { resp.Version = version resp.Upstreams = getUpstreams() resp.Settings = models.Settings{ - StaticColorLabels: config.Config.Labels.Color.Static, + StaticColorLabels: config.Config.Labels.Color.Static, + AnnotationsDefaultHidden: config.Config.Annotations.Default.Hidden, + AnnotationsHidden: config.Config.Annotations.Hidden, + AnnotationsVisible: config.Config.Annotations.Visible, } // use full URI (including query args) as cache key