mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 02:09:28 +08:00
optimize deletion of old records
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/pocketbase/pocketbase"
|
"github.com/pocketbase/pocketbase"
|
||||||
"github.com/pocketbase/pocketbase/daos"
|
"github.com/pocketbase/pocketbase/daos"
|
||||||
"github.com/pocketbase/pocketbase/models"
|
"github.com/pocketbase/pocketbase/models"
|
||||||
|
"github.com/pocketbase/pocketbase/tools/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RecordManager struct {
|
type RecordManager struct {
|
||||||
@@ -262,8 +263,8 @@ func (rm *RecordManager) AverageContainerStats(records []*models.Record) (stats
|
|||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deletes records older than what is displayed in the UI
|
||||||
func (rm *RecordManager) DeleteOldRecords() {
|
func (rm *RecordManager) DeleteOldRecords() {
|
||||||
// start := time.Now()
|
|
||||||
collections := []string{"system_stats", "container_stats"}
|
collections := []string{"system_stats", "container_stats"}
|
||||||
recordData := []RecordDeletionData{
|
recordData := []RecordDeletionData{
|
||||||
{
|
{
|
||||||
@@ -287,29 +288,17 @@ func (rm *RecordManager) DeleteOldRecords() {
|
|||||||
retention: 30 * 24 * time.Hour,
|
retention: 30 * 24 * time.Hour,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
rm.app.Dao().RunInTransaction(func(txDao *daos.Dao) error {
|
db := rm.app.Dao().NonconcurrentDB()
|
||||||
for _, recordData := range recordData {
|
for _, recordData := range recordData {
|
||||||
exp := dbx.NewExp(
|
|
||||||
"type = {:type} AND created < {:created}",
|
|
||||||
dbx.Params{"type": recordData.recordType, "created": time.Now().UTC().Add(-recordData.retention)},
|
|
||||||
)
|
|
||||||
for _, collectionSlug := range collections {
|
for _, collectionSlug := range collections {
|
||||||
collectionRecords, err := txDao.FindRecordsByExpr(collectionSlug, exp)
|
formattedDate := time.Now().UTC().Add(-recordData.retention).Format(types.DefaultDateLayout)
|
||||||
if err != nil {
|
expr := dbx.NewExp("[[created]] < {:date} AND [[type]] = {:type}", dbx.Params{"date": formattedDate, "type": recordData.recordType})
|
||||||
return err
|
_, err := db.Delete(collectionSlug, expr).Execute()
|
||||||
}
|
|
||||||
for _, record := range collectionRecords {
|
|
||||||
err := txDao.DeleteRecord(record)
|
|
||||||
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())
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
// log.Println("finished deleting old records", "time (ms)", time.Since(start).Milliseconds())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Round float to two decimals */
|
/* Round float to two decimals */
|
||||||
|
Reference in New Issue
Block a user