-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
223 lines (194 loc) · 5.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"context"
"fmt"
"image/color"
"math"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
)
const (
defaultPullTime = 60
nightStart = 21
nightEnd = 6
labelTextSize = 120 // or 96
ecoMetricLow = 40
ecoMetricHigh = 80
modeFullScreen = true
)
type Config struct {
prometheusURL string
metricName string
pullPeriod time.Duration
}
type myTheme struct{}
var _ fyne.Theme = (*myTheme)(nil)
// collect display size to set font size
// "github.com/kbinani/screenshot"
// bounds := screenshot.GetDisplayBounds(0)
// screenWidth := bounds.Dx()
// screenHeight := bounds.Dy()
func GetConfig() (Config, error) {
pullTime, err := strconv.Atoi(os.Getenv("PULL_DURATION"))
if err != nil {
pullTime = defaultPullTime
}
return Config{
prometheusURL: os.Getenv("PROMETHEUS_URL"),
metricName: "entsoe_generation_eco",
pullPeriod: time.Duration(pullTime) * time.Second,
}, nil
}
func (m myTheme) Font(style fyne.TextStyle) fyne.Resource {
return theme.DefaultTheme().Font(style)
}
func (m myTheme) Size(name fyne.ThemeSizeName) float32 {
return theme.DefaultTheme().Size(name)
}
func (m myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
c, err := GetConfig()
if err != nil {
fmt.Printf("Error getting config: %v\n", err)
return color.Black
}
carbonColor, err := c.CarbonColor()
if err != nil {
fmt.Printf("Error getting carbon color: %v\n", err)
return color.Black
}
return carbonColor
}
func (m myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
return theme.DefaultTheme().Icon(name)
}
func (c *Config) GetCarbonMetric() (int, error) {
if c.prometheusURL == "" {
return 0, fmt.Errorf("PROMETHEUS_URL environment variable is not set")
}
client, err := api.NewClient(api.Config{
Address: c.prometheusURL,
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
return 0, err
}
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, _, err := v1api.Query(ctx, c.metricName, time.Now())
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
return 0, err
}
vectorVal, ok := result.(model.Vector)
if !ok || len(vectorVal) == 0 {
return 0, fmt.Errorf("no data returned")
}
carbonMetric := vectorVal[0].Value * 100
fmt.Println("carbonMetric: ", carbonMetric)
// Round the metric to two decimal places
roundedMetric := math.Round(float64(carbonMetric)*100) / 100
formatMetric, err := strconv.Atoi(fmt.Sprintf("%.0f", roundedMetric))
if err != nil {
fmt.Printf("Error formatting metric: %v\n", err)
return 0, err
}
return formatMetric, nil
}
func (c *Config) CarbonColor() (color.Color, error) {
// default color grey
carbonColor := color.RGBA{125, 125, 125, 255}
carbonMetric, err := c.GetCarbonMetric()
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
return color.RGBA{}, err
}
if carbonMetric <= ecoMetricLow && carbonMetric > 0 {
if isNight() {
// dark red/brown
carbonColor = color.RGBA{140, 0, 0, 255}
} else {
// red
carbonColor = color.RGBA{255, 0, 0, 255}
}
} else if carbonMetric > ecoMetricLow && carbonMetric <= ecoMetricHigh {
if isNight() {
// dark yellow
carbonColor = color.RGBA{175, 175, 0, 200}
} else {
// light yellow
carbonColor = color.RGBA{255, 255, 0, 255}
}
} else {
if isNight() {
// dark green
carbonColor = color.RGBA{0, 190, 0, 255}
} else {
// light green
carbonColor = color.RGBA{0, 255, 0, 255}
}
}
return carbonColor, nil
}
// find out if it is night to dim the display
func isNight() bool {
now := time.Now()
hour := now.Hour()
return hour >= nightStart || hour < nightEnd
}
func main() {
c, err := GetConfig()
if err != nil {
fmt.Printf("Error reading config: %v\n", err)
return
}
iconResource, err := fyne.LoadResourceFromURLString("https://raw.githubusercontent.com/eumel8/carbon-app/main/icon.png")
if err != nil {
fmt.Printf("Failed to load icon", err)
return
}
carbonApp := app.New()
carbonApp.SetIcon(iconResource)
carbonWindow := carbonApp.NewWindow("Carbon-App")
carbonWindow.SetFullScreen(modeFullScreen)
mainLabel := canvas.NewText("Show the current carbon emission", color.White)
mainContent := container.NewVBox(mainLabel)
go func() {
for {
carbonApp.Settings().SetTheme(&myTheme{})
carbonMetric, err := c.GetCarbonMetric()
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
}
currentTime := time.Now().Format("02.01.2006 15:04:05")
timeLabel := canvas.NewText(currentTime, color.Gray{})
timeLabel.Alignment = fyne.TextAlignCenter
carbonLabel := canvas.NewText(fmt.Sprintf("%d ", carbonMetric), color.Black)
carbonLabel.TextStyle.Bold = true
carbonLabel.TextSize = labelTextSize
carbonLabel.Alignment = fyne.TextAlignCenter
content := container.NewVBox(timeLabel, carbonLabel)
carbonLabel.Refresh()
timeLabel.Refresh()
carbonWindow.SetContent(content)
carbonWindow.Canvas().Refresh(content)
carbonWindow.Canvas().SetOnTypedKey(func(keyEvent *fyne.KeyEvent) {
if keyEvent.Name == fyne.KeyEscape {
carbonApp.Quit()
}
})
time.Sleep(c.pullPeriod)
}
}()
carbonWindow.SetContent(mainContent)
carbonWindow.ShowAndRun()
}