80 Star 451 Fork 139

Ulric Qin/Nightingale

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
alert_cur_event.go 23.85 KB
一键复制 编辑 原始数据 按行查看 历史
ning 提交于 2025-03-04 19:01 +08:00 . refactor notify test api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
package models
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"text/template"
"time"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/ccfos/nightingale/v6/pkg/poster"
"github.com/ccfos/nightingale/v6/pkg/tplx"
"github.com/ccfos/nightingale/v6/pkg/unit"
"github.com/toolkits/pkg/logger"
)
type AlertCurEvent struct {
Id int64 `json:"id" gorm:"primaryKey"`
Cate string `json:"cate"`
Cluster string `json:"cluster"`
DatasourceId int64 `json:"datasource_id"`
GroupId int64 `json:"group_id"` // busi group id
GroupName string `json:"group_name"` // busi group name
Hash string `json:"hash"` // rule_id + vector_key
RuleId int64 `json:"rule_id"`
RuleName string `json:"rule_name"`
RuleNote string `json:"rule_note"`
RuleProd string `json:"rule_prod"`
RuleAlgo string `json:"rule_algo"`
Severity int `json:"severity"`
PromForDuration int `json:"prom_for_duration"`
PromQl string `json:"prom_ql"`
RuleConfig string `json:"-" gorm:"rule_config"` // rule config
RuleConfigJson interface{} `json:"rule_config" gorm:"-"` // rule config for fe
PromEvalInterval int `json:"prom_eval_interval"`
Callbacks string `json:"-"` // for db
CallbacksJSON []string `json:"callbacks" gorm:"-"` // for fe
RunbookUrl string `json:"runbook_url"`
NotifyRecovered int `json:"notify_recovered"`
NotifyChannels string `json:"-"` // for db
NotifyChannelsJSON []string `json:"notify_channels" gorm:"-"` // for fe
NotifyGroups string `json:"-"` // for db
NotifyGroupsJSON []string `json:"notify_groups" gorm:"-"` // for fe
NotifyGroupsObj []*UserGroup `json:"notify_groups_obj" gorm:"-"` // for fe
TargetIdent string `json:"target_ident"`
TargetNote string `json:"target_note"`
TriggerTime int64 `json:"trigger_time"`
TriggerValue string `json:"trigger_value"`
TriggerValues string `json:"trigger_values" gorm:"-"`
TriggerValuesJson EventTriggerValues `json:"trigger_values_json" gorm:"-"`
Tags string `json:"-"` // for db
TagsJSON []string `json:"tags" gorm:"-"` // for fe
TagsMap map[string]string `json:"tags_map" gorm:"-"` // for internal usage
OriginalTags string `json:"-"` // for db
OriginalTagsJSON []string `json:"original_tags" gorm:"-"` // for fe
Annotations string `json:"-"` //
AnnotationsJSON map[string]string `json:"annotations" gorm:"-"` // for fe
IsRecovered bool `json:"is_recovered" gorm:"-"` // for notify.py
NotifyUsersObj []*User `json:"notify_users_obj" gorm:"-"` // for notify.py
LastEvalTime int64 `json:"last_eval_time" gorm:"-"` // for notify.py 上次计算的时间
LastSentTime int64 `json:"last_sent_time" gorm:"-"` // 上次发送时间
NotifyCurNumber int `json:"notify_cur_number"` // notify: current number
FirstTriggerTime int64 `json:"first_trigger_time"` // 连续告警的首次告警时间
ExtraConfig interface{} `json:"extra_config" gorm:"-"`
Status int `json:"status" gorm:"-"`
Claimant string `json:"claimant" gorm:"-"`
SubRuleId int64 `json:"sub_rule_id" gorm:"-"`
ExtraInfo []string `json:"extra_info" gorm:"-"`
Target *Target `json:"target" gorm:"-"`
RecoverConfig RecoverConfig `json:"recover_config" gorm:"-"`
RuleHash string `json:"rule_hash" gorm:"-"`
ExtraInfoMap []map[string]string `json:"extra_info_map" gorm:"-"`
NotifyRuleIDs []int64 `json:"notify_rule_ids" gorm:"-"`
}
func (e *AlertCurEvent) SetTagsMap() {
e.TagsMap = make(map[string]string)
for i := 0; i < len(e.TagsJSON); i++ {
pair := strings.TrimSpace(e.TagsJSON[i])
if pair == "" {
continue
}
arr := strings.SplitN(pair, "=", 2)
if len(arr) != 2 {
continue
}
e.TagsMap[arr[0]] = arr[1]
}
}
func (e *AlertCurEvent) JsonTagsAndValue() map[string]string {
v := reflect.ValueOf(e).Elem()
t := v.Type()
tags := make(map[string]string)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// 获取 json tag
tag := field.Tag.Get("json")
if tag == "" {
continue
}
// 处理类似 `json:",omitempty"` 或 `json:"-"` 的特殊情况
tagParts := strings.Split(tag, ",")
if tagParts[0] == "-" {
continue
}
// 获取字段值并转换为字符串
fieldValue := v.Field(i).Interface()
var strValue string
switch v := fieldValue.(type) {
case string:
strValue = v
case int, int8, int16, int32, int64:
strValue = fmt.Sprintf("%d", v)
case float32, float64:
strValue = fmt.Sprintf("%f", v)
case bool:
strValue = fmt.Sprintf("%v", v)
case []string:
b, _ := json.Marshal(v)
strValue = string(b)
case map[string]string:
b, _ := json.Marshal(v)
strValue = string(b)
default:
// 对于其他类型,尝试 JSON 序列化
if b, err := json.Marshal(v); err == nil {
strValue = string(b)
} else {
strValue = fmt.Sprintf("%v", v)
}
}
// 如果没有指定 tag 名称,使用字段名作为 key
if tagParts[0] == "" {
tags[field.Name] = strValue
} else {
tags[tagParts[0]] = strValue
}
}
return tags
}
type EventTriggerValues struct {
ValuesWithUnit map[string]unit.FormattedValue `json:"values_with_unit"`
}
func (e *AlertCurEvent) TableName() string {
return "alert_cur_event"
}
func (e *AlertCurEvent) Add(ctx *ctx.Context) error {
return Insert(ctx, e)
}
type AggrRule struct {
Type string
Value string
}
func (e *AlertCurEvent) ParseRule(field string) error {
f := e.GetField(field)
f = strings.TrimSpace(f)
if f == "" {
return nil
}
if field == "annotations" {
err := json.Unmarshal([]byte(e.Annotations), &e.AnnotationsJSON)
if err != nil {
logger.Warningf("ruleid:%d failed to parse annotations: %v", e.RuleId, err)
e.AnnotationsJSON = make(map[string]string)
e.AnnotationsJSON["error"] = e.Annotations
}
for k, v := range e.AnnotationsJSON {
f = v
var defs = []string{
"{{$labels := .TagsMap}}",
"{{$value := .TriggerValue}}",
}
templateFuncMapCopy := tplx.NewTemplateFuncMap()
templateFuncMapCopy["query"] = func(promql string, param ...int64) []AnomalyPoint {
datasourceId := e.DatasourceId
if len(param) > 0 {
datasourceId = param[0]
}
value := tplx.Query(datasourceId, promql)
return ConvertAnomalyPoints(value)
}
text := strings.Join(append(defs, f), "")
t, err := template.New(fmt.Sprint(e.RuleId)).Funcs(templateFuncMapCopy).Parse(text)
if err != nil {
e.AnnotationsJSON[k] = fmt.Sprintf("failed to parse annotations: %v", err)
continue
}
var body bytes.Buffer
err = t.Execute(&body, e)
if err != nil {
e.AnnotationsJSON[k] = fmt.Sprintf("failed to parse annotations: %v", err)
continue
}
e.AnnotationsJSON[k] = body.String()
}
b, err := json.Marshal(e.AnnotationsJSON)
if err != nil {
e.AnnotationsJSON = make(map[string]string)
e.AnnotationsJSON["error"] = fmt.Sprintf("failed to parse annotations: %v", err)
} else {
e.Annotations = string(b)
}
return nil
}
var defs = []string{
"{{$labels := .TagsMap}}",
"{{$value := .TriggerValue}}",
"{{$annotations := .AnnotationsJSON}}",
}
text := strings.Join(append(defs, f), "")
t, err := template.New(fmt.Sprint(e.RuleId)).Funcs(template.FuncMap(tplx.TemplateFuncMap)).Parse(text)
if err != nil {
return err
}
var body bytes.Buffer
err = t.Execute(&body, e)
if err != nil {
return err
}
if field == "rule_name" {
e.RuleName = body.String()
}
if field == "rule_note" {
e.RuleNote = body.String()
}
return nil
}
func (e *AlertCurEvent) ParseURL(url string) (string, error) {
f := strings.TrimSpace(url)
if f == "" {
return url, nil
}
var defs = []string{
"{{$labels := .TagsMap}}",
"{{$value := .TriggerValue}}",
"{{$annotations := .AnnotationsJSON}}",
}
text := strings.Join(append(defs, f), "")
t, err := template.New("callbackUrl" + fmt.Sprint(e.RuleId)).Funcs(template.FuncMap(tplx.TemplateFuncMap)).Parse(text)
if err != nil {
return url, nil
}
var body bytes.Buffer
err = t.Execute(&body, e)
if err != nil {
return url, nil
}
return body.String(), nil
}
func (e *AlertCurEvent) GenCardTitle(rules []*AggrRule) string {
arr := make([]string, len(rules))
for i := 0; i < len(rules); i++ {
rule := rules[i]
if rule.Type == "field" {
arr[i] = e.GetField(rule.Value)
}
if rule.Type == "tagkey" {
arr[i] = e.GetTagValue(rule.Value)
}
if len(arr[i]) == 0 {
arr[i] = "Null"
}
}
return strings.Join(arr, "::")
}
func (e *AlertCurEvent) GetTagValue(tagkey string) string {
for _, tag := range e.TagsJSON {
i := strings.Index(tag, tagkey+"=")
if i >= 0 {
return tag[len(tagkey+"="):]
}
}
return ""
}
func (e *AlertCurEvent) GetField(field string) string {
switch field {
case "cluster":
return e.Cluster
case "group_id":
return fmt.Sprint(e.GroupId)
case "group_name":
return e.GroupName
case "rule_id":
return fmt.Sprint(e.RuleId)
case "rule_name":
return e.RuleName
case "rule_note":
return e.RuleNote
case "severity":
return fmt.Sprint(e.Severity)
case "runbook_url":
return e.RunbookUrl
case "target_ident":
return e.TargetIdent
case "target_note":
return e.TargetNote
case "callbacks":
return e.Callbacks
case "annotations":
return e.Annotations
default:
return ""
}
}
func (e *AlertCurEvent) ToHis(ctx *ctx.Context) *AlertHisEvent {
isRecovered := 0
var recoverTime int64 = 0
if e.IsRecovered {
isRecovered = 1
recoverTime = e.LastEvalTime
}
return &AlertHisEvent{
IsRecovered: isRecovered,
Cate: e.Cate,
Cluster: e.Cluster,
DatasourceId: e.DatasourceId,
GroupId: e.GroupId,
GroupName: e.GroupName,
Hash: e.Hash,
RuleId: e.RuleId,
RuleName: e.RuleName,
RuleProd: e.RuleProd,
RuleAlgo: e.RuleAlgo,
RuleNote: e.RuleNote,
Severity: e.Severity,
PromForDuration: e.PromForDuration,
PromQl: e.PromQl,
PromEvalInterval: e.PromEvalInterval,
RuleConfig: e.RuleConfig,
RuleConfigJson: e.RuleConfigJson,
Callbacks: e.Callbacks,
RunbookUrl: e.RunbookUrl,
NotifyRecovered: e.NotifyRecovered,
NotifyChannels: e.NotifyChannels,
NotifyGroups: e.NotifyGroups,
Annotations: e.Annotations,
AnnotationsJSON: e.AnnotationsJSON,
TargetIdent: e.TargetIdent,
TargetNote: e.TargetNote,
TriggerTime: e.TriggerTime,
TriggerValue: e.TriggerValue,
Tags: e.Tags,
OriginalTags: e.OriginalTags,
RecoverTime: recoverTime,
LastEvalTime: e.LastEvalTime,
NotifyCurNumber: e.NotifyCurNumber,
FirstTriggerTime: e.FirstTriggerTime,
}
}
func (e *AlertCurEvent) DB2FE() error {
e.NotifyChannelsJSON = strings.Fields(e.NotifyChannels)
e.NotifyGroupsJSON = strings.Fields(e.NotifyGroups)
e.CallbacksJSON = strings.Fields(e.Callbacks)
e.TagsJSON = strings.Split(e.Tags, ",,")
e.OriginalTagsJSON = strings.Split(e.OriginalTags, ",,")
if err := json.Unmarshal([]byte(e.Annotations), &e.AnnotationsJSON); err != nil {
return err
}
if err := json.Unmarshal([]byte(e.RuleConfig), &e.RuleConfigJson); err != nil {
return err
}
return nil
}
func (e *AlertCurEvent) FE2DB() {
e.NotifyChannels = strings.Join(e.NotifyChannelsJSON, " ")
e.NotifyGroups = strings.Join(e.NotifyGroupsJSON, " ")
e.Callbacks = strings.Join(e.CallbacksJSON, " ")
e.Tags = strings.Join(e.TagsJSON, ",,")
e.OriginalTags = strings.Join(e.OriginalTagsJSON, ",,")
b, _ := json.Marshal(e.AnnotationsJSON)
e.Annotations = string(b)
b, _ = json.Marshal(e.RuleConfigJson)
e.RuleConfig = string(b)
}
func (e *AlertCurEvent) DB2Mem() {
e.IsRecovered = false
e.NotifyGroupsJSON = strings.Fields(e.NotifyGroups)
e.CallbacksJSON = strings.Fields(e.Callbacks)
e.NotifyChannelsJSON = strings.Fields(e.NotifyChannels)
e.TagsJSON = strings.Split(e.Tags, ",,")
e.TagsMap = make(map[string]string)
for i := 0; i < len(e.TagsJSON); i++ {
pair := strings.TrimSpace(e.TagsJSON[i])
if pair == "" {
continue
}
arr := strings.SplitN(pair, "=", 2)
if len(arr) != 2 {
continue
}
e.TagsMap[arr[0]] = arr[1]
}
// 解决之前数据库中 FirstTriggerTime 为 0 的情况
if e.FirstTriggerTime == 0 {
e.FirstTriggerTime = e.TriggerTime
}
}
func (e *AlertCurEvent) OverrideGlobalWebhook() bool {
var rc RuleConfig
if err := json.Unmarshal([]byte(e.RuleConfig), &rc); err != nil {
logger.Warningf("failed to unmarshal rule config: %v", err)
return false
}
return rc.OverrideGlobalWebhook
}
func FillRuleConfigTplName(ctx *ctx.Context, ruleConfig string) (interface{}, bool) {
var config RuleConfig
err := json.Unmarshal([]byte(ruleConfig), &config)
if err != nil {
logger.Warningf("failed to unmarshal rule config: %v", err)
return nil, false
}
if len(config.TaskTpls) == 0 {
return nil, false
}
for i := 0; i < len(config.TaskTpls); i++ {
tpl, err := TaskTplGetById(ctx, config.TaskTpls[i].TplId)
if err != nil {
logger.Warningf("failed to get task tpl by id:%d, %v", config.TaskTpls[i].TplId, err)
return nil, false
}
if tpl == nil {
logger.Warningf("task tpl not found by id:%d", config.TaskTpls[i].TplId)
return nil, false
}
config.TaskTpls[i].TplName = tpl.Title
}
return config, true
}
// for webui
func (e *AlertCurEvent) FillNotifyGroups(ctx *ctx.Context, cache map[int64]*UserGroup) error {
// some user-group already deleted ?
count := len(e.NotifyGroupsJSON)
if count == 0 {
e.NotifyGroupsObj = []*UserGroup{}
return nil
}
for i := range e.NotifyGroupsJSON {
id, err := strconv.ParseInt(e.NotifyGroupsJSON[i], 10, 64)
if err != nil {
continue
}
ug, has := cache[id]
if has {
e.NotifyGroupsObj = append(e.NotifyGroupsObj, ug)
continue
}
ug, err = UserGroupGetById(ctx, id)
if err != nil {
return err
}
if ug != nil {
e.NotifyGroupsObj = append(e.NotifyGroupsObj, ug)
cache[id] = ug
}
}
return nil
}
func AlertCurEventTotal(ctx *ctx.Context, prods []string, bgids []int64, stime, etime int64,
severity int, dsIds []int64, cates []string, ruleId int64, query string) (int64, error) {
session := DB(ctx).Model(&AlertCurEvent{})
if stime != 0 && etime != 0 {
session = session.Where("trigger_time between ? and ?", stime, etime)
}
if len(prods) != 0 {
session = session.Where("rule_prod in ?", prods)
}
if len(bgids) > 0 {
session = session.Where("group_id in ?", bgids)
}
if severity >= 0 {
session = session.Where("severity = ?", severity)
}
if len(dsIds) > 0 {
session = session.Where("datasource_id in ?", dsIds)
}
if len(cates) > 0 {
session = session.Where("cate in ?", cates)
}
if ruleId > 0 {
session = session.Where("rule_id = ?", ruleId)
}
if query != "" {
arr := strings.Fields(query)
for i := 0; i < len(arr); i++ {
qarg := "%" + arr[i] + "%"
session = session.Where("rule_name like ? or tags like ?", qarg, qarg)
}
}
return Count(session)
}
func AlertCurEventsGet(ctx *ctx.Context, prods []string, bgids []int64, stime, etime int64,
severity int, dsIds []int64, cates []string, ruleId int64, query string, limit, offset int) (
[]AlertCurEvent, error) {
session := DB(ctx).Model(&AlertCurEvent{})
if stime != 0 && etime != 0 {
session = session.Where("trigger_time between ? and ?", stime, etime)
}
if len(prods) != 0 {
session = session.Where("rule_prod in ?", prods)
}
if len(bgids) > 0 {
session = session.Where("group_id in ?", bgids)
}
if severity >= 0 {
session = session.Where("severity = ?", severity)
}
if len(dsIds) > 0 {
session = session.Where("datasource_id in ?", dsIds)
}
if len(cates) > 0 {
session = session.Where("cate in ?", cates)
}
if ruleId > 0 {
session = session.Where("rule_id = ?", ruleId)
}
if query != "" {
arr := strings.Fields(query)
for i := 0; i < len(arr); i++ {
qarg := "%" + arr[i] + "%"
session = session.Where("rule_name like ? or tags like ?", qarg, qarg)
}
}
var lst []AlertCurEvent
err := session.Order("trigger_time desc").Limit(limit).Offset(offset).Find(&lst).Error
if err == nil {
for i := 0; i < len(lst); i++ {
lst[i].DB2FE()
}
}
return lst, err
}
func AlertCurEventCountByRuleId(ctx *ctx.Context, rids []int64, stime, etime int64) map[int64]int64 {
type Row struct {
RuleId int64
Cnt int64
}
var rows []Row
err := DB(ctx).Model(&AlertCurEvent{}).Select("rule_id, count(*) as cnt").
Where("trigger_time between ? and ?", stime, etime).Group("rule_id").Find(&rows).Error
if err != nil {
logger.Errorf("Failed to count group by rule_id: %v", err)
return nil
}
curEventTotalByRid := make(map[int64]int64, len(rids))
for _, r := range rows {
curEventTotalByRid[r.RuleId] = r.Cnt
}
return curEventTotalByRid
}
func AlertCurEventDel(ctx *ctx.Context, ids []int64) error {
if len(ids) == 0 {
return nil
}
return DB(ctx).Where("id in ?", ids).Delete(&AlertCurEvent{}).Error
}
func AlertCurEventDelByHash(ctx *ctx.Context, hash string) error {
if !ctx.IsCenter {
_, err := poster.GetByUrls[string](ctx, "/v1/n9e/alert-cur-events-del-by-hash?hash="+hash)
return err
}
return DB(ctx).Where("hash = ?", hash).Delete(&AlertCurEvent{}).Error
}
func AlertCurEventExists(ctx *ctx.Context, where string, args ...interface{}) (bool, error) {
return Exists(DB(ctx).Model(&AlertCurEvent{}).Where(where, args...))
}
func AlertCurEventGet(ctx *ctx.Context, where string, args ...interface{}) (*AlertCurEvent, error) {
var lst []*AlertCurEvent
err := DB(ctx).Where(where, args...).Find(&lst).Error
if err != nil {
return nil, err
}
if len(lst) == 0 {
return nil, nil
}
lst[0].DB2FE()
lst[0].FillNotifyGroups(ctx, make(map[int64]*UserGroup))
return lst[0], nil
}
func AlertCurEventGetById(ctx *ctx.Context, id int64) (*AlertCurEvent, error) {
return AlertCurEventGet(ctx, "id=?", id)
}
type AlertNumber struct {
GroupId int64
GroupCount int64
}
// for busi_group list page
func AlertNumbers(ctx *ctx.Context, bgids []int64) (map[int64]int64, error) {
ret := make(map[int64]int64)
if len(bgids) == 0 {
return ret, nil
}
var arr []AlertNumber
err := DB(ctx).Model(&AlertCurEvent{}).Select("group_id", "count(*) as group_count").Where("group_id in ?", bgids).Group("group_id").Find(&arr).Error
if err != nil {
return nil, err
}
for i := 0; i < len(arr); i++ {
ret[arr[i].GroupId] = arr[i].GroupCount
}
return ret, nil
}
func AlertCurEventGetByIds(ctx *ctx.Context, ids []int64) ([]*AlertCurEvent, error) {
var lst []*AlertCurEvent
if len(ids) == 0 {
return lst, nil
}
err := DB(ctx).Model(&AlertCurEvent{}).Where("id in ?", ids).Order("trigger_time desc").Find(&lst).Error
if err == nil {
for i := 0; i < len(lst); i++ {
lst[i].DB2FE()
}
}
return lst, err
}
func AlertCurEventGetByRuleIdAndDsId(ctx *ctx.Context, ruleId int64, datasourceId int64) ([]*AlertCurEvent, error) {
if !ctx.IsCenter {
lst, err := poster.GetByUrls[[]*AlertCurEvent](ctx, "/v1/n9e/alert-cur-events-get-by-rid?rid="+strconv.FormatInt(ruleId, 10)+"&dsid="+strconv.FormatInt(datasourceId, 10))
if err == nil {
for i := 0; i < len(lst); i++ {
lst[i].FE2DB()
}
}
return lst, err
}
var lst []*AlertCurEvent
err := DB(ctx).Where("rule_id=? and datasource_id = ?", ruleId, datasourceId).Find(&lst).Error
if err == nil {
for i := 0; i < len(lst); i++ {
lst[i].DB2FE()
}
}
return lst, err
}
func AlertCurEventGetMap(ctx *ctx.Context, cluster string) (map[int64]map[string]struct{}, error) {
session := DB(ctx).Model(&AlertCurEvent{})
if cluster != "" {
session = session.Where("datasource_id = ?", cluster)
}
var lst []*AlertCurEvent
err := session.Select("rule_id", "hash").Find(&lst).Error
if err != nil {
return nil, err
}
ret := make(map[int64]map[string]struct{})
for i := 0; i < len(lst); i++ {
rid := lst[i].RuleId
hash := lst[i].Hash
if _, has := ret[rid]; has {
ret[rid][hash] = struct{}{}
} else {
ret[rid] = make(map[string]struct{})
ret[rid][hash] = struct{}{}
}
}
return ret, nil
}
func (e *AlertCurEvent) UpdateFieldsMap(ctx *ctx.Context, fields map[string]interface{}) error {
return DB(ctx).Model(e).Updates(fields).Error
}
func AlertCurEventUpgradeToV6(ctx *ctx.Context, dsm map[string]Datasource) error {
var lst []*AlertCurEvent
err := DB(ctx).Where("trigger_time > ?", time.Now().Unix()-3600*24*30).Find(&lst).Error
if err != nil {
return err
}
for i := 0; i < len(lst); i++ {
ds, exists := dsm[lst[i].Cluster]
if !exists {
continue
}
lst[i].DatasourceId = ds.Id
ruleConfig := PromRuleConfig{
Queries: []PromQuery{
{
PromQl: lst[i].PromQl,
Severity: lst[i].Severity,
},
},
}
b, _ := json.Marshal(ruleConfig)
lst[i].RuleConfig = string(b)
if lst[i].RuleProd == "" {
lst[i].RuleProd = METRIC
}
if lst[i].Cate == "" {
lst[i].Cate = PROMETHEUS
}
err = lst[i].UpdateFieldsMap(ctx, map[string]interface{}{
"datasource_id": lst[i].DatasourceId,
"rule_config": lst[i].RuleConfig,
"rule_prod": lst[i].RuleProd,
"cate": lst[i].Cate,
})
if err != nil {
logger.Errorf("update alert rule:%d datasource ids failed, %v", lst[i].Id, err)
}
}
return nil
}
// AlertCurEventGetsFromAlertMute find current events from db.
func AlertCurEventGetsFromAlertMute(ctx *ctx.Context, alertMute *AlertMute) ([]*AlertCurEvent, error) {
var lst []*AlertCurEvent
tx := DB(ctx).Where("group_id = ?", alertMute.GroupId)
if len(alertMute.SeveritiesJson) != 0 {
tx = tx.Where("severity IN (?)", alertMute.SeveritiesJson)
}
if len(alertMute.DatasourceIdsJson) != 0 && !IsAllDatasource(alertMute.DatasourceIdsJson) {
tx = tx.Where("datasource_id IN (?)", alertMute.DatasourceIdsJson)
}
err := tx.Order("id desc").Find(&lst).Error
return lst, err
}
func AlertCurEventStatistics(ctx *ctx.Context, stime time.Time) map[string]interface{} {
stime24HoursAgoUnix := stime.Add(-24 * time.Hour).Unix()
//Beginning of today
stimeMidnightUnix := time.Date(stime.Year(), stime.Month(), stime.Day(), 0, 0, 0, 0, stime.Location()).Unix()
///Monday of the current week, starting at 00:00
daysToMonday := (int(stime.Weekday()) - 1 + 7) % 7 // (DayOfTheWeek - Monday(1) + DaysAWeek(7))/DaysAWeek(7)
stimeOneWeekAgoUnix := time.Date(stime.Year(), stime.Month(), stime.Day()-daysToMonday, 0, 0, 0, 0, stime.Location()).Unix()
var err error
res := make(map[string]interface{})
res["total"], err = Count(DB(ctx).Model(&AlertCurEvent{}))
if err != nil {
logger.Debugf("count alert current rule failed(total), %v", err)
}
res["total_24_ago"], err = Count(DB(ctx).Model(&AlertCurEvent{}).Where("trigger_time < ?", stime24HoursAgoUnix))
if err != nil {
logger.Debugf("count alert current rule failed(total_24ago), %v", err)
}
res["total_today"], err = Count(DB(ctx).Model(&AlertHisEvent{}).Where("trigger_time >= ? and is_recovered = ? ", stimeMidnightUnix, 0))
if err != nil {
logger.Debugf("count alert his rule failed(total_today), %v", err)
}
res["total_week"], err = Count(DB(ctx).Model(&AlertHisEvent{}).Where("trigger_time >= ? and is_recovered = ? ", stimeOneWeekAgoUnix, 0))
if err != nil {
logger.Debugf("count alert his rule failed(total_today), %v", err)
}
return res
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/cnperl/Nightingale.git
[email protected]:cnperl/Nightingale.git
cnperl
Nightingale
Nightingale
main

搜索帮助