This commit is contained in:
Sakurasan
2023-04-18 22:46:11 +08:00
parent eb22de912a
commit 11dbf4376e
2 changed files with 73 additions and 28 deletions

View File

@@ -323,6 +323,7 @@ func HandleProy(c *gin.Context) {
} }
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
// todo // todo
log.Println(string(bodyRes))
} }
resbody := io.NopCloser(bytes.NewReader(bodyRes)) resbody := io.NopCloser(bytes.NewReader(bodyRes))
// 返回 API 响应主体 // 返回 API 响应主体
@@ -382,41 +383,27 @@ func HandleReverseProxy(c *gin.Context) {
} }
type Usage struct {
Cost string `json:"cost"`
UserID int `json:"userId"`
TotalUnit int `json:"totalUnit"`
}
func HandleUsage(c *gin.Context) { func HandleUsage(c *gin.Context) {
fromStr := c.Query("from") fromStr := c.Query("from")
toStr := c.Query("to") toStr := c.Query("to")
from, err := time.Parse("2006-01-02", fromStr) // from, err := time.Parse("2006-01-02", fromStr)
if err != nil { // if err != nil {
c.JSON(400, gin.H{"error": "Invalid from date format"}) // c.JSON(400, gin.H{"error": "Invalid from date format"})
return // return
} // }
to, err := time.Parse("2006-01-02", toStr) // to, err := time.Parse("2006-01-02", toStr)
if err != nil { // if err != nil {
c.JSON(400, gin.H{"error": "Invalid to date format"}) // c.JSON(400, gin.H{"error": "Invalid to date format"})
return // return
} // }
err = store.QueryUsage(from, to) usage, err := store.QueryUsage(fromStr, toStr)
if err != nil { if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()}) c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return return
} }
// Mock data for testing c.JSON(200, usage)
usage := Usage{
Cost: "0.000076",
UserID: 1,
TotalUnit: 38,
}
c.JSON(200, []Usage{usage})
} }

View File

@@ -1,6 +1,8 @@
package store package store
import "time" import (
"time"
)
type DailyUsage struct { type DailyUsage struct {
ID int `gorm:"column:id"` ID int `gorm:"column:id"`
@@ -33,6 +35,62 @@ func (Usage) TableName() string {
return "usages" return "usages"
} }
func QueryUsage(from, to time.Time) error { type Summary struct {
UserId int
// SumPromptUnits int
// SumCompletionUnits int
SumTotalUnit int
SumCost float64
}
type CalcUsage struct {
UserID int `json:"userId,omitempty"`
TotalUnit int `json:"totalUnit,omitempty"`
Cost string `json:"cost,omitempty"`
}
func QueryUsage(from, to string) ([]CalcUsage, error) {
var results = []CalcUsage{}
err := usage.Model(&DailyUsage{}).Select(`user_id,
--SUM(prompt_units) AS prompt_units,
-- SUM(completion_units) AS completion_units,
SUM(total_unit) AS total_unit,
SUM(cost) AS cost`).
Group("user_id").
Where("date >= ? AND date < ?", from, to).
Find(&results).Error
if err != nil {
return nil, err
}
return results, nil
}
func SumDaily(userid string) ([]Summary, error) {
return nil, nil
}
func SumDailyV2(uid string) error {
// err := usage.Model(&DailyUsage{}).
// Select("user_id, '2023-04-18' as date, sku, SUM(prompt_units) as sum_prompt_units, SUM(completion_units) as sum_completion_units, SUM(total_unit) as sum_total_unit, SUM(cost) as sum_cost").
// Where("date >= ?", "2023-04-18").
// Where("user_id = ?", 2).
// Create(&DailyUsage{}).Error
nowstr := time.Now().Format("2006-01-02")
err := usage.Exec(`INSERT INTO daily_usages
(user_id, date, sku, prompt_units, completion_units, total_unit, cost)
SELECT
user_id,
?,
sku,
SUM(prompt_units) AS sum_prompt_units,
SUM(completion_units) AS sum_completion_units,
SUM(total_unit) AS sum_total_unit,
SUM(cost) AS sum_cost
FROM usages
WHERE date >= ?
AND user_id = ?`, nowstr, nowstr, uid).Error
if err != nil {
return err
}
return nil return nil
} }