1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-22 20:11:18 +00:00

Fix: Welcome dashboard contains no metrics for 1m

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-11-22 22:11:44 +01:00
parent 21e57d0bd9
commit f3932a4d57
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -17,43 +17,46 @@ func runWelcomePage(cfg *config.Config) {
welcomeAPIToken := cfg.APIToken
generateTicker := time.NewTicker(time.Minute)
for {
select {
case <-generateTicker.C:
beers := rand.Intn(24)
status := "OK"
switch {
case beers < 6:
status = "Critical"
case beers < 12:
status = "Warning"
}
// Do one initial push on start
postWelcomeMetric(baseURL, welcomeAPIToken)
beer := dashboardMetric{
Title: "Amount of beer in the fridge",
Description: fmt.Sprintf("Currently there are %d bottles of beer in the fridge", beers),
IgnoreMAD: true,
Status: status,
Expires: 86400,
Freshness: 120,
Value: float64(beers),
}
for range generateTicker.C {
postWelcomeMetric(baseURL, welcomeAPIToken)
}
}
body, err := json.Marshal(beer)
if err != nil {
log.Println(err)
continue
}
url := fmt.Sprintf("%s/welcome/beer_available", baseURL)
req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(body))
req.Header.Add("Authorization", welcomeAPIToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("[WelcomeRunner] %s", err)
continue
}
resp.Body.Close()
}
func postWelcomeMetric(baseURL, welcomeAPIToken string) {
beers := rand.Intn(24)
status := "OK"
switch {
case beers < 6:
status = "Critical"
case beers < 12:
status = "Warning"
}
beer := dashboardMetric{
Title: "Amount of beer in the fridge",
Description: fmt.Sprintf("Currently there are %d bottles of beer in the fridge", beers),
IgnoreMAD: true,
Status: status,
Expires: 86400,
Freshness: 120,
Value: float64(beers),
}
body, err := json.Marshal(beer)
if err != nil {
log.Println(err)
return
}
url := fmt.Sprintf("%s/welcome/beer_available", baseURL)
req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(body))
req.Header.Add("Authorization", welcomeAPIToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("[WelcomeRunner] %s", err)
return
}
resp.Body.Close()
}