db changes

pull/429/head
Hunter Long 2020-03-05 19:03:18 -08:00
parent e2c6bda6b4
commit 66ec613953
19 changed files with 113 additions and 91 deletions

View File

@ -127,7 +127,7 @@ print_details:
@echo \==== Monitoring and IDE ==== @echo \==== Monitoring and IDE ====
@echo \Grafana: http://localhost:3000 \(username: admin, password: admin\) @echo \Grafana: http://localhost:3000 \(username: admin, password: admin\)
build-all: xgo-install build-mac build-linux build-linux build-alpine build-all: xgo-install build-mac build-linux build-windows build-linux build-alpine compress
download-key: download-key:
wget -O statping.gpg $(KEY_URL) wget -O statping.gpg $(KEY_URL)

View File

@ -81,7 +81,7 @@ func prometheusHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
for _, ser := range services.All() { for _, ser := range services.AllInOrder() {
online := 1 online := 1
if !ser.Online { if !ser.Online {
online = 0 online = 0
@ -106,8 +106,22 @@ func prometheusHandler(w http.ResponseWriter, r *http.Request) {
for _, notif := range notifications.All() { for _, notif := range notifications.All() {
PrometheusComment(fmt.Sprintf("Notifier %s:", notif.Method)) PrometheusComment(fmt.Sprintf("Notifier %s:", notif.Method))
PrometheusExportKey("notifier_on_success", notif.Id, notif.Method, 0) enabled := 0
PrometheusExportKey("notifier_on_failure", notif.Id, notif.Method, 0) if notif.Enabled.Bool {
enabled = 1
}
PrometheusExportKey("notifier_enabled", notif.Id, notif.Method, enabled)
PrometheusExportKey("notifier_on_success", notif.Id, notif.Method, notif.Hits.OnSuccess)
PrometheusExportKey("notifier_on_failure", notif.Id, notif.Method, notif.Hits.OnFailure)
PrometheusExportKey("notifier_on_user_new", notif.Id, notif.Method, notif.Hits.OnNewUser)
PrometheusExportKey("notifier_on_user_update", notif.Id, notif.Method, notif.Hits.OnUpdatedUser)
PrometheusExportKey("notifier_on_user_delete", notif.Id, notif.Method, notif.Hits.OnDeletedUser)
PrometheusExportKey("notifier_on_service_new", notif.Id, notif.Method, notif.Hits.OnNewService)
PrometheusExportKey("notifier_on_service_update", notif.Id, notif.Method, notif.Hits.OnUpdatedService)
PrometheusExportKey("notifier_on_service_delete", notif.Id, notif.Method, notif.Hits.OnDeletedService)
PrometheusExportKey("notifier_on_notifier_new", notif.Id, notif.Method, notif.Hits.OnNewNotifier)
PrometheusExportKey("notifier_on_notifier_update", notif.Id, notif.Method, notif.Hits.OnUpdatedNotifier)
PrometheusExportKey("notifier_on_notifier_save", notif.Id, notif.Method, notif.Hits.OnSave)
} }
PrometheusComment("HTTP Metrics") PrometheusComment("HTTP Metrics")

View File

@ -189,8 +189,7 @@ func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
} }
func apiAllServicesHandler(r *http.Request) interface{} { func apiAllServicesHandler(r *http.Request) interface{} {
services := services.All() return services.AllInOrder()
return services
} }
func joinServices(srvss map[int64]*services.Service) []*services.Service { func joinServices(srvss map[int64]*services.Service) []*services.Service {

View File

@ -14,15 +14,15 @@ func DBhits() database.Database {
} }
func Find(id int64) (*Checkin, error) { func Find(id int64) (*Checkin, error) {
var checkin *Checkin var checkin Checkin
db := DB().Where("id = ?", id).Find(&checkin) db := DB().Where("id = ?", id).Find(&checkin)
return checkin, db.Error() return &checkin, db.Error()
} }
func FindByAPI(key string) (*Checkin, error) { func FindByAPI(key string) (*Checkin, error) {
var checkin *Checkin var checkin Checkin
db := DB().Where("api = ?", key).Find(&checkin) db := DB().Where("api = ?", key).Find(&checkin)
return checkin, db.Error() return &checkin, db.Error()
} }
func All() []*Checkin { func All() []*Checkin {
@ -33,7 +33,7 @@ func All() []*Checkin {
func (c *Checkin) Create() error { func (c *Checkin) Create() error {
c.ApiKey = utils.RandomString(7) c.ApiKey = utils.RandomString(7)
db := DB().Create(&c) db := DB().Create(c)
c.Start() c.Start()
go c.CheckinRoutine() go c.CheckinRoutine()
@ -41,12 +41,12 @@ func (c *Checkin) Create() error {
} }
func (c *Checkin) Update() error { func (c *Checkin) Update() error {
db := DB().Update(&c) db := DB().Update(c)
return db.Error() return db.Error()
} }
func (c *Checkin) Delete() error { func (c *Checkin) Delete() error {
c.Close() c.Close()
db := DB().Delete(&c) db := DB().Delete(c)
return db.Error() return db.Error()
} }

View File

@ -1,9 +1,9 @@
package checkins package checkins
func (c *Checkin) LastHit() *CheckinHit { func (c *Checkin) LastHit() *CheckinHit {
var hit *CheckinHit var hit CheckinHit
DBhits().Where("checkin = ?", c.Id).Limit(1).Find(&hit) DBhits().Where("checkin = ?", c.Id).Limit(1).Find(&hit)
return hit return &hit
} }
func (c *Checkin) Hits() []*CheckinHit { func (c *Checkin) Hits() []*CheckinHit {
@ -14,16 +14,16 @@ func (c *Checkin) Hits() []*CheckinHit {
} }
func (c *CheckinHit) Create() error { func (c *CheckinHit) Create() error {
db := DBhits().Create(&c) db := DBhits().Create(c)
return db.Error() return db.Error()
} }
func (c *CheckinHit) Update() error { func (c *CheckinHit) Update() error {
db := DBhits().Update(&c) db := DBhits().Update(c)
return db.Error() return db.Error()
} }
func (c *CheckinHit) Delete() error { func (c *CheckinHit) Delete() error {
db := DBhits().Delete(&c) db := DBhits().Delete(c)
return db.Error() return db.Error()
} }

View File

@ -30,7 +30,6 @@ func Select() (*Core, error) {
App = &c App = &c
App.UseCdn = null.NewNullBool(os.Getenv("USE_CDN") == "true") App.UseCdn = null.NewNullBool(os.Getenv("USE_CDN") == "true")
return App, db.Error() return App, db.Error()
} }
func (c *Core) Create() error { func (c *Core) Create() error {
@ -50,7 +49,7 @@ func (c *Core) Create() error {
} }
func (c *Core) Update() error { func (c *Core) Update() error {
db := DB().Update(&c) db := DB().Update(c)
return db.Error() return db.Error()
} }
@ -74,6 +73,6 @@ func Sample() error {
Footer: null.NewNullString(""), Footer: null.NewNullString(""),
} }
db := DB().Create(&core) db := DB().Create(core)
return db.Error() return db.Error()
} }

View File

@ -7,9 +7,9 @@ func DB() database.Database {
} }
func Find(id int64) (*Failure, error) { func Find(id int64) (*Failure, error) {
var failure *Failure var failure Failure
db := DB().Where("id = ?", id).Find(&failure) db := DB().Where("id = ?", id).Find(&failure)
return failure, db.Error() return &failure, db.Error()
} }
func All() []*Failure { func All() []*Failure {
@ -19,16 +19,16 @@ func All() []*Failure {
} }
func (f *Failure) Create() error { func (f *Failure) Create() error {
db := DB().Create(&f) db := DB().Create(f)
return db.Error() return db.Error()
} }
func (f *Failure) Update() error { func (f *Failure) Update() error {
db := DB().Update(&f) db := DB().Update(f)
return db.Error() return db.Error()
} }
func (f *Failure) Delete() error { func (f *Failure) Delete() error {
db := DB().Delete(&f) db := DB().Delete(f)
return db.Error() return db.Error()
} }

View File

@ -22,17 +22,17 @@ func All() []*Group {
} }
func (g *Group) Create() error { func (g *Group) Create() error {
db := DB().Create(&g) db := DB().Create(g)
return db.Error() return db.Error()
} }
func (g *Group) Update() error { func (g *Group) Update() error {
db := DB().Update(&g) db := DB().Update(g)
return db.Error() return db.Error()
} }
func (g *Group) Delete() error { func (g *Group) Delete() error {
db := DB().Delete(&g) db := DB().Delete(g)
return db.Error() return db.Error()
} }

View File

@ -12,9 +12,9 @@ func DB() database.Database {
} }
func Find(id int64) (*Hit, error) { func Find(id int64) (*Hit, error) {
var group *Hit var group Hit
db := DB().Where("id = ?", id).Find(&group) db := DB().Where("id = ?", id).Find(&group)
return group, db.Error() return &group, db.Error()
} }
func All() []*Hit { func All() []*Hit {
@ -24,16 +24,16 @@ func All() []*Hit {
} }
func (h *Hit) Create() error { func (h *Hit) Create() error {
db := DB().Create(&h) db := DB().Create(h)
return db.Error() return db.Error()
} }
func (h *Hit) Update() error { func (h *Hit) Update() error {
db := DB().Update(&h) db := DB().Update(h)
return db.Error() return db.Error()
} }
func (h *Hit) Delete() error { func (h *Hit) Delete() error {
db := DB().Delete(&h) db := DB().Delete(h)
return db.Error() return db.Error()
} }

View File

@ -3,9 +3,9 @@ package incidents
import "github.com/hunterlong/statping/database" import "github.com/hunterlong/statping/database"
func Find(id int64) (*Incident, error) { func Find(id int64) (*Incident, error) {
var incident *Incident var incident Incident
db := database.DB().Model(&Incident{}).Where("id = ?", id).Find(&incident) db := database.DB().Model(&Incident{}).Where("id = ?", id).Find(&incident)
return incident, db.Error() return &incident, db.Error()
} }
func All() []*Incident { func All() []*Incident {
@ -15,16 +15,16 @@ func All() []*Incident {
} }
func (i *Incident) Create() error { func (i *Incident) Create() error {
db := database.DB().Create(&i) db := database.DB().Create(i)
return db.Error() return db.Error()
} }
func (i *Incident) Update() error { func (i *Incident) Update() error {
db := database.DB().Update(&i) db := database.DB().Update(i)
return db.Error() return db.Error()
} }
func (i *Incident) Delete() error { func (i *Incident) Delete() error {
db := database.DB().Delete(&i) db := database.DB().Delete(i)
return db.Error() return db.Error()
} }

View File

@ -10,9 +10,9 @@ func DB() database.Database {
} }
func Find(name string) (*Integration, error) { func Find(name string) (*Integration, error) {
var integration *Integration var integration Integration
db := DB().Where("name = ?", name).Find(&integration) db := DB().Where("name = ?", name).Find(&integration)
return integration, db.Error() return &integration, db.Error()
} }
func All() []*Integration { func All() []*Integration {
@ -26,16 +26,16 @@ func List(i Integrator) ([]*services.Service, error) {
} }
func (i *Integration) Create() error { func (i *Integration) Create() error {
db := DB().Create(&i) db := DB().Create(i)
return db.Error() return db.Error()
} }
func (i *Integration) Update() error { func (i *Integration) Update() error {
db := DB().Update(&i) db := DB().Update(i)
return db.Error() return db.Error()
} }
func (i *Integration) Delete() error { func (i *Integration) Delete() error {
db := DB().Delete(&i) db := DB().Delete(i)
return db.Error() return db.Error()
} }

View File

@ -9,10 +9,10 @@ func DB() database.Database {
return database.DB().Model(&Notification{}) return database.DB().Model(&Notification{})
} }
func Find(method string) (Notifier, error) { func Find(name string) (Notifier, error) {
for _, n := range AllCommunications { for _, n := range AllCommunications {
notif := n.Select() notif := n.Select()
if notif.Method == method { if notif.Name() == name || notif.Method == name {
return n, nil return n, nil
} }
} }
@ -26,26 +26,21 @@ func All() []*Notification {
} }
func (n *Notification) Create() error { func (n *Notification) Create() error {
db := DB().FirstOrCreate(&n) db := DB().Create(n)
return db.Error() return db.Error()
} }
func Update(notifier Notifier) error { func (n *Notification) Update() error {
n := notifier.Select()
n.ResetQueue() n.ResetQueue()
err := n.Update()
if n.Enabled.Bool { if n.Enabled.Bool {
n.Close() n.Close()
n.Start() n.Start()
go Queue(notifier) go Queue(Notifier(n))
} else { } else {
n.Close() n.Close()
} }
return err err := DB().Update(n)
} return err.Error()
func (n *Notification) Update() error {
return Update(n)
} }
func (n *Notification) Delete() error { func (n *Notification) Delete() error {

View File

@ -60,6 +60,7 @@ sendMessages:
WithField("trigger", "OnFailure"). WithField("trigger", "OnFailure").
WithFields(utils.ToFields(notifier, s)).Debugln(fmt.Sprintf("Sending [OnFailure] '%v' notification for service %v", notifier.Method, s.Name)) WithFields(utils.ToFields(notifier, s)).Debugln(fmt.Sprintf("Sending [OnFailure] '%v' notification for service %v", notifier.Method, s.Name))
comm.(BasicEvents).OnFailure(s, f) comm.(BasicEvents).OnFailure(s, f)
comm.Select().Hits.OnFailure++
} }
} }
} }
@ -82,6 +83,7 @@ func OnSuccess(s *services.Service) {
WithField("trigger", "OnSuccess"). WithField("trigger", "OnSuccess").
WithFields(utils.ToFields(notifier, s)).Debugln(fmt.Sprintf("Sending [OnSuccess] '%v' notification for service %v", notifier.Method, s.Name)) WithFields(utils.ToFields(notifier, s)).Debugln(fmt.Sprintf("Sending [OnSuccess] '%v' notification for service %v", notifier.Method, s.Name))
comm.(BasicEvents).OnSuccess(s) comm.(BasicEvents).OnSuccess(s)
comm.Select().Hits.OnSuccess++
} }
} }
} }
@ -94,6 +96,7 @@ func OnNewService(s *services.Service) {
WithField("trigger", "OnNewService"). WithField("trigger", "OnNewService").
Debugln(fmt.Sprintf("Sending new service notification for service %v", s.Name)) Debugln(fmt.Sprintf("Sending new service notification for service %v", s.Name))
comm.(ServiceEvents).OnNewService(s) comm.(ServiceEvents).OnNewService(s)
comm.Select().Hits.OnNewService++
} }
} }
} }
@ -107,6 +110,7 @@ func OnUpdatedService(s *services.Service) {
if utils.IsType(comm, new(ServiceEvents)) && isEnabled(comm) && inLimits(comm) { if utils.IsType(comm, new(ServiceEvents)) && isEnabled(comm) && inLimits(comm) {
log.Debugln(fmt.Sprintf("Sending updated service notification for service %v", s.Name)) log.Debugln(fmt.Sprintf("Sending updated service notification for service %v", s.Name))
comm.(ServiceEvents).OnUpdatedService(s) comm.(ServiceEvents).OnUpdatedService(s)
comm.Select().Hits.OnUpdatedService++
} }
} }
} }
@ -120,6 +124,7 @@ func OnDeletedService(s *services.Service) {
if utils.IsType(comm, new(ServiceEvents)) && isEnabled(comm) && inLimits(comm) { if utils.IsType(comm, new(ServiceEvents)) && isEnabled(comm) && inLimits(comm) {
log.Debugln(fmt.Sprintf("Sending deleted service notification for service %v", s.Name)) log.Debugln(fmt.Sprintf("Sending deleted service notification for service %v", s.Name))
comm.(ServiceEvents).OnDeletedService(s) comm.(ServiceEvents).OnDeletedService(s)
comm.Select().Hits.OnDeletedService++
} }
} }
} }
@ -130,6 +135,7 @@ func OnNewUser(u *users.User) {
if utils.IsType(comm, new(UserEvents)) && isEnabled(comm) && inLimits(comm) { if utils.IsType(comm, new(UserEvents)) && isEnabled(comm) && inLimits(comm) {
log.Debugln(fmt.Sprintf("Sending new user notification for user %v", u.Username)) log.Debugln(fmt.Sprintf("Sending new user notification for user %v", u.Username))
comm.(UserEvents).OnNewUser(u) comm.(UserEvents).OnNewUser(u)
comm.Select().Hits.OnNewUser++
} }
} }
} }
@ -140,6 +146,7 @@ func OnUpdatedUser(u *users.User) {
if utils.IsType(comm, new(UserEvents)) && isEnabled(comm) && inLimits(comm) { if utils.IsType(comm, new(UserEvents)) && isEnabled(comm) && inLimits(comm) {
log.Debugln(fmt.Sprintf("Sending updated user notification for user %v", u.Username)) log.Debugln(fmt.Sprintf("Sending updated user notification for user %v", u.Username))
comm.(UserEvents).OnUpdatedUser(u) comm.(UserEvents).OnUpdatedUser(u)
comm.Select().Hits.OnUpdatedUser++
} }
} }
} }
@ -150,6 +157,7 @@ func OnDeletedUser(u *users.User) {
if utils.IsType(comm, new(UserEvents)) && isEnabled(comm) && inLimits(comm) { if utils.IsType(comm, new(UserEvents)) && isEnabled(comm) && inLimits(comm) {
log.Debugln(fmt.Sprintf("Sending deleted user notification for user %v", u.Username)) log.Debugln(fmt.Sprintf("Sending deleted user notification for user %v", u.Username))
comm.(UserEvents).OnDeletedUser(u) comm.(UserEvents).OnDeletedUser(u)
comm.Select().Hits.OnDeletedUser++
} }
} }
} }
@ -178,6 +186,7 @@ func OnNewNotifier(n *Notification) {
for _, comm := range AllCommunications { for _, comm := range AllCommunications {
if utils.IsType(comm, new(NotifierEvents)) && isEnabled(comm) && inLimits(comm) { if utils.IsType(comm, new(NotifierEvents)) && isEnabled(comm) && inLimits(comm) {
comm.(NotifierEvents).OnNewNotifier(n) comm.(NotifierEvents).OnNewNotifier(n)
comm.Select().Hits.OnNewNotifier++
} }
} }
} }
@ -188,6 +197,7 @@ func OnUpdatedNotifier(n *Notification) {
if utils.IsType(comm, new(NotifierEvents)) && isEnabled(comm) && inLimits(comm) { if utils.IsType(comm, new(NotifierEvents)) && isEnabled(comm) && inLimits(comm) {
log.Infoln(fmt.Sprintf("Sending updated notifier for %v", n.Id)) log.Infoln(fmt.Sprintf("Sending updated notifier for %v", n.Id))
comm.(NotifierEvents).OnUpdatedNotifier(n) comm.(NotifierEvents).OnUpdatedNotifier(n)
comm.Select().Hits.OnUpdatedNotifier++
} }
} }
} }

View File

@ -8,6 +8,12 @@ import (
"time" "time"
) )
func (n *Notification) Name() string {
newName := strings.ToLower(n.Method)
newName = strings.ReplaceAll(newName, " ", "_")
return newName
}
// AfterFind for Notification will set the timezone // AfterFind for Notification will set the timezone
func (n *Notification) AfterFind() (err error) { func (n *Notification) AfterFind() (err error) {
n.CreatedAt = utils.Now() n.CreatedAt = utils.Now()
@ -97,6 +103,7 @@ func Init(n Notifier) (*Notification, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error selecting notification") return nil, errors.Wrap(err, "error selecting notification")
} }
notify.CreatedAt = time.Now().UTC() notify.CreatedAt = time.Now().UTC()
notify.UpdatedAt = time.Now().UTC() notify.UpdatedAt = time.Now().UTC()
if notify.Delay.Seconds() == 0 { if notify.Delay.Seconds() == 0 {

View File

@ -68,8 +68,17 @@ type Notification struct {
} }
type notificationHits struct { type notificationHits struct {
onSuccess int64 `gorm:"-" json:"-"` OnSuccess int64 `gorm:"-" json:"-"`
onFailure int64 `gorm:"-" json:"-"` OnFailure int64 `gorm:"-" json:"-"`
OnSave int64 `gorm:"-" json:"-"`
OnNewService int64 `gorm:"-" json:"-"`
OnUpdatedService int64 `gorm:"-" json:"-"`
OnDeletedService int64 `gorm:"-" json:"-"`
OnNewUser int64 `gorm:"-" json:"-"`
OnUpdatedUser int64 `gorm:"-" json:"-"`
OnDeletedUser int64 `gorm:"-" json:"-"`
OnNewNotifier int64 `gorm:"-" json:"-"`
OnUpdatedNotifier int64 `gorm:"-" json:"-"`
} }
// QueueData is the struct for the messaging queue with service // QueueData is the struct for the messaging queue with service
@ -155,7 +164,7 @@ func reverseLogs(input []*NotificationLog) []*NotificationLog {
// SelectNotification returns the Notification struct from the database // SelectNotification returns the Notification struct from the database
func SelectNotification(n Notifier) (*Notification, error) { func SelectNotification(n Notifier) (*Notification, error) {
notifier := n.Select() notifier := n.Select()
err := DB().Where("method = ?", notifier.Method).Scan(&notifier) err := DB().Where("method = ?", notifier.Method).Find(&notifier)
return notifier, err.Error() return notifier, err.Error()
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/hunterlong/statping/database" "github.com/hunterlong/statping/database"
"github.com/hunterlong/statping/utils" "github.com/hunterlong/statping/utils"
"sort"
) )
var log = utils.Log var log = utils.Log
@ -31,8 +32,17 @@ func All() map[int64]*Service {
return allServices return allServices
} }
func AllInOrder() []Service {
var services []Service
for _, service := range allServices {
services = append(services, *service)
}
sort.Sort(ServiceOrder(services))
return services
}
func (s *Service) Create() error { func (s *Service) Create() error {
err := DB().Create(&s) err := DB().Create(s)
if err.Error() != nil { if err.Error() != nil {
log.Errorln(fmt.Sprintf("Failed to create service %v #%v: %v", s.Name, s.Id, err)) log.Errorln(fmt.Sprintf("Failed to create service %v #%v: %v", s.Name, s.Id, err))
return err.Error() return err.Error()
@ -40,14 +50,11 @@ func (s *Service) Create() error {
allServices[s.Id] = s allServices[s.Id] = s
go ServiceCheckQueue(allServices[s.Id], true) go ServiceCheckQueue(allServices[s.Id], true)
reorderServices()
//notifications.OnNewService(s)
return nil return nil
} }
func (s *Service) Update() error { func (s *Service) Update() error {
db := DB().Update(&s) db := DB().Update(s)
allServices[s.Id] = s allServices[s.Id] = s
@ -61,18 +68,16 @@ func (s *Service) Update() error {
s.SleepDuration = s.Duration() s.SleepDuration = s.Duration()
go ServiceCheckQueue(allServices[s.Id], true) go ServiceCheckQueue(allServices[s.Id], true)
reorderServices()
//notifier.OnUpdatedService(s.Service) //notifier.OnUpdatedService(s.Service)
return db.Error() return db.Error()
} }
func (s *Service) Delete() error { func (s *Service) Delete() error {
db := database.DB().Delete(&s) db := database.DB().Delete(s)
s.Close() s.Close()
delete(allServices, s.Id) delete(allServices, s.Id)
reorderServices()
//notifier.OnDeletedService(s.Service) //notifier.OnDeletedService(s.Service)
return db.Error() return db.Error()

View File

@ -7,7 +7,6 @@ import (
"github.com/hunterlong/statping/types/null" "github.com/hunterlong/statping/types/null"
"github.com/hunterlong/statping/utils" "github.com/hunterlong/statping/utils"
"net/url" "net/url"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -54,11 +53,6 @@ func (s Service) Hash() string {
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
// reorderServices will sort the services based on 'order_id'
func reorderServices() {
sort.Sort(ServiceOrder(allServices))
}
// SelectAllServices returns a slice of *core.Service to be store on []*core.Services // SelectAllServices returns a slice of *core.Service to be store on []*core.Services
// should only be called once on startup. // should only be called once on startup.
func SelectAllServices(start bool) (map[int64]*Service, error) { func SelectAllServices(start bool) (map[int64]*Service, error) {
@ -85,8 +79,6 @@ func SelectAllServices(start bool) (map[int64]*Service, error) {
s.UpdateStats() s.UpdateStats()
} }
reorderServices()
return allServices, nil return allServices, nil
} }

View File

@ -85,17 +85,9 @@ func (s *Service) BeforeCreate() (err error) {
} }
// ServiceOrder will reorder the services based on 'order_id' (Order) // ServiceOrder will reorder the services based on 'order_id' (Order)
type ServiceOrder map[int64]*Service type ServiceOrder []Service
// Sort interface for resroting the Services in order // Sort interface for resroting the Services in order
func (c ServiceOrder) Len() int { return len(c) } func (c ServiceOrder) Len() int { return len(c) }
func (c ServiceOrder) Swap(i, j int) { c[int64(i)], c[int64(j)] = c[int64(j)], c[int64(i)] } func (c ServiceOrder) Swap(i, j int) { c[int64(i)], c[int64(j)] = c[int64(j)], c[int64(i)] }
func (c ServiceOrder) Less(i, j int) bool { func (c ServiceOrder) Less(i, j int) bool { return c[i].Order < c[j].Order }
if c[int64(i)] == nil {
return false
}
if c[int64(j)] == nil {
return false
}
return c[int64(i)].Order < c[int64(j)].Order
}

View File

@ -12,9 +12,9 @@ func DB() database.Database {
} }
func Find(id int64) (*User, error) { func Find(id int64) (*User, error) {
var user *User var user User
db := DB().Where("id = ?", id).Find(user) db := DB().Where("id = ?", id).Find(user)
return user, db.Error() return &user, db.Error()
} }
func FindByUsername(username string) (*User, error) { func FindByUsername(username string) (*User, error) {
@ -38,7 +38,7 @@ func (u *User) Create() error {
u.ApiKey = utils.NewSHA1Hash(5) u.ApiKey = utils.NewSHA1Hash(5)
u.ApiSecret = utils.NewSHA1Hash(10) u.ApiSecret = utils.NewSHA1Hash(10)
db := DB().Create(&u) db := DB().Create(u)
return db.Error() return db.Error()
} }
@ -50,6 +50,6 @@ func (u *User) Update() error {
} }
func (u *User) Delete() error { func (u *User) Delete() error {
db := DB().Delete(&u) db := DB().Delete(u)
return db.Error() return db.Error()
} }