fix billing record query with app type (#4731)

This commit is contained in:
Jiahui
2024-05-13 17:21:50 +08:00
committed by GitHub
parent dec95d9c94
commit c56ce7aebd
5 changed files with 32 additions and 42 deletions
@@ -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"`
}
@@ -380,13 +380,6 @@ func (in *BillingRecordQuerySpec) DeepCopy() *BillingRecordQuerySpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *BillingRecordQueryStatus) DeepCopyInto(out *BillingRecordQueryStatus) {
*out = *in
if in.DeductionAmount != nil {
in, out := &in.DeductionAmount, &out.DeductionAmount
*out = make(Costs, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]BillingRecordQueryItem, len(*in))
@@ -449,7 +442,7 @@ func (in *Debt) DeepCopyInto(out *Debt) {
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
out.Spec = in.Spec
out.Status = in.Status
in.Status.DeepCopyInto(&out.Status)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Debt.
@@ -520,6 +513,11 @@ func (in *DebtSpec) DeepCopy() *DebtSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *DebtStatus) DeepCopyInto(out *DebtStatus) {
*out = *in
if in.DebtStatusRecords != nil {
in, out := &in.DebtStatusRecords, &out.DebtStatusRecords
*out = make([]DebtStatusRecord, len(*in))
copy(*out, *in)
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DebtStatus.
@@ -532,6 +530,21 @@ func (in *DebtStatus) DeepCopy() *DebtStatus {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *DebtStatusRecord) DeepCopyInto(out *DebtStatusRecord) {
*out = *in
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DebtStatusRecord.
func (in *DebtStatusRecord) DeepCopy() *DebtStatusRecord {
if in == nil {
return nil
}
out := new(DebtStatusRecord)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *NamespaceBillingHistory) DeepCopyInto(out *NamespaceBillingHistory) {
*out = *in
@@ -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:
@@ -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:
+6 -25
View File
@@ -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"}}},
}}},
}
@@ -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