80 lines
1.3 KiB
Go
80 lines
1.3 KiB
Go
package database
|
|
|
|
import (
|
|
"math"
|
|
|
|
"gitnet.fr/deblan/borgmatic-monitor/pkg/database/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Hosts() []model.Host {
|
|
var hosts []model.Host
|
|
|
|
GetDb().
|
|
Model(model.Host{}).
|
|
Preload("Infos", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("infos.id DESC")
|
|
}).
|
|
Order("name ASC").
|
|
Find(&hosts)
|
|
|
|
return hosts
|
|
}
|
|
|
|
func Host(id int) *model.Host {
|
|
var host model.Host
|
|
|
|
GetDb().
|
|
Model(model.Host{}).
|
|
Order("name ASC").
|
|
Where("id = ?", id).
|
|
First(&host)
|
|
|
|
if host.ID > 0 {
|
|
return &host
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func HostByName(label string) *model.Host {
|
|
var host model.Host
|
|
|
|
GetDb().
|
|
Model(model.Host{}).
|
|
Order("name ASC").
|
|
Where("name = ?", label).
|
|
First(&host)
|
|
|
|
if host.ID > 0 {
|
|
return &host
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func HostInfos(host *model.Host, page, itemPerPage int) *[]model.Info {
|
|
var infos []model.Info
|
|
|
|
GetDb().
|
|
Model(model.Info{}).
|
|
Order("infos.id DESC").
|
|
Offset((page-1)*itemPerPage).
|
|
Limit(itemPerPage).
|
|
Where("host_id = ?", host.ID).
|
|
Find(&infos)
|
|
|
|
return &infos
|
|
}
|
|
|
|
func HostInfosMaxPages(host *model.Host, itemPerPage int) int {
|
|
var count int64
|
|
|
|
GetDb().
|
|
Model(model.Info{}).
|
|
Order("infos.id DESC").
|
|
Where("host_id = ?", host.ID).
|
|
Count(&count)
|
|
|
|
return int(math.Ceil(float64(count) / float64(itemPerPage)))
|
|
}
|