Files
nightingale/models/notification_record.go

94 lines
2.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
import (
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/ccfos/nightingale/v6/pkg/strx"
"github.com/toolkits/pkg/logger"
)
const (
NotiStatusSuccess = iota + 1
NotiStatusFailure
)
type NotificationRecord struct {
Id int64 `json:"id" gorm:"primaryKey;type:bigint;autoIncrement"`
NotifyRuleID int64 `json:"notify_rule_id" gorm:"type:bigint;comment:notify rule id"`
EventId int64 `json:"event_id" gorm:"type:bigint;not null;index:idx_evt,priority:1;comment:event history id"`
SubId int64 `json:"sub_id" gorm:"type:bigint;comment:subscribed rule id"`
Channel string `json:"channel" gorm:"type:varchar(255);not null;comment:notification channel name"`
Status int `json:"status" gorm:"type:int;comment:notification status"` // 1-成功2-失败
Target string `json:"target" gorm:"type:varchar(1024);not null;comment:notification target"`
Details string `json:"details" gorm:"type:varchar(2048);default:'';comment:notification other info"`
CreatedAt int64 `json:"created_at" gorm:"type:bigint;not null;comment:create time"`
}
func NewNotificationRecord(event *AlertCurEvent, notifyRuleID int64, channel, target string) *NotificationRecord {
return &NotificationRecord{
NotifyRuleID: notifyRuleID,
EventId: event.Id,
SubId: event.SubRuleId,
Channel: channel,
Status: NotiStatusSuccess,
Target: target,
}
}
func (n *NotificationRecord) SetStatus(status int) {
if n == nil {
return
}
n.Status = status
}
func (n *NotificationRecord) SetDetails(details string) {
if n == nil {
return
}
n.Details = details
}
func (n *NotificationRecord) TableName() string {
return "notification_record"
}
func (n *NotificationRecord) Add(ctx *ctx.Context) error {
return Insert(ctx, n)
}
func (n *NotificationRecord) GetGroupIds(ctx *ctx.Context) (groupIds []int64) {
if n == nil {
return
}
if n.SubId > 0 {
if sub, err := AlertSubscribeGet(ctx, "id=?", n.SubId); err != nil {
logger.Errorf("AlertSubscribeGet failed, err: %v", err)
} else {
groupIds = strx.IdsInt64ForAPI(sub.UserGroupIds, " ")
}
return
}
if event, err := AlertHisEventGetById(ctx, n.EventId); err != nil {
logger.Errorf("AlertHisEventGetById failed, err: %v", err)
} else {
groupIds = strx.IdsInt64ForAPI(event.NotifyGroups, " ")
}
return
}
func NotificationRecordsGetByEventId(ctx *ctx.Context, eid int64) ([]*NotificationRecord, error) {
return NotificationRecordsGet(ctx, "event_id=?", eid)
}
func NotificationRecordsGet(ctx *ctx.Context, where string, args ...interface{}) ([]*NotificationRecord, error) {
var lst []*NotificationRecord
err := DB(ctx).Where(where, args...).Find(&lst).Error
if err != nil {
return nil, err
}
return lst, nil
}