1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-23 12:31:18 +00:00
mondash/welcome_runner.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

2015-02-07 18:32:44 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"time"
2015-07-06 19:41:21 +00:00
"github.com/Luzifer/mondash/config"
2015-02-07 18:32:44 +00:00
)
2015-07-06 19:41:21 +00:00
func runWelcomePage(cfg *config.Config) {
baseURL := cfg.BaseURL
welcomeAPIToken := cfg.APIToken
2015-02-07 18:32:44 +00:00
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"
}
2015-02-20 18:41:36 +00:00
beer := dashboardMetric{
2015-02-07 18:32:44 +00:00
Title: "Amount of beer in the fridge",
Description: fmt.Sprintf("Currently there are %d bottles of beer in the fridge", beers),
Status: status,
Expires: 86400,
Freshness: 120,
Value: float64(beers),
2015-02-07 18:32:44 +00:00
}
body, err := json.Marshal(beer)
if err != nil {
log.Println(err)
continue
2015-02-07 18:32:44 +00:00
}
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)
2015-02-20 18:47:36 +00:00
if err != nil {
log.Printf("[WelcomeRunner] %s", err)
continue
2015-02-20 18:47:36 +00:00
}
resp.Body.Close()
2015-02-07 18:32:44 +00:00
}
}
}