mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 02:09:28 +08:00
refactor records package
This commit is contained in:
@@ -144,9 +144,10 @@ 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 []*models.Record) system.Stats {
|
||||||
var sum system.Stats
|
sum := system.Stats{
|
||||||
sum.Temperatures = make(map[string]float64)
|
Temperatures: make(map[string]float64),
|
||||||
sum.ExtraFs = make(map[string]*system.FsStats)
|
ExtraFs: make(map[string]*system.FsStats),
|
||||||
|
}
|
||||||
|
|
||||||
count := float64(len(records))
|
count := float64(len(records))
|
||||||
// use different counter for temps in case some records don't have them
|
// use different counter for temps in case some records don't have them
|
||||||
@@ -233,7 +234,7 @@ 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) (stats []container.Stats) {
|
func (rm *RecordManager) AverageContainerStats(records []*models.Record) []container.Stats {
|
||||||
sums := make(map[string]*container.Stats)
|
sums := make(map[string]*container.Stats)
|
||||||
count := float64(len(records))
|
count := float64(len(records))
|
||||||
|
|
||||||
@@ -242,7 +243,7 @@ func (rm *RecordManager) AverageContainerStats(records []*models.Record) (stats
|
|||||||
record.UnmarshalJSONField("stats", &containerStats)
|
record.UnmarshalJSONField("stats", &containerStats)
|
||||||
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, Cpu: 0, Mem: 0}
|
sums[stat.Name] = &container.Stats{Name: stat.Name}
|
||||||
}
|
}
|
||||||
sums[stat.Name].Cpu += stat.Cpu
|
sums[stat.Name].Cpu += stat.Cpu
|
||||||
sums[stat.Name].Mem += stat.Mem
|
sums[stat.Name].Mem += stat.Mem
|
||||||
@@ -251,8 +252,9 @@ func (rm *RecordManager) AverageContainerStats(records []*models.Record) (stats
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result := make([]container.Stats, 0, len(sums))
|
||||||
for _, value := range sums {
|
for _, value := range sums {
|
||||||
stats = append(stats, container.Stats{
|
result = append(result, container.Stats{
|
||||||
Name: value.Name,
|
Name: value.Name,
|
||||||
Cpu: twoDecimals(value.Cpu / count),
|
Cpu: twoDecimals(value.Cpu / count),
|
||||||
Mem: twoDecimals(value.Mem / count),
|
Mem: twoDecimals(value.Mem / count),
|
||||||
@@ -260,7 +262,7 @@ func (rm *RecordManager) AverageContainerStats(records []*models.Record) (stats
|
|||||||
NetworkRecv: twoDecimals(value.NetworkRecv / count),
|
NetworkRecv: twoDecimals(value.NetworkRecv / count),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return stats
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes records older than what is displayed in the UI
|
// Deletes records older than what is displayed in the UI
|
||||||
@@ -289,16 +291,16 @@ func (rm *RecordManager) DeleteOldRecords() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
db := rm.app.Dao().NonconcurrentDB()
|
db := rm.app.Dao().NonconcurrentDB()
|
||||||
for _, recordData := range recordData {
|
for _, recordData := range recordData {
|
||||||
for _, collectionSlug := range collections {
|
for _, collectionSlug := range collections {
|
||||||
formattedDate := time.Now().UTC().Add(-recordData.retention).Format(types.DefaultDateLayout)
|
formattedDate := time.Now().UTC().Add(-recordData.retention).Format(types.DefaultDateLayout)
|
||||||
expr := dbx.NewExp("[[created]] < {:date} AND [[type]] = {:type}", dbx.Params{"date": formattedDate, "type": recordData.recordType})
|
expr := dbx.NewExp("[[created]] < {:date} AND [[type]] = {:type}", dbx.Params{"date": formattedDate, "type": recordData.recordType})
|
||||||
_, err := db.Delete(collectionSlug, expr).Execute()
|
_, err := db.Delete(collectionSlug, expr).Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rm.app.Logger().Error("Failed to delete records", "err", err.Error())
|
rm.app.Logger().Error("Failed to delete records", "err", err.Error())
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Round float to two decimals */
|
/* Round float to two decimals */
|
||||||
|
Reference in New Issue
Block a user