mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 10:19:27 +08:00
small longer record creation refactoring
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
"github.com/pocketbase/dbx"
|
"github.com/pocketbase/dbx"
|
||||||
"github.com/pocketbase/pocketbase"
|
"github.com/pocketbase/pocketbase"
|
||||||
"github.com/pocketbase/pocketbase/daos"
|
"github.com/pocketbase/pocketbase/daos"
|
||||||
@@ -31,6 +32,10 @@ type RecordDeletionData struct {
|
|||||||
retention time.Duration
|
retention time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RecordStats []*struct {
|
||||||
|
Stats []byte `db:"stats"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewRecordManager(app *pocketbase.PocketBase) *RecordManager {
|
func NewRecordManager(app *pocketbase.PocketBase) *RecordManager {
|
||||||
return &RecordManager{app}
|
return &RecordManager{app}
|
||||||
}
|
}
|
||||||
@@ -73,6 +78,7 @@ func (rm *RecordManager) CreateLongerRecords() {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// need *models.Collection to create a new record with models.NewRecord
|
||||||
collections := map[string]*models.Collection{}
|
collections := map[string]*models.Collection{}
|
||||||
for _, collectionName := range []string{"system_stats", "container_stats"} {
|
for _, collectionName := range []string{"system_stats", "container_stats"} {
|
||||||
collection, _ := txDao.FindCollectionByNameOrId(collectionName)
|
collection, _ := txDao.FindCollectionByNameOrId(collectionName)
|
||||||
@@ -104,16 +110,31 @@ func (rm *RecordManager) CreateLongerRecords() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// get shorter records from the past x minutes
|
// get shorter records from the past x minutes
|
||||||
allShorterRecords, err := txDao.FindRecordsByExpr(
|
var stats RecordStats
|
||||||
collection.Id,
|
|
||||||
dbx.NewExp(
|
// allShorterRecords, err := txDao.FindRecordsByExpr(
|
||||||
|
// collection,
|
||||||
|
// dbx.NewExp(
|
||||||
|
// "type = {:type} AND system = {:system} AND created > {:created}",
|
||||||
|
// dbx.Params{"type": recordData.shorterType, "system": system.Id, "created": shorterRecordPeriod},
|
||||||
|
// ),
|
||||||
|
// )
|
||||||
|
|
||||||
|
err := txDao.DB().
|
||||||
|
Select("stats").
|
||||||
|
From(collection.Name).
|
||||||
|
AndWhere(dbx.NewExp(
|
||||||
"type={:type} AND system={:system} AND created > {:created}",
|
"type={:type} AND system={:system} AND created > {:created}",
|
||||||
dbx.Params{"type": recordData.shorterType, "system": system.Id, "created": shorterRecordPeriod},
|
dbx.Params{
|
||||||
),
|
"type": recordData.shorterType,
|
||||||
)
|
"system": system.Id,
|
||||||
|
"created": shorterRecordPeriod,
|
||||||
|
},
|
||||||
|
)).
|
||||||
|
All(&stats)
|
||||||
|
|
||||||
// continue if not enough shorter records
|
// continue if not enough shorter records
|
||||||
if err != nil || len(allShorterRecords) < recordData.minShorterRecords {
|
if err != nil || len(stats) < recordData.minShorterRecords {
|
||||||
// log.Println("not enough shorter records. continue.", len(allShorterRecords), recordData.expectedShorterRecords)
|
// log.Println("not enough shorter records. continue.", len(allShorterRecords), recordData.expectedShorterRecords)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -123,9 +144,9 @@ func (rm *RecordManager) CreateLongerRecords() {
|
|||||||
longerRecord.Set("type", recordData.longerType)
|
longerRecord.Set("type", recordData.longerType)
|
||||||
switch collection.Name {
|
switch collection.Name {
|
||||||
case "system_stats":
|
case "system_stats":
|
||||||
longerRecord.Set("stats", rm.AverageSystemStats(allShorterRecords))
|
longerRecord.Set("stats", rm.AverageSystemStats(stats))
|
||||||
case "container_stats":
|
case "container_stats":
|
||||||
longerRecord.Set("stats", rm.AverageContainerStats(allShorterRecords))
|
longerRecord.Set("stats", rm.AverageContainerStats(stats))
|
||||||
}
|
}
|
||||||
if err := txDao.SaveRecord(longerRecord); err != nil {
|
if err := txDao.SaveRecord(longerRecord); err != nil {
|
||||||
log.Println("failed to save longer record", "err", err.Error())
|
log.Println("failed to save longer record", "err", err.Error())
|
||||||
@@ -141,7 +162,7 @@ func (rm *RecordManager) CreateLongerRecords() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the average stats of a list of system_stats records without reflect
|
// Calculate the average stats of a list of system_stats records without reflect
|
||||||
func (rm *RecordManager) AverageSystemStats(records []*models.Record) system.Stats {
|
func (rm *RecordManager) AverageSystemStats(records RecordStats) system.Stats {
|
||||||
sum := system.Stats{
|
sum := system.Stats{
|
||||||
Temperatures: make(map[string]float64),
|
Temperatures: make(map[string]float64),
|
||||||
ExtraFs: make(map[string]*system.FsStats),
|
ExtraFs: make(map[string]*system.FsStats),
|
||||||
@@ -153,7 +174,7 @@ func (rm *RecordManager) AverageSystemStats(records []*models.Record) system.Sta
|
|||||||
|
|
||||||
var stats system.Stats
|
var stats system.Stats
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
record.UnmarshalJSONField("stats", &stats)
|
json.Unmarshal(record.Stats, &stats)
|
||||||
sum.Cpu += stats.Cpu
|
sum.Cpu += stats.Cpu
|
||||||
sum.Mem += stats.Mem
|
sum.Mem += stats.Mem
|
||||||
sum.MemUsed += stats.MemUsed
|
sum.MemUsed += stats.MemUsed
|
||||||
@@ -226,14 +247,14 @@ func (rm *RecordManager) AverageSystemStats(records []*models.Record) system.Sta
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(sum.Temperatures) != 0 {
|
if len(sum.Temperatures) != 0 {
|
||||||
stats.Temperatures = make(map[string]float64)
|
stats.Temperatures = make(map[string]float64, len(sum.Temperatures))
|
||||||
for key, value := range sum.Temperatures {
|
for key, value := range sum.Temperatures {
|
||||||
stats.Temperatures[key] = twoDecimals(value / tempCount)
|
stats.Temperatures[key] = twoDecimals(value / tempCount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(sum.ExtraFs) != 0 {
|
if len(sum.ExtraFs) != 0 {
|
||||||
stats.ExtraFs = make(map[string]*system.FsStats)
|
stats.ExtraFs = make(map[string]*system.FsStats, len(sum.ExtraFs))
|
||||||
for key, value := range sum.ExtraFs {
|
for key, value := range sum.ExtraFs {
|
||||||
stats.ExtraFs[key] = &system.FsStats{
|
stats.ExtraFs[key] = &system.FsStats{
|
||||||
DiskTotal: twoDecimals(value.DiskTotal / count),
|
DiskTotal: twoDecimals(value.DiskTotal / count),
|
||||||
@@ -250,13 +271,17 @@ func (rm *RecordManager) AverageSystemStats(records []*models.Record) system.Sta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the average stats of a list of container_stats records
|
// Calculate the average stats of a list of container_stats records
|
||||||
func (rm *RecordManager) AverageContainerStats(records []*models.Record) []container.Stats {
|
func (rm *RecordManager) AverageContainerStats(records RecordStats) []container.Stats {
|
||||||
sums := make(map[string]*container.Stats)
|
sums := make(map[string]*container.Stats)
|
||||||
count := float64(len(records))
|
count := float64(len(records))
|
||||||
|
|
||||||
var containerStats []container.Stats
|
var containerStats []container.Stats
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
record.UnmarshalJSONField("stats", &containerStats)
|
// Reset the slice length to 0, but keep the capacity
|
||||||
|
containerStats = containerStats[:0]
|
||||||
|
if err := json.Unmarshal(record.Stats, &containerStats); err != nil {
|
||||||
|
return []container.Stats{}
|
||||||
|
}
|
||||||
for _, stat := range containerStats {
|
for _, stat := range containerStats {
|
||||||
if _, ok := sums[stat.Name]; !ok {
|
if _, ok := sums[stat.Name]; !ok {
|
||||||
sums[stat.Name] = &container.Stats{Name: stat.Name}
|
sums[stat.Name] = &container.Stats{Name: stat.Name}
|
||||||
|
Reference in New Issue
Block a user