53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
|
|
package dao
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"model-gateway/consts/public"
|
||
|
|
"model-gateway/model/entity"
|
||
|
|
|
||
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
||
|
|
"github.com/gogf/gf/v2/os/gtime"
|
||
|
|
"github.com/gogf/gf/v2/util/gconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
var ModelGatewayLogsStat = &modelGatewayLogsStatDao{}
|
||
|
|
|
||
|
|
type modelGatewayLogsStatDao struct{}
|
||
|
|
|
||
|
|
// IncRequestCount 原子累加:按天+租户+创建人+模型 +1
|
||
|
|
func (d *modelGatewayLogsStatDao) IncRequestCount(ctx context.Context, day time.Time, tenantId uint64, creator, modelName string) error {
|
||
|
|
_, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameStat).
|
||
|
|
Data(&entity.ModelGatewayLogsStat{
|
||
|
|
Day: gtime.New(day),
|
||
|
|
TenantId: tenantId,
|
||
|
|
Creator: creator,
|
||
|
|
ModelName: modelName,
|
||
|
|
RequestCount: 1,
|
||
|
|
}).
|
||
|
|
OnDuplicate("request_count", "request_count+1").
|
||
|
|
Insert()
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// List 分页查询统计
|
||
|
|
func (d *modelGatewayLogsStatDao) List(ctx context.Context, pageNum, pageSize int, req *entity.ModelGatewayLogsStat) (list []*entity.ModelGatewayLogsStat, total int64, err error) {
|
||
|
|
model := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameStat).
|
||
|
|
OmitEmpty().
|
||
|
|
Where(entity.ModelGatewayLogsStatCols.Creator, req.Creator).
|
||
|
|
WhereLike(entity.ModelGatewayLogsStatCols.ModelName, "%"+req.ModelName+"%").
|
||
|
|
OrderDesc(entity.ModelGatewayLogsStatCols.Day).
|
||
|
|
OrderDesc(entity.ModelGatewayLogsStatCols.RequestCount)
|
||
|
|
if pageNum > 0 && pageSize > 0 {
|
||
|
|
model = model.Page(pageNum, pageSize)
|
||
|
|
}
|
||
|
|
r, totalInt, err := model.AllAndCount(false)
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
total = gconv.Int64(totalInt)
|
||
|
|
err = r.Structs(&list)
|
||
|
|
return
|
||
|
|
}
|