Skip to content

Commit

Permalink
🚀 Optimize rule template supported application to alarm rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Cairry committed Dec 9, 2024
1 parent b639dff commit 2e4109d
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 60 deletions.
14 changes: 12 additions & 2 deletions api/rule_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
type RuleTmplController struct{}

/*
规则模版 API
/api/w8t/ruleTmpl
规则模版 API
/api/w8t/ruleTmpl
*/
func (rtc RuleTmplController) API(gin *gin.RouterGroup) {
ruleTmplA := gin.Group("ruleTmpl")
Expand All @@ -23,6 +23,7 @@ func (rtc RuleTmplController) API(gin *gin.RouterGroup) {
)
{
ruleTmplA.POST("ruleTmplCreate", rtc.Create)
ruleTmplA.POST("ruleTmplUpdate", rtc.Update)
ruleTmplA.POST("ruleTmplDelete", rtc.Delete)
}

Expand All @@ -46,6 +47,15 @@ func (rtc RuleTmplController) Create(ctx *gin.Context) {
})
}

func (rtc RuleTmplController) Update(ctx *gin.Context) {
r := new(models.RuleTemplate)
BindJson(ctx, r)

Service(ctx, func() (interface{}, interface{}) {
return services.RuleTmplService.Update(r)
})
}

func (rtc RuleTmplController) Delete(ctx *gin.Context) {
r := new(models.RuleTemplateQuery)
BindJson(ctx, r)
Expand Down
14 changes: 12 additions & 2 deletions api/rule_tmpl_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
type RuleTmplGroupController struct{}

/*
规则模版组 API
/api/w8t/ruleTmplGroup
规则模版组 API
/api/w8t/ruleTmplGroup
*/
func (rtg RuleTmplGroupController) API(gin *gin.RouterGroup) {
ruleTmplGroupA := gin.Group("ruleTmplGroup")
Expand All @@ -23,6 +23,7 @@ func (rtg RuleTmplGroupController) API(gin *gin.RouterGroup) {
)
{
ruleTmplGroupA.POST("ruleTmplGroupCreate", rtg.Create)
ruleTmplGroupA.POST("ruleTmplGroupUpdate", rtg.Update)
ruleTmplGroupA.POST("ruleTmplGroupDelete", rtg.Delete)
}

Expand All @@ -46,6 +47,15 @@ func (rtg RuleTmplGroupController) Create(ctx *gin.Context) {
})
}

func (rtg RuleTmplGroupController) Update(ctx *gin.Context) {
r := new(models.RuleTemplateGroup)
BindJson(ctx, r)

Service(ctx, func() (interface{}, interface{}) {
return services.RuleTmplGroupService.Update(r)
})
}

func (rtg RuleTmplGroupController) Delete(ctx *gin.Context) {
r := new(models.RuleTemplateGroupQuery)
BindJson(ctx, r)
Expand Down
22 changes: 16 additions & 6 deletions deploy/sql/rule_template_groups.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
use watchalert;
INSERT ignore INTO `rule_template_groups` (`name`, `description`) VALUES ('Node', '服务器节点监控指标');
INSERT ignore INTO `rule_template_groups` (`name`, `description`) VALUES ('ApiSix', 'APISix网关监控指标');
INSERT ignore INTO `rule_template_groups` (`name`, `description`) VALUES ('Elasticsearch', 'Elasticsearch监控告警指标');
INSERT ignore INTO `rule_template_groups` (`name`, `description`) VALUES ('Kafka', 'Kafka监控告警指标');
INSERT ignore INTO `rule_template_groups` (`name`, `description`) VALUES ('Kubernetes', 'Kubernetes监控告警指标');
INSERT ignore INTO `rule_template_groups` (`name`, `description`) VALUES ('Docker', 'Docker监控告警指标');
INSERT ignore INTO watchalert.rule_template_groups (name,`number`,description,`type`) VALUES
('APISIX',0,'APISIX指标监控','Metrics'),
('APISIX日志监控',0,'APISIX日志监控','Logs'),
('Docker',0,'Docker容器监控','Metrics'),
('ElasticSearch',0,'ElasticSearch资源监控','Metrics'),
('ETCD',0,'ETCD','Metrics'),
('Jaeger',0,'Jaeger链路监控','Traces'),
('Kafka',0,'Kafka监控','Metrics'),
('Kubernetes',0,'Kubernetes事件监控','Events'),
('KubernetesMetric',0,'Kubernetes指标监控','Metrics'),
('MongoDB',0,'MongoDB监控','Metrics');
INSERT ignore INTO watchalert.rule_template_groups (name,`number`,description,`type`) VALUES
('MySQL',0,'MySQL资源监控','Metrics'),
('Node节点监控',0,'Node服务器监控','Metrics'),
('Redis',0,'Redis资源监控','Metrics'),
('RocketMQ',0,'RocketMQ监控','Metrics');
126 changes: 88 additions & 38 deletions deploy/sql/rule_templates.sql

Large diffs are not rendered by default.

30 changes: 19 additions & 11 deletions internal/models/rule_template.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
package models

type RuleTemplateGroup struct {
Name string `json:"name"`
Name string `json:"name" gorm:"type:varchar(255);not null"`
Number int `json:"number"`
Type string `json:"type"`
Description string `json:"description"`
}

type RuleTemplateGroupQuery struct {
Name string `json:"name" form:"name"`
Type string `json:"type" form:"type"`
Description string `json:"description" form:"description"`
Query string `json:"query" form:"query"`
}

type RuleTemplate struct {
RuleGroupName string `json:"ruleGroupName"`
RuleName string `json:"ruleName"`
DatasourceType string `json:"datasourceType"`
Severity int64 `json:"severity"`
PrometheusConfig PrometheusConfig `json:"prometheusConfig" gorm:"prometheusConfig;serializer:json"`
AliCloudSLSConfig AliCloudSLSConfig `json:"alicloudSLSConfig" gorm:"alicloudSLSConfig;serializer:json"`
LokiConfig LokiConfig `json:"lokiConfig" gorm:"lokiConfig;serializer:json"`
EvalInterval int64 `json:"evalInterval"`
ForDuration int64 `json:"forDuration"`
Annotations string `json:"annotations"`
Type string `json:"type"`
RuleGroupName string `json:"ruleGroupName"`
RuleName string `json:"ruleName" gorm:"type:varchar(255);not null"`
DatasourceType string `json:"datasourceType"`
EvalInterval int64 `json:"evalInterval"`
ForDuration int64 `json:"forDuration"`
RepeatNoticeInterval int64 `json:"repeatNoticeInterval"`
Description string `json:"description"`
EffectiveTime EffectiveTime `json:"effectiveTime" gorm:"effectiveTime;serializer:json"`
PrometheusConfig PrometheusConfig `json:"prometheusConfig" gorm:"prometheusConfig;serializer:json"`
AliCloudSLSConfig AliCloudSLSConfig `json:"alicloudSLSConfig" gorm:"alicloudSLSConfig;serializer:json"`
LokiConfig LokiConfig `json:"lokiConfig" gorm:"lokiConfig;serializer:json"`
JaegerConfig JaegerConfig `json:"jaegerConfig" gorm:"JaegerConfig;serializer:json"`
KubernetesConfig KubernetesConfig `json:"kubernetesConfig" gorm:"kubernetesConfig;serializer:json"`
ElasticSearchConfig ElasticSearchConfig `json:"elasticSearchConfig" gorm:"elasticSearchConfig;serializer:json"`
}

type RuleTemplateQuery struct {
Type string `json:"type" form:"type"`
RuleGroupName string `json:"ruleGroupName" form:"ruleGroupName"`
RuleName string `json:"ruleName" form:"ruleName"`
DatasourceType string `json:"datasourceType" form:"datasourceType"`
Expand Down
8 changes: 8 additions & 0 deletions internal/models/user_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func PermissionsInfo() map[string]UserPermissions {
Key: "创建规则模版",
API: "/api/w8t/ruleTmpl/ruleTmplCreate",
},
"ruleTmplUpdate": {
Key: "更新规则模版",
API: "/api/w8t/ruleTmpl/ruleTmplUpdate",
},
"ruleTmplDelete": {
Key: "删除规则模版",
API: "/api/w8t/ruleTmpl/ruleTmplDelete",
Expand All @@ -203,6 +207,10 @@ func PermissionsInfo() map[string]UserPermissions {
Key: "创建规则模版组",
API: "/api/w8t/ruleTmplGroup/ruleTmplGroupCreate",
},
"ruleTmplGroupUpdate": {
Key: "更新规则模版组",
API: "/api/w8t/ruleTmplGroup/ruleTmplGroupUpdate",
},
"ruleTmplGroupDelete": {
Key: "删除规则模版组",
API: "/api/w8t/ruleTmplGroup/ruleTmplGroupDelete",
Expand Down
18 changes: 18 additions & 0 deletions internal/repo/rule_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type (
InterRuleTmplRepo interface {
List(r models.RuleTemplateQuery) ([]models.RuleTemplate, error)
Create(r models.RuleTemplate) error
Update(r models.RuleTemplate) error
Delete(r models.RuleTemplateQuery) error
}
)
Expand All @@ -29,6 +30,7 @@ func newRuleTmplInterface(db *gorm.DB, g InterGormDBCli) InterRuleTmplRepo {
func (rt RuleTmplRepo) List(r models.RuleTemplateQuery) ([]models.RuleTemplate, error) {
var data []models.RuleTemplate
db := rt.db.Model(&models.RuleTemplate{}).Where("rule_group_name = ?", r.RuleGroupName)
db.Where("type = ?", r.Type)
if r.Query != "" {
db.Where("rule_name LIKE ? OR datasource_type LIKE ?",
"%"+r.Query+"%", "%"+r.Query+"%")
Expand All @@ -51,6 +53,22 @@ func (rt RuleTmplRepo) Create(r models.RuleTemplate) error {
return nil
}

func (rt RuleTmplRepo) Update(r models.RuleTemplate) error {
u := Updates{
Table: models.RuleTemplate{},
Where: map[string]interface{}{
"rule_name = ?": r.RuleName,
},
Updates: r,
}
err := rt.g.Updates(u)
if err != nil {
return err
}

return nil
}

func (rt RuleTmplRepo) Delete(r models.RuleTemplateQuery) error {
d := Delete{
Table: models.RuleTemplate{},
Expand Down
22 changes: 21 additions & 1 deletion internal/repo/rule_tmpl_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type (
InterRuleTmplGroupRepo interface {
List(r models.RuleTemplateGroupQuery) ([]models.RuleTemplateGroup, error)
Create(r models.RuleTemplateGroup) error
Update(r models.RuleTemplateGroup) error
Delete(r models.RuleTemplateGroupQuery) error
}
)
Expand All @@ -29,6 +30,7 @@ func newRuleTmplGroupInterface(db *gorm.DB, g InterGormDBCli) InterRuleTmplGroup
func (rtg RuleTmplGroupRepo) List(r models.RuleTemplateGroupQuery) ([]models.RuleTemplateGroup, error) {
var data []models.RuleTemplateGroup
db := rtg.db.Model(&models.RuleTemplateGroup{})
db.Where("type = ?", r.Type)
if r.Query != "" {
db.Where("name LIKE ? OR description LIKE ?",
"%"+r.Query+"%", "%"+r.Query+"%")
Expand All @@ -40,7 +42,9 @@ func (rtg RuleTmplGroupRepo) List(r models.RuleTemplateGroupQuery) ([]models.Rul

for k, v := range data {
var ruleCount int64
rtg.db.Model(&models.RuleTemplate{}).Where("rule_group_name = ?", v.Name).Count(&ruleCount)
rtdb := rtg.db.Model(&models.RuleTemplate{})
rtdb.Where("type = ?", r.Type)
rtdb.Where("rule_group_name = ?", v.Name).Count(&ruleCount)
data[k].Number = int(ruleCount)
}

Expand All @@ -56,6 +60,22 @@ func (rtg RuleTmplGroupRepo) Create(r models.RuleTemplateGroup) error {
return nil
}

func (rtg RuleTmplGroupRepo) Update(r models.RuleTemplateGroup) error {
u := Updates{
Table: models.RuleTemplateGroup{},
Where: map[string]interface{}{
"name = ?": r.Name,
},
Updates: r,
}
err := rtg.g.Updates(u)
if err != nil {
return err
}

return nil
}

func (rtg RuleTmplGroupRepo) Delete(r models.RuleTemplateGroupQuery) error {
d := Delete{
Table: &models.RuleTemplateGroup{},
Expand Down
11 changes: 11 additions & 0 deletions internal/services/rule_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ruleTmplService struct {
type InterRuleTmplService interface {
List(req interface{}) (interface{}, interface{})
Create(req interface{}) (interface{}, interface{})
Update(req interface{}) (interface{}, interface{})
Delete(req interface{}) (interface{}, interface{})
}

Expand Down Expand Up @@ -41,6 +42,16 @@ func (rt ruleTmplService) Create(req interface{}) (interface{}, interface{}) {
return nil, nil
}

func (rt ruleTmplService) Update(req interface{}) (interface{}, interface{}) {
r := req.(*models.RuleTemplate)
err := rt.ctx.DB.RuleTmpl().Update(*r)
if err != nil {
return nil, err
}

return nil, nil
}

func (rt ruleTmplService) Delete(req interface{}) (interface{}, interface{}) {
r := req.(*models.RuleTemplateQuery)
err := rt.ctx.DB.RuleTmpl().Delete(*r)
Expand Down
11 changes: 11 additions & 0 deletions internal/services/rule_tmpl_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ruleTmplGroupService struct {
type InterRuleTmplGroupService interface {
List(req interface{}) (interface{}, interface{})
Create(req interface{}) (interface{}, interface{})
Update(req interface{}) (interface{}, interface{})
Delete(req interface{}) (interface{}, interface{})
}

Expand Down Expand Up @@ -41,6 +42,16 @@ func (rtg ruleTmplGroupService) Create(req interface{}) (interface{}, interface{
return nil, nil
}

func (rtg ruleTmplGroupService) Update(req interface{}) (interface{}, interface{}) {
r := req.(*models.RuleTemplateGroup)
err := rtg.ctx.DB.RuleTmplGroup().Update(*r)
if err != nil {
return nil, err
}

return nil, nil
}

func (rtg ruleTmplGroupService) Delete(req interface{}) (interface{}, interface{}) {
r := req.(*models.RuleTemplateGroupQuery)
err := rtg.ctx.DB.RuleTmplGroup().Delete(*r)
Expand Down

0 comments on commit 2e4109d

Please sign in to comment.