<commit_before>package model

import (
	"fmt"
	"strings"

	"github.com/Cepave/open-falcon-backend/common/utils"
)

type Strategy struct {
	Id         int               `json:"id"`
	Metric     string            `json:"metric"`
	Tags       map[string]string `json:"tags"`
	Func       string            `json:"func"`       // e.g. max(#3) all(#3)
	Operator   string            `json:"operator"`   // e.g. < !=
	RightValue float64           `json:"rightValue"` // critical value
	MaxStep    int               `json:"maxStep"`
	Priority   int               `json:"priority"`
	Note       string            `json:"note"`
	Tpl        *Template         `json:"tpl"`
}

func (this *Strategy) String() string {
	return fmt.Sprintf(
		"<Id:%d, Metric:%s, Tags:%v, %s%s%s MaxStep:%d, P%d, %s, %v>",
		this.Id,
		this.Metric,
		this.Tags,
		this.Func,
		this.Operator,
		utils.ReadableFloat(this.RightValue),
		this.MaxStep,
		this.Priority,
		this.Note,
		this.Tpl,
	)
}

type HostStrategy struct {
	Hostname   string     `json:"hostname"`
	Strategies []Strategy `json:"strategies"`
}

func (this *HostStrategy) String() string {
	return fmt.Sprintf(
		"<Hostname:%v, Strategies:%v>",
		this.Hostname,
		this.Strategies,
	)
}

type StrategiesResponse struct {
	HostStrategies []*HostStrategy `json:"hostStrategies"`
}

type NewStrategy struct {
	ID         int    `json:"id"`
	Metric     string `json:"metric"`
	Tags       map[string]string
	TagsStr    string       `json:"tags"`
	Func       string       `json:"func"`               // e.g. max(#3) all(#3)
	Operator   string       `json:"operator"`           // e.g. < !=
	RightValue float64      `json:"right_value,string"` // critical value
	MaxStep    int          `json:"max_step"`
	Priority   int          `json:"priority"`
	Note       string       `json:"note"`
	Tpl        *NewTemplate `json:"tpl"`
}

func (s *NewStrategy) AfterLoad() {
	if s.Tags == nil {
		s.Tags = make(map[string]string)
	}
	kvStrs := strings.Split(s.TagsStr, ",")
	for _, kv := range kvStrs {
		kvArr := strings.Split(kv, "=")
		s.Tags[kvArr[0]] = kvArr[1]
	}
}

func (this *NewStrategy) String() string {
	return fmt.Sprintf(
		"<ID:%d, Metric:%s, Tags:%v, %s%s%s MaxStep:%d, P%d, %s, %v>",
		this.ID,
		this.Metric,
		this.Tags,
		this.Func,
		this.Operator,
		utils.ReadableFloat(this.RightValue),
		this.MaxStep,
		this.Priority,
		this.Note,
		this.Tpl,
	)
}

type NewHostStrategy struct {
	Hostname   string         `json:"hostname"`
	Strategies []*NewStrategy `json:"strategies"`
}
<commit_msg>[OWL-1883][common] Fixed: Panic when no tags set<commit_after>package model

import (
	"fmt"
	"strings"

	"github.com/Cepave/open-falcon-backend/common/utils"
)

type Strategy struct {
	Id         int               `json:"id"`
	Metric     string            `json:"metric"`
	Tags       map[string]string `json:"tags"`
	Func       string            `json:"func"`       // e.g. max(#3) all(#3)
	Operator   string            `json:"operator"`   // e.g. < !=
	RightValue float64           `json:"rightValue"` // critical value
	MaxStep    int               `json:"maxStep"`
	Priority   int               `json:"priority"`
	Note       string            `json:"note"`
	Tpl        *Template         `json:"tpl"`
}

func (this *Strategy) String() string {
	return fmt.Sprintf(
		"<Id:%d, Metric:%s, Tags:%v, %s%s%s MaxStep:%d, P%d, %s, %v>",
		this.Id,
		this.Metric,
		this.Tags,
		this.Func,
		this.Operator,
		utils.ReadableFloat(this.RightValue),
		this.MaxStep,
		this.Priority,
		this.Note,
		this.Tpl,
	)
}

type HostStrategy struct {
	Hostname   string     `json:"hostname"`
	Strategies []Strategy `json:"strategies"`
}

func (this *HostStrategy) String() string {
	return fmt.Sprintf(
		"<Hostname:%v, Strategies:%v>",
		this.Hostname,
		this.Strategies,
	)
}

type StrategiesResponse struct {
	HostStrategies []*HostStrategy `json:"hostStrategies"`
}

type NewStrategy struct {
	ID         int    `json:"id"`
	Metric     string `json:"metric"`
	Tags       map[string]string
	TagsStr    string       `json:"tags"`
	Func       string       `json:"func"`               // e.g. max(#3) all(#3)
	Operator   string       `json:"operator"`           // e.g. < !=
	RightValue float64      `json:"right_value,string"` // critical value
	MaxStep    int          `json:"max_step"`
	Priority   int          `json:"priority"`
	Note       string       `json:"note"`
	Tpl        *NewTemplate `json:"tpl"`
}

func (s *NewStrategy) AfterLoad() {
	if s.Tags == nil {
		s.Tags = make(map[string]string)
	}
	if s.TagsStr == "" {
		return
	}
	kvStrs := strings.Split(s.TagsStr, ",")
	for _, kv := range kvStrs {
		kvArr := strings.Split(kv, "=")
		if len(kvArr) != 2 {
			continue
		}
		s.Tags[kvArr[0]] = kvArr[1]
	}
}

func (this *NewStrategy) String() string {
	return fmt.Sprintf(
		"<ID:%d, Metric:%s, Tags:%v, %s%s%s MaxStep:%d, P%d, %s, %v>",
		this.ID,
		this.Metric,
		this.Tags,
		this.Func,
		this.Operator,
		utils.ReadableFloat(this.RightValue),
		this.MaxStep,
		this.Priority,
		this.Note,
		this.Tpl,
	)
}

type NewHostStrategy struct {
	Hostname   string         `json:"hostname"`
	Strategies []*NewStrategy `json:"strategies"`
}

{'fraction_non_alphanumeric': 0.1575981344733774, 'fraction_numerical': 0.004858142246404975, 'mean_word_length': 2.6093969144460027, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '3915346', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1830, 'n_words': 393}