135 lines
4.2 KiB
Go
135 lines
4.2 KiB
Go
package models
|
|
|
|
import (
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"strings"
|
|
)
|
|
|
|
type HTTPDNSAccessLogDAO dbs.DAO
|
|
|
|
func NewHTTPDNSAccessLogDAO() *HTTPDNSAccessLogDAO {
|
|
return dbs.NewDAO(&HTTPDNSAccessLogDAO{
|
|
DAOObject: dbs.DAOObject{
|
|
DB: Tea.Env,
|
|
Table: "edgeHTTPDNSAccessLogs",
|
|
Model: new(HTTPDNSAccessLog),
|
|
PkName: "id",
|
|
},
|
|
}).(*HTTPDNSAccessLogDAO)
|
|
}
|
|
|
|
var SharedHTTPDNSAccessLogDAO *HTTPDNSAccessLogDAO
|
|
|
|
func init() {
|
|
dbs.OnReady(func() {
|
|
SharedHTTPDNSAccessLogDAO = NewHTTPDNSAccessLogDAO()
|
|
})
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) CreateLog(tx *dbs.Tx, log *HTTPDNSAccessLog) error {
|
|
var op = NewHTTPDNSAccessLogOperator()
|
|
op.RequestId = log.RequestId
|
|
op.ClusterId = log.ClusterId
|
|
op.NodeId = log.NodeId
|
|
op.AppId = log.AppId
|
|
op.AppName = log.AppName
|
|
op.Domain = log.Domain
|
|
op.QType = log.QType
|
|
op.ClientIP = log.ClientIP
|
|
op.ClientRegion = log.ClientRegion
|
|
op.Carrier = log.Carrier
|
|
op.SDKVersion = log.SDKVersion
|
|
op.OS = log.OS
|
|
op.ResultIPs = log.ResultIPs
|
|
op.Status = log.Status
|
|
op.ErrorCode = log.ErrorCode
|
|
op.CostMs = log.CostMs
|
|
op.CreatedAt = log.CreatedAt
|
|
op.Day = log.Day
|
|
op.Summary = log.Summary
|
|
return this.Save(tx, op)
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) BuildListQuery(tx *dbs.Tx, day string, clusterId int64, nodeId int64, appId string, domain string, status string, keyword string) *dbs.Query {
|
|
return this.BuildListQueryWithAppIds(tx, day, clusterId, nodeId, appId, nil, domain, status, keyword)
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) BuildListQueryWithAppIds(tx *dbs.Tx, day string, clusterId int64, nodeId int64, appId string, appIds []string, domain string, status string, keyword string) *dbs.Query {
|
|
query := this.Query(tx).DescPk()
|
|
if len(day) > 0 {
|
|
query = query.Attr("day", day)
|
|
}
|
|
if clusterId > 0 {
|
|
query = query.Attr("clusterId", clusterId)
|
|
}
|
|
if nodeId > 0 {
|
|
query = query.Attr("nodeId", nodeId)
|
|
}
|
|
if len(appIds) > 0 {
|
|
validAppIds := make([]string, 0, len(appIds))
|
|
for _, value := range appIds {
|
|
value = strings.TrimSpace(value)
|
|
if len(value) == 0 {
|
|
continue
|
|
}
|
|
validAppIds = append(validAppIds, value)
|
|
}
|
|
if len(validAppIds) == 0 {
|
|
query = query.Where("1 = 0")
|
|
} else {
|
|
query = query.Attr("appId", validAppIds)
|
|
}
|
|
}
|
|
if len(appId) > 0 {
|
|
query = query.Attr("appId", appId)
|
|
}
|
|
if len(domain) > 0 {
|
|
query = query.Attr("domain", domain)
|
|
}
|
|
if len(status) > 0 {
|
|
query = query.Attr("status", status)
|
|
}
|
|
if len(keyword) > 0 {
|
|
query = query.Where("(summary LIKE :kw OR appName LIKE :kw OR clientIP LIKE :kw OR resultIPs LIKE :kw)").Param("kw", "%"+keyword+"%")
|
|
}
|
|
return query
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) CountLogs(tx *dbs.Tx, day string, clusterId int64, nodeId int64, appId string, domain string, status string, keyword string) (int64, error) {
|
|
return this.BuildListQueryWithAppIds(tx, day, clusterId, nodeId, appId, nil, domain, status, keyword).Count()
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) ListLogs(tx *dbs.Tx, day string, clusterId int64, nodeId int64, appId string, domain string, status string, keyword string, offset int64, size int64) (result []*HTTPDNSAccessLog, err error) {
|
|
_, err = this.BuildListQueryWithAppIds(tx, day, clusterId, nodeId, appId, nil, domain, status, keyword).
|
|
Offset(offset).
|
|
Limit(size).
|
|
Slice(&result).
|
|
FindAll()
|
|
return
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) CountLogsWithAppIds(tx *dbs.Tx, day string, clusterId int64, nodeId int64, appId string, appIds []string, domain string, status string, keyword string) (int64, error) {
|
|
return this.BuildListQueryWithAppIds(tx, day, clusterId, nodeId, appId, appIds, domain, status, keyword).Count()
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) ListLogsWithAppIds(tx *dbs.Tx, day string, clusterId int64, nodeId int64, appId string, appIds []string, domain string, status string, keyword string, offset int64, size int64) (result []*HTTPDNSAccessLog, err error) {
|
|
_, err = this.BuildListQueryWithAppIds(tx, day, clusterId, nodeId, appId, appIds, domain, status, keyword).
|
|
Offset(offset).
|
|
Limit(size).
|
|
Slice(&result).
|
|
FindAll()
|
|
return
|
|
}
|
|
|
|
func (this *HTTPDNSAccessLogDAO) DeleteLogsWithAppId(tx *dbs.Tx, appId string) error {
|
|
if len(appId) == 0 {
|
|
return nil
|
|
}
|
|
_, err := this.Query(tx).
|
|
Attr("appId", appId).
|
|
Delete()
|
|
return err
|
|
}
|