mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-10-31 05:43:42 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			134 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package controller
 | |
| 
 | |
| import (
 | |
| 	"github.com/gin-gonic/gin"
 | |
| 	"one-api/common"
 | |
| 	"one-api/model"
 | |
| 	"strconv"
 | |
| )
 | |
| 
 | |
| func GetAllLogs(c *gin.Context) {
 | |
| 	p, _ := strconv.Atoi(c.Query("p"))
 | |
| 	if p < 0 {
 | |
| 		p = 0
 | |
| 	}
 | |
| 	logType, _ := strconv.Atoi(c.Query("type"))
 | |
| 	startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
 | |
| 	endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
 | |
| 	username := c.Query("username")
 | |
| 	tokenName := c.Query("token_name")
 | |
| 	modelName := c.Query("model_name")
 | |
| 	logs, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
 | |
| 	if err != nil {
 | |
| 		c.JSON(200, gin.H{
 | |
| 			"success": false,
 | |
| 			"message": err.Error(),
 | |
| 		})
 | |
| 		return
 | |
| 	}
 | |
| 	c.JSON(200, gin.H{
 | |
| 		"success": true,
 | |
| 		"message": "",
 | |
| 		"data":    logs,
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func GetUserLogs(c *gin.Context) {
 | |
| 	p, _ := strconv.Atoi(c.Query("p"))
 | |
| 	if p < 0 {
 | |
| 		p = 0
 | |
| 	}
 | |
| 	userId := c.GetInt("id")
 | |
| 	logType, _ := strconv.Atoi(c.Query("type"))
 | |
| 	startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
 | |
| 	endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
 | |
| 	tokenName := c.Query("token_name")
 | |
| 	modelName := c.Query("model_name")
 | |
| 	logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
 | |
| 	if err != nil {
 | |
| 		c.JSON(200, gin.H{
 | |
| 			"success": false,
 | |
| 			"message": err.Error(),
 | |
| 		})
 | |
| 		return
 | |
| 	}
 | |
| 	c.JSON(200, gin.H{
 | |
| 		"success": true,
 | |
| 		"message": "",
 | |
| 		"data":    logs,
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func SearchAllLogs(c *gin.Context) {
 | |
| 	keyword := c.Query("keyword")
 | |
| 	logs, err := model.SearchAllLogs(keyword)
 | |
| 	if err != nil {
 | |
| 		c.JSON(200, gin.H{
 | |
| 			"success": false,
 | |
| 			"message": err.Error(),
 | |
| 		})
 | |
| 		return
 | |
| 	}
 | |
| 	c.JSON(200, gin.H{
 | |
| 		"success": true,
 | |
| 		"message": "",
 | |
| 		"data":    logs,
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func SearchUserLogs(c *gin.Context) {
 | |
| 	keyword := c.Query("keyword")
 | |
| 	userId := c.GetInt("id")
 | |
| 	logs, err := model.SearchUserLogs(userId, keyword)
 | |
| 	if err != nil {
 | |
| 		c.JSON(200, gin.H{
 | |
| 			"success": false,
 | |
| 			"message": err.Error(),
 | |
| 		})
 | |
| 		return
 | |
| 	}
 | |
| 	c.JSON(200, gin.H{
 | |
| 		"success": true,
 | |
| 		"message": "",
 | |
| 		"data":    logs,
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func GetLogsStat(c *gin.Context) {
 | |
| 	logType, _ := strconv.Atoi(c.Query("type"))
 | |
| 	startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
 | |
| 	endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
 | |
| 	tokenName := c.Query("token_name")
 | |
| 	username := c.Query("username")
 | |
| 	modelName := c.Query("model_name")
 | |
| 	quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
 | |
| 	//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
 | |
| 	c.JSON(200, gin.H{
 | |
| 		"success": true,
 | |
| 		"message": "",
 | |
| 		"data": gin.H{
 | |
| 			"quota": quotaNum,
 | |
| 			//"token": tokenNum,
 | |
| 		},
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func GetLogsSelfStat(c *gin.Context) {
 | |
| 	username := c.GetString("username")
 | |
| 	logType, _ := strconv.Atoi(c.Query("type"))
 | |
| 	startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
 | |
| 	endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
 | |
| 	tokenName := c.Query("token_name")
 | |
| 	modelName := c.Query("model_name")
 | |
| 	quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
 | |
| 	//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
 | |
| 	c.JSON(200, gin.H{
 | |
| 		"success": true,
 | |
| 		"message": "",
 | |
| 		"data": gin.H{
 | |
| 			"quota": quotaNum,
 | |
| 			//"token": tokenNum,
 | |
| 		},
 | |
| 	})
 | |
| }
 |