Skip to content

Commit

Permalink
Use region flag for forecast
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
daenney committed Nov 25, 2022
1 parent 1986f51 commit 9447559
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
11 changes: 2 additions & 9 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,16 @@ func fetchFromURL(date time.Time, region string) ([]byte, error) {
// forecastHandler returns JSON in a format compatible with the Grafana JSON-API
// plugin. This allows you to display the forecast as Prometheus doesn't do values
// in the future
func forecastHandler(loc *time.Location) func(w http.ResponseWriter, r *http.Request) {
func forecastHandler(loc *time.Location, regions []string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
var regs []string
if regions, ok := query["region"]; ok {
regs = regions
} else {
regs = []string{"SN1", "SN2", "SN3", "SN4"}
}
type point struct {
Time time.Time `json:"time"`
Region string `json:"region"`
Value float64 `json:"value"`
}
res := []point{}

for _, reg := range regs {
for _, reg := range regions {
now := time.Now().In(loc)
data, err := fetch(now, reg)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func main() {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(p, promhttp.HandlerOpts{}))
mux.Handle("/prices", promhttp.InstrumentMetricHandler(p, promhttp.HandlerFor(c, promhttp.HandlerOpts{})))
mux.Handle("/forecast", promhttp.InstrumentMetricHandler(p, http.HandlerFunc(forecastHandler(loc))))
mux.Handle("/forecast", promhttp.InstrumentMetricHandler(p, http.HandlerFunc(forecastHandler(loc, regions))))
h := &http.Server{
Handler: mux,
}
Expand Down

0 comments on commit 9447559

Please sign in to comment.