2024-08-02 16:16:49 +08:00
|
|
|
// Package database define database operation functions
|
|
|
|
|
package database
|
|
|
|
|
|
2024-08-06 17:03:41 +08:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
2024-08-02 16:16:49 +08:00
|
|
|
|
2024-08-06 17:03:41 +08:00
|
|
|
// 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
|
|
|
|
|
}
|
2024-08-02 16:16:49 +08:00
|
|
|
|
2024-08-06 17:03:41 +08:00
|
|
|
func WithStartTime(measurement string) StartTimeQueryPara {
|
|
|
|
|
return StartTimeQueryPara(measurement)
|
2024-08-02 16:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 17:03:41 +08:00
|
|
|
func InitQueryByPara(paras ...QueryPara) (queryCmd string) {
|
|
|
|
|
for _, para := range paras {
|
|
|
|
|
queryCmd = para.apply(queryCmd)
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("queryCmd:%v\n", queryCmd)
|
|
|
|
|
return
|
2024-08-02 16:16:49 +08:00
|
|
|
}
|