83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
|
|
package influx
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"datart/config"
|
||
|
|
"net"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type influxClient struct {
|
||
|
|
*http.Client
|
||
|
|
url string
|
||
|
|
token string
|
||
|
|
org string
|
||
|
|
}
|
||
|
|
|
||
|
|
type Request struct {
|
||
|
|
RespType string
|
||
|
|
Database string
|
||
|
|
Measure string
|
||
|
|
Station string
|
||
|
|
Device string
|
||
|
|
Field string
|
||
|
|
Begin int64
|
||
|
|
End int64
|
||
|
|
Operate string
|
||
|
|
Step string
|
||
|
|
Default string
|
||
|
|
}
|
||
|
|
|
||
|
|
var client *influxClient
|
||
|
|
|
||
|
|
func init() {
|
||
|
|
client = new(influxClient)
|
||
|
|
|
||
|
|
influxConfig := config.Conf().InfluxConf("demo")
|
||
|
|
client.Client = &http.Client{
|
||
|
|
Timeout: time.Duration(influxConfig.GetTimeout()) * time.Millisecond,
|
||
|
|
Transport: &http.Transport{
|
||
|
|
DialContext: (&net.Dialer{
|
||
|
|
Timeout: time.Second,
|
||
|
|
}).DialContext,
|
||
|
|
MaxIdleConns: 100,
|
||
|
|
MaxIdleConnsPerHost: 100,
|
||
|
|
IdleConnTimeout: 90 * time.Second,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
client.url = influxConfig.GetURL()
|
||
|
|
client.token = influxConfig.GetToken()
|
||
|
|
client.org = influxConfig.GetOrg()
|
||
|
|
}
|
||
|
|
|
||
|
|
func SetLinesData(db string, lines []string) error {
|
||
|
|
return client.setLinesData(db, lines)
|
||
|
|
}
|
||
|
|
|
||
|
|
type TV struct {
|
||
|
|
Time interface{} `json:"time"`
|
||
|
|
Value interface{} `json:"value"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetLast(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
return client.getLast(ctx, req, 1)
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetLastLimit(ctx context.Context, req *Request, limit int) ([]*TV, error) {
|
||
|
|
return client.getLast(ctx, req, limit)
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetPointData(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
return client.getPointData(ctx, req)
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetAfterOne(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
return client.getAfterOne(ctx, req)
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetBeforeOne(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
return client.getBeforeOne(ctx, req)
|
||
|
|
}
|