Skip to content

Commit

Permalink
add statistics support
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Jul 13, 2020
1 parent f070c2d commit 3466e66
Show file tree
Hide file tree
Showing 24 changed files with 50,294 additions and 35,110 deletions.
2 changes: 1 addition & 1 deletion agent/api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/ledisdb/ledisdb/ledis"
"github.com/mylxsw/adanos-alert/agent/store"
"github.com/mylxsw/adanos-alert/misc"
"github.com/mylxsw/adanos-alert/pkg/misc"
"github.com/mylxsw/adanos-alert/rpc/protocol"
"github.com/mylxsw/asteria/log"
"github.com/mylxsw/container"
Expand Down
2 changes: 1 addition & 1 deletion agent/job/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/ledisdb/ledisdb/ledis"
"github.com/mylxsw/adanos-alert/misc"
"github.com/mylxsw/adanos-alert/pkg/misc"
"github.com/mylxsw/adanos-alert/rpc/protocol"
"github.com/mylxsw/asteria/log"
"github.com/mylxsw/container"
Expand Down
2 changes: 1 addition & 1 deletion api/controller/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/mylxsw/adanos-alert/internal/repository"
"github.com/mylxsw/adanos-alert/misc"
"github.com/mylxsw/adanos-alert/pkg/misc"
"github.com/mylxsw/adanos-alert/pkg/template"
"github.com/mylxsw/asteria/log"
"github.com/mylxsw/container"
Expand Down
4 changes: 3 additions & 1 deletion api/controller/rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -421,7 +422,8 @@ func (r RuleController) TestMessageMatch(ctx web.Context) web.Response {

// Tags return all tags existed
func (r RuleController) Tags(ctx web.Context, repo repository.RuleRepo) web.Response {
tags, err := repo.Tags()
timeoutCtx, _ := context.WithTimeout(ctx.Context(), 5*time.Second)
tags, err := repo.Tags(timeoutCtx)
if err != nil {
return ctx.JSONError(fmt.Sprintf("query failed: %v", err), http.StatusInternalServerError)
}
Expand Down
92 changes: 92 additions & 0 deletions api/controller/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package controller

import (
"context"
"time"

"github.com/mylxsw/adanos-alert/internal/repository"
"github.com/mylxsw/asteria/log"
"github.com/mylxsw/container"
"github.com/mylxsw/glacier/web"
)

type StatisticsController struct {
cc container.Container
}

func NewStatisticsController(cc container.Container) web.Controller {
return &StatisticsController{cc: cc}
}

func (s *StatisticsController) Register(router *web.Router) {
router.Group("/statistics", func(router *web.Router) {
router.Get("/daily-group-counts/", s.DailyGroupCounts).Name("statistics:daily-group-counts")
router.Get("/user-group-counts/", s.UserGroupCounts).Name("statistics:user-group-counts")
router.Get("/rule-group-counts/", s.RuleGroupCounts).Name("statistics:rule-group-counts")
})
}

type MessageGroupByDatetimeCount struct {
Datetime string `json:"datetime"`
Total int64 `json:"total"`
TotalMessages int64 `json:"total_messages"`
}

// DailyGroupCount 每日报警次数汇总
func (s *StatisticsController) DailyGroupCounts(ctx web.Context, groupRepo repository.MessageGroupRepo) ([]MessageGroupByDatetimeCount, error) {
timeoutCtx, _ := context.WithTimeout(ctx.Context(), 5*time.Second)
dailyCounts, err := groupRepo.StatByDatetimeCount(timeoutCtx, time.Now().Add(- 30*24*time.Hour), time.Now(), 24)
if err != nil {
return nil, err
}

if len(dailyCounts) == 0 {
return make([]MessageGroupByDatetimeCount, 0), nil
}

dailyCountsByDate := make(map[string]MessageGroupByDatetimeCount)
for _, d := range dailyCounts {
datetime := d.Datetime.Format("2006-01-02")
dailyCountsByDate[datetime] = MessageGroupByDatetimeCount{
Datetime: datetime,
Total: d.Total,
TotalMessages: d.TotalMessages,
}
}

startDate := dailyCounts[0].Datetime
endDate := dailyCounts[len(dailyCounts)-1].Datetime

log.Debugf("%v: %v", startDate, endDate)

results := make([]MessageGroupByDatetimeCount, 0)

for startDate.Before(endDate) || startDate.Equal(endDate) {
startDateF := startDate.Format("2006-01-02")
if d, ok := dailyCountsByDate[startDateF]; ok {
results = append(results, d)
} else {
results = append(results, MessageGroupByDatetimeCount{
Datetime: startDateF,
Total: 0,
TotalMessages: 0,
})
}

startDate = startDate.Add(24 * time.Hour)
}

return results, nil
}

// UserGroupCounts 用户报警次数汇总
func (s *StatisticsController) UserGroupCounts(ctx web.Context, groupRepo repository.MessageGroupRepo) ([]repository.MessageGroupByUserCount, error) {
timeoutCtx, _ := context.WithTimeout(ctx.Context(), 5*time.Second)
return groupRepo.StatByUserCount(timeoutCtx, time.Now().Add(- 30*24*time.Hour), time.Now())
}

// RuleGroupCounts 报警规则报警次数汇总
func (s *StatisticsController) RuleGroupCounts(ctx web.Context, groupRepo repository.MessageGroupRepo) ([]repository.MessageGroupByRuleCount, error) {
timeoutCtx, _ := context.WithTimeout(ctx.Context(), 5*time.Second)
return groupRepo.StatByRuleCount(timeoutCtx, time.Now().Add(- 30*24*time.Hour), time.Now())
}
1 change: 1 addition & 0 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func routers(cc container.Container) func(router *web.Router, mw web.RequestMidd
controller.NewTemplateController(cc),
controller.NewDingdingRobotController(cc),
controller.NewAgentController(cc),
controller.NewStatisticsController(cc),
)

router.WithMiddleware(mw.AccessLog(log.Module("api")), mw.CORS("*")).Controllers(
Expand Down
Loading

0 comments on commit 3466e66

Please sign in to comment.