2018-12-31 11:41:19 +00:00
|
|
|
package core
|
|
|
|
|
2018-12-31 21:36:58 +00:00
|
|
|
import (
|
|
|
|
"github.com/hunterlong/statping/types"
|
2019-02-20 02:11:40 +00:00
|
|
|
"sort"
|
2018-12-31 21:36:58 +00:00
|
|
|
"time"
|
|
|
|
)
|
2018-12-31 11:41:19 +00:00
|
|
|
|
2018-12-31 21:36:58 +00:00
|
|
|
type Group struct {
|
|
|
|
*types.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete will remove a group
|
|
|
|
func (g *Group) Delete() error {
|
2019-01-03 19:13:48 +00:00
|
|
|
for _, s := range g.Services() {
|
|
|
|
s.GroupId = 0
|
|
|
|
s.Update(false)
|
|
|
|
}
|
2019-02-04 20:13:09 +00:00
|
|
|
err := groupsDb().Delete(g)
|
2018-12-31 21:36:58 +00:00
|
|
|
return err.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create will create a group and insert it into the database
|
|
|
|
func (g *Group) Create() (int64, error) {
|
2020-01-04 02:03:59 +00:00
|
|
|
g.CreatedAt = time.Now().UTC()
|
2018-12-31 21:36:58 +00:00
|
|
|
db := groupsDb().Create(g)
|
|
|
|
return g.Id, db.Error
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:11:40 +00:00
|
|
|
// Update will update a group
|
|
|
|
func (g *Group) Update() (int64, error) {
|
2020-01-04 02:03:59 +00:00
|
|
|
g.UpdatedAt = time.Now().UTC()
|
2019-02-20 02:11:40 +00:00
|
|
|
db := groupsDb().Update(g)
|
|
|
|
return g.Id, db.Error
|
|
|
|
}
|
|
|
|
|
2019-01-03 19:13:48 +00:00
|
|
|
// Services returns all services belonging to a group
|
|
|
|
func (g *Group) Services() []*Service {
|
|
|
|
var services []*Service
|
|
|
|
for _, s := range Services() {
|
|
|
|
if s.Select().GroupId == int(g.Id) {
|
|
|
|
services = append(services, s.(*Service))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2019-04-29 18:11:44 +00:00
|
|
|
// VisibleServices returns all services based on authentication
|
|
|
|
func (g *Group) VisibleServices(auth bool) []*Service {
|
|
|
|
var services []*Service
|
|
|
|
for _, g := range g.Services() {
|
|
|
|
if !g.Public.Bool {
|
|
|
|
if auth {
|
|
|
|
services = append(services, g)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
services = append(services, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2018-12-31 21:36:58 +00:00
|
|
|
// SelectGroups returns all groups
|
2019-01-03 21:09:11 +00:00
|
|
|
func SelectGroups(includeAll bool, auth bool) []*Group {
|
2018-12-31 21:36:58 +00:00
|
|
|
var groups []*Group
|
2019-01-03 21:09:11 +00:00
|
|
|
var validGroups []*Group
|
2019-02-20 02:11:40 +00:00
|
|
|
groupsDb().Find(&groups).Order("order_id desc")
|
2019-01-03 21:09:11 +00:00
|
|
|
for _, g := range groups {
|
|
|
|
if !g.Public.Bool {
|
|
|
|
if auth {
|
|
|
|
validGroups = append(validGroups, g)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
validGroups = append(validGroups, g)
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 02:11:40 +00:00
|
|
|
sort.Sort(GroupOrder(validGroups))
|
2019-04-08 17:49:02 +00:00
|
|
|
if includeAll {
|
|
|
|
emptyGroup := &Group{&types.Group{Id: 0, Public: types.NewNullBool(true)}}
|
|
|
|
validGroups = append(validGroups, emptyGroup)
|
|
|
|
}
|
2019-01-03 21:09:11 +00:00
|
|
|
return validGroups
|
2018-12-31 21:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectGroup returns a *core.Group
|
|
|
|
func SelectGroup(id int64) *Group {
|
2019-02-20 02:11:40 +00:00
|
|
|
for _, g := range SelectGroups(true, true) {
|
2018-12-31 21:36:58 +00:00
|
|
|
if g.Id == id {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-12-31 11:41:19 +00:00
|
|
|
}
|
2019-02-20 02:11:40 +00:00
|
|
|
|
|
|
|
// GroupOrder will reorder the groups based on 'order_id' (Order)
|
|
|
|
type GroupOrder []*Group
|
|
|
|
|
|
|
|
// Sort interface for resorting the Groups in order
|
|
|
|
func (c GroupOrder) Len() int { return len(c) }
|
|
|
|
func (c GroupOrder) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
|
|
|
func (c GroupOrder) Less(i, j int) bool { return c[i].Order < c[j].Order }
|