dataRT/data/influx/104_point.go

62 lines
2.0 KiB
Go

package influx
import (
"context"
"fmt"
"net/url"
"time"
)
const (
db104 = "influxBucket"
tb104 = "cl104"
)
const (
mainPos104Key = "ca"
subPos104Key = "ioa"
val104Key = "val"
)
func Get104PointLast(ctx context.Context, req *Request) ([]TV, error) {
req.Begin = time.Now().UnixMilli() - int64(15*60*1000)
return client.Get104PointLastLimit(ctx, req, 1)
}
func Get104PointDuration(ctx context.Context, req *Request) ([]TV, error) {
return client.Get104PointLastLimit(ctx, req, 1)
}
func (client *influxClient) Get104PointLastLimit(ctx context.Context, req *Request, limit int) ([]TV, error) {
sql := fmt.Sprintf("select last(%s) as %s from %s where station='%s' and %s='%s' and %s='%s';",
val104Key, val104Key, req.Table, req.Station, mainPos104Key, req.MainPos, subPos104Key, req.SubPos)
if limit > 1 {
sql = fmt.Sprintf("select %s from %s where station='%s' and and %s='%s' and %s='%s' and time>=%dms order by time desc limit %d;",
val104Key, req.Table, req.Station, mainPos104Key, req.MainPos, subPos104Key, req.SubPos, req.Begin, limit)
}
reqData := url.Values{
"db": {req.DB},
"q": {sql},
}
return client.getTVsResp(ctx, reqData, "csv")
}
func (client *influxClient) Get104PointDurationData(ctx context.Context, req *Request) ([]TV, error) {
sql := fmt.Sprintf("select %s from %s where station='%s' and %s='%s' and %s='%s' and time>=%dms and time<=%dms;",
val104Key, req.Table, req.Station, mainPos104Key, req.MainPos, subPos104Key, req.SubPos, 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 %s='%s' and %s='%s' and time>=%dms and time<=%dms group by time(%s) fill(%s);",
req.Operate, req.SubPos, req.SubPos, req.Table, req.Station, mainPos104Key, req.MainPos, subPos104Key, req.SubPos, req.Begin, req.End, req.Step, req.Default)
}
reqData := url.Values{
"db": {req.DB},
"q": {sql},
}
return client.getTVsResp(ctx, reqData, "csv")
}