Skip to content

Commit 9447559

Browse files
committed
Use region flag for forecast
Not sure why, but when initially implementing the forecast support I made it take region as a query parameter instead of grabbing the ones we were already given from the region flag. This commit makes the behaviour consistent.
1 parent 1986f51 commit 9447559

2 files changed

Lines changed: 3 additions & 10 deletions

File tree

data.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,16 @@ func fetchFromURL(date time.Time, region string) ([]byte, error) {
121121
// forecastHandler returns JSON in a format compatible with the Grafana JSON-API
122122
// plugin. This allows you to display the forecast as Prometheus doesn't do values
123123
// in the future
124-
func forecastHandler(loc *time.Location) func(w http.ResponseWriter, r *http.Request) {
124+
func forecastHandler(loc *time.Location, regions []string) func(w http.ResponseWriter, r *http.Request) {
125125
return func(w http.ResponseWriter, r *http.Request) {
126-
query := r.URL.Query()
127-
var regs []string
128-
if regions, ok := query["region"]; ok {
129-
regs = regions
130-
} else {
131-
regs = []string{"SN1", "SN2", "SN3", "SN4"}
132-
}
133126
type point struct {
134127
Time time.Time `json:"time"`
135128
Region string `json:"region"`
136129
Value float64 `json:"value"`
137130
}
138131
res := []point{}
139132

140-
for _, reg := range regs {
133+
for _, reg := range regions {
141134
now := time.Now().In(loc)
142135
data, err := fetch(now, reg)
143136
if err != nil {

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func main() {
8888
mux := http.NewServeMux()
8989
mux.Handle("/metrics", promhttp.HandlerFor(p, promhttp.HandlerOpts{}))
9090
mux.Handle("/prices", promhttp.InstrumentMetricHandler(p, promhttp.HandlerFor(c, promhttp.HandlerOpts{})))
91-
mux.Handle("/forecast", promhttp.InstrumentMetricHandler(p, http.HandlerFunc(forecastHandler(loc))))
91+
mux.Handle("/forecast", promhttp.InstrumentMetricHandler(p, http.HandlerFunc(forecastHandler(loc, regions))))
9292
h := &http.Server{
9393
Handler: mux,
9494
}

0 commit comments

Comments
 (0)