Skip to content

Commit c73b668

Browse files
committed
fix: cur event api
1 parent cd74442 commit c73b668

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

center/router/router_alert_cur_event.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ func (rt *Router) alertCurEventsCard(c *gin.Context) {
6565
cates = strings.Split(cate, ",")
6666
}
6767

68-
bgids, err := GetBusinessGroupIds(c, rt.Ctx, rt.Center.EventHistoryGroupView)
68+
bgids, err := GetBusinessGroupIds(c, rt.Ctx, rt.Center.EventHistoryGroupView, myGroups)
6969
ginx.Dangerous(err)
7070

7171
// 最多获取50000个,获取太多也没啥意义
7272
list, err := models.AlertCurEventsGet(rt.Ctx, prods, bgids, stime, etime, severity, dsIds,
73-
cates, 0, query, 50000, 0, gids, []int64{})
73+
cates, 0, query, 50000, 0, []int64{})
7474
ginx.Dangerous(err)
7575

7676
cardmap := make(map[string]*AlertCard)
@@ -178,25 +178,15 @@ func (rt *Router) alertCurEventsList(c *gin.Context) {
178178

179179
ruleId := ginx.QueryInt64(c, "rid", 0)
180180

181-
var gids []int64
182-
var err error
183-
if myGroups {
184-
gids, err = getUserGroupIds(c, rt, myGroups)
185-
ginx.Dangerous(err)
186-
if len(gids) == 0 {
187-
gids = append(gids, -1)
188-
}
189-
}
190-
191-
bgids, err := GetBusinessGroupIds(c, rt.Ctx, rt.Center.EventHistoryGroupView)
181+
bgids, err := GetBusinessGroupIds(c, rt.Ctx, rt.Center.EventHistoryGroupView, myGroups)
192182
ginx.Dangerous(err)
193183

194184
total, err := models.AlertCurEventTotal(rt.Ctx, prods, bgids, stime, etime, severity, dsIds,
195-
cates, ruleId, query, gids, eventIds)
185+
cates, ruleId, query, eventIds)
196186
ginx.Dangerous(err)
197187

198188
list, err := models.AlertCurEventsGet(rt.Ctx, prods, bgids, stime, etime, severity, dsIds,
199-
cates, ruleId, query, limit, ginx.Offset(c, limit), gids, eventIds)
189+
cates, ruleId, query, limit, ginx.Offset(c, limit), eventIds)
200190
ginx.Dangerous(err)
201191

202192
cache := make(map[int64]*models.UserGroup)

center/router/router_alert_his_event.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (rt *Router) alertHisEventsList(c *gin.Context) {
5656

5757
ruleId := ginx.QueryInt64(c, "rid", 0)
5858

59-
bgids, err := GetBusinessGroupIds(c, rt.Ctx, rt.Center.EventHistoryGroupView)
59+
bgids, err := GetBusinessGroupIds(c, rt.Ctx, rt.Center.EventHistoryGroupView, false)
6060
ginx.Dangerous(err)
6161

6262
total, err := models.AlertHisEventTotal(rt.Ctx, prods, bgids, stime, etime, severity,
@@ -103,43 +103,39 @@ func (rt *Router) alertHisEventGet(c *gin.Context) {
103103
ginx.NewRender(c).Data(event, err)
104104
}
105105

106-
func GetBusinessGroupIds(c *gin.Context, ctx *ctx.Context, eventHistoryGroupView bool) ([]int64, error) {
106+
func GetBusinessGroupIds(c *gin.Context, ctx *ctx.Context, onlySelfGroupView bool, myGroups bool) ([]int64, error) {
107107
bgid := ginx.QueryInt64(c, "bgid", 0)
108108
var bgids []int64
109109

110-
if !eventHistoryGroupView || strings.HasPrefix(c.Request.URL.Path, "/v1") {
111-
if bgid > 0 {
112-
return []int64{bgid}, nil
110+
user := c.MustGet("user").(*models.User)
111+
112+
if myGroups || (onlySelfGroupView && !strings.HasPrefix(c.Request.URL.Path, "/v1") && !user.IsAdmin()) {
113+
// 1. 页面上勾选了我的业务组,需要查询用户所属的业务组
114+
// 2. 如果 onlySelfGroupView 为 true,表示只允许查询用户所属的业务组
115+
bussGroupIds, err := models.MyBusiGroupIds(ctx, user.Id)
116+
if err != nil {
117+
return nil, err
113118
}
114-
return bgids, nil
115-
}
116119

117-
user := c.MustGet("user").(*models.User)
118-
if user.IsAdmin() {
119-
if bgid > 0 {
120-
return []int64{bgid}, nil
120+
if len(bussGroupIds) == 0 {
121+
// 如果没查到用户属于任何业务组,需要返回一个0,否则会导致查询到全部告警历史
122+
return []int64{0}, nil
121123
}
122-
return bgids, nil
123-
}
124124

125-
bussGroupIds, err := models.MyBusiGroupIds(ctx, user.Id)
126-
if err != nil {
127-
return nil, err
128-
}
125+
if bgid > 0 {
126+
if !slices.Contains(bussGroupIds, bgid) && !user.IsAdmin() {
127+
return nil, fmt.Errorf("business group ID not allowed")
128+
}
129129

130-
if len(bussGroupIds) == 0 {
131-
// 如果没查到用户属于任何业务组,需要返回一个0,否则会导致查询到全部告警历史
132-
return []int64{0}, nil
133-
}
130+
return []int64{bgid}, nil
131+
}
134132

135-
if bgid > 0 && !slices.Contains(bussGroupIds, bgid) {
136-
return nil, fmt.Errorf("business group ID not allowed")
133+
return bussGroupIds, nil
137134
}
138135

139136
if bgid > 0 {
140-
// Pass filter parameters, priority to use
141137
return []int64{bgid}, nil
142138
}
143139

144-
return bussGroupIds, nil
140+
return bgids, nil
145141
}

models/alert_cur_event.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func (e *AlertCurEvent) FillNotifyGroups(ctx *ctx.Context, cache map[int64]*User
594594
}
595595

596596
func AlertCurEventTotal(ctx *ctx.Context, prods []string, bgids []int64, stime, etime int64,
597-
severity []int64, dsIds []int64, cates []string, ruleId int64, query string, myGroups []int64, eventIds []int64) (int64, error) {
597+
severity []int64, dsIds []int64, cates []string, ruleId int64, query string, eventIds []int64) (int64, error) {
598598
session := DB(ctx).Model(&AlertCurEvent{})
599599
if stime != 0 && etime != 0 {
600600
session = session.Where("trigger_time between ? and ?", stime, etime)
@@ -623,10 +623,6 @@ func AlertCurEventTotal(ctx *ctx.Context, prods []string, bgids []int64, stime,
623623
session = session.Where("rule_id = ?", ruleId)
624624
}
625625

626-
if len(myGroups) > 0 {
627-
session = session.Where("group_id in ?", myGroups)
628-
}
629-
630626
if len(eventIds) > 0 {
631627
session = session.Where("id in ?", eventIds)
632628
}
@@ -642,7 +638,7 @@ func AlertCurEventTotal(ctx *ctx.Context, prods []string, bgids []int64, stime,
642638
}
643639

644640
func AlertCurEventsGet(ctx *ctx.Context, prods []string, bgids []int64, stime, etime int64,
645-
severity []int64, dsIds []int64, cates []string, ruleId int64, query string, limit, offset int, myGroups []int64, eventIds []int64) (
641+
severity []int64, dsIds []int64, cates []string, ruleId int64, query string, limit, offset int, eventIds []int64) (
646642
[]AlertCurEvent, error) {
647643
session := DB(ctx).Model(&AlertCurEvent{})
648644

@@ -672,12 +668,11 @@ func AlertCurEventsGet(ctx *ctx.Context, prods []string, bgids []int64, stime, e
672668
if ruleId > 0 {
673669
session = session.Where("rule_id = ?", ruleId)
674670
}
675-
if len(myGroups) > 0 {
676-
session = session.Where("group_id in ?", myGroups)
677-
}
671+
678672
if len(eventIds) > 0 {
679673
session = session.Where("id in ?", eventIds)
680674
}
675+
681676
if query != "" {
682677
arr := strings.Fields(query)
683678
for i := 0; i < len(arr); i++ {

0 commit comments

Comments
 (0)