update record usage
This commit is contained in:
@@ -212,11 +212,7 @@ func (d *DailyUsageDAO) UpsertDailyUsage(ctx context.Context, usage *model.Usage
|
|||||||
return db.Clauses(clause.OnConflict{
|
return db.Clauses(clause.OnConflict{
|
||||||
Columns: []clause.Column{
|
Columns: []clause.Column{
|
||||||
{Name: "user_id"},
|
{Name: "user_id"},
|
||||||
{Name: "token_id"},
|
|
||||||
{Name: "capability"},
|
|
||||||
{Name: "date"},
|
{Name: "date"},
|
||||||
{Name: "model"},
|
|
||||||
{Name: "stream"},
|
|
||||||
},
|
},
|
||||||
DoUpdates: clause.Assignments(updateColumns),
|
DoUpdates: clause.Assignments(updateColumns),
|
||||||
}).Create(dailyUsage).Error
|
}).Create(dailyUsage).Error
|
||||||
@@ -231,11 +227,7 @@ func (d *DailyUsageDAO) UpsertDailyUsage(ctx context.Context, usage *model.Usage
|
|||||||
return db.Clauses(clause.OnConflict{
|
return db.Clauses(clause.OnConflict{
|
||||||
Columns: []clause.Column{
|
Columns: []clause.Column{
|
||||||
{Name: "user_id"},
|
{Name: "user_id"},
|
||||||
{Name: "token_id"},
|
|
||||||
{Name: "capability"},
|
|
||||||
{Name: "date"},
|
{Name: "date"},
|
||||||
{Name: "model"},
|
|
||||||
{Name: "stream"},
|
|
||||||
},
|
},
|
||||||
DoUpdates: clause.Assignments(updateColumns),
|
DoUpdates: clause.Assignments(updateColumns),
|
||||||
}).Create(dailyUsage).Error
|
}).Create(dailyUsage).Error
|
||||||
@@ -244,8 +236,8 @@ func (d *DailyUsageDAO) UpsertDailyUsage(ctx context.Context, usage *model.Usage
|
|||||||
default:
|
default:
|
||||||
return db.Transaction(func(tx *gorm.DB) error {
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
var existing model.DailyUsage
|
var existing model.DailyUsage
|
||||||
err := tx.Where("user_id = ? AND token_id = ? AND capability = ? AND date = ? AND model = ? AND stream = ?",
|
err := tx.Where("user_id = ? AND date = ?",
|
||||||
usage.UserID, usage.TokenID, usage.Capability, date, usage.Model, usage.Stream).
|
usage.UserID, date).
|
||||||
First(&existing).Error
|
First(&existing).Error
|
||||||
|
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"opencatd-open/store"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Usage struct {
|
type Usage struct {
|
||||||
@@ -28,9 +24,9 @@ func (Usage) TableName() string {
|
|||||||
|
|
||||||
type DailyUsage struct {
|
type DailyUsage struct {
|
||||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||||
UserID int64 `gorm:"column:user_id;uniqueIndex:idx_daily_unique,priority:1"`
|
UserID int64 `gorm:"column:user_id;uniqueIndex:idx_daily_unique,priority:1"` // uniqueIndex:idx_daily_unique,priority:1
|
||||||
TokenID int64 `gorm:"column:token_id;index:idx_daily_token_id"`
|
TokenID int64 `gorm:"column:token_id;uniqueIndex:idx_daily_unique,priority:2"`
|
||||||
Capability string `gorm:"column:capability;uniqueIndex:idx_daily_unique,priority:2;comment:模型能力"`
|
Capability string `gorm:"column:capability;index:idx_daily_usage_capability;comment:模型能力"`
|
||||||
Date time.Time `gorm:"column:date;autoCreateTime;uniqueIndex:idx_daily_unique,priority:3"`
|
Date time.Time `gorm:"column:date;autoCreateTime;uniqueIndex:idx_daily_unique,priority:3"`
|
||||||
Model string `gorm:"column:model"`
|
Model string `gorm:"column:model"`
|
||||||
Stream bool `gorm:"column:stream"`
|
Stream bool `gorm:"column:stream"`
|
||||||
@@ -43,32 +39,3 @@ type DailyUsage struct {
|
|||||||
func (DailyUsage) TableName() string {
|
func (DailyUsage) TableName() string {
|
||||||
return "daily_usages"
|
return "daily_usages"
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleUsage(c *gin.Context) {
|
|
||||||
fromStr := c.Query("from")
|
|
||||||
toStr := c.Query("to")
|
|
||||||
getMonthStartAndEnd := func() (start, end string) {
|
|
||||||
loc, _ := time.LoadLocation("Local")
|
|
||||||
now := time.Now().In(loc)
|
|
||||||
|
|
||||||
year, month, _ := now.Date()
|
|
||||||
|
|
||||||
startOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, loc)
|
|
||||||
endOfMonth := startOfMonth.AddDate(0, 1, 0)
|
|
||||||
|
|
||||||
start = startOfMonth.Format("2006-01-02")
|
|
||||||
end = endOfMonth.Format("2006-01-02")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if fromStr == "" || toStr == "" {
|
|
||||||
fromStr, toStr = getMonthStartAndEnd()
|
|
||||||
}
|
|
||||||
|
|
||||||
usage, err := store.QueryUsage(fromStr, toStr)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(200, usage)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user