2
0

pricing.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package model
  2. import (
  3. "one-api/common"
  4. "one-api/dto"
  5. "sync"
  6. "time"
  7. )
  8. var (
  9. pricingMap []dto.ModelPricing
  10. lastGetPricingTime time.Time
  11. updatePricingLock sync.Mutex
  12. )
  13. func GetPricing(group string) []dto.ModelPricing {
  14. updatePricingLock.Lock()
  15. defer updatePricingLock.Unlock()
  16. if time.Since(lastGetPricingTime) > time.Minute*1 || len(pricingMap) == 0 {
  17. updatePricing()
  18. }
  19. if group != "" {
  20. userPricingMap := make([]dto.ModelPricing, 0)
  21. models := GetGroupModels(group)
  22. for _, pricing := range pricingMap {
  23. if !common.StringsContains(models, pricing.ModelName) {
  24. pricing.Available = false
  25. }
  26. userPricingMap = append(userPricingMap, pricing)
  27. }
  28. return userPricingMap
  29. }
  30. return pricingMap
  31. }
  32. func updatePricing() {
  33. //modelRatios := common.GetModelRatios()
  34. enabledModels := GetEnabledModels()
  35. allModels := make(map[string]int)
  36. for i, model := range enabledModels {
  37. allModels[model] = i
  38. }
  39. pricingMap = make([]dto.ModelPricing, 0)
  40. for model, _ := range allModels {
  41. pricing := dto.ModelPricing{
  42. Available: true,
  43. ModelName: model,
  44. }
  45. modelPrice, findPrice := common.GetModelPrice(model, false)
  46. if findPrice {
  47. pricing.ModelPrice = modelPrice
  48. pricing.QuotaType = 1
  49. } else {
  50. pricing.ModelRatio = common.GetModelRatio(model)
  51. pricing.CompletionRatio = common.GetCompletionRatio(model)
  52. pricing.QuotaType = 0
  53. }
  54. pricingMap = append(pricingMap, pricing)
  55. }
  56. lastGetPricingTime = time.Now()
  57. }