<commit_before>package alerting

import (
	"fmt"
	"regexp"
	"strconv"

	"github.com/grafana/grafana/pkg/components/simplejson"
	"github.com/grafana/grafana/pkg/services/alerting/transformers"

	m "github.com/grafana/grafana/pkg/models"
)

type AlertRule struct {
	Id              int64
	OrgId           int64
	DashboardId     int64
	PanelId         int64
	Frequency       int64
	Name            string
	Description     string
	State           string
	Warning         Level
	Critical        Level
	Query           AlertQuery
	Transform       string
	TransformParams simplejson.Json
	Transformer     transformers.Transformer
}

var (
	ValueFormatRegex = regexp.MustCompile("^\\d+")
	UnitFormatRegex  = regexp.MustCompile("\\w{1}$")
)

var unitMultiplier = map[string]int{
	"s": 1,
	"m": 60,
	"h": 3600,
}

func getTimeDurationStringToSeconds(str string) int64 {
	multiplier := 1

	value, _ := strconv.Atoi(ValueFormatRegex.FindAllString(str, 1)[0])
	unit := UnitFormatRegex.FindAllString(str, 1)[0]

	if val, ok := unitMultiplier[unit]; ok {
		multiplier = val
	}

	return int64(value * multiplier)
}

func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) {
	model := &AlertRule{}
	model.Id = ruleDef.Id
	model.OrgId = ruleDef.OrgId
	model.Name = ruleDef.Name
	model.Description = ruleDef.Description
	model.State = ruleDef.State

	critical := ruleDef.Settings.Get("critical")
	model.Critical = Level{
		Operator: critical.Get("op").MustString(),
		Value:    critical.Get("value").MustFloat64(),
	}

	warning := ruleDef.Settings.Get("warn")
	model.Warning = Level{
		Operator: warning.Get("op").MustString(),
		Value:    warning.Get("value").MustFloat64(),
	}

	model.Frequency = getTimeDurationStringToSeconds(ruleDef.Settings.Get("frequency").MustString())
	model.Transform = ruleDef.Settings.Get("transform").Get("type").MustString()
	model.TransformParams = *ruleDef.Settings.Get("transform")

	if model.Transform == "aggregation" {
		method := ruleDef.Settings.Get("transform").Get("method").MustString()
		model.Transformer = transformers.NewAggregationTransformer(method)
	}

	query := ruleDef.Settings.Get("query")
	model.Query = AlertQuery{
		Query:        query.Get("query").MustString(),
		DatasourceId: query.Get("datasourceId").MustInt64(),
		From:         query.Get("from").MustString(),
		To:           query.Get("to").MustString(),
		Aggregator:   query.Get("agg").MustString(),
	}

	if model.Query.Query == "" {
		return nil, fmt.Errorf("missing query.query")
	}

	if model.Query.DatasourceId == 0 {
		return nil, fmt.Errorf("missing query.datasourceId")
	}

	return model, nil
}
<commit_msg>fix(alerting): use alert frequency<commit_after>package alerting

import (
	"fmt"
	"regexp"
	"strconv"

	"github.com/grafana/grafana/pkg/components/simplejson"
	"github.com/grafana/grafana/pkg/services/alerting/transformers"

	m "github.com/grafana/grafana/pkg/models"
)

type AlertRule struct {
	Id              int64
	OrgId           int64
	DashboardId     int64
	PanelId         int64
	Frequency       int64
	Name            string
	Description     string
	State           string
	Warning         Level
	Critical        Level
	Query           AlertQuery
	Transform       string
	TransformParams simplejson.Json
	Transformer     transformers.Transformer
}

var (
	ValueFormatRegex = regexp.MustCompile("^\\d+")
	UnitFormatRegex  = regexp.MustCompile("\\w{1}$")
)

var unitMultiplier = map[string]int{
	"s": 1,
	"m": 60,
	"h": 3600,
}

func getTimeDurationStringToSeconds(str string) int64 {
	multiplier := 1

	value, _ := strconv.Atoi(ValueFormatRegex.FindAllString(str, 1)[0])
	unit := UnitFormatRegex.FindAllString(str, 1)[0]

	if val, ok := unitMultiplier[unit]; ok {
		multiplier = val
	}

	return int64(value * multiplier)
}

func NewAlertRuleFromDBModel(ruleDef *m.Alert) (*AlertRule, error) {
	model := &AlertRule{}
	model.Id = ruleDef.Id
	model.OrgId = ruleDef.OrgId
	model.Name = ruleDef.Name
	model.Description = ruleDef.Description
	model.State = ruleDef.State

	critical := ruleDef.Settings.Get("critical")
	model.Critical = Level{
		Operator: critical.Get("op").MustString(),
		Value:    critical.Get("value").MustFloat64(),
	}

	warning := ruleDef.Settings.Get("warn")
	model.Warning = Level{
		Operator: warning.Get("op").MustString(),
		Value:    warning.Get("value").MustFloat64(),
	}

	model.Transform = ruleDef.Settings.Get("transform").Get("type").MustString()
	model.TransformParams = *ruleDef.Settings.Get("transform")

	if model.Transform == "aggregation" {
		method := ruleDef.Settings.Get("transform").Get("method").MustString()
		model.Transformer = transformers.NewAggregationTransformer(method)
	}

	query := ruleDef.Settings.Get("query")
	model.Query = AlertQuery{
		Query:        query.Get("query").MustString(),
		DatasourceId: query.Get("datasourceId").MustInt64(),
		From:         query.Get("from").MustString(),
		To:           query.Get("to").MustString(),
		Aggregator:   query.Get("agg").MustString(),
	}

	if model.Query.Query == "" {
		return nil, fmt.Errorf("missing query.query")
	}

	if model.Query.DatasourceId == 0 {
		return nil, fmt.Errorf("missing query.datasourceId")
	}

	return model, nil
}

{'fraction_non_alphanumeric': 0.14061592097617664, 'fraction_numerical': 0.013170637226418749, 'mean_word_length': 3.8171641791044775, 'pattern_counts': {'":': 6, '<': 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': '5610905', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1706, 'n_words': 307}