mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-07 16:05:47 +08:00
[日志审计]增加删除、清理和别的一些设置
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LogDAO dbs.DAO
|
||||
@@ -88,6 +91,43 @@ func (this *LogDAO) ListLogs(offset int64, size int64, dayFrom string, dayTo str
|
||||
return
|
||||
}
|
||||
|
||||
// 物理删除日志
|
||||
func (this *LogDAO) DeleteLogPermanently(logId int64) error {
|
||||
if logId <= 0 {
|
||||
return errors.New("invalid logId")
|
||||
}
|
||||
_, err := this.Delete(logId)
|
||||
return err
|
||||
}
|
||||
|
||||
// 物理删除所有日志
|
||||
func (this *LogDAO) DeleteAllLogsPermanently() error {
|
||||
_, err := this.Query().
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// 物理删除某些天之前的日志
|
||||
func (this *LogDAO) DeleteLogsPermanentlyBeforeDays(days int) error {
|
||||
if days <= 0 {
|
||||
days = 0
|
||||
}
|
||||
untilDay := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))
|
||||
_, err := this.Query().
|
||||
Lte("day", untilDay).
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// 计算当前日志容量大小
|
||||
func (this *LogDAO) SumLogsSize() (int64, error) {
|
||||
col, err := this.Instance.FindCol(0, "SELECT DATA_LENGTH FROM information_schema.TABLES WHERE TABLE_SCHEMA=? AND TABLE_NAME=? LIMIT 1", this.Instance.Name(), this.Table)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(col), nil
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
func (this *LogDAO) formatDay(day string) string {
|
||||
if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(day) {
|
||||
|
||||
@@ -2,4 +2,16 @@ package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLogDAO_SumLogsSize(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
size, err := SharedLogDAO.SumLogsSize()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("size:", size)
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ const (
|
||||
MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
|
||||
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
||||
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
||||
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
||||
)
|
||||
|
||||
type MessageDAO dbs.DAO
|
||||
|
||||
Reference in New Issue
Block a user