PowerEngine/wave_record/database/influxdb_para.go

66 lines
1.6 KiB
Go
Raw Normal View History

// Package database define database operation functions
package database
import (
"fmt"
"strings"
)
// QueryPara define struct of influxdb query parameters
type InfluxDBQueryPara struct {
Bucket string `json:"bucket"`
Measurement string `json:"measurement"`
Start string `json:"start"`
Stop string `json:"stop"`
Field string `json:"field"`
// Value float64 `json:"value"`
}
type QueryPara interface {
apply(string) string
}
type BucketQueryPara string
func (b BucketQueryPara) apply(query string) string {
template := " from(bucket:\"{bucketId}\") "
template = strings.Replace(template, "{bucketId}", string(b), -1)
return query + template
}
func WithBucket(bucketID string) BucketQueryPara {
return BucketQueryPara(bucketID)
}
type MeasurementQueryPara string
func (m MeasurementQueryPara) apply(query string) string {
template := " |> filter(fn: (r) => r[\"_measurement\"] == \"{measurement}\") "
template = strings.Replace(template, "{measurement}", string(m), -1)
return query + template
}
func WithMeasurement(measurement string) MeasurementQueryPara {
return MeasurementQueryPara(measurement)
}
type StartTimeQueryPara string
func (s StartTimeQueryPara) apply(query string) string {
template := " |> range(start: {start}) "
template = strings.Replace(template, "{start}", string(s), -1)
return query + template
}
func WithStartTime(measurement string) StartTimeQueryPara {
return StartTimeQueryPara(measurement)
}
func InitQueryByPara(paras ...QueryPara) (queryCmd string) {
for _, para := range paras {
queryCmd = para.apply(queryCmd)
}
fmt.Printf("queryCmd:%v\n", queryCmd)
return
}