Skip to content

Commit

Permalink
cost base price is float64
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sva committed Jan 14, 2025
1 parent 8db7f04 commit a9b751c
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 64 deletions.
12 changes: 6 additions & 6 deletions v3/pkg/apis/hobbyfarm.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,10 @@ type CostSpec struct {
}

type CostResource struct {
Id string `json:"id"` // id of the resource
Kind string `json:"kind"` // name like VirtualMachine
BasePrice uint64 `json:"base_price"`
TimeUnit string `json:"time_unit"` // one of [seconds, minutes, hours]
CreationUnixTimestamp int64 `json:"creation_unix_timestamp"` // unix timestamp in seconds
DeletionUnixTimestamp int64 `json:"deletion_unix_timestamp,omitempty"` // unix timestamp in seconds
Id string `json:"id"` // id of the resource
Kind string `json:"kind"` // name like VirtualMachine
BasePrice float64 `json:"base_price"`
TimeUnit string `json:"time_unit"` // one of [seconds, minutes, hours]
CreationUnixTimestamp int64 `json:"creation_unix_timestamp"` // unix timestamp in seconds
DeletionUnixTimestamp int64 `json:"deletion_unix_timestamp,omitempty"` // unix timestamp in seconds
}
44 changes: 22 additions & 22 deletions v3/protos/cost/cost.pb.go

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

16 changes: 8 additions & 8 deletions v3/protos/cost/cost.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ service CostSvc {

message Cost {
string cost_group = 1; // name of the cost group
uint64 total = 2; // total cost for all sources
double total = 2; // total cost for all sources
repeated CostSource source = 3;
}

message CostSource {
string kind = 1; // resource kind like VirtualMachine
uint64 cost = 2; // total cost for this kind
double cost = 2; // total cost for this kind
uint64 count = 3;
}

message CreateOrUpdateCostRequest {
string cost_group = 1;
string kind = 3; // like VirtualMachine
uint64 base_price = 4;
string time_unit = 5;
string id = 6; // resource id
int64 creation_unix_timestamp = 7; // unix timestamp in seconds
optional int64 deletion_unix_timestamp = 8; // unix timestamp in seconds
string kind = 2; // like VirtualMachine
double base_price = 3;
string time_unit = 4;
string id = 5; // resource id
int64 creation_unix_timestamp = 6; // unix timestamp in seconds
optional int64 deletion_unix_timestamp = 7; // unix timestamp in seconds
}

message ListCostsResponse {
Expand Down
4 changes: 2 additions & 2 deletions v3/services/costsvc/internal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type costGroup struct {
Id string
Kind string
CostGroup string
BasePrice uint64
BasePrice float64
TimeUnit util.TimeUnit
CreationTimestamp int64
DeletionTimestamp *int64
Expand All @@ -42,7 +42,7 @@ func newCostGroup(obj interface{}) (*costGroup, error) {
if !found {
return nil, fmt.Errorf("%s label not found", labels.CostBasePrice)
}
basePrice, err := strconv.ParseUint(basePriceLabel, 10, 64)
basePrice, err := strconv.ParseFloat(basePriceLabel, 64)
if err != nil {
return nil, fmt.Errorf("%s label value is not an uint", labels.CostBasePrice)
}
Expand Down
8 changes: 4 additions & 4 deletions v3/services/costsvc/internal/costservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (

type PreparedCost struct {
CostGroup string `json:"cost_group"`
Total uint64 `json:"total"`
Total float64 `json:"total"`
Sources []PreparedCostSource `json:"source"`
}

type PreparedCostSource struct {
Kind string `json:"kind"`
Cost uint64 `json:"cost"`
Count uint64 `json:"count"`
Kind string `json:"kind"`
Cost float64 `json:"cost"`
Count uint64 `json:"count"`
}

func NewPreparedCost(cost *costpb.Cost) PreparedCost {
Expand Down
24 changes: 12 additions & 12 deletions v3/services/costsvc/internal/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ func TestGrpcCostServer_GetCostHistory(t *testing.T) {
costs, err := server.GetCostHistory(context.TODO(), &generalpb.GetRequest{Id: "my-cost-group"})
assert.NoError(t, err)
assert.Equal(t, costs.GetCostGroup(), expectedCost.Name, "cost group matches")
assert.Equal(t, costs.GetTotal(), uint64(10+10+10+50), "cost group total")
assert.Equal(t, costs.GetTotal(), float64(10+10+10+50), "cost group total")

assert.Len(t, costs.GetSource(), 2, "size of cost source")
for _, source := range costs.Source {
switch source.GetKind() {
case "Pod":
assert.Equal(t, source.GetCount(), uint64(2), "pod count")
assert.Equal(t, source.GetCost(), uint64(20), "pod costs")
assert.Equal(t, source.GetCost(), float64(20), "pod costs")
case "VirtualMachine":
assert.Equal(t, source.GetCount(), uint64(2), "virtual machine count")
assert.Equal(t, source.GetCost(), uint64(60), "virtual machine costs")
assert.Equal(t, source.GetCost(), float64(60), "virtual machine costs")
default:
t.Errorf("unkown source kind = %s; want Pod or VirtualMachine", source.Kind)
}
Expand Down Expand Up @@ -190,17 +190,17 @@ func TestGrpcCostServer_GetCostPresent(t *testing.T) {
costs, err := server.GetCostPresent(context.TODO(), &generalpb.GetRequest{Id: "my-cost-group"})
assert.NoError(t, err)
assert.Equal(t, costs.GetCostGroup(), expectedCost.Name, "cost group matches")
assert.Equal(t, costs.GetTotal(), uint64(10+10+10+50), "cost group total")
assert.Equal(t, costs.GetTotal(), float64(10+10+10+50), "cost group total")

assert.Len(t, costs.GetSource(), 2, "size of cost source")
for _, source := range costs.Source {
switch source.GetKind() {
case "Pod":
assert.Equal(t, source.GetCount(), uint64(2), "pod count")
assert.Equal(t, source.GetCost(), uint64(20), "pod costs")
assert.Equal(t, source.GetCost(), float64(20), "pod costs")
case "VirtualMachine":
assert.Equal(t, source.GetCount(), uint64(2), "virtual machine count")
assert.Equal(t, source.GetCost(), uint64(60), "virtual machine costs")
assert.Equal(t, source.GetCost(), float64(60), "virtual machine costs")
default:
t.Errorf("unkown source kind = %s; want Pod or VirtualMachine", source.Kind)
}
Expand Down Expand Up @@ -285,17 +285,17 @@ func TestGrpcCostServer_GetCost(t *testing.T) {
costs, err := server.GetCost(context.TODO(), &generalpb.GetRequest{Id: "my-cost-group"})
assert.NoError(t, err)
assert.Equal(t, costs.GetCostGroup(), expectedCost.Name, "cost group matches")
assert.Equal(t, costs.GetTotal(), uint64(10+10+10+10+50+10), "cost group total")
assert.Equal(t, costs.GetTotal(), float64(10+10+10+10+50+10), "cost group total")

assert.Len(t, costs.GetSource(), 2, "size of cost source")
for _, source := range costs.Source {
switch source.GetKind() {
case "Pod":
assert.Equal(t, source.GetCount(), uint64(3), "pod count")
assert.Equal(t, source.GetCost(), uint64(30), "pod costs")
assert.Equal(t, source.GetCost(), float64(30), "pod costs")
case "VirtualMachine":
assert.Equal(t, source.GetCount(), uint64(3), "virtual machine count")
assert.Equal(t, source.GetCost(), uint64(70), "virtual machine costs")
assert.Equal(t, source.GetCost(), float64(70), "virtual machine costs")
default:
t.Errorf("unkown source kind = %s; want Pod or VirtualMachine", source.Kind)
}
Expand Down Expand Up @@ -428,10 +428,10 @@ func TestGrpcCostServer_ListCost(t *testing.T) {
switch source.GetKind() {
case "Pod":
assert.Equal(t, source.GetCount(), uint64(3), "pod count")
assert.Equal(t, source.GetCost(), uint64(30), "pod costs")
assert.Equal(t, source.GetCost(), float64(30), "pod costs")
case "VirtualMachine":
assert.Equal(t, source.GetCount(), uint64(3), "virtual machine count")
assert.Equal(t, source.GetCost(), uint64(70), "virtual machine costs")
assert.Equal(t, source.GetCost(), float64(70), "virtual machine costs")
default:
t.Errorf("unkown source kind = %s; want Pod or VirtualMachine", source.Kind)
}
Expand All @@ -441,7 +441,7 @@ func TestGrpcCostServer_ListCost(t *testing.T) {
switch source.GetKind() {
case "VirtualMachine":
assert.Equal(t, source.GetCount(), uint64(3), "virtual machine count")
assert.Equal(t, source.GetCost(), uint64(70), "virtual machine costs")
assert.Equal(t, source.GetCost(), float64(70), "virtual machine costs")
default:
t.Errorf("unkown source kind = %s; want Pod or VirtualMachine", source.Kind)
}
Expand Down
14 changes: 7 additions & 7 deletions v3/services/costsvc/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"time"
)

func CostResourceCalcCost(cr v1.CostResource, duration time.Duration) uint64 {
var durationInTimeUnit uint64
func CostResourceCalcCost(cr v1.CostResource, duration time.Duration) float64 {
var durationInTimeUnit float64

switch cr.TimeUnit {
case util.TimeUnitSeconds:
durationInTimeUnit = uint64(math.Ceil(duration.Seconds()))
durationInTimeUnit = math.Ceil(duration.Seconds())
case util.TimeUnitMinutes:
durationInTimeUnit = uint64(math.Ceil(duration.Minutes()))
durationInTimeUnit = math.Ceil(duration.Minutes())
case util.TimeUnitHours:
durationInTimeUnit = uint64(math.Ceil(duration.Hours()))
durationInTimeUnit = math.Ceil(duration.Hours())
default:
durationInTimeUnit = 0
}
Expand Down Expand Up @@ -78,10 +78,10 @@ func (cb *CostBuilder) WithHistoricCosts() *CostBuilder {

func (cb *CostBuilder) Build(now time.Time) *costpb.Cost {
var costSources []*costpb.CostSource
var totalCost uint64
var totalCost float64

for kind, resources := range GroupCostResourceByKind(cb.cost.Spec.Resources) {
var costForKind uint64
var costForKind float64
var count uint64

for _, resource := range resources {
Expand Down
2 changes: 1 addition & 1 deletion v3/services/costsvc/internal/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestCostResourceCalcCost(t *testing.T) {
name string
input v1.CostResource
duration time.Duration
want uint64
want float64
}{
{
name: "seconds",
Expand Down
4 changes: 2 additions & 2 deletions v3/services/vmtemplatesvc/internal/vmtemplateservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ func normalizeCost(costBasePrice, costTimeUnit string) (basePrice, timeUnit *str
return
}

_, err = strconv.ParseUint(costBasePrice, 10, 64)
_, err = strconv.ParseFloat(costBasePrice, 64)
if err != nil {
err = errors.New("cost_base_price needs to be a positive number")
err = errors.New("cost_base_price needs to be a float64")
return
}

Expand Down

0 comments on commit a9b751c

Please sign in to comment.