94 lines
3.2 KiB
Go
94 lines
3.2 KiB
Go
|
|
package influx
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"net/url"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
bucket string = "influxBucket"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (client *influxClient) GetSSUPointLastLimit(ctx context.Context, req *Request, limit int) ([]*TV, error) {
|
||
|
|
sql := fmt.Sprintf("select last(%s) as %s from %s where station='%s' and device='%s';",
|
||
|
|
req.SubPos, req.SubPos, req.Measure, req.Station, req.MainPos)
|
||
|
|
if limit > 1 {
|
||
|
|
sql = fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>%dms order by time desc limit %d;",
|
||
|
|
req.SubPos, req.Measure, req.Station, req.MainPos, req.Begin, limit) // begin = time.Now().UnixMilli()-int64(limit*20+20)
|
||
|
|
}
|
||
|
|
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {bucket},
|
||
|
|
"q": {sql},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getTVsResp(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) GetSSUPointsLastLimit(ctx context.Context, req *Request, limit int) (map[string][]*TV, error) {
|
||
|
|
sql := ""
|
||
|
|
if limit == 1 {
|
||
|
|
fields := strings.Split(req.SubPos, ",")
|
||
|
|
for i, field := range fields {
|
||
|
|
fields[i] = "last(" + field + ") as " + field
|
||
|
|
}
|
||
|
|
sql = fmt.Sprintf("select %s from %s where station='%s' and device='%s';",
|
||
|
|
strings.Join(fields, ","), req.Measure, req.Station, req.MainPos)
|
||
|
|
} else {
|
||
|
|
sql = fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>%dms order by time desc limit %d;",
|
||
|
|
req.SubPos, req.Measure, req.Station, req.MainPos, req.Begin, limit) // begin = time.Now().UnixMilli()-int64(limit*20+20)
|
||
|
|
}
|
||
|
|
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {bucket},
|
||
|
|
"q": {sql},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getF2TVsResp(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) GetSSUPointData(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
sql := fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms and time<%dms;",
|
||
|
|
req.SubPos, req.Measure, req.Station, req.MainPos, req.Begin, req.End)
|
||
|
|
|
||
|
|
if req.Operate != "" && req.Step != "" && req.Default != "" {
|
||
|
|
sql = fmt.Sprintf("select %s(%s) as %s from %s where station='%s' and device='%s' and time>=%dms and time<%dms group by time(%s) fill(%s);",
|
||
|
|
req.Operate, req.SubPos, req.SubPos, req.Measure, req.Station, req.MainPos, req.Begin, req.End, req.Step, req.Default)
|
||
|
|
}
|
||
|
|
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {bucket},
|
||
|
|
"q": {sql},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getTVsResp(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) GetSSUPointAfterOne(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
sql := fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms limit 1;",
|
||
|
|
req.SubPos, req.Measure, req.Station, req.MainPos, req.Begin)
|
||
|
|
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {bucket},
|
||
|
|
"q": {sql},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getTVsResp(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) GetSSUPointBeforeOne(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {bucket},
|
||
|
|
"q": {fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>%dms and time<=%dms order by time desc limit 1;",
|
||
|
|
req.SubPos, req.Measure, req.Station, req.MainPos, req.Begin, req.End)}, // begin = req.End-20-20
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getTVsResp(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) WriteLinesData(ctx context.Context, data []byte) error {
|
||
|
|
return client.writeLinesData(ctx, bucket, data, true)
|
||
|
|
}
|