From c040dffb5f74a209eedbd86f2bd7ab79d41acf7f Mon Sep 17 00:00:00 2001 From: Yening Qin <710leo@gmail.com> Date: Wed, 25 May 2022 15:14:52 +0800 Subject: [PATCH] feat: add some service api * feat: add some service api --- README.md | 2 +- src/models/alert_cur_event.go | 8 ++++---- src/models/alert_his_event.go | 8 ++++---- src/models/alert_mute.go | 19 +++++++++++++++++-- src/webapi/router/router.go | 11 +++++++++-- src/webapi/router/router_alert_cur_event.go | 8 +++++--- src/webapi/router/router_alert_his_event.go | 5 +++-- src/webapi/router/router_mute.go | 20 ++++++++++++++++++-- 8 files changed, 61 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ee65177a..061eb7a3 100644 --- a/README.md +++ b/README.md @@ -81,4 +81,4 @@ We welcome your participation in the Nightingale open source project and open so - [ ] support pushgateway api ## License -Nightingale with [Apache License V2.0](https://github.com/didi/nightingale/blob/main/LICENSE) open source license. \ No newline at end of file +Nightingale with [Apache License V2.0](https://github.com/didi/nightingale/blob/main/LICENSE) open source license. diff --git a/src/models/alert_cur_event.go b/src/models/alert_cur_event.go index 7fa101ca..02899311 100644 --- a/src/models/alert_cur_event.go +++ b/src/models/alert_cur_event.go @@ -245,8 +245,8 @@ func (e *AlertCurEvent) FillNotifyGroups(cache map[int64]*UserGroup) error { return nil } -func AlertCurEventTotal(bgid, stime, etime int64, severity int, clusters []string, query string) (int64, error) { - session := DB().Model(&AlertCurEvent{}).Where("trigger_time between ? and ?", stime, etime) +func AlertCurEventTotal(prod string, bgid, stime, etime int64, severity int, clusters []string, query string) (int64, error) { + session := DB().Model(&AlertCurEvent{}).Where("trigger_time between ? and ? and rule_prod = ?", stime, etime, prod) if bgid > 0 { session = session.Where("group_id = ?", bgid) @@ -271,8 +271,8 @@ func AlertCurEventTotal(bgid, stime, etime int64, severity int, clusters []strin return Count(session) } -func AlertCurEventGets(bgid, stime, etime int64, severity int, clusters []string, query string, limit, offset int) ([]AlertCurEvent, error) { - session := DB().Where("trigger_time between ? and ?", stime, etime) +func AlertCurEventGets(prod string, bgid, stime, etime int64, severity int, clusters []string, query string, limit, offset int) ([]AlertCurEvent, error) { + session := DB().Where("trigger_time between ? and ? and rule_prod = ?", stime, etime, prod) if bgid > 0 { session = session.Where("group_id = ?", bgid) diff --git a/src/models/alert_his_event.go b/src/models/alert_his_event.go index 7b01404d..df7091c7 100644 --- a/src/models/alert_his_event.go +++ b/src/models/alert_his_event.go @@ -89,8 +89,8 @@ func (e *AlertHisEvent) FillNotifyGroups(cache map[int64]*UserGroup) error { return nil } -func AlertHisEventTotal(bgid, stime, etime int64, severity int, recovered int, clusters []string, query string) (int64, error) { - session := DB().Model(&AlertHisEvent{}).Where("last_eval_time between ? and ?", stime, etime) +func AlertHisEventTotal(prod string, bgid, stime, etime int64, severity int, recovered int, clusters []string, query string) (int64, error) { + session := DB().Model(&AlertHisEvent{}).Where("last_eval_time between ? and ? and rule_prod = ?", stime, etime, prod) if bgid > 0 { session = session.Where("group_id = ?", bgid) @@ -119,8 +119,8 @@ func AlertHisEventTotal(bgid, stime, etime int64, severity int, recovered int, c return Count(session) } -func AlertHisEventGets(bgid, stime, etime int64, severity int, recovered int, clusters []string, query string, limit, offset int) ([]AlertHisEvent, error) { - session := DB().Where("last_eval_time between ? and ?", stime, etime) +func AlertHisEventGets(prod string, bgid, stime, etime int64, severity int, recovered int, clusters []string, query string, limit, offset int) ([]AlertHisEvent, error) { + session := DB().Where("last_eval_time between ? and ? and rule_prod = ?", stime, etime, prod) if bgid > 0 { session = session.Where("group_id = ?", bgid) diff --git a/src/models/alert_mute.go b/src/models/alert_mute.go index f941d37c..08a537f4 100644 --- a/src/models/alert_mute.go +++ b/src/models/alert_mute.go @@ -36,13 +36,28 @@ func (m *AlertMute) TableName() string { return "alert_mute" } -func AlertMuteGets(groupId int64) (lst []AlertMute, err error) { +func AlertMuteGets(bgid int64, query string) (lst []AlertMute, err error) { + session := DB().Where("group_id = ?", bgid) + + if query != "" { + arr := strings.Fields(query) + for i := 0; i < len(arr); i++ { + qarg := "%\"" + arr[i] + "\"%" + session = session.Where("tags like ?", qarg, qarg) + } + } + + err = session.Order("id desc").Find(&lst).Error + return +} + +func AlertMuteGetsByBG(groupId int64) (lst []AlertMute, err error) { err = DB().Where("group_id=?", groupId).Order("id desc").Find(&lst).Error return } func (m *AlertMute) Verify() error { - if m.GroupId <= 0 { + if m.GroupId < 0 { return errors.New("group_id invalid") } diff --git a/src/webapi/router/router.go b/src/webapi/router/router.go index 29ff4057..3f13e3e9 100644 --- a/src/webapi/router/router.go +++ b/src/webapi/router/router.go @@ -218,7 +218,7 @@ func configRoute(r *gin.Engine, version string) { pages.PUT("/busi-group/:id/alert-rule/:arid", jwtAuth(), user(), perm("/alert-rules/put"), alertRulePutByFE) pages.GET("/alert-rule/:arid", jwtAuth(), user(), perm("/alert-rules"), alertRuleGet) - pages.GET("/busi-group/:id/alert-mutes", jwtAuth(), user(), perm("/alert-mutes"), bgro(), alertMuteGets) + pages.GET("/busi-group/:id/alert-mutes", jwtAuth(), user(), perm("/alert-mutes"), bgro(), alertMuteGetsByBG) pages.POST("/busi-group/:id/alert-mutes", jwtAuth(), user(), perm("/alert-mutes/add"), bgrw(), alertMuteAdd) pages.DELETE("/busi-group/:id/alert-mutes", jwtAuth(), user(), perm("/alert-mutes/del"), bgrw(), alertMuteDel) @@ -279,8 +279,15 @@ func configRoute(r *gin.Engine, version string) { service.GET("/alert-rules", alertRuleGets) service.POST("/alert-rules", alertRuleAddByService) service.DELETE("/alert-rules", alertRuleDel) - service.PUT("/alert-rule", alertRulePutByService) + service.PUT("/alert-rule/:arid", alertRulePutByService) service.GET("/alert-rule/:arid", alertRuleGet) service.GET("/alert-rules-get-by-prod", alertRulesGetByProds) + + service.GET("/alert-mutes", alertMuteGets) + service.POST("/alert-mutes", alertMuteAddByService) + service.DELETE("/alert-mutes", alertMuteDel) + + pages.GET("/alert-cur-events", alertCurEventsList) + pages.GET("/alert-his-events", alertHisEventsList) } } diff --git a/src/webapi/router/router_alert_cur_event.go b/src/webapi/router/router_alert_cur_event.go index ff677859..30f49984 100644 --- a/src/webapi/router/router_alert_cur_event.go +++ b/src/webapi/router/router_alert_cur_event.go @@ -45,9 +45,10 @@ func alertCurEventsCard(c *gin.Context) { busiGroupId := ginx.QueryInt64(c, "bgid", 0) clusters := queryClusters(c) rules := parseAggrRules(c) + prod := ginx.QueryStr(c, "prod", "") // 最多获取50000个,获取太多也没啥意义 - list, err := models.AlertCurEventGets(busiGroupId, stime, etime, severity, clusters, query, 50000, 0) + list, err := models.AlertCurEventGets(prod, busiGroupId, stime, etime, severity, clusters, query, 50000, 0) ginx.Dangerous(err) cardmap := make(map[string]*AlertCard) @@ -121,11 +122,12 @@ func alertCurEventsList(c *gin.Context) { limit := ginx.QueryInt(c, "limit", 20) busiGroupId := ginx.QueryInt64(c, "bgid", 0) clusters := queryClusters(c) + prod := ginx.QueryStr(c, "prod", "") - total, err := models.AlertCurEventTotal(busiGroupId, stime, etime, severity, clusters, query) + total, err := models.AlertCurEventTotal(prod, busiGroupId, stime, etime, severity, clusters, query) ginx.Dangerous(err) - list, err := models.AlertCurEventGets(busiGroupId, stime, etime, severity, clusters, query, limit, ginx.Offset(c, limit)) + list, err := models.AlertCurEventGets(prod, busiGroupId, stime, etime, severity, clusters, query, limit, ginx.Offset(c, limit)) ginx.Dangerous(err) cache := make(map[int64]*models.UserGroup) diff --git a/src/webapi/router/router_alert_his_event.go b/src/webapi/router/router_alert_his_event.go index ba7c38e9..631b0454 100644 --- a/src/webapi/router/router_alert_his_event.go +++ b/src/webapi/router/router_alert_his_event.go @@ -34,11 +34,12 @@ func alertHisEventsList(c *gin.Context) { limit := ginx.QueryInt(c, "limit", 20) busiGroupId := ginx.QueryInt64(c, "bgid", 0) clusters := queryClusters(c) + prod := ginx.QueryStr(c, "prod", "") - total, err := models.AlertHisEventTotal(busiGroupId, stime, etime, severity, recovered, clusters, query) + total, err := models.AlertHisEventTotal(prod, busiGroupId, stime, etime, severity, recovered, clusters, query) ginx.Dangerous(err) - list, err := models.AlertHisEventGets(busiGroupId, stime, etime, severity, recovered, clusters, query, limit, ginx.Offset(c, limit)) + list, err := models.AlertHisEventGets(prod, busiGroupId, stime, etime, severity, recovered, clusters, query, limit, ginx.Offset(c, limit)) ginx.Dangerous(err) cache := make(map[int64]*models.UserGroup) diff --git a/src/webapi/router/router_mute.go b/src/webapi/router/router_mute.go index 13599e7d..fc241113 100644 --- a/src/webapi/router/router_mute.go +++ b/src/webapi/router/router_mute.go @@ -8,9 +8,17 @@ import ( ) // Return all, front-end search and paging -func alertMuteGets(c *gin.Context) { +func alertMuteGetsByBG(c *gin.Context) { bgid := ginx.UrlParamInt64(c, "id") - lst, err := models.AlertMuteGets(bgid) + lst, err := models.AlertMuteGetsByBG(bgid) + ginx.NewRender(c).Data(lst, err) +} + +func alertMuteGets(c *gin.Context) { + bgid := ginx.QueryInt64(c, "bgid", 0) + query := ginx.QueryStr(c, "query", "") + lst, err := models.AlertMuteGets(bgid, query) + ginx.NewRender(c).Data(lst, err) } @@ -25,6 +33,14 @@ func alertMuteAdd(c *gin.Context) { ginx.NewRender(c).Message(f.Add()) } +func alertMuteAddByService(c *gin.Context) { + var f models.AlertMute + ginx.BindJSON(c, &f) + f.GroupId = 0 + + ginx.NewRender(c).Message(f.Add()) +} + func alertMuteDel(c *gin.Context) { var f idsForm ginx.BindJSON(c, &f)