Skip to content

Commit

Permalink
restructured files,use real time temp data
Browse files Browse the repository at this point in the history
  • Loading branch information
metonymic-smokey committed Sep 15, 2021
1 parent d56b31e commit ac1ec91
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 39 deletions.
53 changes: 18 additions & 35 deletions async.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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&timesteps=%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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 9 additions & 4 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
50 changes: 50 additions & 0 deletions temp.go
Original file line number Diff line number Diff line change
@@ -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&timesteps=%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
}

0 comments on commit ac1ec91

Please sign in to comment.