consumeerr.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package model
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/bytedance/sonic"
  7. "github.com/labring/aiproxy/core/common"
  8. )
  9. type ConsumeError struct {
  10. RequestAt time.Time `gorm:"index;index:idx_consume_error_group_reqat,priority:2" json:"request_at"`
  11. CreatedAt time.Time ` json:"created_at"`
  12. GroupID string `gorm:"size:64;index;index:idx_consume_error_group_reqat,priority:1" json:"group_id"`
  13. RequestID string `gorm:"type:char(16);index" json:"request_id"`
  14. TokenName EmptyNullString `gorm:"size:32;not null" json:"token_name"`
  15. Model string `gorm:"size:64" json:"model"`
  16. Content string `gorm:"type:text" json:"content"`
  17. ID int `gorm:"primaryKey" json:"id"`
  18. UsedAmount float64 ` json:"used_amount"`
  19. TokenID int ` json:"token_id"`
  20. }
  21. func (c *ConsumeError) MarshalJSON() ([]byte, error) {
  22. type Alias ConsumeError
  23. return sonic.Marshal(&struct {
  24. *Alias
  25. CreatedAt int64 `json:"created_at"`
  26. RequestAt int64 `json:"request_at"`
  27. }{
  28. Alias: (*Alias)(c),
  29. CreatedAt: c.CreatedAt.UnixMilli(),
  30. RequestAt: c.RequestAt.UnixMilli(),
  31. })
  32. }
  33. func CreateConsumeError(
  34. requestID string,
  35. requestAt time.Time,
  36. group, tokenName, model, content string,
  37. usedAmount float64,
  38. tokenID int,
  39. ) error {
  40. return LogDB.Create(&ConsumeError{
  41. RequestID: requestID,
  42. RequestAt: requestAt,
  43. GroupID: group,
  44. TokenName: EmptyNullString(tokenName),
  45. Model: model,
  46. Content: content,
  47. UsedAmount: usedAmount,
  48. TokenID: tokenID,
  49. }).Error
  50. }
  51. func SearchConsumeError(
  52. keyword, requestID, group, tokenName, model string,
  53. tokenID, page, perPage int,
  54. order string,
  55. ) ([]*ConsumeError, int64, error) {
  56. tx := LogDB.Model(&ConsumeError{})
  57. // Handle exact match conditions for non-zero values
  58. if group != "" {
  59. tx = tx.Where("group_id = ?", group)
  60. }
  61. if requestID != "" {
  62. tx = tx.Where("request_id = ?", requestID)
  63. }
  64. if tokenName != "" {
  65. tx = tx.Where("token_name = ?", tokenName)
  66. }
  67. if model != "" {
  68. tx = tx.Where("model = ?", model)
  69. }
  70. if tokenID != 0 {
  71. tx = tx.Where("token_id = ?", tokenID)
  72. }
  73. // Handle keyword search for zero value fields
  74. if keyword != "" {
  75. var (
  76. conditions []string
  77. values []any
  78. )
  79. if requestID == "" {
  80. if !common.UsingSQLite {
  81. conditions = append(conditions, "request_id ILIKE ?")
  82. } else {
  83. conditions = append(conditions, "request_id LIKE ?")
  84. }
  85. values = append(values, "%"+keyword+"%")
  86. }
  87. if group == "" {
  88. if !common.UsingSQLite {
  89. conditions = append(conditions, "group_id ILIKE ?")
  90. } else {
  91. conditions = append(conditions, "group_id LIKE ?")
  92. }
  93. values = append(values, "%"+keyword+"%")
  94. }
  95. if tokenName == "" {
  96. if !common.UsingSQLite {
  97. conditions = append(conditions, "token_name ILIKE ?")
  98. } else {
  99. conditions = append(conditions, "token_name LIKE ?")
  100. }
  101. values = append(values, "%"+keyword+"%")
  102. }
  103. if model == "" {
  104. if !common.UsingSQLite {
  105. conditions = append(conditions, "model ILIKE ?")
  106. } else {
  107. conditions = append(conditions, "model LIKE ?")
  108. }
  109. values = append(values, "%"+keyword+"%")
  110. }
  111. if len(conditions) > 0 {
  112. tx = tx.Where(fmt.Sprintf("(%s)", strings.Join(conditions, " OR ")), values...)
  113. }
  114. }
  115. var total int64
  116. err := tx.Count(&total).Error
  117. if err != nil {
  118. return nil, 0, err
  119. }
  120. if total <= 0 {
  121. return nil, 0, nil
  122. }
  123. var errors []*ConsumeError
  124. limit, offset := toLimitOffset(page, perPage)
  125. err = tx.Order(getLogOrder(order)).Limit(limit).Offset(offset).Find(&errors).Error
  126. return errors, total, err
  127. }