Commit cfa9a376 authored by Jeremy Norris's avatar Jeremy Norris
Browse files

Minor repl patch

parent eca6a21a
......@@ -3,13 +3,12 @@ package cmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
"gitlab.qobserve.com/teamq/flux"
"gitlab.qobserve.com/teamq/flux/dependencies/filesystem"
"gitlab.qobserve.com/teamq/flux/dependencies/influxdb"
"gitlab.qobserve.com/teamq/flux/fluxinit"
"gitlab.qobserve.com/teamq/flux/repl"
"github.com/spf13/cobra"
)
// executeCmd represents the execute command
......@@ -50,8 +49,12 @@ func execute(cmd *cobra.Command, args []string) error {
fluxinit.FluxInit()
ctx, deps := injectDependencies(context.Background())
r := repl.New(ctx, deps)
if err := r.Input(args[0]); err != nil {
s, err := r.EvalX(args[0])
if err != nil {
return fmt.Errorf("failed to execute query: %v", err)
}
fmt.Print(s)
return nil
}
This diff is collapsed.
#group,false,false,true,true,false,false,true,true,true
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string
#default,_result,,,,,,,,
,result,table,_start,_stop,_time,_value,_field,_measurement,location
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:00:00Z,82,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:06:00Z,73,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:12:00Z,86,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:18:00Z,89,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:24:00Z,77,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:30:00Z,70,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:36:00Z,84,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:42:00Z,76,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:48:00Z,85,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T00:54:00Z,80,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T01:00:00Z,70,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T01:06:00Z,77,degrees,average_temperature,coyote_creek
,,0,1920-03-05T22:10:01.711964667Z,2020-03-05T22:10:01.711964667Z,2019-08-17T01:12:00Z,90,degrees,average_temperature,coyote_creek
......@@ -2,8 +2,10 @@
package repl
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
......@@ -166,6 +168,42 @@ func (r *REPL) Eval(t string) ([]interpreter.SideEffect, error) {
return r.itrp.Eval(r.ctx, pkg, r.scope, r.importer)
}
func (r *REPL) EvalX(t string) (string, error) {
ses, err := r.Eval(t)
if err != nil {
return "", err
}
var o bytes.Buffer
for _, se := range ses {
if _, ok := se.Node.(*semantic.ExpressionStatement); ok {
if t, ok := se.Value.(*flux.TableObject); ok {
now, ok := r.scope.Lookup("now")
if !ok {
return "", fmt.Errorf("now option not set")
}
ctx := r.deps.Inject(context.TODO())
nowTime, err := now.Function().Call(ctx, nil)
if err != nil {
return "", err
}
s, err := spec.FromTableObject(r.ctx, t, nowTime.Time().Time())
if err != nil {
return "", err
}
if err := r.doQuery(r.ctx, s, r.deps, &o); err != nil {
return "", err
}
} else {
fmt.Println(se.Value)
}
}
}
return o.String(), nil
}
// executeLine processes a line of input.
// If the input evaluates to a valid value, that value is returned.
func (r *REPL) executeLine(t string) error {
......@@ -190,7 +228,7 @@ func (r *REPL) executeLine(t string) error {
if err != nil {
return err
}
if err := r.doQuery(r.ctx, s, r.deps); err != nil {
if err := r.doQuery(r.ctx, s, r.deps, os.Stdout); err != nil {
return err
}
} else {
......@@ -214,7 +252,7 @@ func (r *REPL) analyzeLine(t string) (*semantic.Package, error) {
return semantic.DeserializeFromFlatBuffer(bs)
}
func (r *REPL) doQuery(ctx context.Context, spec *flux.Spec, deps flux.Dependencies) error {
func (r *REPL) doQuery(ctx context.Context, spec *flux.Spec, deps flux.Dependencies, o io.Writer) error {
// Setup cancel context
ctx, cancelFunc := context.WithCancel(ctx)
r.setCancel(cancelFunc)
......@@ -239,9 +277,9 @@ func (r *REPL) doQuery(ctx context.Context, spec *flux.Spec, deps flux.Dependenc
for result := range qry.Results() {
tables := result.Tables()
fmt.Println("Result:", result.Name())
//fmt.Println("Result:", result.Name())
if err := tables.Do(func(tbl flux.Table) error {
_, err := execute.NewFormatter(tbl, nil).WriteTo(os.Stdout)
_, err := execute.NewFormatter(tbl, nil).WriteTo(o)
return err
}); err != nil {
return err
......
import "experimental/csv"
csv.from(url: "https://influx-testdata.s3.amazonaws.com/noaa.csv")
import "csv"
csv.from(file: "noaa_small.csv")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment