Skip to content

Commit

Permalink
Merge pull request #125 from samwafgo/fix_weblog
Browse files Browse the repository at this point in the history
fix: weblog load slowly
  • Loading branch information
samwafgo authored Jan 23, 2025
2 parents bb6bb3e + ff2e68e commit 70c7755
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
31 changes: 23 additions & 8 deletions service/waf_service/waf_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"SamWaf/model/request"
"SamWaf/wafdb"
"errors"
"fmt"
"strconv"
"strings"
)

Expand Down Expand Up @@ -103,14 +105,29 @@ func (receiver *WafLogService) GetListApi(req request.WafAttackLogSearch) ([]inn
//强制索引
{
if strings.Contains(whereField, "unix_add_time") {
forceIndex = "web_logs INDEXED BY idx_web_time_tenant_user_code"
forceIndex = "web_logs INDEXED BY idx_web_time_desc_tenant_user_code"
} else if strings.Contains(whereField, "src_ip") {
forceIndex = "web_logs INDEXED BY idx_web_time_desc_tenant_user_code_ip"
}
}

// 将字符串转换为 int64 类型
unixBegin, err := strconv.ParseInt(req.UnixAddTimeBegin, 10, 64)
if err != nil {
fmt.Println("Error converting UnixAddTimeBegin to int64:", err)

}

unixEnd, err := strconv.ParseInt(req.UnixAddTimeEnd, 10, 64)
if err != nil {
fmt.Println("Error converting UnixAddTimeEnd to int64:", err)

}

//where字段赋值
{
whereValues = append(whereValues, req.UnixAddTimeBegin)
whereValues = append(whereValues, req.UnixAddTimeEnd)
whereValues = append(whereValues, unixBegin)
whereValues = append(whereValues, unixEnd)
if len(req.HostCode) > 0 {
whereValues = append(whereValues, req.HostCode)
}
Expand Down Expand Up @@ -153,9 +170,6 @@ func (receiver *WafLogService) GetListApi(req request.WafAttackLogSearch) ([]inn
if len(req.CurrrentDbName) == 0 || req.CurrrentDbName == "local_log.db" {
global.GWAF_LOCAL_LOG_DB.Table(forceIndex).Limit(req.PageSize).Where(whereField, whereValues...).Offset(req.PageSize * (req.PageIndex - 1)).Order(orderInfo).Find(&weblogs)
global.GWAF_LOCAL_LOG_DB.Table(forceIndex).Where(whereField, whereValues...).Count(&total)
/*global.GWAF_LOCAL_LOG_DB.Table("web_logs INDEXED BY idx_web_time_tenant_user_code ").Limit(req.PageSize).Where(whereField, whereValues...).Offset(req.PageSize * (req.PageIndex - 1)).Order(orderInfo).Find(&weblogs)
global.GWAF_LOCAL_LOG_DB.Table("web_logs INDEXED BY idx_web_time_tenant_user_code ").Model(&innerbean.WebLog{}).Where(whereField, whereValues...).Count(&total)
*/
} else {
wafdb.InitManaulLogDb("", req.CurrrentDbName)
global.GDATA_CURRENT_LOG_DB_MAP[req.CurrrentDbName].Table(forceIndex).Limit(req.PageSize).Where(whereField, whereValues...).Offset(req.PageSize * (req.PageIndex - 1)).Order(orderInfo).Find(&weblogs)
Expand All @@ -178,7 +192,8 @@ func (receiver *WafLogService) DeleteHistory(day string) {
// GetUnixTimeByCounter 依据开始时间和到期时间获取一个最新的时间戳
func (receiver *WafLogService) GetUnixTimeByCounter(lastStartCreateUnix int64, lastEndCreateUnix int64) innerbean.WebLog {
var weblog innerbean.WebLog
global.GWAF_LOCAL_LOG_DB.Where("unix_add_time>=? and unix_add_time<?", lastStartCreateUnix, lastEndCreateUnix).Order("unix_add_time desc").Limit(1).Find(&weblog)
forceIndex := "web_logs INDEXED BY idx_web_time_desc_tenant_user_code"
global.GWAF_LOCAL_LOG_DB.Table(forceIndex).Where("unix_add_time>=? and unix_add_time<?", lastStartCreateUnix, lastEndCreateUnix).Order("unix_add_time desc").Limit(1).Find(&weblog)

return weblog
}
Expand Down Expand Up @@ -323,7 +338,7 @@ order by sum(cnt) desc
判断是否合法
*/
func (receiver *WafLogService) isValidSortField(field string) bool {
var allowedSortFields = []string{"time_spent", "create_time"}
var allowedSortFields = []string{"time_spent", "create_time", "unix_add_time"}

for _, allowedField := range allowedSortFields {
if field == allowedField {
Expand Down
16 changes: 16 additions & 0 deletions wafdb/patch_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ func pathLogSql(db *gorm.DB) {
} else {
zlog.Info("db", "idx_tenant_usercode_web_logs created")
}

// 20250123 创建联合索引
err = db.Exec("CREATE INDEX IF NOT EXISTS idx_web_time_desc_tenant_user_code ON web_logs (unix_add_time desc, tenant_id,user_code)").Error
if err != nil {
panic("failed to create index: idx_web_time_desc_tenant_user_code " + err.Error())
} else {
zlog.Info("db", "idx_web_time_desc_tenant_user_code created")
}
//20250123 建立带IP得
err = db.Exec("CREATE INDEX IF NOT EXISTS idx_web_time_desc_tenant_user_code_ip ON web_logs (unix_add_time desc, tenant_id,user_code,src_ip)").Error
if err != nil {
panic("failed to create index: idx_web_time_desc_tenant_user_code_ip " + err.Error())
} else {
zlog.Info("db", "idx_web_time_desc_tenant_user_code_ip created")
}

// 记录结束时间并计算耗时
duration := time.Since(startTime)
zlog.Info("create index completely", "duration", duration.String())
Expand Down

0 comments on commit 70c7755

Please sign in to comment.