示例#1
0
// runContinuousQueryAndWriteResult will run the query against the cluster and write the results back in
func (s *Service) runContinuousQueryAndWriteResult(cq *ContinuousQuery) error {
	// Wrap the CQ's inner SELECT statement in a Query for the QueryExecutor.
	q := &influxql.Query{
		Statements: influxql.Statements([]influxql.Statement{cq.q}),
	}

	// Execute the SELECT.
	ch, err := s.QueryExecutor.ExecuteQuery(q, cq.Database, NoChunkingSize)
	if err != nil {
		return err
	}
	// There is only one statement, so we will only ever receive one result
	res, ok := <-ch
	if !ok {
		panic("result channel was closed")
	}
	if res.Err != nil {
		return res.Err
	}
	return nil
}
示例#2
0
// runContinuousQueryAndWriteResult will run the query against the cluster and write the results back in
func (s *Service) runContinuousQueryAndWriteResult(cq *ContinuousQuery) error {
	// Wrap the CQ's inner SELECT statement in a Query for the QueryExecutor.
	q := &influxql.Query{
		Statements: influxql.Statements([]influxql.Statement{cq.q}),
	}

	// Execute the SELECT.
	ch, err := s.QueryExecutor.ExecuteQuery(q, cq.Database, NoChunkingSize)
	if err != nil {
		return err
	}

	// Read all rows from the result channel.
	points := make([]models.Point, 0, 100)
	for result := range ch {
		if result.Err != nil {
			return result.Err
		}

		for _, row := range result.Series {
			// Get the measurement name for the result.
			measurement := cq.intoMeasurement()
			if measurement == "" {
				measurement = row.Name
			}
			// Convert the result row to points.
			part, err := s.convertRowToPoints(measurement, row)
			if err != nil {
				log.Println(err)
				continue
			}

			if len(part) == 0 {
				continue
			}

			// If the points have any nil values, can't write.
			// This happens if the CQ is created and running before data is written to the measurement.
			for _, p := range part {
				fields := p.Fields()
				for _, v := range fields {
					if v == nil {
						return nil
					}
				}
			}
			points = append(points, part...)
		}
	}

	if len(points) == 0 {
		return nil
	}

	// Create a write request for the points.
	req := &cluster.WritePointsRequest{
		Database:         cq.intoDB(),
		RetentionPolicy:  cq.intoRP(),
		ConsistencyLevel: cluster.ConsistencyLevelAny,
		Points:           points,
	}

	// Write the request.
	if err := s.PointsWriter.WritePoints(req); err != nil {
		s.Logger.Println(err)
		return err
	}

	s.statMap.Add(statPointsWritten, int64(len(points)))
	if s.loggingEnabled {
		s.Logger.Printf("wrote %d point(s) to %s.%s", len(points), cq.intoDB(), cq.intoRP())
	}

	return nil
}