Skip to content

Commit

Permalink
refactor: move FluxLanguageService interface to fluxlang
Browse files Browse the repository at this point in the history
  • Loading branch information
lesam committed Mar 31, 2021
1 parent bb51e12 commit b60fad0
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 67 deletions.
5 changes: 3 additions & 2 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package influxdb
import (
"context"
"encoding/json"
"github.com/influxdata/influxdb/v2/query/fluxlang"

"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
Expand All @@ -16,14 +17,14 @@ const (

// Check represents the information required to generate a periodic check task.
type Check interface {
Valid(lang FluxLanguageService) error
Valid(lang fluxlang.FluxLanguageService) error
Type() string
ClearPrivateData()
SetTaskID(platform.ID)
GetTaskID() platform.ID
GetOwnerID() platform.ID
SetOwnerID(platform.ID)
GenerateFlux(lang FluxLanguageService) (string, error)
GenerateFlux(lang fluxlang.FluxLanguageService) (string, error)
json.Marshaler

CRUDLogSetter
Expand Down
3 changes: 2 additions & 1 deletion http/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"net/http"

"github.com/influxdata/influxdb/v2/kit/platform"
Expand Down Expand Up @@ -85,7 +86,7 @@ type APIBackend struct {
InfluxQLService query.ProxyQueryService
InfluxqldService influxql.ProxyQueryService
FluxService query.ProxyQueryService
FluxLanguageService influxdb.FluxLanguageService
FluxLanguageService fluxlang.FluxLanguageService
TaskService influxdb.TaskService
CheckService influxdb.CheckService
TelegrafService influxdb.TelegrafConfigStore
Expand Down
7 changes: 4 additions & 3 deletions http/check_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"io/ioutil"
"net/http"
"path"
Expand Down Expand Up @@ -34,7 +35,7 @@ type CheckBackend struct {
LabelService influxdb.LabelService
UserService influxdb.UserService
OrganizationService influxdb.OrganizationService
FluxLanguageService influxdb.FluxLanguageService
FluxLanguageService fluxlang.FluxLanguageService
}

// NewCheckBackend returns a new instance of CheckBackend.
Expand Down Expand Up @@ -65,7 +66,7 @@ type CheckHandler struct {
LabelService influxdb.LabelService
UserService influxdb.UserService
OrganizationService influxdb.OrganizationService
FluxLanguageService influxdb.FluxLanguageService
FluxLanguageService fluxlang.FluxLanguageService
}

const (
Expand Down Expand Up @@ -441,7 +442,7 @@ func decodePostCheckRequest(r *http.Request) (postCheckRequest, error) {
}, nil
}

func decodePutCheckRequest(ctx context.Context, lang influxdb.FluxLanguageService, r *http.Request) (influxdb.CheckCreate, error) {
func decodePutCheckRequest(ctx context.Context, lang fluxlang.FluxLanguageService, r *http.Request) (influxdb.CheckCreate, error) {
params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
Expand Down
5 changes: 3 additions & 2 deletions http/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"io"
"io/ioutil"
"mime"
Expand Down Expand Up @@ -144,7 +145,7 @@ type queryParseError struct {

// Analyze attempts to parse the query request and returns any errors
// encountered in a structured way.
func (r QueryRequest) Analyze(l influxdb.FluxLanguageService) (*QueryAnalysis, error) {
func (r QueryRequest) Analyze(l fluxlang.FluxLanguageService) (*QueryAnalysis, error) {
switch r.Type {
case "flux":
return r.analyzeFluxQuery(l)
Expand All @@ -155,7 +156,7 @@ func (r QueryRequest) Analyze(l influxdb.FluxLanguageService) (*QueryAnalysis, e
return nil, fmt.Errorf("unknown query request type %s", r.Type)
}

func (r QueryRequest) analyzeFluxQuery(l influxdb.FluxLanguageService) (*QueryAnalysis, error) {
func (r QueryRequest) analyzeFluxQuery(l fluxlang.FluxLanguageService) (*QueryAnalysis, error) {
a := &QueryAnalysis{}
pkg, err := query.Parse(l, r.Query)
if pkg == nil {
Expand Down
5 changes: 3 additions & 2 deletions http/query_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -51,7 +52,7 @@ type FluxBackend struct {
AlgoWProxy FeatureProxyHandler
OrganizationService influxdb.OrganizationService
ProxyQueryService query.ProxyQueryService
FluxLanguageService influxdb.FluxLanguageService
FluxLanguageService fluxlang.FluxLanguageService
Flagger feature.Flagger
}

Expand Down Expand Up @@ -86,7 +87,7 @@ type FluxHandler struct {
Now func() time.Time
OrganizationService influxdb.OrganizationService
ProxyQueryService query.ProxyQueryService
FluxLanguageService influxdb.FluxLanguageService
FluxLanguageService fluxlang.FluxLanguageService

EventRecorder metric.EventRecorder

Expand Down
5 changes: 3 additions & 2 deletions kv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/benbjohnson/clock"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"github.com/influxdata/influxdb/v2/rand"
"github.com/influxdata/influxdb/v2/resource"
"github.com/influxdata/influxdb/v2/resource/noop"
Expand All @@ -26,7 +27,7 @@ type Service struct {
// FluxLanguageService is used for parsing flux.
// If this is unset, operations that require parsing flux
// will fail.
FluxLanguageService influxdb.FluxLanguageService
FluxLanguageService fluxlang.FluxLanguageService

TokenGenerator influxdb.TokenGenerator
// TODO(desa:ariel): this should not be embedded
Expand Down Expand Up @@ -67,7 +68,7 @@ func NewService(log *zap.Logger, kv Store, orgs influxdb.OrganizationService, co
// ServiceConfig allows us to configure Services
type ServiceConfig struct {
Clock clock.Clock
FluxLanguageService influxdb.FluxLanguageService
FluxLanguageService fluxlang.FluxLanguageService
}

// WithResourceLogger sets the resource audit logger for the service.
Expand Down
3 changes: 2 additions & 1 deletion notification/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package check
import (
"encoding/json"
"fmt"
"github.com/influxdata/influxdb/v2/query/fluxlang"

"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
Expand Down Expand Up @@ -39,7 +40,7 @@ type Base struct {
}

// Valid returns err if the check is invalid.
func (b Base) Valid(lang influxdb.FluxLanguageService) error {
func (b Base) Valid(lang fluxlang.FluxLanguageService) error {
if !b.ID.Valid() {
return &errors.Error{
Code: errors.EInvalid,
Expand Down
11 changes: 6 additions & 5 deletions notification/check/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package check

import (
"encoding/json"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"time"

"github.com/influxdata/influxdb/v2/kit/platform"
Expand Down Expand Up @@ -64,12 +65,12 @@ type Custom struct {
// |> monitor.check(data: check, messageFn:messageFn, warn:warn, crit:crit, info:info)

// GenerateFlux returns the check query text directly
func (c Custom) GenerateFlux(lang influxdb.FluxLanguageService) (string, error) {
func (c Custom) GenerateFlux(lang fluxlang.FluxLanguageService) (string, error) {
return c.Query.Text, nil
}

// sanitizeFlux modifies the check query text to include correct _check_id param in check object
func (c Custom) sanitizeFlux(lang influxdb.FluxLanguageService) (string, error) {
func (c Custom) sanitizeFlux(lang fluxlang.FluxLanguageService) (string, error) {
p, err := query.Parse(lang, c.Query.Text)
if p == nil {
return "", err
Expand Down Expand Up @@ -106,7 +107,7 @@ func propertyHasValue(prop *ast.Property, key string, value string) bool {
return ok && prop.Key.Key() == key && stringLit.Value == value
}

func (c *Custom) hasRequiredTaskOptions(lang influxdb.FluxLanguageService) (err error) {
func (c *Custom) hasRequiredTaskOptions(lang fluxlang.FluxLanguageService) (err error) {

p, err := query.Parse(lang, c.Query.Text)
if p == nil {
Expand Down Expand Up @@ -175,7 +176,7 @@ func (c *Custom) hasRequiredTaskOptions(lang influxdb.FluxLanguageService) (err
return nil
}

func (c *Custom) hasRequiredCheckParameters(lang influxdb.FluxLanguageService) (err error) {
func (c *Custom) hasRequiredCheckParameters(lang fluxlang.FluxLanguageService) (err error) {
p, err := query.Parse(lang, c.Query.Text)
if p == nil {
return err
Expand Down Expand Up @@ -223,7 +224,7 @@ func (c *Custom) hasRequiredCheckParameters(lang influxdb.FluxLanguageService) (
}

// Valid checks whether check flux is valid, returns error if invalid
func (c *Custom) Valid(lang influxdb.FluxLanguageService) error {
func (c *Custom) Valid(lang fluxlang.FluxLanguageService) error {

if err := c.hasRequiredCheckParameters(lang); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions notification/check/deadman.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package check
import (
"encoding/json"
"fmt"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"strings"

"github.com/influxdata/flux/ast"
Expand Down Expand Up @@ -31,7 +32,7 @@ func (c Deadman) Type() string {
}

// GenerateFlux returns a flux script for the Deadman provided.
func (c Deadman) GenerateFlux(lang influxdb.FluxLanguageService) (string, error) {
func (c Deadman) GenerateFlux(lang fluxlang.FluxLanguageService) (string, error) {
p, err := c.GenerateFluxAST(lang)
if err != nil {
return "", err
Expand All @@ -43,7 +44,7 @@ func (c Deadman) GenerateFlux(lang influxdb.FluxLanguageService) (string, error)
// GenerateFluxAST returns a flux AST for the deadman provided. If there
// are any errors in the flux that the user provided the function will return
// an error for each error found when the script is parsed.
func (c Deadman) GenerateFluxAST(lang influxdb.FluxLanguageService) (*ast.Package, error) {
func (c Deadman) GenerateFluxAST(lang fluxlang.FluxLanguageService) (*ast.Package, error) {
p, err := query.Parse(lang, c.Query.Text)
if p == nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions notification/check/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package check
import (
"encoding/json"
"fmt"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"strings"

"github.com/influxdata/influxdb/v2/kit/platform/errors"
Expand All @@ -28,7 +29,7 @@ func (t Threshold) Type() string {
}

// Valid returns error if something is invalid.
func (t Threshold) Valid(lang influxdb.FluxLanguageService) error {
func (t Threshold) Valid(lang fluxlang.FluxLanguageService) error {
if err := t.Base.Valid(lang); err != nil {
return err
}
Expand Down Expand Up @@ -106,7 +107,7 @@ func multiError(errs []error) error {
// GenerateFlux returns a flux script for the threshold provided. If there
// are any errors in the flux that the user provided the function will return
// an error for each error found when the script is parsed.
func (t Threshold) GenerateFlux(lang influxdb.FluxLanguageService) (string, error) {
func (t Threshold) GenerateFlux(lang fluxlang.FluxLanguageService) (string, error) {
p, err := t.GenerateFluxAST(lang)
if err != nil {
return "", err
Expand All @@ -118,7 +119,7 @@ func (t Threshold) GenerateFlux(lang influxdb.FluxLanguageService) (string, erro
// GenerateFluxAST returns a flux AST for the threshold provided. If there
// are any errors in the flux that the user provided the function will return
// an error for each error found when the script is parsed.
func (t Threshold) GenerateFluxAST(lang influxdb.FluxLanguageService) (*ast.Package, error) {
func (t Threshold) GenerateFluxAST(lang fluxlang.FluxLanguageService) (*ast.Package, error) {
p, err := query.Parse(lang, t.Query.Text)
if p == nil {
return nil, err
Expand Down
35 changes: 0 additions & 35 deletions query.go

This file was deleted.

24 changes: 22 additions & 2 deletions query/fluxlang/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,31 @@ import (
"github.com/influxdata/flux/parser"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
"github.com/influxdata/influxdb/v2"
)

// SourceQuery is a query for a source.
type SourceQuery struct {
Query string `json:"query"`
Type string `json:"type"`
}

// FluxLanguageService is a service for interacting with flux code.
type FluxLanguageService interface {
// Parse will take flux source code and produce a package.
// If there are errors when parsing, the first error is returned.
// An ast.Package may be returned when a parsing error occurs,
// but it may be null if parsing didn't even occur.
Parse(source string) (*ast.Package, error)

// EvalAST will evaluate and run an AST.
EvalAST(ctx context.Context, astPkg *ast.Package) ([]interpreter.SideEffect, values.Scope, error)

// Completer will return a flux completer.
Completer() complete.Completer
}

// DefaultService is the default language service.
var DefaultService influxdb.FluxLanguageService = defaultService{}
var DefaultService FluxLanguageService = defaultService{}

type defaultService struct{}

Expand Down
6 changes: 3 additions & 3 deletions query/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package query

import (
"context"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"io"

"github.com/influxdata/influxdb/v2/kit/platform/errors"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/values"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/check"
)

Expand Down Expand Up @@ -46,7 +46,7 @@ type ProxyQueryService interface {
// but it may be null if parsing didn't even occur.
//
// This will return an error if the FluxLanguageService is nil.
func Parse(lang influxdb.FluxLanguageService, source string) (*ast.Package, error) {
func Parse(lang fluxlang.FluxLanguageService, source string) (*ast.Package, error) {
if lang == nil {
return nil, &errors.Error{
Code: errors.EInternal,
Expand All @@ -59,7 +59,7 @@ func Parse(lang influxdb.FluxLanguageService, source string) (*ast.Package, erro
// EvalAST will evaluate and run an AST.
//
// This will return an error if the FluxLanguageService is nil.
func EvalAST(ctx context.Context, lang influxdb.FluxLanguageService, astPkg *ast.Package) ([]interpreter.SideEffect, values.Scope, error) {
func EvalAST(ctx context.Context, lang fluxlang.FluxLanguageService, astPkg *ast.Package) ([]interpreter.SideEffect, values.Scope, error) {
if lang == nil {
return nil, nil, &errors.Error{
Code: errors.EInternal,
Expand Down
Loading

0 comments on commit b60fad0

Please sign in to comment.