Browse Source

Compute statistics for alert_cur_events (#1697)

* add statistics of alert_cur_events

* code refactor

* code refactor

* code refactor

---------

Co-authored-by: shardingHe <wangzihe@flashcat.cloud>
main
shardingHe GitHub 2 years ago
parent
commit
74491c666d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions
  1. +1
    -0
      center/router/router.go
  2. +6
    -0
      center/router/router_alert_cur_event.go
  3. +39
    -0
      models/alert_cur_event.go

+ 1
- 0
center/router/router.go View File

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


+ 6
- 0
center/router/router_alert_cur_event.go View File

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

+ 39
- 0
models/alert_cur_event.go View File

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

Loading…
Cancel
Save