package po import ( "fmt" "jollia.cn/golib/jokode" "strings" "time" ) const tableNamePrefixNgAccLog = "ng_access_log" type NginxAccessLog struct { Id int64 `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` RequestId string `json:"request_id" gorm:"column:request_id;type:varchar(255);not null;comment:请求ID"` TimeLocal time.Time `json:"time_local" gorm:"column:time_local;index;type:DATETIME;not null;comment:请求时间"` Host string `json:"host" gorm:"column:host;type:varchar(255);not null;comment:主机名"` RequestStr string `json:"request" gorm:"-"` Method string `json:"-" gorm:"column:method;type:varchar(30);not null;comment:Http请求方法"` Request string `json:"-" gorm:"column:request;type:varchar(2000);index;not null;comment;请求 Url"` HttpVer string `json:"-" gorm:"column:http_ver;type:VARCHAR(10);not null;comment:Http请求版本"` Status int `json:"status" gorm:"column:status;type:INT;not null;comment:响应状态码"` RemoteAddr string `json:"remote_addr" gorm:"column:remote_addr;type:VARCHAR(255);not null;comment:客户端IP地址"` Referer string `json:"referer,omitempty" gorm:"column:referer;type:VARCHAR(255);comment:来源页面"` Agent string `json:"agent,omitempty" gorm:"column:agent;type:TEXT;comment:用户代理(浏览器信息)"` XForwarded string `json:"x_forwarded,omitempty" gorm:"column:x_forwarded;type:VARCHAR(255);comment:X-Forwarded-For头域"` RequestLengthStr string `json:"request_length,omitempty" gorm:"-"` RequestLength int64 `json:"-" gorm:"column:request_length;type:BIGINT;comment:请求字节数"` BodyBytesSentStr string `json:"body_bytes_sent,omitempty" gorm:"-"` BodyBytesSent int64 `json:"-" gorm:"column:body_bytes_sent;type:BIGINT;comment:响应字节数"` UpAddr string `json:"up_addr,omitempty" gorm:"column:up_addr;type:VARCHAR(255);comment:上游服务器地址"` UpHost string `json:"up_host,omitempty" gorm:"column:up_host;type:VARCHAR(255);comment:上游服务器主机名"` UpRespTimeStr string `json:"up_resp_time,omitempty" gorm:"-"` UpRespTime float64 `json:"-" gorm:"column:up_resp_time;type:FLOAT;comment:上游服务器响应时间"` RequestTimeStr string `json:"request_time,omitempty" gorm:"-"` RequestTime float64 `json:"-" gorm:"column:request_time;type:FLOAT;comment:响应时间"` } //func (l *NginxAccessLog) TableName() string { // return tableNamePrefixNgAccLog //} func (l *NginxAccessLog) GetTableName() string { return fmt.Sprintf("%s_%s", tableNamePrefixNgAccLog, l.TimeLocal.Format("20060102")) } func (l *NginxAccessLog) FixFields() { if l.TimeLocal.IsZero() { l.TimeLocal = time.Now() } l.Method, l.Request, l.HttpVer = splitRequest(l.RequestStr) if l.RequestLengthStr != "" { l.RequestLength = jokode.AtoI64(l.RequestLengthStr) } if l.BodyBytesSentStr != "" { l.BodyBytesSent = jokode.AtoI64(l.BodyBytesSentStr) } if l.UpRespTimeStr != "" && l.UpRespTimeStr != "-" { l.UpRespTime = jokode.AtoF64(l.UpRespTimeStr) } if l.RequestTimeStr != "" && l.RequestTimeStr != "-" { l.RequestTime = jokode.AtoF64(l.RequestTimeStr) } } func splitRequest(r string) (method, url, ver string) { r = strings.TrimSpace(r) if leftIdx := strings.Index(r, " "); leftIdx != -1 { method = strings.TrimSpace(r[:leftIdx]) if rightIdx := strings.LastIndex(r, " "); rightIdx != -1 { ver = r[rightIdx+1:] url = r[leftIdx+1 : rightIdx] } else { url = r[leftIdx+1:] } } else { url = r } return }