2026-04-29 15:54:14 +08:00
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-05-15 14:56:26 +08:00
|
|
|
"model-gateway/consts/public"
|
|
|
|
|
"model-gateway/model/entity"
|
2026-04-29 15:54:14 +08:00
|
|
|
|
2026-06-10 16:16:05 +08:00
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
2026-04-29 15:54:14 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type statDao struct{}
|
|
|
|
|
|
|
|
|
|
var Stat = &statDao{}
|
|
|
|
|
|
|
|
|
|
// IncRequestCount 原子累加(支持分布式/多协程):按天+租户+创建人+模型 +1
|
|
|
|
|
func (d *statDao) IncRequestCount(ctx context.Context, day time.Time, tenantId int64, creator, modelName string) error {
|
|
|
|
|
sql := fmt.Sprintf(`
|
|
|
|
|
INSERT INTO %s(day, tenant_id, creator, model_name, request_count, created_at, updated_at)
|
|
|
|
|
VALUES(?, ?, ?, ?, 1, NOW(), NOW())
|
|
|
|
|
ON CONFLICT (day, tenant_id, creator, model_name)
|
|
|
|
|
DO UPDATE SET request_count = %s.request_count + 1, updated_at = NOW()`,
|
|
|
|
|
public.TableNameStat, public.TableNameStat,
|
|
|
|
|
)
|
2026-05-21 10:41:37 +08:00
|
|
|
_, err := gfdb.DB(ctx, public.DbNameModelGateway).Exec(ctx, sql, gtime.New(day).Format("Y-m-d"), tenantId, creator, modelName)
|
2026-04-29 15:54:14 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 13:45:08 +08:00
|
|
|
func (d *statDao) List(ctx context.Context, pageNum, pageSize int, startDay, endDay string, tenantId *int64, creator, modelName string) (list []*entity.LogsModelStat, total int64, err error) {
|
2026-04-29 15:54:14 +08:00
|
|
|
m := gfdb.DB(ctx).Model(ctx, public.TableNameStat).Where("1=1")
|
|
|
|
|
if startDay != "" {
|
|
|
|
|
m = m.Where("day >= ?", startDay)
|
|
|
|
|
}
|
|
|
|
|
if endDay != "" {
|
|
|
|
|
m = m.Where("day <= ?", endDay)
|
|
|
|
|
}
|
|
|
|
|
if tenantId != nil {
|
|
|
|
|
m = m.Where("tenant_id = ?", *tenantId)
|
|
|
|
|
}
|
|
|
|
|
if creator != "" {
|
|
|
|
|
m = m.WhereLike("creator", "%"+creator+"%")
|
|
|
|
|
}
|
|
|
|
|
if modelName != "" {
|
|
|
|
|
m = m.WhereLike("model_name", "%"+modelName+"%")
|
|
|
|
|
}
|
|
|
|
|
m = m.OrderDesc("day").OrderDesc("request_count")
|
|
|
|
|
if pageNum > 0 && pageSize > 0 {
|
|
|
|
|
m = m.Page(pageNum, pageSize)
|
|
|
|
|
}
|
|
|
|
|
r, totalInt, err := m.AllAndCount(false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
total = int64(totalInt)
|
|
|
|
|
err = r.Structs(&list)
|
|
|
|
|
return
|
|
|
|
|
}
|