pricing.go 2.0 KB

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