-
Notifications
You must be signed in to change notification settings - Fork 81
/
model_alert.go
89 lines (78 loc) · 2.59 KB
/
model_alert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"strconv"
"time"
"gorm.io/datatypes"
"kubegems.io/kubegems/pkg/utils"
"kubegems.io/kubegems/pkg/utils/msgbus"
)
type AlertInfo struct {
Fingerprint string `gorm:"type:varchar(50);primaryKey"` // 指纹作为主键
Name string `gorm:"type:varchar(50);"`
Namespace string `gorm:"type:varchar(50);"`
ClusterName string `gorm:"type:varchar(50);"`
TenantName string `gorm:"type:varchar(50);index"`
ProjectName string `gorm:"type:varchar(50);index"`
EnvironmentName string `gorm:"type:varchar(50);index"`
Labels datatypes.JSON
LabelMap map[string]string `gorm:"-" json:"-"`
SilenceStartsAt *time.Time
SilenceUpdatedAt *time.Time
SilenceEndsAt *time.Time
SilenceCreator string `gorm:"type:varchar(50);"`
Summary string `gorm:"-"` // 黑名单概要
}
type AlertMessage struct {
ID uint
// 级联删除
Fingerprint string `gorm:"type:varchar(50);"`
AlertInfo *AlertInfo `gorm:"foreignKey:Fingerprint;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Value string
Message string
StartsAt *time.Time `gorm:"index"` // 告警开始时间
EndsAt *time.Time // 告警结束时间
CreatedAt *time.Time `gorm:"index"` // 本次告警产生时间
Status string // firing or resolved
}
func (a *AlertMessage) ToNormalMessage() Message {
return Message{
ID: a.ID,
MessageType: string(msgbus.Alert),
Title: a.Message,
CreatedAt: *a.CreatedAt,
}
}
func (a *AlertMessage) ColumnSlice() []string {
return []string{"id", "fingerprint", "value", "message", "starts_at", "ends_at", "created_at", "status", "labels"}
}
func (a *AlertMessage) ValueSlice() []string {
return []string{
strconv.Itoa(int(a.ID)),
a.Fingerprint,
a.Value,
a.Message,
utils.FormatMysqlDumpTime(a.StartsAt),
utils.FormatMysqlDumpTime(a.EndsAt),
utils.FormatMysqlDumpTime(a.CreatedAt),
a.Status,
func() string {
if a.AlertInfo != nil {
return a.AlertInfo.Labels.String()
}
return ""
}(),
}
}