mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 17:16:38 +08:00
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package model
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"sync"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/songquanpeng/one-api/common/helper"
|
|
"github.com/songquanpeng/one-api/common/logger"
|
|
)
|
|
|
|
type UserRequestCost struct {
|
|
Id int `json:"id"`
|
|
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
|
UserID int `json:"user_id"`
|
|
RequestID string `json:"request_id"`
|
|
Quota int64 `json:"quota"`
|
|
CostUSD float64 `json:"cost_usd" gorm:"-"`
|
|
}
|
|
|
|
// NewUserRequestCost create a new UserRequestCost
|
|
func NewUserRequestCost(userID int, quotaID string, quota int64) *UserRequestCost {
|
|
return &UserRequestCost{
|
|
CreatedTime: helper.GetTimestamp(),
|
|
UserID: userID,
|
|
RequestID: quotaID,
|
|
Quota: quota,
|
|
}
|
|
}
|
|
|
|
func (docu *UserRequestCost) Insert() error {
|
|
go removeOldRequestCost()
|
|
|
|
err := DB.Create(docu).Error
|
|
return errors.Wrap(err, "failed to insert UserRequestCost")
|
|
}
|
|
|
|
// GetCostByRequestId get cost by request id
|
|
func GetCostByRequestId(reqid string) (*UserRequestCost, error) {
|
|
if reqid == "" {
|
|
return nil, errors.New("request id is empty")
|
|
}
|
|
|
|
docu := &UserRequestCost{RequestID: reqid}
|
|
var err error = nil
|
|
if err = DB.First(docu, "request_id = ?", reqid).Error; err != nil {
|
|
return nil, errors.Wrap(err, "failed to get cost by request id")
|
|
}
|
|
|
|
docu.CostUSD = float64(docu.Quota) / 500000
|
|
return docu, nil
|
|
}
|
|
|
|
var muRemoveOldRequestCost sync.Mutex
|
|
|
|
// removeOldRequestCost remove old request cost data,
|
|
// this function will be executed every 1/1000 times.
|
|
func removeOldRequestCost() {
|
|
if rand.Float32() > 0.001 {
|
|
return
|
|
}
|
|
|
|
if ok := muRemoveOldRequestCost.TryLock(); !ok {
|
|
return
|
|
}
|
|
defer muRemoveOldRequestCost.Unlock()
|
|
|
|
err := DB.
|
|
Where("created_time < ?", helper.GetTimestamp()-3600*24*7).
|
|
Delete(&UserRequestCost{}).Error
|
|
if err != nil {
|
|
logger.SysError(fmt.Sprintf("failed to remove old request cost: %s", err.Error()))
|
|
}
|
|
}
|