Skip to content

Commit ea7a572

Browse files
committed
Computing total price
1 parent d55c0b6 commit ea7a572

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

pkg/cli/x_billing.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cli
66

77
import (
88
"fmt"
9+
"math/big"
910
"text/tabwriter"
1011
"time"
1112

@@ -61,6 +62,9 @@ func runBilling(cmd *Command, rawArgs []string) error {
6162
if err != nil {
6263
return err
6364
}
65+
66+
totalMonthPrice := new(big.Rat)
67+
6468
for _, server := range *servers {
6569
if server.State != "running" {
6670
continue
@@ -73,8 +77,12 @@ func runBilling(cmd *Command, rawArgs []string) error {
7377
usage := pricing.NewUsageByPath("/compute/c1/run")
7478
usage.SetStartEnd(modificationTime, time.Now().UTC())
7579

80+
totalMonthPrice = totalMonthPrice.Add(totalMonthPrice, usage.Total())
81+
7682
fmt.Fprintf(w, "server/%s\t%s\t%s\t%s\n", shortID, shortName, shortModificationDate, usage.TotalString())
7783
}
7884

85+
fmt.Fprintf(w, "TOTAL\t\t\t%s\n", pricing.PriceString(totalMonthPrice, "EUR"))
86+
7987
return nil
8088
}

pkg/pricing/usage.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package pricing
22

33
import (
4-
"fmt"
54
"math/big"
65
"time"
7-
8-
"github.com/dustin/go-humanize"
96
)
107

118
type Usage struct {
@@ -82,7 +79,5 @@ func (u *Usage) Total() *big.Rat {
8279
}
8380

8481
func (u *Usage) TotalString() string {
85-
total := u.Total()
86-
floatVal, _ := total.Float64()
87-
return fmt.Sprintf("%s %s", humanize.Ftoa(floatVal), u.PricingObject.Currency)
82+
return PriceString(u.Total(), u.PricingObject.Currency)
8883
}

pkg/pricing/utils.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package pricing
22

3-
import "math/big"
3+
import (
4+
"fmt"
5+
"math/big"
6+
7+
"github.com/scaleway/scaleway-cli/vendor/github.com/dustin/go-humanize"
8+
)
49

510
var (
611
intZero = big.NewInt(0)
@@ -35,3 +40,8 @@ func ratMin(x, y *big.Rat) *big.Rat {
3540
}
3641
return x
3742
}
43+
44+
func PriceString(price *big.Rat, currency string) string {
45+
floatVal, _ := price.Float64()
46+
return fmt.Sprintf("%s %s", humanize.Ftoa(floatVal), currency)
47+
}

0 commit comments

Comments
 (0)