1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-11-10 00:20:02 +00:00
mondash/welcome_runner.go

64 lines
1.3 KiB
Go
Raw Normal View History

2015-02-07 18:32:44 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
2015-07-06 19:41:21 +00:00
log "github.com/sirupsen/logrus"
2015-02-07 18:32:44 +00:00
)
func runWelcomePage() {
var (
baseURL = cfg.BaseURL
welcomeAPIToken = cfg.APIToken
)
2015-02-07 18:32:44 +00:00
for tick := time.NewTicker(10 * time.Minute); ; <-tick.C {
postWelcomeMetric(baseURL, welcomeAPIToken)
}
}
func postWelcomeMetric(baseURL, welcomeAPIToken string) {
beers := rand.Intn(24)
status := "OK"
switch {
case beers < 6:
status = "Critical"
case beers < 12:
status = "Warning"
2015-02-07 18:32:44 +00:00
}
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: 900,
Value: float64(beers),
}
body := new(bytes.Buffer)
if err := json.NewEncoder(body).Encode(beer); err != nil {
log.WithError(err).Error("Unable to marshal dashboard metric")
return
}
url := fmt.Sprintf("%s/welcome/beer_available", baseURL)
req, _ := http.NewRequest(http.MethodPut, url, body)
req.Header.Add("Authorization", welcomeAPIToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.WithError(err).Error("Unable to put welcome-runner metric")
return
}
resp.Body.Close()
log.Debug("Successfully put welcome-runner metric")
2015-02-07 18:32:44 +00:00
}