62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
|
|
package influx
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"net/url"
|
||
|
|
)
|
||
|
|
|
||
|
|
// terminal unique
|
||
|
|
func (client *influxClient) getLast(ctx context.Context, req *Request, limit int) ([]*TV, error) {
|
||
|
|
q := fmt.Sprintf("select last(%s) as %s from %s where station='%s' and device='%s';",
|
||
|
|
req.Field, req.Field, req.Measure, req.Station, req.Device)
|
||
|
|
if limit > 1 {
|
||
|
|
q = fmt.Sprintf("select %s from %s where station='%s' and device='%s' order by time desc limit %d;",
|
||
|
|
req.Field, req.Measure, req.Station, req.Device, limit)
|
||
|
|
}
|
||
|
|
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {req.Database},
|
||
|
|
"q": {q},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getRespData(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) getPointData(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.Field, req.Measure, req.Station, req.Device, 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.Field, req.Field, req.Measure, req.Station, req.Device, req.Begin, req.End, req.Step, req.Default)
|
||
|
|
}
|
||
|
|
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {req.Database},
|
||
|
|
"q": {sql},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getRespData(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) getAfterOne(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {req.Database},
|
||
|
|
"q": {fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms limit 1;",
|
||
|
|
req.Field, req.Measure, req.Station, req.Device, req.Begin)},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getRespData(ctx, reqData, req.RespType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (client *influxClient) getBeforeOne(ctx context.Context, req *Request) ([]*TV, error) {
|
||
|
|
reqData := url.Values{
|
||
|
|
"db": {req.Database},
|
||
|
|
"q": {fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time<=%dms limit 1;",
|
||
|
|
req.Field, req.Measure, req.Station, req.Device, req.End)},
|
||
|
|
}
|
||
|
|
|
||
|
|
return client.getRespData(ctx, reqData, req.RespType)
|
||
|
|
}
|