增加${browser.xxx}相关变量

This commit is contained in:
GoEdgeLab
2022-01-06 17:05:04 +08:00
parent 18c20deee5
commit 0b214b4d28
4 changed files with 39 additions and 14 deletions

View File

@@ -928,11 +928,26 @@ func (this *HTTPRequest) Format(source string) string {
return "" return ""
} }
// os
// TODO
// browser // browser
// TODO if prefix == "browser" {
var result = stats.SharedUserAgentParser.Parse(this.RawReq.UserAgent())
switch suffix {
case "os.name":
return result.OS.Name
case "os.version":
return result.OS.Version
case "name":
return result.BrowserName
case "version":
return result.BrowserVersion
case "isMobile":
if result.IsMobile {
return "1"
} else {
return "0"
}
}
}
return "${" + varName + "}" return "${" + varName + "}"
}) })

View File

@@ -29,7 +29,6 @@ type StatItem struct {
} }
var SharedHTTPRequestStatManager = NewHTTPRequestStatManager() var SharedHTTPRequestStatManager = NewHTTPRequestStatManager()
var sharedUserAgentParser = NewUserAgentParser()
// HTTPRequestStatManager HTTP请求相关的统计 // HTTPRequestStatManager HTTP请求相关的统计
// 这里的统计是一个辅助统计,注意不要因为统计而影响服务工作性能 // 这里的统计是一个辅助统计,注意不要因为统计而影响服务工作性能
@@ -223,8 +222,8 @@ Loop:
serverId := userAgentString[:atIndex] serverId := userAgentString[:atIndex]
userAgent := userAgentString[atIndex+1:] userAgent := userAgentString[atIndex+1:]
var result = sharedUserAgentParser.Parse(userAgent) var result = SharedUserAgentParser.Parse(userAgent)
var osInfo = result.os var osInfo = result.OS
if len(osInfo.Name) > 0 { if len(osInfo.Name) > 0 {
dotIndex := strings.Index(osInfo.Version, ".") dotIndex := strings.Index(osInfo.Version, ".")
if dotIndex > -1 { if dotIndex > -1 {
@@ -233,7 +232,7 @@ Loop:
this.systemMap[serverId+"@"+osInfo.Name+"@"+osInfo.Version]++ this.systemMap[serverId+"@"+osInfo.Name+"@"+osInfo.Version]++
} }
var browser, browserVersion = result.browserName, result.browserVersion var browser, browserVersion = result.BrowserName, result.BrowserVersion
if len(browser) > 0 { if len(browser) > 0 {
dotIndex := strings.Index(browserVersion, ".") dotIndex := strings.Index(browserVersion, ".")
if dotIndex > -1 { if dotIndex > -1 {

View File

@@ -5,10 +5,12 @@ package stats
import ( import (
"github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/mssola/user_agent" "github.com/mssola/user_agent"
"sync"
) )
var SharedUserAgentParser = NewUserAgentParser()
// UserAgentParser UserAgent解析器 // UserAgentParser UserAgent解析器
// 只支持单线程
type UserAgentParser struct { type UserAgentParser struct {
parser *user_agent.UserAgent parser *user_agent.UserAgent
@@ -17,6 +19,7 @@ type UserAgentParser struct {
maxCacheItems int maxCacheItems int
cacheCursor int cacheCursor int
locker sync.RWMutex
} }
func NewUserAgentParser() *UserAgentParser { func NewUserAgentParser() *UserAgentParser {
@@ -50,19 +53,25 @@ func (this *UserAgentParser) Parse(userAgent string) (result UserAgentParserResu
return return
} }
this.locker.RLock()
cacheResult, ok := this.cacheMap1[userAgent] cacheResult, ok := this.cacheMap1[userAgent]
if ok { if ok {
this.locker.RUnlock()
return cacheResult return cacheResult
} }
cacheResult, ok = this.cacheMap2[userAgent] cacheResult, ok = this.cacheMap2[userAgent]
if ok { if ok {
this.locker.RUnlock()
return cacheResult return cacheResult
} }
this.locker.RUnlock()
this.locker.Lock()
this.parser.Parse(userAgent) this.parser.Parse(userAgent)
result.os = this.parser.OSInfo() result.OS = this.parser.OSInfo()
result.browserName, result.browserVersion = this.parser.Browser() result.BrowserName, result.BrowserVersion = this.parser.Browser()
result.IsMobile = this.parser.Mobile()
if this.cacheCursor == 0 { if this.cacheCursor == 0 {
this.cacheMap1[userAgent] = result this.cacheMap1[userAgent] = result
@@ -77,6 +86,7 @@ func (this *UserAgentParser) Parse(userAgent string) (result UserAgentParserResu
this.cacheMap1 = map[string]UserAgentParserResult{} this.cacheMap1 = map[string]UserAgentParserResult{}
} }
} }
this.locker.Unlock()
return return
} }

View File

@@ -5,7 +5,8 @@ package stats
import "github.com/mssola/user_agent" import "github.com/mssola/user_agent"
type UserAgentParserResult struct { type UserAgentParserResult struct {
os user_agent.OSInfo OS user_agent.OSInfo
browserName string BrowserName string
browserVersion string BrowserVersion string
IsMobile bool
} }