98 lines
2.1 KiB
Go
98 lines
2.1 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
|
|
Measure string
|
|
Station string
|
|
MainPos string
|
|
SubPos string // separate whith ','
|
|
Begin int64
|
|
End int64
|
|
Operate string
|
|
Step string
|
|
Default string
|
|
}
|
|
|
|
var client *influxClient
|
|
|
|
func init() {
|
|
client = new(influxClient)
|
|
|
|
influxConfig := config.Conf().InfluxConf("default")
|
|
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 Close() {
|
|
client.CloseIdleConnections()
|
|
}
|
|
|
|
func NewInfluxClient(cli *http.Client, url, org, token string) *influxClient {
|
|
return &influxClient{
|
|
Client: cli,
|
|
url: url,
|
|
org: org,
|
|
token: token,
|
|
}
|
|
}
|
|
|
|
func WriteLinesData(ctx context.Context, data []byte) error {
|
|
return client.WriteLinesData(ctx, data)
|
|
}
|
|
|
|
type TV struct {
|
|
Time int64 `json:"time"`
|
|
Value float64 `json:"value"`
|
|
}
|
|
|
|
func GetSSUPointLastLimit(ctx context.Context, req *Request, limit int) ([]*TV, error) {
|
|
req.Begin = time.Now().UnixMilli() - int64(limit*20+20)
|
|
return client.GetSSUPointLastLimit(ctx, req, limit)
|
|
}
|
|
|
|
func GetSSUPointsLastLimit(ctx context.Context, req *Request, limit int) (map[string][]*TV, error) {
|
|
req.Begin = time.Now().UnixMilli() - int64(limit*20+20)
|
|
return client.GetSSUPointsLastLimit(ctx, req, limit)
|
|
}
|
|
|
|
func GetSSUPointData(ctx context.Context, req *Request) ([]*TV, error) {
|
|
return client.GetSSUPointData(ctx, req)
|
|
}
|
|
|
|
func GetSSUPointAfterOne(ctx context.Context, req *Request) ([]*TV, error) {
|
|
return client.GetSSUPointAfterOne(ctx, req)
|
|
}
|
|
|
|
func GetSSUPointBeforeOne(ctx context.Context, req *Request) ([]*TV, error) {
|
|
req.Begin = req.End - 20 - 20
|
|
return client.GetSSUPointBeforeOne(ctx, req)
|
|
}
|