add usage
This commit is contained in:
@@ -38,6 +38,8 @@ func main() {
|
|||||||
// 获取所有用户信息
|
// 获取所有用户信息
|
||||||
group.GET("/users", router.HandleUsers)
|
group.GET("/users", router.HandleUsers)
|
||||||
|
|
||||||
|
group.GET("/usages", router.HandleUsage)
|
||||||
|
|
||||||
// 添加Key
|
// 添加Key
|
||||||
group.POST("/keys", router.HandleAddKey)
|
group.POST("/keys", router.HandleAddKey)
|
||||||
|
|
||||||
|
|||||||
@@ -381,3 +381,42 @@ func HandleReverseProxy(c *gin.Context) {
|
|||||||
proxy.ServeHTTP(c.Writer, req)
|
proxy.ServeHTTP(c.Writer, req)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Usage struct {
|
||||||
|
Cost string `json:"cost"`
|
||||||
|
UserID int `json:"userId"`
|
||||||
|
TotalUnit int `json:"totalUnit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleUsage(c *gin.Context) {
|
||||||
|
fromStr := c.Query("from")
|
||||||
|
toStr := c.Query("to")
|
||||||
|
|
||||||
|
from, err := time.Parse("2006-01-02", fromStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "Invalid from date format"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
to, err := time.Parse("2006-01-02", toStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "Invalid to date format"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = store.QueryUsage(from, to)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mock data for testing
|
||||||
|
usage := Usage{
|
||||||
|
Cost: "0.000076",
|
||||||
|
UserID: 1,
|
||||||
|
TotalUnit: 38,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, []Usage{usage})
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
11
store/db.go
11
store/db.go
@@ -11,6 +11,8 @@ import (
|
|||||||
|
|
||||||
var db *gorm.DB
|
var db *gorm.DB
|
||||||
|
|
||||||
|
var usage *gorm.DB
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if _, err := os.Stat("db"); os.IsNotExist(err) {
|
if _, err := os.Stat("db"); os.IsNotExist(err) {
|
||||||
errDir := os.MkdirAll("db", 0755)
|
errDir := os.MkdirAll("db", 0755)
|
||||||
@@ -31,4 +33,13 @@ func init() {
|
|||||||
}
|
}
|
||||||
LoadKeysCache()
|
LoadKeysCache()
|
||||||
LoadAuthCache()
|
LoadAuthCache()
|
||||||
|
|
||||||
|
usage, err = gorm.Open(sqlite.Open("./db/usage.db"), &gorm.Config{})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = usage.AutoMigrate(&DailyUsage{}, &Usage{})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
38
store/usage.go
Normal file
38
store/usage.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type DailyUsage struct {
|
||||||
|
ID int `gorm:"column:id"`
|
||||||
|
UserID int `gorm:"column:user_id primarykey"`
|
||||||
|
Date time.Time `gorm:"column:date"`
|
||||||
|
SKU string `gorm:"column:sku"`
|
||||||
|
PromptUnits int `gorm:"column:prompt_units"`
|
||||||
|
CompletionUnits int `gorm:"column:completion_units"`
|
||||||
|
TotalUnit int `gorm:"column:total_unit"`
|
||||||
|
Cost string `gorm:"column:cost"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (DailyUsage) TableName() string {
|
||||||
|
return "daily_usages"
|
||||||
|
}
|
||||||
|
|
||||||
|
type Usage struct {
|
||||||
|
ID int `gorm:"column:id"`
|
||||||
|
PromptHash string `gorm:"column:prompt_hash"`
|
||||||
|
UserID int `gorm:"column:user_id"`
|
||||||
|
Date time.Time `gorm:"column:date"`
|
||||||
|
SKU string `gorm:"column:sku"`
|
||||||
|
PromptUnits int `gorm:"column:prompt_units"`
|
||||||
|
CompletionUnits int `gorm:"column:completion_units"`
|
||||||
|
TotalUnit int `gorm:"column:total_unit"`
|
||||||
|
Cost string `gorm:"column:cost"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (Usage) TableName() string {
|
||||||
|
return "usages"
|
||||||
|
}
|
||||||
|
|
||||||
|
func QueryUsage(from, to time.Time) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user