pricing.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package model
  2. import (
  3. "one-api/common"
  4. "sync"
  5. "time"
  6. )
  7. type Pricing struct {
  8. ModelName string `json:"model_name"`
  9. QuotaType int `json:"quota_type"`
  10. ModelRatio float64 `json:"model_ratio"`
  11. ModelPrice float64 `json:"model_price"`
  12. OwnerBy string `json:"owner_by"`
  13. CompletionRatio float64 `json:"completion_ratio"`
  14. EnableGroup []string `json:"enable_groups,omitempty"`
  15. }
  16. var (
  17. pricingMap []Pricing
  18. lastGetPricingTime time.Time
  19. updatePricingLock sync.Mutex
  20. )
  21. func GetPricing() []Pricing {
  22. updatePricingLock.Lock()
  23. defer updatePricingLock.Unlock()
  24. if time.Since(lastGetPricingTime) > time.Minute*1 || len(pricingMap) == 0 {
  25. updatePricing()
  26. }
  27. //if group != "" {
  28. // userPricingMap := make([]Pricing, 0)
  29. // models := GetGroupModels(group)
  30. // for _, pricing := range pricingMap {
  31. // if !common.StringsContains(models, pricing.ModelName) {
  32. // pricing.Available = false
  33. // }
  34. // userPricingMap = append(userPricingMap, pricing)
  35. // }
  36. // return userPricingMap
  37. //}
  38. return pricingMap
  39. }
  40. func updatePricing() {
  41. //modelRatios := common.GetModelRatios()
  42. enableAbilities := GetAllEnableAbilities()
  43. modelGroupsMap := make(map[string][]string)
  44. for _, ability := range enableAbilities {
  45. groups := modelGroupsMap[ability.Model]
  46. if groups == nil {
  47. groups = make([]string, 0)
  48. }
  49. if !common.StringsContains(groups, ability.Group) {
  50. groups = append(groups, ability.Group)
  51. }
  52. modelGroupsMap[ability.Model] = groups
  53. }
  54. pricingMap = make([]Pricing, 0)
  55. for model, groups := range modelGroupsMap {
  56. pricing := Pricing{
  57. ModelName: model,
  58. EnableGroup: groups,
  59. }
  60. modelPrice, findPrice := common.GetModelPrice(model, false)
  61. if findPrice {
  62. pricing.ModelPrice = modelPrice
  63. pricing.QuotaType = 1
  64. } else {
  65. pricing.ModelRatio = common.GetModelRatio(model)
  66. pricing.CompletionRatio = common.GetCompletionRatio(model)
  67. pricing.QuotaType = 0
  68. }
  69. pricingMap = append(pricingMap, pricing)
  70. }
  71. lastGetPricingTime = time.Now()
  72. }