diff --git a/src/models/team.go b/src/models/team.go index bc2d59e6..c28f715a 100644 --- a/src/models/team.go +++ b/src/models/team.go @@ -314,6 +314,12 @@ func TeamGets(query string, limit, offset int) ([]Team, error) { return objs, err } +func AllTeams() ([]Team, error) { + var teams []Team + err := DB["rdb"].Find(&teams) + return teams, err +} + func TeamGetsInIds(ids []int64, query string, limit, offset int) ([]Team, error) { session := DB["rdb"].In("id", ids).Limit(limit, offset).OrderBy("ident") if query != "" { diff --git a/src/models/user.go b/src/models/user.go index 97655d2c..1efae93e 100644 --- a/src/models/user.go +++ b/src/models/user.go @@ -3,7 +3,6 @@ package models import ( "fmt" "log" - "strconv" "strings" "time" @@ -680,29 +679,6 @@ func (u *User) NopriResIdents(resIds []int64, op string) ([]string, error) { return ResourceIdentsByIds(nopris) } -func GetUsersNameByIds(ids string) ([]string, error) { - var names []string - ids = strings.Replace(ids, "[", "", -1) - ids = strings.Replace(ids, "]", "", -1) - idsStrArr := strings.Split(ids, ",") - - userIds := []int64{} - for _, userId := range idsStrArr { - id, _ := strconv.ParseInt(userId, 10, 64) - userIds = append(userIds, id) - } - - users, err := UserGetByIds(userIds) - - if err != nil { - return names, err - } - for _, user := range users { - names = append(names, user.Username) - } - return names, err -} - func UsersGet(where string, args ...interface{}) ([]User, error) { var objs []User err := DB["rdb"].Where(where, args...).Find(&objs) diff --git a/src/modules/server/cache/init.go b/src/modules/server/cache/init.go index fba29930..81cbcdcd 100644 --- a/src/modules/server/cache/init.go +++ b/src/modules/server/cache/init.go @@ -27,6 +27,11 @@ func Init(regions []string) { ApiCollectCache = NewApiCollectCache() SnmpCollectCache = NewSnmpCollectCache() SnmpHWCache = NewSnmpHWCache() + TeamUsersCache = NewTeamUsersCache() + TeamCache = NewTeamCache() + UserCache = NewUserCache() + TreeNodeCache = NewTreeNodeCache() + LoadMetrics() go InitJudgeHashRing() diff --git a/src/modules/server/cache/monapi_team.go b/src/modules/server/cache/monapi_team_user.go similarity index 86% rename from src/modules/server/cache/monapi_team.go rename to src/modules/server/cache/monapi_team_user.go index df9d6725..f6881504 100644 --- a/src/modules/server/cache/monapi_team.go +++ b/src/modules/server/cache/monapi_team_user.go @@ -11,7 +11,7 @@ type TeamUsersMap struct { var TeamUsersCache *TeamUsersMap -func NewTeamCache() *TeamUsersMap { +func NewTeamUsersCache() *TeamUsersMap { return &TeamUsersMap{Data: make(map[int64][]int64)} } @@ -27,9 +27,13 @@ func (s *TeamUsersMap) GetByTeamIds(ids []int64) []int64 { defer s.RUnlock() m := make(map[int64]struct{}) var userIds []int64 + for _, id := range ids { - m[id] = struct{}{} + for _, uid := range s.Data[id] { + m[uid] = struct{}{} + } } + for id, _ := range m { userIds = append(userIds, id) } diff --git a/src/modules/server/cache/monapi_teams.go b/src/modules/server/cache/monapi_teams.go new file mode 100644 index 00000000..8d22584d --- /dev/null +++ b/src/modules/server/cache/monapi_teams.go @@ -0,0 +1,69 @@ +package cache + +import ( + "strconv" + "strings" + "sync" + + "github.com/didi/nightingale/v4/src/models" +) + +type TeamMap struct { + sync.RWMutex + Data map[int64]*models.Team +} + +var TeamCache *TeamMap + +func NewTeamCache() *TeamMap { + return &TeamMap{Data: make(map[int64]*models.Team)} +} + +func (s *TeamMap) GetBy(id int64) *models.Team { + s.RLock() + defer s.RUnlock() + + return s.Data[id] +} + +func (s *TeamMap) GetByIds(ids []int64) []*models.Team { + s.RLock() + defer s.RUnlock() + var objs []*models.Team + for _, id := range ids { + if s.Data[id] == nil { + continue + } + objs = append(objs, s.Data[id]) + } + + return objs +} + +func (s *TeamMap) SetAll(objs map[int64]*models.Team) { + s.Lock() + defer s.Unlock() + + s.Data = objs + return +} + +func (s *TeamMap) GetTeamNamesByIds(ids string) []string { + var names []string + ids = strings.Replace(ids, "[", "", -1) + ids = strings.Replace(ids, "]", "", -1) + idsStrArr := strings.Split(ids, ",") + + teamIds := []int64{} + for _, teamId := range idsStrArr { + id, _ := strconv.ParseInt(teamId, 10, 64) + teamIds = append(teamIds, id) + } + + objs := s.GetByIds(teamIds) + for _, obj := range objs { + names = append(names, obj.Name) + } + + return names +} diff --git a/src/modules/server/cache/monapi_users.go b/src/modules/server/cache/monapi_users.go index 3cbf5255..d08b4114 100644 --- a/src/modules/server/cache/monapi_users.go +++ b/src/modules/server/cache/monapi_users.go @@ -1,6 +1,8 @@ package cache import ( + "strconv" + "strings" "sync" "github.com/didi/nightingale/v4/src/models" @@ -29,6 +31,9 @@ func (s *UserMap) GetByIds(ids []int64) []*models.User { defer s.RUnlock() var users []*models.User for _, id := range ids { + if s.Data[id] == nil { + continue + } users = append(users, s.Data[id]) } @@ -42,3 +47,23 @@ func (s *UserMap) SetAll(users map[int64]*models.User) { s.Data = users return } + +func (s *UserMap) GetUsernamesByIds(ids string) []string { + var names []string + ids = strings.Replace(ids, "[", "", -1) + ids = strings.Replace(ids, "]", "", -1) + idsStrArr := strings.Split(ids, ",") + + userIds := []int64{} + for _, userId := range idsStrArr { + id, _ := strconv.ParseInt(userId, 10, 64) + userIds = append(userIds, id) + } + + users := s.GetByIds(userIds) + for _, user := range users { + names = append(names, user.Username) + } + + return names +} diff --git a/src/modules/server/cache/node.go b/src/modules/server/cache/node.go new file mode 100644 index 00000000..182c63e5 --- /dev/null +++ b/src/modules/server/cache/node.go @@ -0,0 +1,44 @@ +package cache + +import ( + "sync" + + "github.com/didi/nightingale/v4/src/models" +) + +type TreeNodeMap struct { + sync.RWMutex + Data map[int64]*models.Node +} + +var TreeNodeCache *TreeNodeMap + +func NewTreeNodeCache() *TreeNodeMap { + return &TreeNodeMap{Data: make(map[int64]*models.Node)} +} + +func (t *TreeNodeMap) GetBy(id int64) *models.Node { + t.RLock() + defer t.RUnlock() + + return t.Data[id] +} + +func (t *TreeNodeMap) GetByIds(ids []int64) []*models.Node { + t.RLock() + defer t.RUnlock() + var objs []*models.Node + for _, id := range ids { + objs = append(objs, t.Data[id]) + } + + return objs +} + +func (t *TreeNodeMap) SetAll(objs map[int64]*models.Node) { + t.Lock() + defer t.Unlock() + + t.Data = objs + return +} diff --git a/src/modules/server/cron/init.go b/src/modules/server/cron/init.go index 5f6371bf..b1ca35fb 100644 --- a/src/modules/server/cron/init.go +++ b/src/modules/server/cron/init.go @@ -11,6 +11,7 @@ func Init() { go CleanStraLoop() go SyncCollects() go SyncUsers() + go SyncTeams() go SyncTeamUsers() go CleanCollectLoop() diff --git a/src/modules/server/cron/monapi_team.go b/src/modules/server/cron/monapi_team.go index f0e76745..04b2df78 100644 --- a/src/modules/server/cron/monapi_team.go +++ b/src/modules/server/cron/monapi_team.go @@ -8,31 +8,28 @@ import ( "github.com/toolkits/pkg/logger" ) -func SyncTeamUsers() { +func SyncTeams() { t1 := time.NewTicker(time.Duration(cache.CHECK_INTERVAL) * time.Second) - syncTeamUsers() + syncTeam() logger.Info("[cron] sync team start...") for { <-t1.C - syncTeamUsers() + syncTeam() } } -func syncTeamUsers() { - teamUsers, err := models.TeamUsers() - +func syncTeam() { + teams, err := models.AllTeams() if err != nil { - logger.Warningf("get Teams err:%v %v", err) + logger.Warningf("get teams err:%v %v", err) + return } - teamUsersMap := make(map[int64][]int64) - for _, teamUser := range teamUsers { - if _, exists := teamUsersMap[teamUser.TeamId]; exists { - teamUsersMap[teamUser.TeamId] = append(teamUsersMap[teamUser.TeamId], teamUser.UserId) - } else { - teamUsersMap[teamUser.TeamId] = []int64{teamUser.UserId} - } + teamsMap := make(map[int64]*models.Team) + for _, team := range teams { + teamsMap[team.Id] = &team } - cache.TeamUsersCache.SetAll(teamUsersMap) + + cache.TeamCache.SetAll(teamsMap) } diff --git a/src/modules/server/cron/monapi_team_user.go b/src/modules/server/cron/monapi_team_user.go new file mode 100644 index 00000000..f0e76745 --- /dev/null +++ b/src/modules/server/cron/monapi_team_user.go @@ -0,0 +1,38 @@ +package cron + +import ( + "time" + + "github.com/didi/nightingale/v4/src/models" + "github.com/didi/nightingale/v4/src/modules/server/cache" + "github.com/toolkits/pkg/logger" +) + +func SyncTeamUsers() { + t1 := time.NewTicker(time.Duration(cache.CHECK_INTERVAL) * time.Second) + + syncTeamUsers() + logger.Info("[cron] sync team start...") + for { + <-t1.C + syncTeamUsers() + } +} + +func syncTeamUsers() { + teamUsers, err := models.TeamUsers() + + if err != nil { + logger.Warningf("get Teams err:%v %v", err) + } + + teamUsersMap := make(map[int64][]int64) + for _, teamUser := range teamUsers { + if _, exists := teamUsersMap[teamUser.TeamId]; exists { + teamUsersMap[teamUser.TeamId] = append(teamUsersMap[teamUser.TeamId], teamUser.UserId) + } else { + teamUsersMap[teamUser.TeamId] = []int64{teamUser.UserId} + } + } + cache.TeamUsersCache.SetAll(teamUsersMap) +} diff --git a/src/modules/server/cron/monapi_user.go b/src/modules/server/cron/monapi_user.go index a65ad870..aadd7517 100644 --- a/src/modules/server/cron/monapi_user.go +++ b/src/modules/server/cron/monapi_user.go @@ -21,9 +21,9 @@ func SyncUsers() { func syncUsers() { users, err := models.AllUsers() - if err != nil { logger.Warningf("get users err:%v %v", err) + return } usersMap := make(map[int64]*models.User) diff --git a/src/modules/server/http/router_event.go b/src/modules/server/http/router_event.go index a26aa827..0068604d 100644 --- a/src/modules/server/http/router_event.go +++ b/src/modules/server/http/router_event.go @@ -7,6 +7,7 @@ import ( "time" "github.com/didi/nightingale/v4/src/models" + "github.com/didi/nightingale/v4/src/modules/server/cache" "github.com/gin-gonic/gin" "github.com/toolkits/pkg/errors" @@ -84,14 +85,11 @@ func eventCurGets(c *gin.Context) { datList := []eventData{} for i := 0; i < len(events); i++ { - users, err := models.GetUsersNameByIds(events[i].Users) - errors.Dangerous(err) + users := cache.UserCache.GetUsernamesByIds(events[i].Users) - groups, err := models.GetTeamsNameByIds(events[i].Groups) - errors.Dangerous(err) + groups := cache.TeamCache.GetTeamNamesByIds(events[i].Groups) - claimants, err := models.GetUsersNameByIds(events[i].Claimants) - errors.Dangerous(err) + claimants := cache.UserCache.GetUsernamesByIds(events[i].Claimants) var detail []models.EventDetail err = json.Unmarshal([]byte(events[i].Detail), &detail) @@ -110,11 +108,9 @@ func eventCurGets(c *gin.Context) { alertUpgrade, err := models.EventAlertUpgradeUnMarshal(events[i].AlertUpgrade) errors.Dangerous(err) - alertUsers, err := models.GetUsersNameByIds(alertUpgrade.Users) - errors.Dangerous(err) + alertUsers := cache.UserCache.GetUsernamesByIds(alertUpgrade.Users) - alertGroups, err := models.GetTeamsNameByIds(alertUpgrade.Groups) - errors.Dangerous(err) + alertGroups := cache.TeamCache.GetTeamNamesByIds(alertUpgrade.Groups) dat := eventData{ Id: events[i].Id, @@ -195,11 +191,9 @@ func eventHisGets(c *gin.Context) { datList := []eventData{} for i := 0; i < len(events); i++ { - users, err := models.GetUsersNameByIds(events[i].Users) - errors.Dangerous(err) + users := cache.UserCache.GetUsernamesByIds(events[i].Users) - groups, err := models.GetTeamsNameByIds(events[i].Groups) - errors.Dangerous(err) + groups := cache.TeamCache.GetTeamNamesByIds(events[i].Groups) var detail []models.EventDetail err = json.Unmarshal([]byte(events[i].Detail), &detail) @@ -218,11 +212,9 @@ func eventHisGets(c *gin.Context) { alertUpgrade, err := models.EventAlertUpgradeUnMarshal(events[i].AlertUpgrade) errors.Dangerous(err) - alertUsers, err := models.GetUsersNameByIds(alertUpgrade.Users) - errors.Dangerous(err) + alertUsers := cache.UserCache.GetUsernamesByIds(alertUpgrade.Users) - alertGroups, err := models.GetTeamsNameByIds(alertUpgrade.Groups) - errors.Dangerous(err) + alertGroups := cache.TeamCache.GetTeamNamesByIds(alertUpgrade.Groups) dat := eventData{ Id: events[i].Id, @@ -274,14 +266,12 @@ func eventHisGetById(c *gin.Context) { event := mustEvent(urlParamInt64(c, "id")) - users, err := models.GetUsersNameByIds(event.Users) - errors.Dangerous(err) + users := cache.UserCache.GetUsernamesByIds(event.Users) - groups, err := models.GetTeamsNameByIds(event.Groups) - errors.Dangerous(err) + groups := cache.TeamCache.GetTeamNamesByIds(event.Groups) var detail []models.EventDetail - err = json.Unmarshal([]byte(event.Detail), &detail) + err := json.Unmarshal([]byte(event.Detail), &detail) errors.Dangerous(err) tagsList := []string{} @@ -294,11 +284,9 @@ func eventHisGetById(c *gin.Context) { alertUpgrade, err := models.EventAlertUpgradeUnMarshal(event.AlertUpgrade) errors.Dangerous(err) - alertUsers, err := models.GetUsersNameByIds(alertUpgrade.Users) - errors.Dangerous(err) + alertUsers := cache.UserCache.GetUsernamesByIds(alertUpgrade.Users) - alertGroups, err := models.GetTeamsNameByIds(alertUpgrade.Groups) - errors.Dangerous(err) + alertGroups := cache.TeamCache.GetTeamNamesByIds(alertUpgrade.Groups) dat := eventData{ Id: event.Id, @@ -339,17 +327,14 @@ func eventCurGetById(c *gin.Context) { eventCur := mustEventCur(urlParamInt64(c, "id")) - users, err := models.GetUsersNameByIds(eventCur.Users) - errors.Dangerous(err) + users := cache.UserCache.GetUsernamesByIds(eventCur.Users) - groups, err := models.GetTeamsNameByIds(eventCur.Groups) - errors.Dangerous(err) + groups := cache.TeamCache.GetTeamNamesByIds(eventCur.Groups) - claimants, err := models.GetUsersNameByIds(eventCur.Claimants) - errors.Dangerous(err) + claimants := cache.UserCache.GetUsernamesByIds(eventCur.Claimants) var detail []models.EventDetail - err = json.Unmarshal([]byte(eventCur.Detail), &detail) + err := json.Unmarshal([]byte(eventCur.Detail), &detail) errors.Dangerous(err) tagsList := []string{} @@ -362,10 +347,9 @@ func eventCurGetById(c *gin.Context) { alertUpgrade, err := models.EventAlertUpgradeUnMarshal(eventCur.AlertUpgrade) errors.Dangerous(err) - alertUsers, err := models.GetUsersNameByIds(alertUpgrade.Users) - errors.Dangerous(err) + alertUsers := cache.UserCache.GetUsernamesByIds(alertUpgrade.Users) - alertGroups, err := models.GetTeamsNameByIds(alertUpgrade.Groups) + alertGroups := cache.TeamCache.GetTeamNamesByIds(alertUpgrade.Groups) dat := eventData{ Id: eventCur.Id,