1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-11-09 16:10:01 +00:00
mondash/welcome_runner.go
Knut Ahlers f3c31476b4
Major: Rework frontend, API and improve code quality (#5)
* Update dependencies
* Improve code quality
* Apply linter advices
* Add gzip compression to all requests
* Rework frontend
* Apply bootswatch theme
* Hide historic details when not requested
* Remove debugging header
* Apply auto-migration of meta fields
* Fix broken "last update" time
* Pre-sort metrics for frontend / API
* Add tooltip with absolute time
* Some design fixes
* Add tooltip with absolute date to last ok
* Implement filters
* Apply eslint --fix
* Remove unused var
* Remove remains of old template engine
* Update baked in assets
* Update Dockerfile for new version

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-05-25 00:03:06 +02:00

63 lines
1.3 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
func runWelcomePage() {
var (
baseURL = cfg.BaseURL
welcomeAPIToken = cfg.APIToken
)
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"
}
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")
}