예제 #1
0
func mustParseStatement(s string) influxql.Statement {
	stmt, err := influxql.ParseStatement(s)
	if err != nil {
		panic(err)
	}
	return stmt
}
예제 #2
0
func main() {
	args := os.Args
	if len(args) != 2 && len(args) != 3 {
		log.Fatalln("Usage: " + os.Args[0] + " NUM_PRODUCTIONS [PRODUCTION=statement]")
	}
	numProductions, err := strconv.Atoi(args[1])
	if err != nil {
		log.Fatalln(err)
	}

	production := "statement"
	if len(args) == 3 {
		production = args[2]
	}

	grammar, err := rebnf.Parse("influxql.ebnf", nil)
	if err != nil {
		log.Fatalln(err)
	}

	if _, ok := grammar[production]; !ok {
		log.Fatalln(production + " is not a known production!")
	}

	maxRepetitions := 30
	maxRecursionDepth := 15
	padding := "???"
	isDebug := false
	ctx := rebnf.NewCtx(maxRepetitions, maxRecursionDepth, padding, isDebug)

	for i := 0; i < numProductions; i++ {
		buf := new(bytes.Buffer)
		ctx.Random(buf, grammar, production)

		line, _ := ioutil.ReadAll(buf)

		fmt.Println("GENERATED: " + string(line))
		if stmt, err := influxql.ParseStatement(string(line)); err != nil {
			fmt.Fprintln(os.Stderr, "ERROR    : "+err.Error())
		} else {
			sanitized := stmt.String()
			fmt.Println("SANITIZED: " + sanitized)
			resanitized := influxql.MustParseStatement(sanitized).String()
			if resanitized != sanitized {
				fmt.Println("IMPURE   : " + resanitized)
			}
		}

		fmt.Println()
	}
}
예제 #3
0
파일: query.go 프로젝트: md14454/kapacitor
func NewQuery(q string) (*Query, error) {
	query := &Query{}
	// Parse and validate query
	stmt, err := influxql.ParseStatement(q)
	if err != nil {
		return nil, err
	}
	var ok bool
	query.stmt, ok = stmt.(*influxql.SelectStatement)
	if !ok {
		return nil, fmt.Errorf("query is not a select statement %q", q)
	}

	// Add in time condition nodes
	query.startTL = &influxql.TimeLiteral{}
	startExpr := &influxql.BinaryExpr{
		Op:  influxql.GT,
		LHS: &influxql.VarRef{Val: "time"},
		RHS: query.startTL,
	}

	query.stopTL = &influxql.TimeLiteral{}
	stopExpr := &influxql.BinaryExpr{
		Op:  influxql.LT,
		LHS: &influxql.VarRef{Val: "time"},
		RHS: query.stopTL,
	}

	if query.stmt.Condition != nil {
		query.stmt.Condition = &influxql.BinaryExpr{
			Op:  influxql.AND,
			LHS: query.stmt.Condition,
			RHS: &influxql.BinaryExpr{
				Op:  influxql.AND,
				LHS: startExpr,
				RHS: stopExpr,
			},
		}
	} else {
		query.stmt.Condition = &influxql.BinaryExpr{
			Op:  influxql.AND,
			LHS: startExpr,
			RHS: stopExpr,
		}
	}
	return query, nil
}
예제 #4
0
func (r *Service) doRecordQuery(rid uuid.UUID, q string, tt kapacitor.TaskType) error {
	// Parse query to determine dbrp
	var db, rp string
	s, err := influxql.ParseStatement(q)
	if err != nil {
		return err
	}
	if slct, ok := s.(*influxql.SelectStatement); ok && len(slct.Sources) == 1 {
		if m, ok := slct.Sources[0].(*influxql.Measurement); ok {
			db = m.Database
			rp = m.RetentionPolicy
		}
	}
	if db == "" || rp == "" {
		return errors.New("could not determine database and retention policy. Is the query fully qualified?")
	}
	// Query InfluxDB
	con, err := r.InfluxDBService.NewClient()
	if err != nil {
		return err
	}
	query := client.Query{
		Command: q,
	}
	resp, err := con.Query(query)
	if err != nil {
		return err
	}
	if resp.Err != nil {
		return resp.Err
	}
	// Open appropriate writer
	var w io.Writer
	var c io.Closer
	switch tt {
	case kapacitor.StreamTask:
		sw, err := r.newStreamWriter(rid)
		if err != nil {
			return err
		}
		w = sw
		c = sw
	case kapacitor.BatchTask:
		archive, err := r.newBatchArchive(rid)
		if err != nil {
			return err
		}
		w, err = archive.Create(0)
		if err != nil {
			return err
		}
		c = archive
	}
	// Write results to writer
	for _, res := range resp.Results {
		batches, err := models.ResultToBatches(res)
		if err != nil {
			c.Close()
			return err
		}
		for _, batch := range batches {
			switch tt {
			case kapacitor.StreamTask:
				for _, bp := range batch.Points {
					p := models.Point{
						Name:            batch.Name,
						Database:        db,
						RetentionPolicy: rp,
						Tags:            bp.Tags,
						Fields:          bp.Fields,
						Time:            bp.Time,
					}
					kapacitor.WritePointForRecording(w, p, precision)
				}
			case kapacitor.BatchTask:
				kapacitor.WriteBatchForRecording(w, batch)
			}
		}
	}
	return c.Close()
}