Example #1
0
func functionRenderer(queryContext *tsdb.QueryContext, part *QueryPart, innerExpr string) string {
	for i, v := range part.Params {
		if v == "$interval" {
			part.Params[i] = tsdb.CalculateInterval(queryContext.TimeRange)
		}
	}

	if innerExpr != "" {
		part.Params = append([]string{innerExpr}, part.Params...)
	}

	params := strings.Join(part.Params, ", ")

	return fmt.Sprintf("%s(%s)", part.Type, params)
}
Example #2
0
func getDefinedInterval(query *Query, queryContext *tsdb.QueryContext) string {
	setInterval := strings.Replace(strings.Replace(query.Interval, "<", "", 1), ">", "", 1)
	defaultInterval := tsdb.CalculateInterval(queryContext.TimeRange)

	if strings.Contains(query.Interval, ">") {
		parsedDefaultInterval, err := time.ParseDuration(defaultInterval)
		parsedSetInterval, err2 := time.ParseDuration(setInterval)

		if err == nil && err2 == nil && parsedDefaultInterval > parsedSetInterval {
			return defaultInterval
		}
	}

	return setInterval
}
Example #3
0
func (query *Query) Build(queryContext *tsdb.QueryContext) (string, error) {
	if query.UseRawQuery && query.RawQuery != "" {
		q := query.RawQuery

		q = strings.Replace(q, "$timeFilter", query.renderTimeFilter(queryContext), 1)
		q = strings.Replace(q, "$interval", tsdb.CalculateInterval(queryContext.TimeRange), 1)

		return q, nil
	}

	res := query.renderSelectors(queryContext)
	res += query.renderMeasurement()
	res += query.renderWhereClause()
	res += query.renderTimeFilter(queryContext)
	res += query.renderGroupBy(queryContext)

	return res, nil
}
Example #4
0
func getDefinedInterval(query *Query, queryContext *tsdb.QueryContext) (*tsdb.Interval, error) {
	defaultInterval := tsdb.CalculateInterval(queryContext.TimeRange)

	if query.Interval == "" {
		return &defaultInterval, nil
	}

	setInterval := strings.Replace(strings.Replace(query.Interval, "<", "", 1), ">", "", 1)
	parsedSetInterval, err := time.ParseDuration(setInterval)

	if err != nil {
		return nil, err
	}

	if strings.Contains(query.Interval, ">") {
		if defaultInterval.Value > parsedSetInterval {
			return &defaultInterval, nil
		}
	}

	return &tsdb.Interval{Value: parsedSetInterval, Text: setInterval}, nil
}