Skip to content

Commit

Permalink
feat: fetch next day data
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Dec 30, 2021
1 parent d502943 commit 4b237ae
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions plugins/inputs/pvpc/pvpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

type Pvpc struct {
TimeTrunc string `toml:"time_trunc"`
GeoID uint32 `toml:"geo_id"`
StartDate MarshableTime `toml:"start_date"`
EndDate MarshableTime `toml:"end_date"`
HTTPTimeout config.Duration `toml:"http_timeout"`
httpClient *http.Client

Log telegraf.Logger `toml:"-"`
}

type MarshableTime struct {
time.Time
}
Expand All @@ -27,15 +38,6 @@ func (m *MarshableTime) UnmarshalJSON(b []byte) (err error) {
return nil
}

type Pvpc struct {
TimeTrunc string `toml:"time_trunc"`
GeoID uint32 `toml:"geo_id"`
StartDate MarshableTime `toml:"start_date"`
EndDate MarshableTime `toml:"end_date"`
HTTPTimeout config.Duration `toml:"http_timeout"`
httpClient *http.Client
}

type Entry struct {
Value float64 `json:"value"`
Percentage float64 `json:"percentage"`
Expand Down Expand Up @@ -103,7 +105,7 @@ func (p *Pvpc) craftURL() string {
if p.StartDate.Year() == 1 || p.EndDate.Year() == 1 {
now := time.Now()
p.StartDate = MarshableTime{Time: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())}
p.EndDate = MarshableTime{Time: time.Date(now.Year(), now.Month(), now.Day(), 23, 00, 0, 0, now.Location())}
p.EndDate = MarshableTime{Time: time.Date(now.Year(), now.Month(), now.Day()+1, 23, 00, 0, 0, now.Location())}
}

query.Set("start_date", p.StartDate.Format("2006-01-02T15:04"))
Expand Down Expand Up @@ -136,6 +138,8 @@ func (p *Pvpc) fetch() (*ReeData, error) {
}

func (p *Pvpc) Gather(acc telegraf.Accumulator) error {
p.Log.Info("Gathering PVPC price")

if p.httpClient == nil {
p.httpClient = p.createHTTPClient()
}
Expand All @@ -145,6 +149,10 @@ func (p *Pvpc) Gather(acc telegraf.Accumulator) error {
return err
}

if len(data.Entities) == 0 || len(data.Entities[0].Attributes.Values) == 0 {
p.Log.Info("No values returned")
}

for _, price := range data.Entities[0].Attributes.Values {
acc.AddFields("pvpc", map[string]interface{}{"price": price.Value}, map[string]string{"geo_id": fmt.Sprint(p.GeoID)}, price.Date.Local())
}
Expand Down

0 comments on commit 4b237ae

Please sign in to comment.