bug on types, updated send function
This commit is contained in:
parent
7baad7e839
commit
0ed528c6ef
26
functions.go
26
functions.go
@ -61,14 +61,14 @@ func FetchData(city string) (Data, error) {
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
err = json.Unmarshal(b, &d)
|
||||
|
||||
d.Humidity = d.Main["humidity"]
|
||||
d.Temperature = kelvin + d.Main["temp"]
|
||||
d.Humidity = int64(d.Main["humidity"])
|
||||
|
||||
return d, err
|
||||
}
|
||||
|
||||
// SendToInflux sends time series data to influxdb
|
||||
func SendToInflux(wc *WeatherConfig, city string, measurement string, value float64) error {
|
||||
func SendToInflux(wc *WeatherConfig, d Data) error {
|
||||
|
||||
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
|
||||
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
|
||||
@ -86,11 +86,27 @@ func SendToInflux(wc *WeatherConfig, city string, measurement string, value floa
|
||||
return err
|
||||
}
|
||||
|
||||
tags := map[string]string{"city": city}
|
||||
fields := map[string]interface{}{"value": value}
|
||||
tags := map[string]string{"city": d.City}
|
||||
fields := map[string]interface{}{"value": d.Temperature}
|
||||
|
||||
point, _ := client.NewPoint(
|
||||
measurement,
|
||||
"temperature",
|
||||
tags,
|
||||
fields,
|
||||
time.Now(),
|
||||
)
|
||||
|
||||
bp.AddPoint(point)
|
||||
err = httpClient.Write(bp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tags = map[string]string{"city": d.City}
|
||||
fields = map[string]interface{}{"value": d.Humidity}
|
||||
|
||||
point, _ = client.NewPoint(
|
||||
"humidity",
|
||||
tags,
|
||||
fields,
|
||||
time.Now(),
|
||||
|
2
types.go
2
types.go
@ -18,6 +18,6 @@ type WeatherConfig struct {
|
||||
type Data struct {
|
||||
City string `json:"name"`
|
||||
Temperature float64
|
||||
Humidity float64
|
||||
Humidity int64
|
||||
Main map[string]float64
|
||||
}
|
||||
|
13
weather.go
13
weather.go
@ -32,20 +32,11 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = SendToInflux(&wc, d.City, "temperature", d.Temperature)
|
||||
err = SendToInflux(&wc, d)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
log.Println(fmt.Sprintf("Successfully sent temperature data for %s", city))
|
||||
log.Println(fmt.Sprintf("Successfully sent data for %s", city))
|
||||
}
|
||||
|
||||
err = SendToInflux(&wc, d.City, "humidity", d.Humidity)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
log.Println(fmt.Sprintf("Successfully sent humidity data for %s", city))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user