Update main.go, main.go, and size_enforcer.go

This commit is contained in:
RamiBerm 2021-07-15 09:38:12 +03:00
parent 79a8ee37f9
commit 21b91ea6e4
3 changed files with 13 additions and 6 deletions

View File

@ -161,7 +161,7 @@ func saveHarToDb(entry *har.Entry, connectionInfo *tap.ConnectionInfo) {
IsOutgoing: connectionInfo.IsOutgoing,
}
mizuEntry.EstimatedSizeBytes = getEstimatedEntrySizeBytes(mizuEntry)
database.GetEntriesTable().Create(&mizuEntry)
database.CreateEntry(&mizuEntry)
baseEntry := models.BaseEntryDetails{}
if err := models.GetEntry(&mizuEntry, &baseEntry); err != nil {

View File

@ -12,11 +12,6 @@ import (
const (
DBPath = "./entries.db"
)
var DB *gorm.DB
const (
OrderDesc = "desc"
OrderAsc = "asc"
LT = "lt"
@ -24,6 +19,8 @@ const (
)
var (
DB *gorm.DB
IsDBLocked = false
OperatorToSymbolMapping = map[string]string{
LT: "<",
GT: ">",
@ -43,6 +40,13 @@ func GetEntriesTable() *gorm.DB {
return DB.Table("mizu_entries")
}
func CreateEntry(entry *models.MizuEntry) {
if IsDBLocked {
return
}
GetEntriesTable().Create(entry)
}
func initDataBase(databasePath string) *gorm.DB {
temp, _ := gorm.Open(sqlite.Open(databasePath), &gorm.Config{
Logger: &utils.TruncatingLogger{LogLevel: logger.Warn, SlowThreshold: 500 * time.Millisecond},

View File

@ -81,6 +81,9 @@ func checkFileSize(maxSizeBytes int64) {
}
func pruneOldEntries(currentFileSize int64) {
IsDBLocked = true
defer func() {IsDBLocked = false}()
amountOfBytesToTrim := currentFileSize / (100 / percentageOfMaxSizeBytesToPrune)
rows, err := GetEntriesTable().Limit(10000).Order("id").Rows()