From 74491c666d9eeebdb39b001db880dafda64538fb Mon Sep 17 00:00:00 2001 From: shardingHe <125482062+shardingHe@users.noreply.github.com> Date: Tue, 29 Aug 2023 15:15:32 +0800 Subject: [PATCH] Compute statistics for alert_cur_events (#1697) * add statistics of alert_cur_events * code refactor * code refactor * code refactor --------- Co-authored-by: shardingHe --- center/router/router.go | 1 + center/router/router_alert_cur_event.go | 6 ++++ models/alert_cur_event.go | 39 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/center/router/router.go b/center/router/router.go index 1e0b4f7d..256e16e1 100644 --- a/center/router/router.go +++ b/center/router/router.go @@ -311,6 +311,7 @@ func (rt *Router) Config(r *gin.Engine) { pages.POST("/alert-cur-events/card/details", rt.auth(), rt.alertCurEventsCardDetails) pages.GET("/alert-his-events/list", rt.auth(), rt.alertHisEventsList) pages.DELETE("/alert-cur-events", rt.auth(), rt.user(), rt.perm("/alert-cur-events/del"), rt.alertCurEventDel) + pages.GET("/alert-cur-events/stats", rt.auth(), rt.alertCurEventsStatistics) pages.GET("/alert-aggr-views", rt.auth(), rt.alertAggrViewGets) pages.DELETE("/alert-aggr-views", rt.auth(), rt.user(), rt.alertAggrViewDel) diff --git a/center/router/router_alert_cur_event.go b/center/router/router_alert_cur_event.go index 67c586d6..17ac6163 100644 --- a/center/router/router_alert_cur_event.go +++ b/center/router/router_alert_cur_event.go @@ -4,6 +4,7 @@ import ( "net/http" "sort" "strings" + "time" "github.com/ccfos/nightingale/v6/models" @@ -215,3 +216,8 @@ func (rt *Router) alertCurEventGet(c *gin.Context) { ginx.NewRender(c).Data(event, nil) } + +func (rt *Router) alertCurEventsStatistics(c *gin.Context) { + + ginx.NewRender(c).Data(models.AlertCurEventStatistics(rt.Ctx, time.Now()), nil) +} diff --git a/models/alert_cur_event.go b/models/alert_cur_event.go index b560f1a9..3fef30f7 100644 --- a/models/alert_cur_event.go +++ b/models/alert_cur_event.go @@ -585,3 +585,42 @@ func AlertCurEventGetsFromAlertMute(ctx *ctx.Context, alertMute *AlertMute) ([]* err := tx.Order("id desc").Find(&lst).Error return lst, err } + +func AlertCurEventStatistics(ctx *ctx.Context, stime time.Time) (res map[string]interface{}) { + stime24HoursAgoUnix := stime.Add(-24 * time.Hour).Unix() + //Beginning of today + stimeMidnightUnix := time.Date(stime.Year(), stime.Month(), stime.Day(), 0, 0, 0, 0, stime.Location()).Unix() + ///Monday of the current week, starting at 00:00 + daysToMonday := (int(stime.Weekday()) - 1 + 7) % 7 // (DayOfTheWeek - Monday(1) + DaysAWeek(7))/DaysAWeek(7) + stimeOneWeekAgoUnix := time.Date(stime.Year(), stime.Month(), stime.Day()-daysToMonday, 0, 0, 0, 0, stime.Location()).Unix() + + var countNum int64 + err := DB(ctx).Model(&AlertCurEvent{}).Count(&countNum).Error + if err != nil { + logger.Debugf("count alert current rule failed(total), %v", err) + } + res["total"] = countNum + + countNum = 0 + err = DB(ctx).Model(&AlertCurEvent{}).Where("trigger_time < ?", stime24HoursAgoUnix).Count(&countNum).Error + if err != nil { + logger.Debugf("count alert current rule failed(total_24ago), %v", err) + } + res["total_24_ago"] = countNum + + countNum = 0 + err = DB(ctx).Model(&AlertHisEvent{}).Where("trigger_time >= ? and is_recovered = ? ", stimeMidnightUnix, 0).Count(&countNum).Error + if err != nil { + logger.Debugf("count alert his rule failed(total_today), %v", err) + } + res["total_today"] = countNum + + countNum = 0 + err = DB(ctx).Model(&AlertHisEvent{}).Where("trigger_time >= ? and is_recovered = ? ", stimeOneWeekAgoUnix, 0).Count(&countNum).Error + if err != nil { + logger.Debugf("count alert his rule failed(total_today), %v", err) + } + res["total_week"] = countNum + + return res +}