diff --git a/controllers/account/api/v1/billingrecordquery_types.go b/controllers/account/api/v1/billingrecordquery_types.go index 772085523..853894f5c 100644 --- a/controllers/account/api/v1/billingrecordquery_types.go +++ b/controllers/account/api/v1/billingrecordquery_types.go @@ -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"` } diff --git a/controllers/account/api/v1/zz_generated.deepcopy.go b/controllers/account/api/v1/zz_generated.deepcopy.go index 88145842d..279d4cc62 100644 --- a/controllers/account/api/v1/zz_generated.deepcopy.go +++ b/controllers/account/api/v1/zz_generated.deepcopy.go @@ -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 diff --git a/controllers/account/config/crd/bases/account.sealos.io_billingrecordqueries.yaml b/controllers/account/config/crd/bases/account.sealos.io_billingrecordqueries.yaml index 30138ea24..798603fc1 100644 --- a/controllers/account/config/crd/bases/account.sealos.io_billingrecordqueries.yaml +++ b/controllers/account/config/crd/bases/account.sealos.io_billingrecordqueries.yaml @@ -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: diff --git a/controllers/account/deploy/manifests/deploy.yaml.tmpl b/controllers/account/deploy/manifests/deploy.yaml.tmpl index 1669ae6a8..d50144c9b 100644 --- a/controllers/account/deploy/manifests/deploy.yaml.tmpl +++ b/controllers/account/deploy/manifests/deploy.yaml.tmpl @@ -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: diff --git a/controllers/pkg/database/mongo/account.go b/controllers/pkg/database/mongo/account.go index c7c620e26..0264d6c82 100644 --- a/controllers/pkg/database/mongo/account.go +++ b/controllers/pkg/database/mongo/account.go @@ -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