Example #1
0
func (c *QueryCondition) Eval(context *alerting.EvalContext) {
	timerange := tsdb.NewTimerange(c.Query.From, c.Query.To)
	seriesList, err := c.executeQuery(context, timerange)
	if err != nil {
		context.Error = err
		return
	}

	emptySerieCount := 0
	for _, series := range seriesList {
		reducedValue := c.Reducer.Reduce(series)
		evalMatch := c.Evaluator.Eval(reducedValue)

		if reducedValue == nil {
			emptySerieCount++
			continue
		}

		if context.IsTestRun {
			context.Logs = append(context.Logs, &alerting.ResultLogEntry{
				Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, *reducedValue),
			})
		}

		if evalMatch {
			context.EvalMatches = append(context.EvalMatches, &alerting.EvalMatch{
				Metric: series.Name,
				Value:  *reducedValue,
			})
		}
	}

	context.NoDataFound = emptySerieCount == len(seriesList)
	context.Firing = len(context.EvalMatches) > 0
}