Skip to content

Commit

Permalink
♻️Add get pointer feild func
Browse files Browse the repository at this point in the history
  • Loading branch information
Cairry committed Dec 20, 2024
1 parent ca3fa9d commit fb2c2fa
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 15 deletions.
2 changes: 1 addition & 1 deletion alert/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (ec *Consume) handleAlert(rule models.AlertRule, alerts []models.AlertCurEv

mp := mute.MuteParams{
EffectiveTime: alert.EffectiveTime,
RecoverNotify: *alert.RecoverNotify,
RecoverNotify: alert.GetRecoverNotify(),
IsRecovered: alert.IsRecovered,
TenantId: alert.TenantId,
Fingerprint: alert.Fingerprint,
Expand Down
4 changes: 2 additions & 2 deletions alert/mute/mute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type MuteParams struct {
EffectiveTime models.EffectiveTime
RecoverNotify bool
RecoverNotify *bool
IsRecovered bool
TenantId string
Fingerprint string
Expand Down Expand Up @@ -65,7 +65,7 @@ func InTheEffectiveTime(mp MuteParams) bool {
// RecoverNotify 判断是否推送恢复通知
func RecoverNotify(mp MuteParams) bool {
// 如果是恢复告警,并且 恢复通知 == 1,即关闭恢复通知
if mp.IsRecovered && !mp.RecoverNotify {
if mp.IsRecovered && !*mp.RecoverNotify {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion alert/probing/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ func buildEvent(event models.ProbingEvent) models.AlertCurEvent {
RecoverTime: event.RecoverTime,
RecoverTimeFormat: event.RecoverTimeFormat,
DutyUser: event.DutyUser,
RecoverNotify: event.RecoverNotify,
RecoverNotify: event.GetRecoverNotify(),
}
}
2 changes: 1 addition & 1 deletion alert/probing/producter.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (t *ProductProbing) processDefaultEvent(rule models.ProbingRule) models.Pro
Severity: rule.Severity,
IsRecovered: false,
RepeatNoticeInterval: rule.RepeatNoticeInterval,
RecoverNotify: rule.RecoverNotify,
RecoverNotify: rule.GetRecoverNotify(),
ProbingEndpointConfig: rule.ProbingEndpointConfig,
}
}
Expand Down
4 changes: 2 additions & 2 deletions alert/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func BuildEvent(rule models.AlertRule) models.AlertCurEvent {
RepeatNoticeInterval: rule.RepeatNoticeInterval,
Severity: rule.Severity,
EffectiveTime: rule.EffectiveTime,
RecoverNotify: rule.RecoverNotify,
AlarmAggregation: rule.AlarmAggregation,
RecoverNotify: rule.GetRecoverNotify(),
AlarmAggregation: rule.GetAlarmAggregation(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions initialization/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func importClientPools(ctx *ctx.Context) {

g := new(errgroup.Group)
for _, datasource := range list {
datasource := datasource
if !*datasource.Enabled {
ds := datasource
if !*ds.GetEnabled() {
continue
}
g.Go(func() error {
Expand Down
16 changes: 16 additions & 0 deletions internal/models/alert_current_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ func (ace *AlertCurEvent) GetPendingAlertCacheKey() string {
func (ace *AlertCurEvent) AlertCacheTailKey() string {
return ace.RuleId + "-" + ace.DatasourceId + "-" + ace.Fingerprint
}

func (ace *AlertCurEvent) GetRecoverNotify() *bool {
if ace.RecoverNotify == nil {
isOk := false
return &isOk
}
return ace.RecoverNotify
}

func (ace *AlertCurEvent) GetAlarmAggregation() *bool {
if ace.AlarmAggregation == nil {
isOk := false
return &isOk
}
return ace.AlarmAggregation
}
8 changes: 8 additions & 0 deletions internal/models/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ type result struct {
Metric map[string]interface{} `json:"metric"`
Value []interface{} `json:"value"`
}

func (d *AlertDataSource) GetEnabled() *bool {
if d.Enabled == nil {
isOk := false
return &isOk
}
return d.Enabled
}
24 changes: 24 additions & 0 deletions internal/models/probing.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ func (n *ProbingRule) GetProbingMappingKey() string {
return "w8t" + ":" + n.TenantId + ":" + "netValue" + ":" + n.RuleId
}

func (n *ProbingRule) GetRecoverNotify() *bool {
if n.RecoverNotify == nil {
isOk := false
return &isOk
}
return n.RecoverNotify
}

func (n *ProbingRule) GetEnabled() *bool {
if n.Enabled == nil {
isOk := false
return &isOk
}
return n.Enabled
}

type OnceProbing struct {
RuleType string `json:"ruleType"`
ProbingEndpointConfig ProbingEndpointConfig `json:"probingEndpointConfig"`
Expand Down Expand Up @@ -146,3 +162,11 @@ func (n *ProbingEvent) GetFiringAlertCacheKey() string {
func (n *ProbingEvent) GetProbingMappingKey() string {
return "w8t" + ":" + n.TenantId + ":" + "netValue" + ":" + n.RuleId
}

func (n *ProbingEvent) GetRecoverNotify() *bool {
if n.RecoverNotify == nil {
isOk := false
return &isOk
}
return n.RecoverNotify
}
24 changes: 24 additions & 0 deletions internal/models/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,27 @@ func (a *AlertRule) Fingerprint() Fingerprint {
}

func (a *AlertRule) GetRuleType() string { return a.DatasourceType }

func (a *AlertRule) GetRecoverNotify() *bool {
if a.RecoverNotify == nil {
isOk := false
return &isOk
}
return a.RecoverNotify
}

func (a *AlertRule) GetAlarmAggregation() *bool {
if a.AlarmAggregation == nil {
isOk := false
return &isOk
}
return a.AlarmAggregation
}

func (a *AlertRule) GetEnabled() *bool {
if a.Enabled == nil {
isOk := false
return &isOk
}
return a.RecoverNotify
}
8 changes: 8 additions & 0 deletions internal/models/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ type Tenant struct {
UserId string `json:"userId" gorm:"-"`
}

func (t *Tenant) GetRemoveProtection() *bool {
if t.RemoveProtection == nil {
isOk := false
return &isOk
}
return t.RemoveProtection
}

type TenantQuery struct {
ID string `json:"id" form:"id"`
Name string `json:"name" form:"name"`
Expand Down
4 changes: 2 additions & 2 deletions internal/services/probing.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m probingService) Create(req interface{}) (interface{}, interface{}) {
return nil, err
}

if *r.Enabled {
if *r.GetEnabled() {
m.ProductTask.Submit(*r)
}
m.ConsumerTask.Add(*r)
Expand All @@ -64,7 +64,7 @@ func (m probingService) Update(req interface{}) (interface{}, interface{}) {

m.ProductTask.Stop(r.RuleId)
m.ConsumerTask.Stop(r.RuleId)
if *r.Enabled {
if *r.GetEnabled() {
m.ProductTask.Submit(*r)
m.ConsumerTask.Add(*r)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (rs ruleService) Update(req interface{}) (interface{}, interface{}) {
}

// 启动协程
if *rule.Enabled {
if *rule.GetEnabled() {
alert.AlertRule.Submit(*rule)
logc.Infof(rs.ctx.Ctx, fmt.Sprintf("重启 RuleId 为 %s 的 Worker 进程", rule.RuleId))
} else {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (rs ruleService) Delete(req interface{}) (interface{}, interface{}) {
}

// 退出该规则的协程
if *info.Enabled {
if *info.GetEnabled() {
logc.Infof(rs.ctx.Ctx, fmt.Sprintf("停止 RuleId 为 %s 的 Worker 进程", rule.RuleId))
alert.AlertRule.Stop(rule.RuleId)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (ts tenantService) Create(req interface{}) (data interface{}, err interface
UserNumber: r.UserNumber,
DutyNumber: r.DutyNumber,
NoticeNumber: r.NoticeNumber,
RemoveProtection: r.RemoveProtection,
RemoveProtection: r.GetRemoveProtection(),
}

err = ts.ctx.DB.Tenant().Create(nt)
Expand All @@ -72,7 +72,7 @@ func (ts tenantService) Delete(req interface{}) (data interface{}, err interface
var t models.Tenant
ts.ctx.DB.DB().Model(&models.Tenant{}).Where("id = ?", r.ID).Find(&t)

if *t.RemoveProtection {
if *t.GetRemoveProtection() {
return nil, fmt.Errorf("删除失败, 删除保护已开启 关闭后再删除")
}

Expand Down

0 comments on commit fb2c2fa

Please sign in to comment.