Skip to content

Commit

Permalink
fix billing record query with app type (#4731)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 authored May 13, 2024
1 parent dec95d9 commit c56ce7a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 42 deletions.
2 changes: 1 addition & 1 deletion controllers/account/api/v1/billingrecordquery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type BillingRecordQueryStatus struct {
TotalCount int `json:"totalCount"`
PageLength int `json:"pageLength"`
RechargeAmount int64 `json:"rechargeAmount"`
DeductionAmount Costs `json:"deductionAmount,omitempty"`
DeductionAmount int64 `json:"deductionAmount,omitempty"`
Items []BillingRecordQueryItem `json:"item,omitempty"`
Status string `json:"status"`
}
Expand Down
29 changes: 21 additions & 8 deletions controllers/account/api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ spec:
description: BillingRecordQueryStatus defines the observed state of BillingRecordQuery
properties:
deductionAmount:
additionalProperties:
format: int64
type: integer
type: object
format: int64
type: integer
item:
items:
properties:
Expand Down
6 changes: 2 additions & 4 deletions controllers/account/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,8 @@ spec:
description: BillingRecordQueryStatus defines the observed state of BillingRecordQuery
properties:
deductionAmount:
additionalProperties:
format: int64
type: integer
type: object
format: int64
type: integer
item:
items:
properties:
Expand Down
31 changes: 6 additions & 25 deletions controllers/pkg/database/mongo/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,22 +687,10 @@ func (m *mongoDB) QueryBillingRecords(billingRecordQuery *accountv1.BillingRecor
}

pipelineCountAndAmount := bson.A{
bson.D{{Key: "$match", Value: bson.D{
{Key: "time", Value: timeMatchValue},
{Key: "owner", Value: owner},
{Key: "type", Value: accountv1.Consumption},
}}},
bson.D{{Key: "$addFields", Value: bson.D{
{Key: "costsArray", Value: bson.D{{Key: "$objectToArray", Value: "$costs"}}},
}}},
bson.D{{Key: "$unwind", Value: "$costsArray"}},
matchStage,
bson.D{{Key: "$group", Value: bson.D{
{Key: "_id", Value: bson.D{
{Key: "type", Value: "$type"},
{Key: "key", Value: "$costsArray.k"},
}},
{Key: "total", Value: bson.D{{Key: "$sum", Value: "$costsArray.v"}}},
{Key: "count", Value: bson.D{{Key: "$sum", Value: 1}}},
primitive.E{Key: "_id", Value: nil},
primitive.E{Key: "result", Value: bson.D{primitive.E{Key: "$sum", Value: "$amount"}}},
}}},
}

Expand Down Expand Up @@ -785,23 +773,16 @@ func (m *mongoDB) QueryBillingRecords(billingRecordQuery *accountv1.BillingRecor
}
defer cursorCountAndAmount.Close(ctx)

totalDeductionAmount := make(map[string]int64)
totalRechargeAmount := int64(0)
totalDeductionAmount, totalRechargeAmount := int64(0), int64(0)

for cursorCountAndAmount.Next(ctx) {
var result struct {
ID struct {
Type int `bson:"type"`
Key string `bson:"key"`
} `bson:"_id"`
Total int64 `bson:"total"`
Result int64 `bson:"result"`
}
if err := cursorCountAndAmount.Decode(&result); err != nil {
return fmt.Errorf("failed to decode billing record: %w", err)
}
if result.ID.Type == 0 {
totalDeductionAmount[result.ID.Key] = result.Total
}
totalDeductionAmount = result.Result
}

// the total amount
Expand Down

0 comments on commit c56ce7a

Please sign in to comment.