mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			94 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package models
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
						|
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
						|
	_ "github.com/go-sql-driver/mysql"
 | 
						|
	"github.com/iwind/TeaGo/Tea"
 | 
						|
	"github.com/iwind/TeaGo/dbs"
 | 
						|
	timeutil "github.com/iwind/TeaGo/utils/time"
 | 
						|
	"strings"
 | 
						|
	"time"
 | 
						|
)
 | 
						|
 | 
						|
type HTTPAccessLogDAO dbs.DAO
 | 
						|
 | 
						|
var SharedHTTPAccessLogDAO = NewHTTPAccessLogDAO()
 | 
						|
 | 
						|
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
 | 
						|
	return dbs.NewDAO(&HTTPAccessLogDAO{
 | 
						|
		DAOObject: dbs.DAOObject{
 | 
						|
			DB:     Tea.Env,
 | 
						|
			Table:  "edgeHTTPAccessLogs",
 | 
						|
			Model:  new(HTTPAccessLog),
 | 
						|
			PkName: "id",
 | 
						|
		},
 | 
						|
	}).(*HTTPAccessLogDAO)
 | 
						|
}
 | 
						|
 | 
						|
// 创建访问日志
 | 
						|
func CreateHTTPAccessLogs(accessLogs []*pb.HTTPAccessLog) error {
 | 
						|
	dao := randomAccessLogDAO()
 | 
						|
	if dao == nil {
 | 
						|
		dao = SharedHTTPAccessLogDAO
 | 
						|
	}
 | 
						|
	return CreateHTTPAccessLogsWithDAO(dao, accessLogs)
 | 
						|
}
 | 
						|
 | 
						|
// 使用特定的DAO创建访问日志
 | 
						|
func CreateHTTPAccessLogsWithDAO(dao *HTTPAccessLogDAO, accessLogs []*pb.HTTPAccessLog) error {
 | 
						|
	if dao == nil {
 | 
						|
		return errors.New("dao should not be nil")
 | 
						|
	}
 | 
						|
	if len(accessLogs) == 0 {
 | 
						|
		return nil
 | 
						|
	}
 | 
						|
 | 
						|
	// TODO 改成事务批量提交,以加快速度
 | 
						|
 | 
						|
	for _, accessLog := range accessLogs {
 | 
						|
		day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0))
 | 
						|
		table, err := findAccessLogTable(dao.Instance, day, false)
 | 
						|
		if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
 | 
						|
		fields := map[string]interface{}{}
 | 
						|
		fields["serverId"] = accessLog.ServerId
 | 
						|
		fields["nodeId"] = accessLog.NodeId
 | 
						|
		fields["status"] = accessLog.Status
 | 
						|
		fields["createdAt"] = accessLog.Timestamp
 | 
						|
		fields["day"] = day
 | 
						|
 | 
						|
		content, err := json.Marshal(accessLog)
 | 
						|
		if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		fields["content"] = content
 | 
						|
 | 
						|
		_, err = dao.Query().
 | 
						|
			Table(table).
 | 
						|
			Sets(fields).
 | 
						|
			Insert()
 | 
						|
		if err != nil {
 | 
						|
			// 是否为 Error 1146: Table 'xxx.xxx' doesn't exist  如果是,则创建表之后重试
 | 
						|
			if strings.Contains(err.Error(), "1146") {
 | 
						|
				table, err = findAccessLogTable(dao.Instance, day, true)
 | 
						|
				if err != nil {
 | 
						|
					return err
 | 
						|
				}
 | 
						|
				_, err = dao.Query().
 | 
						|
					Table(table).
 | 
						|
					Sets(fields).
 | 
						|
					Insert()
 | 
						|
				if err != nil {
 | 
						|
					return err
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 |