mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 17:59:28 +08:00
create longer container_stats records
This commit is contained in:
76
records.go
76
records.go
@@ -66,12 +66,18 @@ func createLongerRecords(collectionName string, shorterRecord *models.Record) {
|
|||||||
// log.Println("not enough shorter records. returning")
|
// log.Println("not enough shorter records. returning")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// average the shorter records and create 10m record
|
// average the shorter records and create longer record
|
||||||
averagedStats := averageSystemStats(allShorterRecords)
|
var stats interface{}
|
||||||
|
switch collectionName {
|
||||||
|
case "system_stats":
|
||||||
|
stats = averageSystemStats(allShorterRecords)
|
||||||
|
case "container_stats":
|
||||||
|
stats = averageContainerStats(allShorterRecords)
|
||||||
|
}
|
||||||
collection, _ := app.Dao().FindCollectionByNameOrId(collectionName)
|
collection, _ := app.Dao().FindCollectionByNameOrId(collectionName)
|
||||||
tenMinRecord := models.NewRecord(collection)
|
tenMinRecord := models.NewRecord(collection)
|
||||||
tenMinRecord.Set("system", systemId)
|
tenMinRecord.Set("system", systemId)
|
||||||
tenMinRecord.Set("stats", averagedStats)
|
tenMinRecord.Set("stats", stats)
|
||||||
tenMinRecord.Set("type", longerRecordType)
|
tenMinRecord.Set("type", longerRecordType)
|
||||||
if err := app.Dao().SaveRecord(tenMinRecord); err != nil {
|
if err := app.Dao().SaveRecord(tenMinRecord); err != nil {
|
||||||
fmt.Println("failed to save longer record", "err", err.Error())
|
fmt.Println("failed to save longer record", "err", err.Error())
|
||||||
@@ -79,7 +85,7 @@ func createLongerRecords(collectionName string, shorterRecord *models.Record) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate the average of a list of SystemStats using reflection
|
// calculate the average stats of a list of system_stats records
|
||||||
func averageSystemStats(records []*models.Record) SystemStats {
|
func averageSystemStats(records []*models.Record) SystemStats {
|
||||||
count := float64(len(records))
|
count := float64(len(records))
|
||||||
sum := reflect.New(reflect.TypeOf(SystemStats{})).Elem()
|
sum := reflect.New(reflect.TypeOf(SystemStats{})).Elem()
|
||||||
@@ -102,42 +108,30 @@ func averageSystemStats(records []*models.Record) SystemStats {
|
|||||||
return average.Interface().(SystemStats)
|
return average.Interface().(SystemStats)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func averageSystemStats(records []*models.Record) SystemStats {
|
// calculate the average stats of a list of container_stats records
|
||||||
// var sum SystemStats
|
func averageContainerStats(records []*models.Record) (stats []ContainerStats) {
|
||||||
// count := float64(len(records))
|
sums := make(map[string]*ContainerStats)
|
||||||
|
count := float64(len(records))
|
||||||
// for _, record := range records {
|
for _, record := range records {
|
||||||
// var stats SystemStats
|
var stats []ContainerStats
|
||||||
// json.Unmarshal([]byte(record.Get("stats").(types.JsonRaw)), &stats)
|
json.Unmarshal([]byte(record.Get("stats").(types.JsonRaw)), &stats)
|
||||||
// sum.Cpu += stats.Cpu
|
for _, stat := range stats {
|
||||||
// sum.Mem += stats.Mem
|
if _, ok := sums[stat.Name]; !ok {
|
||||||
// sum.MemUsed += stats.MemUsed
|
sums[stat.Name] = &ContainerStats{Name: stat.Name, Cpu: 0, Mem: 0}
|
||||||
// sum.MemPct += stats.MemPct
|
}
|
||||||
// sum.MemBuffCache += stats.MemBuffCache
|
sums[stat.Name].Cpu += stat.Cpu
|
||||||
// sum.Disk += stats.Disk
|
sums[stat.Name].Mem += stat.Mem
|
||||||
// sum.DiskUsed += stats.DiskUsed
|
}
|
||||||
// sum.DiskPct += stats.DiskPct
|
}
|
||||||
// sum.DiskRead += stats.DiskRead
|
for _, value := range sums {
|
||||||
// sum.DiskWrite += stats.DiskWrite
|
stats = append(stats, ContainerStats{
|
||||||
// sum.NetworkSent += stats.NetworkSent
|
Name: value.Name,
|
||||||
// sum.NetworkRecv += stats.NetworkRecv
|
Cpu: twoDecimals(value.Cpu / count),
|
||||||
// }
|
Mem: twoDecimals(value.Mem / count),
|
||||||
|
})
|
||||||
// return SystemStats{
|
}
|
||||||
// Cpu: twoDecimals(sum.Cpu / count),
|
return stats
|
||||||
// Mem: twoDecimals(sum.Mem / count),
|
}
|
||||||
// MemUsed: twoDecimals(sum.MemUsed / count),
|
|
||||||
// MemPct: twoDecimals(sum.MemPct / count),
|
|
||||||
// MemBuffCache: twoDecimals(sum.MemBuffCache / count),
|
|
||||||
// Disk: twoDecimals(sum.Disk / count),
|
|
||||||
// DiskUsed: twoDecimals(sum.DiskUsed / count),
|
|
||||||
// DiskPct: twoDecimals(sum.DiskPct / count),
|
|
||||||
// DiskRead: twoDecimals(sum.DiskRead / count),
|
|
||||||
// DiskWrite: twoDecimals(sum.DiskWrite / count),
|
|
||||||
// NetworkSent: twoDecimals(sum.NetworkSent / count),
|
|
||||||
// NetworkRecv: twoDecimals(sum.NetworkRecv / count),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/* Round float to two decimals */
|
/* Round float to two decimals */
|
||||||
func twoDecimals(value float64) float64 {
|
func twoDecimals(value float64) float64 {
|
||||||
@@ -146,7 +140,7 @@ func twoDecimals(value float64) float64 {
|
|||||||
|
|
||||||
/* Delete records of specified collection and type that are older than timeLimit */
|
/* Delete records of specified collection and type that are older than timeLimit */
|
||||||
func deleteOldRecords(collection string, recordType string, timeLimit time.Duration) {
|
func deleteOldRecords(collection string, recordType string, timeLimit time.Duration) {
|
||||||
log.Println("Deleting old", recordType, "records...")
|
// log.Println("Deleting old", recordType, "records...")
|
||||||
timeLimitStamp := time.Now().UTC().Add(timeLimit).Format("2006-01-02 15:04:05")
|
timeLimitStamp := time.Now().UTC().Add(timeLimit).Format("2006-01-02 15:04:05")
|
||||||
records, _ := app.Dao().FindRecordsByExpr(collection,
|
records, _ := app.Dao().FindRecordsByExpr(collection,
|
||||||
dbx.NewExp("type = {:type}", dbx.Params{"type": recordType}),
|
dbx.NewExp("type = {:type}", dbx.Params{"type": recordType}),
|
||||||
|
Reference in New Issue
Block a user