2025-09-19 16:17:46 +08:00
|
|
|
package influx
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
2025-11-06 21:09:50 +08:00
|
|
|
"time"
|
2025-09-19 16:17:46 +08:00
|
|
|
)
|
|
|
|
|
|
2025-10-11 14:56:11 +08:00
|
|
|
const (
|
2025-12-05 17:54:25 +08:00
|
|
|
dbphasor = "ssuBucket"
|
|
|
|
|
dbsample = "ssuBucket"
|
2025-11-06 21:09:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// keep consistent with telegraf
|
|
|
|
|
const (
|
|
|
|
|
FieldYCPrefix string = "tm"
|
|
|
|
|
FieldYXPrefix string = "ts"
|
2025-10-11 14:56:11 +08:00
|
|
|
|
|
|
|
|
// FieldP string = "p"
|
|
|
|
|
// FieldQ string = "q"
|
|
|
|
|
// FieldS string = "s"
|
|
|
|
|
// FieldPF string = "pf"
|
|
|
|
|
|
|
|
|
|
// FieldF string = "f"
|
|
|
|
|
FieldDF string = "df"
|
|
|
|
|
|
|
|
|
|
// FieldUABPrefix string = "uab"
|
|
|
|
|
// FieldUBCPrefix string = "ubc"
|
|
|
|
|
// FieldUCAPrefix string = "uca"
|
|
|
|
|
|
|
|
|
|
FieldSuffixAMP = "_amp"
|
|
|
|
|
FieldSuffixPA = "_pa"
|
|
|
|
|
FieldSuffixRMS = "_rms"
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-05 17:54:25 +08:00
|
|
|
const adaptedms = 5000
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
func GetSSUPointLastLimit(ctx context.Context, req *Request, limit int) ([]TV, error) {
|
2025-12-05 17:54:25 +08:00
|
|
|
req.Begin = time.Now().UnixMilli() - int64(limit*20+adaptedms)
|
2025-11-06 21:09:50 +08:00
|
|
|
return client.GetSSUPointLastLimit(ctx, req, limit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetSSUPointsLastLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
2025-12-05 17:54:25 +08:00
|
|
|
req.Begin = time.Now().UnixMilli() - int64(limit*20+adaptedms)
|
2025-11-06 21:09:50 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 14:56:11 +08:00
|
|
|
func (client *influxClient) GetSSUPointLastLimit(ctx context.Context, req *Request, limit int) ([]TV, error) {
|
2025-09-19 16:17:46 +08:00
|
|
|
sql := fmt.Sprintf("select last(%s) as %s from %s where station='%s' and device='%s';",
|
2025-11-06 21:09:50 +08:00
|
|
|
req.SubPos, req.SubPos, req.Table, req.Station, req.MainPos)
|
2025-09-19 16:17:46 +08:00
|
|
|
if limit > 1 {
|
2025-11-06 21:09:50 +08:00
|
|
|
sql = fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms order by time desc limit %d;",
|
2025-12-05 17:54:25 +08:00
|
|
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, limit)
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reqData := url.Values{
|
2025-11-06 21:09:50 +08:00
|
|
|
"db": {req.DB},
|
2025-09-19 16:17:46 +08:00
|
|
|
"q": {sql},
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
return client.getTVsResp(ctx, reqData, "csv")
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-11 14:56:11 +08:00
|
|
|
func (client *influxClient) GetSSUPointsLastLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
2025-09-19 16:17:46 +08:00
|
|
|
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';",
|
2025-11-06 21:09:50 +08:00
|
|
|
strings.Join(fields, ","), req.Table, req.Station, req.MainPos)
|
2025-09-19 16:17:46 +08:00
|
|
|
} else {
|
2025-11-06 21:09:50 +08:00
|
|
|
sql = fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms order by time desc limit %d;",
|
2025-12-05 17:54:25 +08:00
|
|
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, limit)
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reqData := url.Values{
|
2025-11-06 21:09:50 +08:00
|
|
|
"db": {req.DB},
|
2025-09-19 16:17:46 +08:00
|
|
|
"q": {sql},
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return f2tvs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make(map[string][]TV, len(f2tvs))
|
|
|
|
|
for f, tvs := range f2tvs {
|
2025-12-05 17:54:25 +08:00
|
|
|
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
2025-11-06 21:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 18:02:29 +08:00
|
|
|
func (client *influxClient) GetSSUPointDurationData(ctx context.Context, req *Request) ([]TV, error) {
|
2025-11-06 21:09:50 +08:00
|
|
|
sql := fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms and time<=%dms;",
|
|
|
|
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, req.End)
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
if req.Operate != "" && req.Step != "" && req.Default != "" {
|
2025-11-06 21:09:50 +08:00
|
|
|
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.Table, req.Station, req.MainPos, req.Begin, req.End, req.Step, req.Default)
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reqData := url.Values{
|
2025-11-06 21:09:50 +08:00
|
|
|
"db": {req.DB},
|
2025-09-19 16:17:46 +08:00
|
|
|
"q": {sql},
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
return client.getTVsResp(ctx, reqData, "csv")
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 18:02:29 +08:00
|
|
|
func (client *influxClient) GetSSUPointsDurationData(ctx context.Context, req *Request) (map[string][]TV, error) {
|
2025-11-06 21:09:50 +08:00
|
|
|
sql := fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms and time<=%dms;",
|
|
|
|
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, req.End)
|
2025-10-23 18:02:29 +08:00
|
|
|
|
|
|
|
|
if req.Operate != "" && req.Step != "" && req.Default != "" {
|
|
|
|
|
subPoss := strings.Split(req.SubPos, ",")
|
|
|
|
|
selectSections := make([]string, len(subPoss))
|
|
|
|
|
for i, subPos := range subPoss {
|
|
|
|
|
selectSections[i] = req.Operate + "(" + subPos + ")" + " as " + subPos
|
|
|
|
|
}
|
2025-11-06 21:09:50 +08:00
|
|
|
sql = fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms and time<=%dms group by time(%s) fill(%s);",
|
|
|
|
|
strings.Join(selectSections, ", "), req.Table, req.Station, req.MainPos, req.Begin, req.End, req.Step, req.Default)
|
2025-10-23 18:02:29 +08:00
|
|
|
}
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
reqData := url.Values{
|
2025-11-06 21:09:50 +08:00
|
|
|
"db": {req.DB},
|
2025-09-19 16:17:46 +08:00
|
|
|
"q": {sql},
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return f2tvs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make(map[string][]TV, len(f2tvs))
|
|
|
|
|
for f, tvs := range f2tvs {
|
2025-12-05 17:54:25 +08:00
|
|
|
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
2025-11-06 21:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 18:02:29 +08:00
|
|
|
func (client *influxClient) GetSSUPointAfterLimit(ctx context.Context, req *Request, limit int) ([]TV, error) {
|
|
|
|
|
sql := fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms and time<=%dms limit %d;",
|
2025-11-06 21:09:50 +08:00
|
|
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, req.End, limit)
|
2025-10-23 18:02:29 +08:00
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
reqData := url.Values{
|
2025-11-06 21:09:50 +08:00
|
|
|
"db": {req.DB},
|
2025-10-23 18:02:29 +08:00
|
|
|
"q": {sql},
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
return client.getTVsResp(ctx, reqData, "csv")
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 18:02:29 +08:00
|
|
|
func (client *influxClient) GetSSUPointsAfterLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
|
|
|
|
sql := fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms and time<=%dms limit %d;",
|
2025-11-06 21:09:50 +08:00
|
|
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, req.End, limit)
|
2025-10-23 18:02:29 +08:00
|
|
|
|
|
|
|
|
reqData := url.Values{
|
2025-11-06 21:09:50 +08:00
|
|
|
"db": {req.DB},
|
2025-10-23 18:02:29 +08:00
|
|
|
"q": {sql},
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return f2tvs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make(map[string][]TV, len(f2tvs))
|
|
|
|
|
for f, tvs := range f2tvs {
|
2025-12-05 17:54:25 +08:00
|
|
|
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
2025-11-06 21:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
2025-10-23 18:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (client *influxClient) GetSSUPointsBeforeLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
|
|
|
|
reqData := url.Values{
|
2025-11-06 21:09:50 +08:00
|
|
|
"db": {req.DB},
|
|
|
|
|
"q": {fmt.Sprintf("select %s from %s where station='%s' and device='%s' and time>=%dms and time<=%dms order by time desc limit %d;",
|
2025-12-05 17:54:25 +08:00
|
|
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, req.End, limit)},
|
2025-11-06 21:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return f2tvs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := make(map[string][]TV, len(f2tvs))
|
|
|
|
|
for f, tvs := range f2tvs {
|
2025-12-05 17:54:25 +08:00
|
|
|
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
2025-10-23 18:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
return ret, nil
|
2025-10-23 18:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
func (client *influxClient) WriteLinesData(ctx context.Context, db string, data []byte) error {
|
|
|
|
|
return client.writeLinesData(ctx, db, data, true)
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|