<commit_before>package alerting

import (
	"time"

	"github.com/grafana/grafana/pkg/bus"
	"github.com/grafana/grafana/pkg/log"
	"github.com/grafana/grafana/pkg/metrics"
	m "github.com/grafana/grafana/pkg/models"
	"github.com/grafana/grafana/pkg/services/annotations"
)

type ResultHandler interface {
	Handle(ctx *EvalContext)
}

type DefaultResultHandler struct {
	notifier Notifier
	log      log.Logger
}

func NewResultHandler() *DefaultResultHandler {
	return &DefaultResultHandler{
		log:      log.New("alerting.resultHandler"),
		notifier: NewRootNotifier(),
	}
}

func (handler *DefaultResultHandler) Handle(ctx *EvalContext) {
	oldState := ctx.Rule.State

	exeuctionError := ""
	if ctx.Error != nil {
		handler.log.Error("Alert Rule Result Error", "ruleId", ctx.Rule.Id, "error", ctx.Error)
		ctx.Rule.State = m.AlertStateExeuctionError
		exeuctionError = ctx.Error.Error()
	} else if ctx.Firing {
		ctx.Rule.State = m.AlertStateType(ctx.Rule.Severity)
	} else {
		ctx.Rule.State = m.AlertStateOK
	}

	countSeverity(ctx.Rule.Severity)

	if ctx.Rule.State != oldState {
		handler.log.Info("New state change", "alertId", ctx.Rule.Id, "newState", ctx.Rule.State, "oldState", oldState)

		cmd := &m.SetAlertStateCommand{
			AlertId: ctx.Rule.Id,
			OrgId:   ctx.Rule.OrgId,
			State:   ctx.Rule.State,
			Error:   exeuctionError,
		}

		if err := bus.Dispatch(cmd); err != nil {
			handler.log.Error("Failed to save state", "error", err)
		}

		// save annotation
		item := annotations.Item{
			OrgId:     ctx.Rule.OrgId,
			Type:      annotations.AlertType,
			AlertId:   ctx.Rule.Id,
			Title:     ctx.Rule.Name,
			Text:      ctx.GetStateText(),
			NewState:  string(ctx.Rule.State),
			PrevState: string(oldState),
			Timestamp: time.Now(),
		}
<commit_msg>tech(alerting): empty string does not update database<commit_after>package alerting

import (
	"time"

	"github.com/grafana/grafana/pkg/bus"
	"github.com/grafana/grafana/pkg/log"
	"github.com/grafana/grafana/pkg/metrics"
	m "github.com/grafana/grafana/pkg/models"
	"github.com/grafana/grafana/pkg/services/annotations"
)

type ResultHandler interface {
	Handle(ctx *EvalContext)
}

type DefaultResultHandler struct {
	notifier Notifier
	log      log.Logger
}

func NewResultHandler() *DefaultResultHandler {
	return &DefaultResultHandler{
		log:      log.New("alerting.resultHandler"),
		notifier: NewRootNotifier(),
	}
}

func (handler *DefaultResultHandler) Handle(ctx *EvalContext) {
	oldState := ctx.Rule.State

	exeuctionError := " "
	if ctx.Error != nil {
		handler.log.Error("Alert Rule Result Error", "ruleId", ctx.Rule.Id, "error", ctx.Error)
		ctx.Rule.State = m.AlertStateExeuctionError
		exeuctionError = ctx.Error.Error()
	} else if ctx.Firing {
		ctx.Rule.State = m.AlertStateType(ctx.Rule.Severity)
	} else {
		ctx.Rule.State = m.AlertStateOK
	}

	countSeverity(ctx.Rule.Severity)
	if ctx.Rule.State != oldState {
		handler.log.Info("New state change", "alertId", ctx.Rule.Id, "newState", ctx.Rule.State, "oldState", oldState)

		cmd := &m.SetAlertStateCommand{
			AlertId: ctx.Rule.Id,
			OrgId:   ctx.Rule.OrgId,
			State:   ctx.Rule.State,
			Error:   exeuctionError,
		}

		if err := bus.Dispatch(cmd); err != nil {
			handler.log.Error("Failed to save state", "error", err)
		}

		// save annotation
		item := annotations.Item{
			OrgId:     ctx.Rule.OrgId,
			Type:      annotations.AlertType,
			AlertId:   ctx.Rule.Id,
			Title:     ctx.Rule.Name,
			Text:      ctx.GetStateText(),
			NewState:  string(ctx.Rule.State),
			PrevState: string(oldState),
			Timestamp: time.Now(),
		}

{'fraction_non_alphanumeric': 0.1392156862745098, 'fraction_numerical': 0.0, 'mean_word_length': 3.742363877822045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '397680', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1277, 'n_words': 238}