Skip to content

Commit 9ebe5bd

Browse files
authored
fix: content clean sql (#92)
* fix: content clean sql * chore: request id null index
1 parent 148a57c commit 9ebe5bd

5 files changed

Lines changed: 9 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ docker-compose up -d
110110
- `LOG_DETAIL_REQUEST_BODY_MAX_SIZE`: Maximum size for request body in log details, default is `128KB`
111111
- `LOG_DETAIL_RESPONSE_BODY_MAX_SIZE`: Maximum size for response body in log details, default is `128KB`
112112
- `LOG_DETAIL_STORAGE_HOURS`: Hours to store log details, default is `72` (3 days)
113-
- `CLEAN_LOG_BATCH_SIZE`: Batch size for cleaning logs, default is `5000`
113+
- `CLEAN_LOG_BATCH_SIZE`: Batch size for cleaning logs, default is `2000`
114114

115115
### Service Control
116116

README.zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ docker-compose up -d
111111
- `LOG_DETAIL_REQUEST_BODY_MAX_SIZE`: 日志详情请求体最大大小,默认 `128KB`
112112
- `LOG_DETAIL_RESPONSE_BODY_MAX_SIZE`: 日志详情响应体最大大小,默认 `128KB`
113113
- `LOG_DETAIL_STORAGE_HOURS`: 日志详情存储时间,默认 `72`(3 天)
114-
- `CLEAN_LOG_BATCH_SIZE`: 日志清理批量大小,默认 `5000`
114+
- `CLEAN_LOG_BATCH_SIZE`: 日志清理批量大小,默认 `2000`
115115

116116
### 服务控制
117117

common/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
logDetailRequestBodyMaxSize int64 = 128 * 1024 // 128KB
3030
logDetailResponseBodyMaxSize int64 = 128 * 1024 // 128KB
3131
logDetailStorageHours int64 = 3 * 24 // 3 days
32-
cleanLogBatchSize int64 = 5000
32+
cleanLogBatchSize int64 = 2000
3333
internalToken atomic.Value
3434
notifyNote atomic.Value
3535
ipGroupsThreshold int64

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func cleanLog(ctx context.Context) {
251251
case <-ctx.Done():
252252
return
253253
case <-ticker.C:
254-
optimize := trylock.Lock("optimizeLog", time.Minute*15)
254+
optimize := trylock.Lock("optimizeLog", time.Hour*24)
255255
err := model.CleanLog(int(config.GetCleanLogBatchSize()), optimize)
256256
if err != nil {
257257
notify.ErrorThrottle("cleanLog", time.Minute, "clean log failed", err.Error())

model/log.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ type Log struct {
6565
Content EmptyNullString `gorm:"type:text" json:"content,omitempty"`
6666
GroupID string `json:"group,omitempty"`
6767
Model string `json:"model"`
68-
RequestID EmptyNullString `gorm:"index" json:"request_id"`
68+
RequestID EmptyNullString `gorm:"index:,where:request_id is not null" json:"request_id"`
6969
ID int `gorm:"primaryKey" json:"id"`
7070
TokenID int `gorm:"index" json:"token_id,omitempty"`
7171
ChannelID int `json:"channel,omitempty"`
7272
Code int `gorm:"index" json:"code,omitempty"`
7373
Mode int `json:"mode,omitempty"`
74-
IP EmptyNullString `gorm:"index" json:"ip,omitempty"`
74+
IP EmptyNullString `gorm:"index:,where:ip is not null" json:"ip,omitempty"`
7575
RetryTimes ZeroNullInt64 `json:"retry_times,omitempty"`
7676
DownstreamResult bool `json:"downstream_result,omitempty"`
7777
Price Price `gorm:"embedded" json:"price,omitempty"`
@@ -264,9 +264,9 @@ func cleanLog(batchSize int, optimize bool) error {
264264
Model(&Log{}).
265265
Where(
266266
"created_at < ?",
267-
time.Now().Add(-time.Duration(logContentStorageHours)*time.Hour),
267+
time.Now().Truncate(time.Hour).Add(-time.Duration(logContentStorageHours)*time.Hour),
268268
).
269-
Where("endpoint IS NOT NULL OR ip IS NOT NULL OR content IS NOT NULL OR ttfb_milliseconds IS NOT NULL").
269+
Where("endpoint IS NOT NULL").
270270
Order("created_at DESC").
271271
Limit(1).
272272
Select("id").
@@ -282,7 +282,7 @@ func cleanLog(batchSize int, optimize bool) error {
282282
Model(&Log{}).
283283
Session(&gorm.Session{SkipDefaultTransaction: true}).
284284
Where(
285-
"id BETWEEN ? AND ?",
285+
"id BETWEEN ? AND ? AND content IS NOT NULL",
286286
id-int64(batchSize),
287287
id,
288288
).

0 commit comments

Comments
 (0)