From ac1ec91b9b1fd21adabc740a0bad19944bc1c245 Mon Sep 17 00:00:00 2001 From: metonymic-smokey Date: Wed, 15 Sep 2021 12:58:29 +0530 Subject: [PATCH] restructured files,use real time temp data --- async.go | 53 ++++++++++++++++++----------------------------------- go.mod | 1 + go.sum | 2 ++ sync.go | 13 +++++++++---- temp.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 39 deletions(-) create mode 100644 temp.go diff --git a/async.go b/async.go index 0ce184e..f37b53d 100644 --- a/async.go +++ b/async.go @@ -1,12 +1,10 @@ package main import ( - "encoding/json" - "fmt" - "io/ioutil" "net/http" "time" + "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -36,49 +34,34 @@ var jobsInQueue = promauto.NewGauge( }, ) -type Temperature struct { - Temp float64 `json:"temperature"` -} +var httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ + Name: "http_response_time_seconds", + Help: "Duration of HTTP requests.", +}, []string{"path"}) -type Final struct { - StartTime string `json:"startTime"` - Values Temperature `json:"values"` -} +func prometheusMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + route := mux.CurrentRoute(r) + path, _ := route.GetPathTemplate() -type Interval struct { - Timestep string `json:"timestep"` - StartTime string `json:"startTime"` - EndTime string `json:"endTime"` - TempVal []Final `json:"intervals"` -} - -type Timelines struct { - Timestep []Interval `json:"timelines"` -} + timer := prometheus.NewTimer(httpDuration.WithLabelValues(path)) -type Response struct { - Data Timelines `json:"data"` + timer.ObserveDuration() + }) } func main() { - url := fmt.Sprintf("https://api.tomorrow.io/v4/timelines?location=%f,%f&fields=temperature×teps=%s&units=%s", 73.98529171943665, 40.75872069597532, "1h", "metric") - req, _ := http.NewRequest("GET", url, nil) - req.Header.Add("apikey", "APIKEY") - res, _ := http.DefaultClient.Do(req) - defer res.Body.Close() - - body, _ := ioutil.ReadAll(res.Body) - - var dat Response - if err := json.Unmarshal(body, &dat); err != nil { - panic(err) - } + dat := getTempData() for _, interval := range dat.Data.Timestep[0].TempVal { recordMetrics(interval.Values.Temp) } - http.Handle("/metrics", promhttp.Handler()) + router := mux.NewRouter() + router.Use(prometheusMiddleware) + + //http.Handle("/metrics", promhttp.Handler()) + router.Path("/metrics").Handler(promhttp.Handler()) http.ListenAndServe(":2112", nil) } diff --git a/go.mod b/go.mod index e970c26..0f45560 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/google/go-cmp v0.5.6 // indirect + github.com/gorilla/mux v1.8.0 github.com/prometheus/client_golang v1.11.0 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect ) diff --git a/go.sum b/go.sum index 8cc5161..26cb9d2 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= diff --git a/sync.go b/sync.go index 2b268be..266c5e3 100644 --- a/sync.go +++ b/sync.go @@ -15,12 +15,17 @@ type CityStats struct { //temperature by country func (c *CityStats) TemperatureAndHumidity() ( - tempByCity map[string]int, humidityByCity map[string]float64, + tempByCity map[string]float64, humidityByCity map[string]float64, ) { - tempByCity = map[string]int{ - "bangalore": rand.Intn(100), - "london": rand.Intn(1000), + // get real time API temp data here + tempByCity = make(map[string]float64) + dat := getTempData() + cities := []string{"bangalore", "london"} + + for ind, interval := range dat.Data.Timestep[0].TempVal { + tempByCity[cities[ind%2]] = interval.Values.Temp } + humidityByCity = map[string]float64{ "bangalore": rand.Float64(), "london": rand.Float64(), diff --git a/temp.go b/temp.go new file mode 100644 index 0000000..e08c2d5 --- /dev/null +++ b/temp.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +type Temperature struct { + Temp float64 `json:"temperature"` +} + +type Final struct { + StartTime string `json:"startTime"` + Values Temperature `json:"values"` +} + +type Interval struct { + Timestep string `json:"timestep"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + TempVal []Final `json:"intervals"` +} + +type Timelines struct { + Timestep []Interval `json:"timelines"` +} + +type Response struct { + Data Timelines `json:"data"` +} + +func getTempData() Response { + url := fmt.Sprintf("https://api.tomorrow.io/v4/timelines?location=%f,%f&fields=temperature×teps=%s&units=%s", 73.98529171943665, 40.75872069597532, "1h", "metric") + + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("apikey", "APIKEY") + res, _ := http.DefaultClient.Do(req) + defer res.Body.Close() + + body, _ := ioutil.ReadAll(res.Body) + + var dat Response + if err := json.Unmarshal(body, &dat); err != nil { + panic(err) + } + + return dat +}