From b4cb6d992bb8f37a87f6ba1ac6b5fa2d8657f4ff Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 9 Mar 2022 11:00:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8=E5=B0=8F?= =?UTF-8?q?=E6=97=B6=E7=AD=9B=E9=80=89=E8=AE=BF=E9=97=AE=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/http_access_log_dao.go | 49 +++++++++++++++++-- .../rpc/services/service_http_access_log.go | 2 +- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/internal/db/models/http_access_log_dao.go b/internal/db/models/http_access_log_dao.go index 471f94f1..aedd77af 100644 --- a/internal/db/models/http_access_log_dao.go +++ b/internal/db/models/http_access_log_dao.go @@ -235,6 +235,8 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLog func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, + hourFrom string, + hourTo string, clusterId int64, nodeId int64, serverId int64, @@ -257,18 +259,36 @@ func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size = 1000 } - result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, clusterId, nodeId, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword, ip, domain) + result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, hourFrom, hourTo, clusterId, nodeId, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword, ip, domain) if err != nil || int64(len(result)) < size { return } - moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, clusterId, nodeId, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword, ip, domain) + moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, hourFrom, hourTo, clusterId, nodeId, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword, ip, domain) hasMore = len(moreResult) > 0 return } // 读取往前的单页访问日志 -func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, clusterId int64, nodeId int64, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64, keyword string, ip string, domain string) (result []*HTTPAccessLog, nextLastRequestId string, err error) { +func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, + lastRequestId string, + size int64, + day string, + hourFrom string, + hourTo string, + clusterId int64, + nodeId int64, + serverId int64, + reverse bool, + hasError bool, + firewallPolicyId int64, + firewallRuleGroupId int64, + firewallRuleSetId int64, + hasFirewallPolicy bool, + userId int64, + keyword string, + ip string, + domain string) (result []*HTTPAccessLog, nextLastRequestId string, err error) { if size <= 0 { return nil, lastRequestId, nil } @@ -331,7 +351,8 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s var locker = sync.Mutex{} - var statusPrefixReg = regexp.MustCompile(`status:\s*(\d{3})`) + var statusPrefixReg = regexp.MustCompile(`status:\s*(\d{3})\b`) + var statusRangeReg = regexp.MustCompile(`status:\s*(\d{3})-(\d{3})\b`) var count = len(tableQueries) var wg = &sync.WaitGroup{} @@ -433,9 +454,15 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s } else { query.Between("INET_ATON(remoteAddr)", utils.IP2Long(pieces[0]), utils.IP2Long(pieces[1])) } + } else if statusRangeReg.MatchString(keyword) { + var matches = statusRangeReg.FindStringSubmatch(keyword) + query.Between("status", types.Int(matches[1]), types.Int(matches[2])) + + // TODO 处理剩余的关键词 } else if statusPrefixReg.MatchString(keyword) { var matches = statusPrefixReg.FindStringSubmatch(keyword) query.Attr("status", matches[1]) + // TODO 处理剩余的关键词 } else { if regexp.MustCompile(`^ip:.+`).MatchString(keyword) { keyword = keyword[3:] @@ -491,6 +518,20 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s } } + // hourFrom - hourTo + if len(hourFrom) > 0 && len(hourTo) > 0 { + var hourFromInt = types.Int(hourFrom) + var hourToInt = types.Int(hourTo) + if hourFromInt >= 0 && hourFromInt <= 23 && hourToInt >= hourFromInt && hourToInt <= 23 { + var y = types.Int(day[:4]) + var m = types.Int(day[4:6]) + var d = types.Int(day[6:]) + var timeFrom = time.Date(y, time.Month(m), d, hourFromInt, 0, 0, 0, time.Local) + var timeTo = time.Date(y, time.Month(m), d, hourToInt, 59, 59, 0, time.Local) + query.Between("createdAt", timeFrom.Unix(), timeTo.Unix()) + } + } + // offset if len(lastRequestId) > 0 { if !reverse { diff --git a/internal/rpc/services/service_http_access_log.go b/internal/rpc/services/service_http_access_log.go index 7b095454..97fcb107 100644 --- a/internal/rpc/services/service_http_access_log.go +++ b/internal/rpc/services/service_http_access_log.go @@ -72,7 +72,7 @@ func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *p } } - accessLogs, requestId, hasMore, err := models.SharedHTTPAccessLogDAO.ListAccessLogs(tx, req.RequestId, req.Size, req.Day, req.NodeClusterId, req.NodeId, req.ServerId, req.Reverse, req.HasError, req.FirewallPolicyId, req.FirewallRuleGroupId, req.FirewallRuleSetId, req.HasFirewallPolicy, req.UserId, req.Keyword, req.Ip, req.Domain) + accessLogs, requestId, hasMore, err := models.SharedHTTPAccessLogDAO.ListAccessLogs(tx, req.RequestId, req.Size, req.Day, req.HourFrom, req.HourTo, req.NodeClusterId, req.NodeId, req.ServerId, req.Reverse, req.HasError, req.FirewallPolicyId, req.FirewallRuleGroupId, req.FirewallRuleSetId, req.HasFirewallPolicy, req.UserId, req.Keyword, req.Ip, req.Domain) if err != nil { return nil, err }