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
|
|
|
|
2019-05-24 22:03:06 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-02-07 18:32:44 +00:00
|
|
|
)
|
|
|
|
|
2019-05-24 22:03:06 +00:00
|
|
|
func runWelcomePage() {
|
|
|
|
var (
|
|
|
|
baseURL = cfg.BaseURL
|
|
|
|
welcomeAPIToken = cfg.APIToken
|
|
|
|
)
|
2015-02-07 18:32:44 +00:00
|
|
|
|
2019-05-24 22:03:06 +00:00
|
|
|
for tick := time.NewTicker(10 * time.Minute); ; <-tick.C {
|
2017-11-22 21:11:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-22 21:11: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,
|
2019-05-24 22:03:06 +00:00
|
|
|
Freshness: 900,
|
2017-11-22 21:11:44 +00:00
|
|
|
Value: float64(beers),
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:03:06 +00:00
|
|
|
body := new(bytes.Buffer)
|
|
|
|
if err := json.NewEncoder(body).Encode(beer); err != nil {
|
|
|
|
log.WithError(err).Error("Unable to marshal dashboard metric")
|
2017-11-22 21:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-24 22:03:06 +00:00
|
|
|
|
2017-11-22 21:11:44 +00:00
|
|
|
url := fmt.Sprintf("%s/welcome/beer_available", baseURL)
|
2019-05-24 22:03:06 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPut, url, body)
|
2017-11-22 21:11:44 +00:00
|
|
|
req.Header.Add("Authorization", welcomeAPIToken)
|
2019-05-24 22:03:06 +00:00
|
|
|
|
2017-11-22 21:11:44 +00:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2019-05-24 22:03:06 +00:00
|
|
|
log.WithError(err).Error("Unable to put welcome-runner metric")
|
2017-11-22 21:11:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
2019-05-24 22:03:06 +00:00
|
|
|
|
|
|
|
log.Debug("Successfully put welcome-runner metric")
|
2015-02-07 18:32:44 +00:00
|
|
|
}
|