- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 0.133.0
- 0.132.1
- 0.132.0
- 0.131.12
- 0.131.11
- 0.131.10
- 0.131.9
- 0.131.8
- 0.131.7
- 0.131.6
- 0.131.5
- 0.131.4
- 0.131.3
- 0.131.2
- 0.131.1
- 0.131.0
- 0.130.13
- 0.130.12
- 0.130.11
- 0.130.10
- 0.130.9
- 0.130.8
- 0.130.7
- 0.130.6
- 0.130.5
- 0.130.4
- 0.130.3
- 0.130.2
- 0.130.1
- 0.130.0
- 0.129.0
- 0.128.4
- 0.128.3
- 0.128.2
- 0.128.1
- 0.128.0
- 0.127.3
- 0.127.2
- 0.127.1
- 0.127.0
- 0.126.6
- 0.126.5
- 0.126.4
- 0.126.3
- 0.126.2
- 0.126.1
- 0.126.0
- 0.125.0
- 0.124.10
- 0.124.9
- 0.124.8
- 0.124.7
- 0.124.6
- 0.124.5
- 0.124.4
- 0.124.2
- 0.124.1
- 0.124.0
- 0.123.9
- 0.123.8
- 0.123.7
- 0.123.6
- 0.123.5
- 0.123.4
- 0.123.3
- 0.123.2
- 0.123.1
- 0.123.0
- 0.122.1
- 0.122.0
- 0.121.5
- 0.121.4
- 0.121.3
- 0.121.2
- 0.121.1
- 0.121.0
- 0.120.3
- 0.120.2
- 0.120.1
- 0.120.0
- 0.119.5
- 0.119.4
- 0.119.3
- 0.119.2
- 0.119.1
- 0.119.0
- 0.118.11
- 0.118.10
- 0.118.9
- 0.118.8
- 0.118.7
- 0.118.6
- 0.118.5
- 0.118.4
- 0.118.3
- 0.118.2
- 0.118.1
- 0.118.0
- 0.117.4
- 0.117.3
- 0.117.2
- 0.117.1
- 0.117.0
- 0.116.7
- 0.116.6
- 0.116.5
- 0.116.4
- 0.116.3
- 0.116.2
- 0.116.1
- 0.116.0
- 0.115.0
- 0.114.1
- 0.114.0
- 0.113.2
- 0.113.1
- 0.113.0
- 0.112.5
- 0.112.4
- 0.112.3
- 0.112.2
- 0.112.1
- 0.112.0
- 0.111.1
- 0.111.0
- 0.110.1
- 0.110.0
- 0.109.2
- 0.109.1
- 0.109.0
- 0.108.3
- 0.108.2
- 0.108.1
- 0.108.0
- 0.107.1
- 0.107.0
- 0.106.5
- 0.106.4
- 0.106.3
- 0.106.2
- 0.106.1
- 0.106.0
- 0.105.2
- 0.105.1
- 0.105.0
- 0.104.2
- 0.104.1
- 0.104
- 0.103
- 0.102.1
- 0.102
- 0.101.3
- 0.101.2
- 0.101.1
- 0.101
- 0.100
- 0.99
- 0.98
- 0.97.1
- 0.97
- 0.96
- 0.95
- 0.94
- 0.93
- 0.92
- 0.91
- 0.90
- 0.89
- 0.88
- 0.87
- 0.86
- 0.85
- 0.84
- 0.83
- 0.82
- 0.81
- 0.80
- 0.79
- 0.78
- 0.77
- 0.76
- 0.75
- 0.74
- 0.73
- 0.72
- 0.71
- 0.70
- 0.69
- 0.68
- 0.67
- 0.66
- 0.65
- 0.64
- 0.63
- 0.62
- 0.61
- 0.60
- 0.59
- 0.58
- 0.57
- 0.56
- 0.55
- 0.54
- 0.53
- 0.52
- 0.51
- 0.50
- 0.49
- 0.48
- 0.47
- 0.46
- 0.45
- 0.44
- 0.43
- 0.42
- 0.41
- 0.40
- 0.39
- 0.38
- 0.37
- 0.36
- 0.35
- 0.34
- 0.33
- 0.32
- 0.31
- 0.30
- 0.29
- 0.28
- 0.27
- 0.26
- 0.25
- 0.24
- 0.23
- 0.22
- 0.21
- 0.20
- 0.19
- 0.18
- 0.17
- 0.16
- 0.15
- 0.14
- 0.13
- 0.12
Showing
6 changed files
with
103 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,169 +1,85 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/andig/evcc/util" | ||
influxdb "github.com/influxdata/influxdb1-client/v2" | ||
influxdb2 "github.com/influxdata/influxdb-client-go" | ||
) | ||
|
||
const ( | ||
influxWriteTimeout = 10 * time.Second | ||
influxWriteInterval = 30 * time.Second | ||
precision = "s" | ||
) | ||
// InfluxConfig is the influx db configuration | ||
type InfluxConfig struct { | ||
URL string | ||
Database string | ||
Token string | ||
Org string | ||
User string | ||
Password string | ||
Interval time.Duration | ||
} | ||
|
||
// Influx is a influx publisher | ||
type Influx struct { | ||
sync.Mutex | ||
log *util.Logger | ||
client influxdb.Client | ||
points []*influxdb.Point | ||
pointsConf influxdb.BatchPointsConfig | ||
interval time.Duration | ||
log *util.Logger | ||
client influxdb2.Client | ||
org string | ||
database string | ||
} | ||
|
||
// NewInfluxClient creates new publisher for influx | ||
func NewInfluxClient( | ||
url string, | ||
database string, | ||
interval time.Duration, | ||
user string, | ||
password string, | ||
) *Influx { | ||
func NewInfluxClient(url, token, org, user, password, database string) *Influx { | ||
log := util.NewLogger("iflx") | ||
|
||
if database == "" { | ||
log.FATAL.Fatal("missing database") | ||
} | ||
if interval == 0 { | ||
interval = influxWriteInterval | ||
// InfluxDB v1 compatibility | ||
if token == "" && user != "" { | ||
token = fmt.Sprintf("%s:%s", user, password) | ||
} | ||
|
||
client, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{ | ||
Addr: url, | ||
Username: user, | ||
Password: password, | ||
Timeout: influxWriteTimeout, | ||
}) | ||
if err != nil { | ||
log.FATAL.Fatalf("error creating client: %v", err) | ||
} | ||
|
||
// check connection | ||
go func(client influxdb.Client) { | ||
if _, _, err := client.Ping(influxWriteTimeout); err != nil { | ||
log.FATAL.Fatalf("%v", err) | ||
} | ||
}(client) | ||
client := influxdb2.NewClient(url, token) | ||
|
||
return &Influx{ | ||
log: log, | ||
client: client, | ||
interval: interval, | ||
pointsConf: influxdb.BatchPointsConfig{ | ||
Database: database, | ||
Precision: precision, | ||
}, | ||
} | ||
} | ||
|
||
// writeBatchPoints asynchronously writes the collected points | ||
func (m *Influx) writeBatchPoints() { | ||
m.Lock() | ||
|
||
// get current batch | ||
if len(m.points) == 0 { | ||
m.Unlock() | ||
return | ||
} | ||
|
||
// create new batch | ||
batch, err := influxdb.NewBatchPoints(m.pointsConf) | ||
if err != nil { | ||
m.log.ERROR.Print(err) | ||
m.Unlock() | ||
return | ||
} | ||
|
||
// replace current batch | ||
points := m.points | ||
m.points = nil | ||
m.Unlock() | ||
|
||
// write batch | ||
batch.AddPoints(points) | ||
m.log.TRACE.Printf("writing %d point(s)", len(points)) | ||
|
||
if err := m.client.Write(batch); err != nil { | ||
m.log.ERROR.Print(err) | ||
|
||
// put points back at beginning of next batch | ||
m.Lock() | ||
m.points = append(points, m.points...) | ||
m.Unlock() | ||
org: org, | ||
database: database, | ||
} | ||
} | ||
|
||
// asyncWriter periodically calls writeBatchPoints | ||
func (m *Influx) asyncWriter(exit <-chan struct{}) <-chan struct{} { | ||
done := make(chan struct{}) // signal writer stopped | ||
// Run Influx publisher | ||
func (m *Influx) Run(in <-chan util.Param) { | ||
writer := m.client.WriteApi(m.org, m.database) | ||
|
||
// async batch writer | ||
// log errors | ||
go func() { | ||
ticker := time.NewTicker(m.interval) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
m.writeBatchPoints() | ||
case <-exit: | ||
ticker.Stop() | ||
m.writeBatchPoints() | ||
close(done) | ||
return | ||
} | ||
for err := range writer.Errors() { | ||
m.log.ERROR.Println(err) | ||
} | ||
}() | ||
|
||
return done | ||
} | ||
|
||
// Run Influx publisher | ||
func (m *Influx) Run(in <-chan util.Param) { | ||
exit := make(chan struct{}) // exit signals to stop writer | ||
done := m.asyncWriter(exit) // done signals writer stopped | ||
|
||
// add points to batch for async writing | ||
for param := range in { | ||
// allow nil value to be written as series gaps | ||
if _, ok := param.Val.(float64); param.Val != nil && !ok { | ||
continue | ||
} | ||
|
||
p, err := influxdb.NewPoint( | ||
param.Key, | ||
map[string]string{ | ||
"loadpoint": param.LoadPoint, | ||
}, | ||
map[string]interface{}{ | ||
"value": param.Val, | ||
}, | ||
time.Now(), | ||
) | ||
if err != nil { | ||
m.log.ERROR.Printf("failed creating point: %v", err) | ||
continue | ||
tags := map[string]string{} | ||
if param.LoadPoint != "" { | ||
tags["loadpoint"] = param.LoadPoint | ||
} | ||
|
||
m.Lock() | ||
m.points = append(m.points, p) | ||
m.Unlock() | ||
} | ||
fields := map[string]interface{}{ | ||
"value": param.Val, | ||
} | ||
|
||
// close write loop | ||
exit <- struct{}{} | ||
<-done | ||
// write asynchronously | ||
m.log.TRACE.Printf("write %s=%v (%v)", param.Key, param.Val, tags) | ||
p := influxdb2.NewPoint(param.Key, tags, fields, time.Now()) | ||
writer.WritePoint(p) | ||
} | ||
|
||
m.client.Close() | ||
} |