influxdb/cmd/influx/transpile.go

80 lines
2.4 KiB
Go

package main
import (
"context"
"fmt"
"time"
"github.com/influxdata/flux/ast"
"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/kit/errors"
"github.com/influxdata/influxdb/query/influxql"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var transpileCmd = &cobra.Command{
Use: "transpile [InfluxQL query]",
Short: "Transpile an InfluxQL query to Flux source code",
Long: `Transpile an InfluxQL query to Flux source code.
The transpiled query assumes that the bucket name is the of the form '<database>/<retention policy>'.
The transpiled query will be written for absolute time ranges using the provided now() time.`,
Args: cobra.ExactArgs(1),
RunE: transpileF,
}
var transpileFlags struct {
Now string
}
func init() {
transpileCmd.PersistentFlags().StringVar(&transpileFlags.Now, "now", "", "An RFC3339Nano formatted time to use as the now() time. Defaults to the current time.")
viper.BindEnv("NOW")
if h := viper.GetString("NOW"); h != "" {
transpileFlags.Now = h
}
}
func transpileF(cmd *cobra.Command, args []string) error {
now := time.Now()
if transpileFlags.Now != "" {
var err error
now, err = time.Parse(time.RFC3339Nano, transpileFlags.Now)
if err != nil {
return errors.Wrap(err, "invalid now time")
}
}
t := influxql.NewTranspilerWithConfig(dbrpMapper{}, influxql.Config{
Now: now,
FallbackToDBRP: true,
})
pkg, err := t.Transpile(context.Background(), args[0])
if err != nil {
return err
}
fmt.Println(ast.Format(pkg))
return nil
}
type dbrpMapper struct {
}
func (m dbrpMapper) FindBy(ctx context.Context, cluster string, db string, rp string) (*influxdb.DBRPMapping, error) {
return nil, errors.New("mapping not found")
}
func (m dbrpMapper) Find(ctx context.Context, filter influxdb.DBRPMappingFilter) (*influxdb.DBRPMapping, error) {
return nil, errors.New("mapping not found")
}
func (m dbrpMapper) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) {
return nil, 0, errors.New("mapping not found")
}
func (m dbrpMapper) Create(ctx context.Context, dbrpMap *influxdb.DBRPMapping) error {
return errors.New("dbrpMapper does not support creating new mappings")
}
func (m dbrpMapper) Delete(ctx context.Context, cluster string, db string, rp string) error {
return errors.New("dbrpMapper does not support deleteing mappings")
}