feat: add some service api

* feat: add some service api
This commit is contained in:
Yening Qin 2022-05-25 15:14:52 +08:00 committed by GitHub
parent c2f2a7d5e2
commit c040dffb5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 20 deletions

View File

@ -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.
Nightingale with [Apache License V2.0](https://github.com/didi/nightingale/blob/main/LICENSE) open source license.

View File

@ -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)

View File

@ -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)

View File

@ -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")
}

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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)