Skip to content

Commit

Permalink
Made changes requested in glinton's review
Browse files Browse the repository at this point in the history
  • Loading branch information
reimda committed Oct 10, 2019
1 parent 39836f6 commit b510d4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions plugins/inputs/openweathermap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ condition ID, icon, and main is at [weather conditions][].
### Example Output

```
> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=*,host=debian cloudiness=1i,condition_description="clear sky",condition_icon="01d",humidity=35i,pressure=1012,rain=0,sunrise=1570630329000000000i,sunset=1570671689000000000i,temperature=21.52,visibility=16093i,wind_degrees=280,wind_speed=5.7 1570659256000000000
> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=3h,host=debian cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=41i,pressure=1010,rain=0,temperature=22.34,wind_degrees=249.393,wind_speed=2.085 1570665600000000000
> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=6h,host=debian cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=50i,pressure=1012,rain=0,temperature=17.09,wind_degrees=310.754,wind_speed=3.009 1570676400000000000
> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=* cloudiness=1i,condition_description="clear sky",condition_icon="01d",humidity=35i,pressure=1012,rain=0,sunrise=1570630329000000000i,sunset=1570671689000000000i,temperature=21.52,visibility=16093i,wind_degrees=280,wind_speed=5.7 1570659256000000000
> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=3h cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=41i,pressure=1010,rain=0,temperature=22.34,wind_degrees=249.393,wind_speed=2.085 1570665600000000000
> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=6h cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=50i,pressure=1012,rain=0,temperature=17.09,wind_degrees=310.754,wind_speed=3.009 1570676400000000000
```

[api key]: https://openweathermap.org/appid
Expand Down
29 changes: 16 additions & 13 deletions plugins/inputs/openweathermap/openweathermap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const (

defaultSiteURL = "https://api.openweathermap.org/"
defaultResponseTimeout time.Duration = time.Second * 5
defaultUnits string = "metric"
defaultLang string = "en"
)

type OpenWeatherMap struct {
Expand All @@ -50,7 +48,7 @@ var sampleConfig = `
city_id = ["5391959"]
## Language of the description field.
lang = "en"
# lang = "en"
## APIs to fetch; can contain "weather" or "forecast".
fetch = ["weather", "forecast"]
Expand Down Expand Up @@ -103,7 +101,7 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error {
}

switch n.Units {
case "imperial", "standard", "metric":
case "imperial", "standard", "metric", "":
default:
return fmt.Errorf("unknown units: %s", n.Units)
}
Expand Down Expand Up @@ -285,6 +283,12 @@ func gatherWeather(acc telegraf.Accumulator, status *Status) {
}

func gatherForecast(acc telegraf.Accumulator, status *Status) {
tags := map[string]string{
"city_id": strconv.FormatInt(status.City.Id, 10),
"forecast": "*",
"city": status.City.Name,
"country": status.City.Country,
}
for i, e := range status.List {
tm := time.Unix(e.Dt, 0)
fields := map[string]interface{}{
Expand All @@ -296,12 +300,6 @@ func gatherForecast(acc telegraf.Accumulator, status *Status) {
"wind_degrees": e.Wind.Deg,
"wind_speed": e.Wind.Speed,
}
tags := map[string]string{
"city_id": strconv.FormatInt(status.City.Id, 10),
"forecast": "*",
"city": status.City.Name,
"country": status.City.Country,
}
if len(e.Weather) > 0 {
fields["condition_description"] = e.Weather[0].Description
fields["condition_icon"] = e.Weather[0].Icon
Expand All @@ -320,7 +318,6 @@ func init() {
}
return &OpenWeatherMap{
ResponseTimeout: tmout,
Units: defaultUnits,
SiteURL: defaultSiteURL,
}
})
Expand All @@ -330,8 +327,14 @@ func (n *OpenWeatherMap) formatURL(path string, city string) string {
v := url.Values{
"id": []string{city},
"APPID": []string{n.AppId},
"units": []string{n.Units},
"lang": []string{n.Lang},
}

if n.Units != "" {
v["units"] = []string{n.Units}

This comment has been minimized.

Copy link
@glinton

glinton Oct 10, 2019

Contributor

I take it OWM's default unit is "metric" if none is specified? My only hesitation doing it this way is that it gives them the control, if they change their default settings, telegraf user's are affected..

No change needed unless "metric" isn't their default unit

}

if n.Lang != "" {
v["lang"] = []string{n.Lang}
}

relative := &url.URL{
Expand Down

0 comments on commit b510d4d

Please sign in to comment.