Skip to content

Commit d07cc5c

Browse files
Merge use_limit column changes (#654)
- Add use_limit_price to account_prices - Remove CountNumberType from ChargeValueType - Update SubscriptionUpdateReq structure - Introduce AcctPriceListFilter for pagination - Add Discount and RegularValue to AcctEventReq - Enhance SubscriptionBillResp with Discount - Add SubUsageReq for usage tracking Co-authored-by: Haihui.Wang <[email protected]>
1 parent a7a597f commit d07cc5c

File tree

5 files changed

+102
-63
lines changed

5 files changed

+102
-63
lines changed

accounting/consumer/metering.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (m *Metering) startMetering() {
5252

5353
func (m *Metering) preReadMsgs() {
5454
var err error
55-
var i int = 0
55+
var i = 0
5656
for {
5757
i++
5858
err = m.sysMQ.BuildMeterEventStream()
@@ -75,10 +75,7 @@ func (m *Metering) preReadMsgs() {
7575

7676
func (m *Metering) handleReadMsgs(failedLimit int) {
7777
failReadTime := 0
78-
for {
79-
if failReadTime >= failedLimit {
80-
break
81-
}
78+
for failReadTime < failedLimit {
8279
err := m.sysMQ.VerifyMeteringStream()
8380
if err != nil {
8481
tip := fmt.Sprintf("fail to verify metering stream for the %d time", (failReadTime + 1))
@@ -232,8 +229,6 @@ func (m *Metering) pubFeeEventWithReTry(msg jetstream.Msg, evt *types.MeteringEv
232229
err = m.sysMQ.PublishFeeTokenData(msg.Data())
233230
case types.QuotaNumberType:
234231
err = m.sysMQ.PublishFeeQuotaData(msg.Data())
235-
case types.CountNumberType:
236-
err = m.sysMQ.PublishSubscriptionData(msg.Data())
237232
default:
238233
slog.Warn("unsupported metering event value type", slog.Any("value-type", evt.ValueType))
239234
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SET statement_timeout = 0;
2+
3+
--bun:split
4+
5+
ALTER TABLE account_prices DROP COLUMN IF EXISTS use_limit_price;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SET statement_timeout = 0;
2+
3+
--bun:split
4+
5+
ALTER TABLE account_prices ADD COLUMN IF NOT EXISTS use_limit_price BIGINT DEFAULT 0;
6+

common/types/accounting.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const (
2020
MeterFromSource = "from_source"
2121
PromptTokenNum = "prompt_token_num"
2222
CompletionTokenNum = "completion_token_num"
23-
TotalQuotaCount = "total_quota_count"
24-
SubBillID = "sub_bill_id"
2523
)
2624

2725
type OrderStatus int
@@ -101,8 +99,6 @@ var (
10199
// starship
102100
SceneStarship SceneType = 20 // starship
103101
SceneGuiAgent SceneType = 22 // gui agent
104-
105-
SceneSubscription SceneType = 30 // accounting subscription
106102
// unknow
107103
SceneUnknow SceneType = 99 // unknow
108104
)
@@ -113,7 +109,6 @@ var (
113109
TimeDurationMinType ChargeValueType = 0
114110
TokenNumberType ChargeValueType = 1
115111
QuotaNumberType ChargeValueType = 2
116-
CountNumberType ChargeValueType = 3
117112
)
118113

119114
type AcctEventReq struct {
@@ -137,6 +132,8 @@ type AcctEventReq struct {
137132
SkuPriceCurrency string `json:"sku_price_currency"`
138133
Quota float64 `json:"quota"`
139134
SubBillID int64 `json:"sub_bill_id"`
135+
Discount float64 `json:"discount"`
136+
RegularValue float64 `json:"regular_value"`
140137
}
141138

142139
// generate charge event from client
@@ -305,6 +302,8 @@ type AcctPriceCreateReq struct {
305302
SkuKind SKUKind `json:"sku_kind"`
306303
Quota string `json:"quota"`
307304
SkuPriceID int64 `json:"sku_price_id"`
305+
Discount float64 `json:"discount" binding:"omitempty,min=0,max=1"`
306+
UseLimitPrice int64 `json:"use_limit_price"`
308307
}
309308

310309
type AcctPriceResp struct {
@@ -353,12 +352,31 @@ type AcctOrderExpiredEvent struct {
353352
CreatedAt time.Time `json:"created_at"`
354353
}
355354

355+
// used for listing prices with pagination
356+
//
357+
// in accounting price DB
358+
type AcctPriceListDBReq struct {
359+
SkuType SKUType `json:"sku_type"`
360+
SkuKind string `json:"sku_kind"`
361+
ResourceID []string `json:"resource_id"`
362+
Per int `json:"per"`
363+
Page int `json:"page"`
364+
}
365+
366+
// used for listing prices with pagination and filter
367+
//
368+
// in accounting service and starhub server
356369
type AcctPriceListReq struct {
357-
SkuType SKUType `json:"sku_type"`
358-
SkuKind string `json:"sku_kind"`
359-
ResourceID string `json:"resource_id"`
360-
Per int `json:"per"`
361-
Page int `json:"page"`
370+
SkuType SKUType `json:"sku_type"`
371+
SkuKind string `json:"sku_kind"`
372+
ResourceID []string `json:"resource_id"`
373+
Filter AcctPriceListFilter `json:"filter"`
374+
Per int `json:"per"`
375+
Page int `json:"page"`
376+
}
377+
378+
type AcctPriceListFilter struct {
379+
HardwareType string `json:"hardware_type"`
362380
}
363381

364382
type AcctRechargeReq struct {
@@ -438,8 +456,9 @@ type AcctRecharge struct {
438456
}
439457

440458
type AcctRechargeListResp struct {
441-
Data []AcctRecharge `json:"data"`
442-
Total int `json:"total"`
459+
Data []AcctRecharge `json:"data"`
460+
Total int `json:"total"`
461+
TotalValue float64 `json:"total_value"`
443462
}
444463

445464
type RechargesIndexReq struct {
@@ -604,3 +623,11 @@ type RechargeStats struct {
604623
Count int `bun:"count"`
605624
Sum int64 `bun:"sum"`
606625
}
626+
627+
type AccountPresentStatus int
628+
629+
const (
630+
AccountPresentStatusInit AccountPresentStatus = 0
631+
AccountPresentStatusUsed AccountPresentStatus = 1
632+
AccountPresentStatusCanceled AccountPresentStatus = 2
633+
)

common/types/accounting_subscription.go

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"github.com/google/uuid"
77
)
88

9-
var AccountingSubscriptionQueue = "accounting_subscription_queue"
10-
119
var (
1210
SubscriptionFree = "free"
1311
)
@@ -39,13 +37,13 @@ const (
3937
BillingStatusFailed BillingStatus = "unpaid"
4038
)
4139

42-
type SubscriptionCreateReq struct {
43-
CurrentUser string `json:"-"`
44-
UserUUID string `json:"-"`
45-
SkuType SKUType `json:"sku_type" binding:"required"`
46-
ResourceID string `json:"resource_id" binding:"required"`
47-
SkuUnitType string `json:"sku_unit_type" binding:"required"`
48-
EventUUID uuid.UUID `json:"-"`
40+
type SubscriptionUpdateReq struct {
41+
CurrentUser string `json:"-"`
42+
UserUUID string `json:"-"`
43+
EventUUID uuid.UUID `json:"-"`
44+
SkuType SKUType `json:"sku_type" binding:"required,oneof=1 2"`
45+
ResourceID string `json:"resource_id" binding:"required"`
46+
SkuUnitType SkuUnitType `json:"sku_unit_type" binding:"required,oneof=month"`
4947
}
5048

5149
type SubscriptionResp struct {
@@ -69,15 +67,15 @@ type SubscriptionResp struct {
6967
}
7068

7169
type SubscriptionListReq struct {
72-
CurrentUser string `json:"-"`
73-
UserUUID string `json:"-"`
74-
Status string `json:"-"`
75-
SkuType int `json:"-"`
76-
StartTime string `json:"-"`
77-
EndTime string `json:"-"`
78-
Per int `json:"-"`
79-
Page int `json:"-"`
80-
QueryUserUUID string `json:"-"`
70+
CurrentUser string `json:"-"`
71+
UserUUID string `json:"-"`
72+
Status SubscriptionStatus `json:"-"`
73+
SkuType SKUType `json:"-"`
74+
StartTime string `json:"-"`
75+
EndTime string `json:"-"`
76+
Per int `json:"-"`
77+
Page int `json:"-"`
78+
QueryUserUUID string `json:"-"`
8179
}
8280

8381
type SubscriptionAllRes struct {
@@ -88,14 +86,15 @@ type SubscriptionAllRes struct {
8886
}
8987

9088
type SubscriptionBillListReq struct {
91-
CurrentUser string `json:"-"`
92-
UserUUID string `json:"-"`
93-
QueryUserUUID string `json:"-"`
94-
Status string `json:"-"`
95-
StartTime string `json:"-"`
96-
EndTime string `json:"-"`
97-
Per int `json:"-"`
98-
Page int `json:"-"`
89+
CurrentUser string `json:"-"`
90+
UserUUID string `json:"-"`
91+
QueryUserUUID string `json:"-"`
92+
Status BillingStatus `json:"-"`
93+
StartTime string `json:"-"`
94+
EndTime string `json:"-"`
95+
Per int `json:"-"`
96+
Page int `json:"-"`
97+
SkuType SKUType `json:"-"`
9998
}
10099

101100
type SubscriptionBillResp struct {
@@ -112,6 +111,7 @@ type SubscriptionBillResp struct {
112111
ResourceID string `json:"resource_id"`
113112
Explain string `json:"explain"`
114113
CreatedAt time.Time `json:"created_at"`
114+
Discount float64 `json:"discount"`
115115
}
116116

117117
type SubscriptionBillAllRes struct {
@@ -125,40 +125,35 @@ type SubscriptionGetReq struct {
125125
UserUUID string `json:"-"`
126126
SubID int64 `json:"-"`
127127
EventUUID uuid.UUID `json:"-"`
128-
SkuType int `json:"-"`
129-
}
130-
131-
type SubscriptionUpdateReq struct {
132-
CurrentUser string `json:"-"`
133-
UserUUID string `json:"-"`
134-
SubID int64 `json:"-"`
135-
SkuType int `json:"sku_type" binding:"required"`
136-
ResourceID string `json:"resource_id" binding:"required"`
137-
SkuUnitType string `json:"sku_unit_type" binding:"required"`
138-
EventUUID uuid.UUID `json:"-"`
128+
SkuType SKUType `json:"-"`
139129
}
140130

141131
type SubscriptionStatusResp struct {
142132
UserUUID string `json:"user_uuid"`
143133
SkuType SKUType `json:"sku_type"`
144134
SubID int64 `json:"sub_id"`
145-
Status string `json:"status"`
135+
Status SubscriptionStatus `json:"status"`
136+
PriceID int64 `json:"price_id"`
146137
ResourceID string `json:"resource_id"`
138+
StartAt time.Time `json:"start_at"`
139+
EndAt time.Time `json:"end_at"`
147140
PeriodStart int64 `json:"period_start"`
148141
PeriodEnd int64 `json:"period_end"`
149142
BillID int64 `json:"bill_id"`
150143
BillMonth string `json:"bill_month"`
151144
NextPriceID int64 `json:"next_price_id"`
152145
NextResourceID string `json:"next_resource_id"`
153146
Usage []SubscriptionUsage `json:"usage"`
147+
UpdateAt time.Time `json:"update_at"`
154148
}
155149

156150
type SubscriptionUsage struct {
157-
ResourceID string `json:"resource_id"`
158-
ResourceName string `json:"resource_name"`
159-
CustomerID string `json:"customer_id"`
160-
Used float64 `json:"used"`
161-
Quota float64 `json:"quota"`
151+
ResourceID string `json:"resource_id"`
152+
ResourceName string `json:"resource_name"`
153+
CustomerID string `json:"customer_id"`
154+
Used float64 `json:"used"`
155+
Quota float64 `json:"quota"`
156+
ValueType ChargeValueType `json:"value_type"`
162157
}
163158

164159
type SubscriptionBatchStatusReq struct {
@@ -182,7 +177,18 @@ type SubscriptionEvent struct {
182177
Uuid string `json:"uuid"`
183178
UserUUID string `json:"user_uuid"`
184179
CreatedAt time.Time `json:"created_at"`
185-
ReasonCode int `json:"reason_code"`
180+
ReasonCode ACCTStatus `json:"reason_code"`
186181
ReasonMsg string `json:"reason_msg"`
187182
Subscription SubscriptionEventDetail `json:"subscription"`
188183
}
184+
185+
type SubUsageReq struct {
186+
SkuType SKUType `json:"sku_type"`
187+
UserUUID string `json:"user_uuid"`
188+
BillID int64 `json:"bill_id"`
189+
ResourceID string `json:"resource_id"`
190+
ResourceName string `json:"resource_name"`
191+
CustomerID string `json:"customer_id"`
192+
ValueType ChargeValueType `json:"value_type"`
193+
UseLimitPrice int64 `json:"use_limit_price"`
194+
}

0 commit comments

Comments
 (0)