forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan.go
285 lines (227 loc) · 7.06 KB
/
plan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package channel
import (
"chat/globals"
"chat/utils"
"database/sql"
"errors"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
"strings"
"time"
)
type PlanManager struct {
Enabled bool `json:"enabled" mapstructure:"enabled"`
Plans []Plan `json:"plans" mapstructure:"plans"`
}
type Plan struct {
Level int `json:"level" mapstructure:"level"`
Price float32 `json:"price" mapstructure:"price"`
Items []PlanItem `json:"items" mapstructure:"items"`
}
type PlanItem struct {
Id string `json:"id" mapstructure:"id"`
Name string `json:"name" mapstructure:"name"`
Icon string `json:"icon" mapstructure:"icon"`
Value int64 `json:"value" mapstructure:"value"`
Models []string `json:"models" mapstructure:"models"`
}
type Usage struct {
Used int64 `json:"used" mapstructure:"used"`
Total int64 `json:"total" mapstructure:"total"`
}
type UsageMap map[string]Usage
var planExp int64 = 0
func NewPlanManager() *PlanManager {
manager := &PlanManager{}
if err := viper.UnmarshalKey("subscription", manager); err != nil {
panic(err)
}
return manager
}
func (c *PlanManager) SaveConfig() error {
viper.Set("subscription", c)
return viper.WriteConfig()
}
func (c *PlanManager) UpdateConfig(data *PlanManager) error {
c.Enabled = data.Enabled
c.Plans = data.Plans
return c.SaveConfig()
}
func (c *PlanManager) GetPlan(level int) Plan {
for _, plan := range c.Plans {
if plan.Level == level {
return plan
}
}
return Plan{}
}
func (c *PlanManager) GetPlans() []Plan {
if c.Enabled {
return c.Plans
}
return []Plan{}
}
func (c *PlanManager) GetRawPlans() []Plan {
return c.Plans
}
func (c *PlanManager) IsEnabled() bool {
return c.Enabled
}
func getOffsetFormat(offset time.Time, usage int64) string {
return fmt.Sprintf("%s/%d", offset.Format("2006-01-02:15:04:05"), usage)
}
func GetSubscriptionUsage(cache *redis.Client, user globals.AuthLike, t string) (usage int64, offset time.Time) {
// example cache value: 2021-09-01:19:00:00/100
// if date is longer than 1 month, reset usage
offset = time.Now()
key := globals.GetSubscriptionLimitFormat(t, user.HitID())
v, err := utils.GetCache(cache, key)
if (err != nil && errors.Is(err, redis.Nil)) || len(v) == 0 {
usage = 0
}
seg := strings.Split(v, "/")
if len(seg) != 2 {
usage = 0
} else {
date, err := time.Parse("2006-01-02:15:04:05", seg[0])
usage = utils.ParseInt64(seg[1])
if err != nil {
usage = 0
}
// check if date is longer than current date after 1 month, if true, reset usage
if date.AddDate(0, 1, 0).Before(time.Now()) {
// date is longer than 1 month, reset usage
usage = 0
// get current date offset (1 month step)
// example: 2021-09-01:19:00:0/100 -> 2021-10-01:19:00:00/100
// copy date to offset
offset = date
// example:
// current time: 2021-09-08:14:00:00
// offset: 2021-07-01:19:00:00
// expected offset: 2021-09-01:19:00:00
// offset is not longer than current date, stop adding 1 month
for offset.AddDate(0, 1, 0).Before(time.Now()) {
offset = offset.AddDate(0, 1, 0)
}
} else {
// date is not longer than 1 month, use current date value
offset = date
}
}
// set new cache value
_ = utils.SetCache(cache, key, getOffsetFormat(offset, usage), planExp)
return
}
func IncreaseSubscriptionUsage(cache *redis.Client, user globals.AuthLike, t string, limit int64) bool {
key := globals.GetSubscriptionLimitFormat(t, user.HitID())
usage, offset := GetSubscriptionUsage(cache, user, t)
usage += 1
if usage > limit {
return false
}
// set new cache value
err := utils.SetCache(cache, key, getOffsetFormat(offset, usage), planExp)
return err == nil
}
func DecreaseSubscriptionUsage(cache *redis.Client, user globals.AuthLike, t string) bool {
key := globals.GetSubscriptionLimitFormat(t, user.HitID())
usage, offset := GetSubscriptionUsage(cache, user, t)
usage -= 1
if usage < 0 {
return true
}
// set new cache value
err := utils.SetCache(cache, key, getOffsetFormat(offset, usage), planExp)
return err == nil
}
func ReleaseSubscriptionUsage(cache *redis.Client, user globals.AuthLike, t string) bool {
key := globals.GetSubscriptionLimitFormat(t, user.HitID())
_, offset := GetSubscriptionUsage(cache, user, t)
// set new cache value
err := utils.SetCache(cache, key, getOffsetFormat(offset, 0), planExp)
return err == nil
}
func (p *Plan) GetUsage(user globals.AuthLike, db *sql.DB, cache *redis.Client) UsageMap {
return utils.EachObject[PlanItem, Usage](p.Items, func(usage PlanItem) (string, Usage) {
return usage.Id, usage.GetUsageForm(user, db, cache)
})
}
func (p *PlanItem) GetUsage(user globals.AuthLike, db *sql.DB, cache *redis.Client) int64 {
// preflight check
user.GetID(db)
usage, _ := GetSubscriptionUsage(cache, user, p.Id)
return usage
}
func (p *PlanItem) ResetUsage(user globals.AuthLike, cache *redis.Client) bool {
key := globals.GetSubscriptionLimitFormat(p.Id, user.HitID())
_, offset := GetSubscriptionUsage(cache, user, p.Id)
err := utils.SetCache(cache, key, getOffsetFormat(offset, 0), planExp)
return err == nil
}
func (p *PlanItem) CreateUsage(user globals.AuthLike, cache *redis.Client) bool {
key := globals.GetSubscriptionLimitFormat(p.Id, user.HitID())
err := utils.SetCache(cache, key, getOffsetFormat(time.Now(), 0), planExp)
return err == nil
}
func (p *PlanItem) GetUsageForm(user globals.AuthLike, db *sql.DB, cache *redis.Client) Usage {
return Usage{
Used: p.GetUsage(user, db, cache),
Total: p.Value,
}
}
func (p *PlanItem) IsInfinity() bool {
return p.Value == -1
}
func (p *PlanItem) IsExceeded(user globals.AuthLike, db *sql.DB, cache *redis.Client) bool {
return p.IsInfinity() || p.GetUsage(user, db, cache) < p.Value
}
func (p *PlanItem) Increase(user globals.AuthLike, cache *redis.Client) bool {
state := IncreaseSubscriptionUsage(cache, user, p.Id, p.Value)
return state || p.IsInfinity()
}
func (p *PlanItem) Decrease(user globals.AuthLike, cache *redis.Client) bool {
if p.Value == -1 {
return true
}
return DecreaseSubscriptionUsage(cache, user, p.Id)
}
func (p *PlanItem) Release(user globals.AuthLike, cache *redis.Client) bool {
return ReleaseSubscriptionUsage(cache, user, p.Id)
}
func (p *Plan) IncreaseUsage(user globals.AuthLike, cache *redis.Client, model string) bool {
for _, usage := range p.Items {
if utils.Contains(model, usage.Models) {
return usage.Increase(user, cache)
}
}
return false
}
func (p *Plan) DecreaseUsage(user globals.AuthLike, cache *redis.Client, model string) bool {
for _, usage := range p.Items {
if utils.Contains(model, usage.Models) {
return usage.Decrease(user, cache)
}
}
return false
}
func (p *Plan) ReleaseUsage(user globals.AuthLike, cache *redis.Client, model string) bool {
for _, usage := range p.Items {
if utils.Contains(model, usage.Models) {
return usage.Release(user, cache)
}
}
return false
}
func (p *Plan) ReleaseAll(user globals.AuthLike, cache *redis.Client) bool {
for _, usage := range p.Items {
if !usage.Release(user, cache) {
return false
}
}
return true
}
func IsValidPlan(level int) bool {
return utils.InRange(level, 1, 3)
}