Skip to content

Commit

Permalink
feat: change baseCount to common module for account charge (#3673)
Browse files Browse the repository at this point in the history
* change baseCount to common module for account charge

* change the name of basecount to currency unit
  • Loading branch information
yxxchange committed Aug 14, 2023
1 parent 61d9ee9 commit 72e5d94
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
20 changes: 20 additions & 0 deletions controllers/common/account/count.go
@@ -0,0 +1,20 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package account

// CurrencyUnit is the unit of currency for accounting.
const CurrencyUnit = 1000000
5 changes: 3 additions & 2 deletions controllers/licenseissuer/internal/controller/util/collect.go
Expand Up @@ -22,6 +22,7 @@ import (
"time"

accountv1 "github.com/labring/sealos/controllers/account/api/v1"
account "github.com/labring/sealos/controllers/common/account"
"github.com/labring/sealos/controllers/pkg/database"
ntf "github.com/labring/sealos/controllers/pkg/notification"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -165,7 +166,7 @@ func (c *collect) getUsageYesterday(instance *TaskInstance) error {
instance.logger.Error(err, "failed to get billing amount")
return err
}
dailyClusterUsage.TotalUsageFee = float64(amount) / float64(BaseCount)
dailyClusterUsage.TotalUsageFee = float64(amount) / float64(account.CurrencyUnit)
// get billing amount for yesterday hourly usage fee
hourlyUsage := make([]float64, 24)
for cnt := 0; cnt < 24; cnt++ {
Expand All @@ -176,7 +177,7 @@ func (c *collect) getUsageYesterday(instance *TaskInstance) error {
instance.logger.Error(err, "failed to get billing amount")
return err
}
hourlyUsage[cnt] = float64(amount) / float64(BaseCount)
hourlyUsage[cnt] = float64(amount) / float64(account.CurrencyUnit)
}
dailyClusterUsage.HourlyUsage = hourlyUsage
c.DailyClusterUsage = dailyClusterUsage
Expand Down
2 changes: 0 additions & 2 deletions controllers/licenseissuer/internal/controller/util/const.go
Expand Up @@ -57,8 +57,6 @@ const (

const NoticeFrom = "Sealos Cloud"

const BaseCount = 1000000

const Key = "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFvbFBTSzB0UjFKeDZtb25lL2ppeApSWGN6UGlxcU5SSXRmdW1mdWNyNGMxc2dqdlJha0NwcWtDU21lMTR1akJkU0x6QlZzRjkvUWl0UnFNb2NvaEN1CkJ6R25EQ29hWnZXbWVHeE96NEZSejVTeUg1QTlDa3dnbUEzYnFnMWxKSEZTMlZyVjVHVFhFWnphZTZtRmhHOVcKenJMTnpZMlptYTMzOVE1WTNJSDZ6RXIrcTRQbTZDOXBHVGpsSnVodlRvb0dSY2w0bmpZRXc2eHB6ZHZrdi9uSApmZmxsWGZVNDNyRGdQaGkwZDRjWnNuTUJlazUxQkNiRFRuSHlNVFdGT1RoTjc1VVM0bzJxRm9JSEhsM0N0RzE4ClZIZEdRSE1IR0dYcGN3bVhhck1EQndwVWFOSk9kMkhjTTB5dlZEY2xDZzRITkIwVUFWeFNweFlRV3BwNWJzN2gKbHdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg=="

const MaxSizeThresholdStr = "800Ki"
Expand Down
Expand Up @@ -25,6 +25,7 @@ import (
accountv1 "github.com/labring/sealos/controllers/account/api/v1"

"github.com/go-logr/logr"
count "github.com/labring/sealos/controllers/common/account"
issuerv1 "github.com/labring/sealos/controllers/licenseissuer/api/v1"
"github.com/labring/sealos/controllers/pkg/crypto"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -94,7 +95,7 @@ func RechargeByLicense(ctx context.Context, client client.Client, account accoun
if err != nil {
return errors.New("amount error type")
}
charge := amount * BaseCount
charge := amount * count.CurrencyUnit
account.Status.Balance += charge
err = crypto.RechargeBalance(account.Status.EncryptBalance, charge)
if err != nil {
Expand Down

0 comments on commit 72e5d94

Please sign in to comment.