2018-07-25 18:27:10 +00:00
|
|
|
package influxql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-06 16:18:33 +00:00
|
|
|
"time"
|
2018-07-25 18:27:10 +00:00
|
|
|
|
2018-09-06 18:09:52 +00:00
|
|
|
"github.com/influxdata/flux"
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
2018-07-25 18:27:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const CompilerType = "influxql"
|
|
|
|
|
|
|
|
// AddCompilerMappings adds the influxql specific compiler mappings.
|
2018-09-06 18:09:52 +00:00
|
|
|
func AddCompilerMappings(mappings flux.CompilerMappings, dbrpMappingSvc platform.DBRPMappingService) error {
|
|
|
|
return mappings.Add(CompilerType, func() flux.Compiler {
|
2018-07-25 18:27:10 +00:00
|
|
|
return NewCompiler(dbrpMappingSvc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compiler is the transpiler to convert InfluxQL to a Flux specification.
|
|
|
|
type Compiler struct {
|
2019-02-06 16:18:33 +00:00
|
|
|
Cluster string `json:"cluster,omitempty"`
|
|
|
|
DB string `json:"db,omitempty"`
|
|
|
|
RP string `json:"rp,omitempty"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Now *time.Time `json:"now,omitempty"`
|
2018-07-25 18:27:10 +00:00
|
|
|
|
|
|
|
dbrpMappingSvc platform.DBRPMappingService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCompiler(dbrpMappingSvc platform.DBRPMappingService) *Compiler {
|
|
|
|
return &Compiler{
|
|
|
|
dbrpMappingSvc: dbrpMappingSvc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile tranpiles the query into a specification.
|
2018-09-06 18:09:52 +00:00
|
|
|
func (c *Compiler) Compile(ctx context.Context) (*flux.Spec, error) {
|
2019-02-06 16:18:33 +00:00
|
|
|
var now time.Time
|
|
|
|
if c.Now != nil {
|
|
|
|
now = *c.Now
|
|
|
|
} else {
|
|
|
|
now = time.Now()
|
|
|
|
}
|
2018-07-25 18:27:10 +00:00
|
|
|
transpiler := NewTranspilerWithConfig(
|
|
|
|
c.dbrpMappingSvc,
|
|
|
|
Config{
|
|
|
|
Cluster: c.Cluster,
|
|
|
|
DefaultDatabase: c.DB,
|
|
|
|
DefaultRetentionPolicy: c.RP,
|
2019-02-06 16:18:33 +00:00
|
|
|
Now: now,
|
2018-07-25 18:27:10 +00:00
|
|
|
},
|
|
|
|
)
|
2019-02-06 16:18:33 +00:00
|
|
|
astPkg, err := transpiler.Transpile(ctx, c.Query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-07 16:02:23 +00:00
|
|
|
return flux.CompileAST(ctx, astPkg, now)
|
2018-07-25 18:27:10 +00:00
|
|
|
}
|
2018-09-06 18:09:52 +00:00
|
|
|
func (c *Compiler) CompilerType() flux.CompilerType {
|
2018-07-25 18:27:10 +00:00
|
|
|
return CompilerType
|
|
|
|
}
|