usage
This commit is contained in:
@@ -410,14 +410,25 @@ func HandleProy(c *gin.Context) {
|
|||||||
chatlog.TotalTokens = chatres.Usage.TotalTokens
|
chatlog.TotalTokens = chatres.Usage.TotalTokens
|
||||||
}
|
}
|
||||||
chatlog.Cost = Cost(chatlog.Model, chatlog.PromptCount, chatlog.CompletionCount)
|
chatlog.Cost = Cost(chatlog.Model, chatlog.PromptCount, chatlog.CompletionCount)
|
||||||
store.Record(&chatlog)
|
if err := store.Record(&chatlog); err != nil {
|
||||||
// todo insert usage && calc daily_usage
|
log.Println(err)
|
||||||
|
}
|
||||||
|
if err := store.SumDaily(chatlog.UserID); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
resbody := io.NopCloser(reader)
|
|
||||||
// 返回 API 响应主体
|
|
||||||
c.Writer.WriteHeader(resp.StatusCode)
|
c.Writer.WriteHeader(resp.StatusCode)
|
||||||
if _, err := io.Copy(c.Writer, resbody); err != nil {
|
if localuser {
|
||||||
|
// 返回 API 响应主体
|
||||||
|
if _, err := io.Copy(c.Writer, resbuf); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 返回 API 响应主体
|
||||||
|
if _, err := io.Copy(c.Writer, io.NopCloser(reader)); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Sakurasan/to"
|
"github.com/Sakurasan/to"
|
||||||
@@ -39,11 +38,11 @@ func (Usage) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Summary struct {
|
type Summary struct {
|
||||||
UserId int
|
UserId int `gorm:"column:user_id"`
|
||||||
// SumPromptUnits int
|
SumPromptUnits int `gorm:"column:sum_prompt_units"`
|
||||||
// SumCompletionUnits int
|
SumCompletionUnits int `gorm:"column:sum_completion_units"`
|
||||||
SumTotalUnit int
|
SumTotalUnit int `gorm:"column:sum_total_unit"`
|
||||||
SumCost float64
|
SumCost float64 `gorm:"column:sum_cost"`
|
||||||
}
|
}
|
||||||
type CalcUsage struct {
|
type CalcUsage struct {
|
||||||
UserID int `json:"userId,omitempty"`
|
UserID int `json:"userId,omitempty"`
|
||||||
@@ -93,22 +92,25 @@ func Record(chatlog *Tokens) (err error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SumDaily(userid string) ([]Summary, error) {
|
func SumDaily(userid int) error {
|
||||||
var count int64
|
var count int64
|
||||||
err := usage.Model(&DailyUsage{}).Where("user_id = ? and date = ?", userid, time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)).Count(&count).Error
|
err := usage.Model(&DailyUsage{}).Where("user_id = ? and date = ?", userid, time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)).Count(&count).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
return err
|
||||||
}
|
}
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
|
if err := insertSumDaily(userid); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if err := updateSumDaily(userid, time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertSumDaily(uid string) error {
|
func insertSumDaily(uid int) error {
|
||||||
nowstr := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
|
nowstr := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
|
||||||
err := usage.Exec(`INSERT INTO daily_usages
|
err := usage.Exec(`INSERT INTO daily_usages
|
||||||
(user_id, date, sku, prompt_units, completion_units, total_unit, cost)
|
(user_id, date, sku, prompt_units, completion_units, total_unit, cost)
|
||||||
@@ -129,23 +131,15 @@ func insertSumDaily(uid string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateSumDaily(uid string, date time.Time) error {
|
func updateSumDaily(uid int, date time.Time) error {
|
||||||
var u = Usage{}
|
// var u = Summary{}
|
||||||
err := usage.Exec(`SELECT
|
err := usage.Model(&Usage{}).Exec(`UPDATE daily_usages
|
||||||
user_id,
|
SET
|
||||||
?,
|
prompt_units = (SELECT SUM(prompt_units) FROM usages WHERE user_id = daily_usages.user_id AND date >= daily_usages.date),
|
||||||
sku,
|
completion_units = (SELECT SUM(completion_units) FROM usages WHERE user_id = daily_usages.user_id AND date >= daily_usages.date),
|
||||||
SUM(prompt_units) AS prompt_units,
|
total_unit = (SELECT SUM(total_unit) FROM usages WHERE user_id = daily_usages.user_id AND date >= daily_usages.date),
|
||||||
SUM(completion_units) AS completion_units,
|
cost = (SELECT SUM(cost) FROM usages WHERE user_id = daily_usages.user_id AND date >= daily_usages.date)
|
||||||
SUM(total_unit) AS total_unit,
|
WHERE user_id = ? AND date >= ?`, uid, date).Error
|
||||||
SUM(cost) AS cost
|
|
||||||
FROM usages
|
|
||||||
WHERE date >= ?
|
|
||||||
AND user_id = ?`, date, date, uid).First(&u).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = usage.Model(&DailyUsage{}).Where("user_id = ? and date = ?", uid, date).Updates(u).Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user