141 lines
3.1 KiB
Go
141 lines
3.1 KiB
Go
package influx
|
|
|
|
import (
|
|
"context"
|
|
"datart/config"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type influxClient struct {
|
|
*http.Client
|
|
url string
|
|
token string
|
|
org string
|
|
}
|
|
|
|
type Request struct {
|
|
RespType string
|
|
Bucket string
|
|
Measure string
|
|
Station string
|
|
MainPos string
|
|
SubPos string // separate whith ','
|
|
Begin int64
|
|
End int64
|
|
Operate string
|
|
Step string
|
|
Default string
|
|
}
|
|
|
|
const (
|
|
PhasorBucket = "influxBucket"
|
|
SampleBucket = "influxBucket"
|
|
)
|
|
|
|
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 GetBucket(tp string) (string, error) {
|
|
switch tp {
|
|
case "phasor":
|
|
return PhasorBucket, nil
|
|
case "sample":
|
|
return SampleBucket, nil
|
|
}
|
|
|
|
return "", errors.New("invalid type")
|
|
}
|
|
|
|
// serverConf
|
|
func GetMeasurement(tp string, mainPos string) (string, error) {
|
|
switch tp {
|
|
case "phasor":
|
|
ssu2Type := config.Conf().ServerConf().GetSSUType()
|
|
switch ssu2Type[mainPos] {
|
|
case 1:
|
|
return "current", nil
|
|
case 2:
|
|
return "voltage", nil
|
|
default:
|
|
return "", errors.New("invalid main_pos")
|
|
}
|
|
case "sample":
|
|
return "sample", nil
|
|
}
|
|
|
|
return "", errors.New("invalid type")
|
|
}
|
|
|
|
func WriteLinesData(ctx context.Context, bucket string, data []byte) error {
|
|
return client.WriteLinesData(ctx, bucket, 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 GetSSUPointDurationData(ctx context.Context, req *Request) ([]TV, error) {
|
|
return client.GetSSUPointDurationData(ctx, req)
|
|
}
|
|
|
|
func GetSSUPointsDurationData(ctx context.Context, req *Request) (map[string][]TV, error) {
|
|
return client.GetSSUPointsDurationData(ctx, req)
|
|
}
|
|
|
|
func GetSSUPointsAfterLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
|
req.End = req.Begin + int64(limit*20+20)
|
|
return client.GetSSUPointsAfterLimit(ctx, req, limit)
|
|
}
|
|
|
|
func GetSSUPointsBeforeLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
|
req.Begin = req.End - int64(limit*20+20)
|
|
return client.GetSSUPointsBeforeLimit(ctx, req, limit)
|
|
}
|