package models import "rocketgit.ru/rsmon/worker/app/models/concerns" // Group represents a monitor group. type Group struct { concerns.Model AccountID int64 `gorm:"type:bigint REFERENCES accounts(id)" json:"account_id"` Account *Account `json:"-"` Name string `json:"name" gorm:"not null"` IsSystem *bool `json:"is_system" gorm:"default:false"` MonitorsCount int `gorm:"-:all" json:"monitors_count"` Monitors []Monitor `json:"-"` Notifications []Notification `gorm:"many2many:notification_groups;" json:"-"` concerns.Timestamped `json:"-"` Audited } // SystemGroups returns all groups marked as system/internal (is_system=true). // These groups are converted to distributed monitors (system checks running // on the distributed worker pool). func SystemGroups() ([]Group, error) { var groups []Group err := DB().Where("is_system = ?", true).Find(&groups).Error return groups, err } // GroupIdsForAccountId returns all group IDs for the given account. func GroupIdsForAccountId(accountID int64) []int64 { //nolint:revive // accepted lint exception rows, err := DB().Raw("SELECT id FROM groups WHERE account_id = ?", accountID).Rows() if err != nil { panic(err) } defer rows.Close() //nolint:errcheck // accepted lint exception var cid int64 groupIDs := make([]int64, 0) for rows.Next() { rows.Scan(&cid) //nolint:errcheck // accepted lint exception groupIDs = append(groupIDs, cid) } return groupIDs } // CountGroups counts monitors per group. func CountGroups(groupIDs []int64, groupCount *map[int64]int) { //nolint:gocritic // ptrToRefParam: accepted pattern rows, err := DB().Raw("select group_id, count(id) from monitors where group_id IN (?) group by group_id ", groupIDs).Rows() if err != nil { panic(err) } defer rows.Close() //nolint:errcheck // accepted lint exception var gid int64 var count int for rows.Next() { rows.Scan(&gid, &count) //nolint:errcheck // accepted lint exception (*groupCount)[gid] = count } } // GroupsCounts fills MonitorsCount for each group. func GroupsCounts(groups *[]Group) { groupIDs := make([]int64, len(*groups)) groupCount := make(map[int64]int, len(*groups)) for i, g := range *groups { //nolint:gocritic // range copy is acceptable here groupIDs[i] = g.ID groupCount[g.ID] = 0 } CountGroups(groupIDs, &groupCount) for i, g := range *groups { //nolint:gocritic // range copy is acceptable here (*groups)[i].MonitorsCount = groupCount[g.ID] } }