Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/cmd/vm_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func (r *runners) listVMs(_ *cobra.Command, args []string) error {
vmsToPrint = append(vmsToPrint, newVM)
} else {
// Check if properties of existing vms have changed
// reset EstimatedCost (as it is calculated on the fly and not stored in the API response)
oldVM.EstimatedCost = 0
if !reflect.DeepEqual(newVM, oldVM) {
vmsToPrint = append(vmsToPrint, newVM)
}
Expand Down
5 changes: 0 additions & 5 deletions cli/print/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ DISTRIBUTION: {{ $d.Name }}
{{ end }}`
var clusterVersionsTmpl = template.Must(template.New("clusterVersions").Funcs(funcs).Parse(clusterVersionsTmplSrc))

func CreditsToDollarsDisplay(credits int64) string {
dollars := float64(credits) / 100000.0
return fmt.Sprintf("$%.2f", dollars)
}

func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluster, header bool) error {
for _, cluster := range clusters {
updateEstimatedCost(cluster)
Expand Down
5 changes: 5 additions & 0 deletions cli/print/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ var funcs = template.FuncMap{
return t.Local().Format("2006-01-02 15:04 MST")
},
}

func CreditsToDollarsDisplay(credits int64) string {
dollars := float64(credits) / 100000.0
return fmt.Sprintf("$%.2f", dollars)
}
25 changes: 16 additions & 9 deletions cli/print/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ import (
"github.com/replicatedhq/replicated/pkg/types"
)

var vmFuncs = template.FuncMap{
"CreditsToDollarsDisplay": CreditsToDollarsDisplay,
}

// Table formatting
var vmsTmplTableHeaderSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES`
var vmsTmplTableHeaderSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES COST`
var vmsTmplTableRowSrc = `{{ range . -}}
{{ .ID }} {{ padding .Name 27 }} {{ padding .Distribution 12 }} {{ padding .Version 10 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}}
{{ .ID }} {{ padding .Name 27 }} {{ padding .Distribution 12 }} {{ padding .Version 10 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}} {{ padding (CreditsToDollarsDisplay .EstimatedCost) 11 }}
{{ end }}`
var vmsTmplTableSrc = fmt.Sprintln(vmsTmplTableHeaderSrc) + vmsTmplTableRowSrc
var vmsTmplTable = template.Must(template.New("vms").Funcs(funcs).Parse(vmsTmplTableSrc))
var vmsTmplTableNoHeader = template.Must(template.New("vms").Funcs(funcs).Parse(vmsTmplTableRowSrc))
var vmsTmplTable = template.Must(template.New("vms").Funcs(vmFuncs).Funcs(funcs).Parse(vmsTmplTableSrc))
var vmsTmplTableNoHeader = template.Must(template.New("vms").Funcs(vmFuncs).Funcs(funcs).Parse(vmsTmplTableRowSrc))

// Wide table formatting
var vmsTmplWideHeaderSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES TAGS`
var vmsTmplWideHeaderSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES COST TAGS`
var vmsTmplWideRowSrc = `{{ range . -}}
{{ .ID }} {{ padding .Name 27 }} {{ padding .Distribution 12 }} {{ padding .Version 10 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}} {{ range $index, $tag := .Tags }}{{if $index}}, {{end}}{{ $tag.Key }}={{ $tag.Value }}{{ end }}
{{ .ID }} {{ padding .Name 27 }} {{ padding .Distribution 12 }} {{ padding .Version 10 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}} {{ padding (CreditsToDollarsDisplay .EstimatedCost) 11 }} {{ range $index, $tag := .Tags }}{{if $index}}, {{end}}{{ $tag.Key }}={{ $tag.Value }}{{ end }}
{{ end }}`
var vmsTmplWideSrc = fmt.Sprintln(vmsTmplWideHeaderSrc) + vmsTmplWideRowSrc
var vmsTmplWide = template.Must(template.New("vms").Funcs(funcs).Parse(vmsTmplWideSrc))
var vmsTmplWideNoHeader = template.Must(template.New("vms").Funcs(funcs).Parse(vmsTmplWideRowSrc))
var vmsTmplWide = template.Must(template.New("vms").Funcs(vmFuncs).Funcs(funcs).Parse(vmsTmplWideSrc))
var vmsTmplWideNoHeader = template.Must(template.New("vms").Funcs(vmFuncs).Funcs(funcs).Parse(vmsTmplWideRowSrc))

// VM versions
var vmVersionsTmplSrc = `Supported VM distributions and versions are:
Expand All @@ -42,6 +46,9 @@ DISTRIBUTION: {{ $d.Name }}
var vmVersionsTmpl = template.Must(template.New("vmVersions").Funcs(funcs).Parse(vmVersionsTmplSrc))

func VMs(outputFormat string, w *tabwriter.Writer, vms []*types.VM, header bool) error {
for _, vm := range vms {
updateEstimatedVMCost(vm)
}
switch outputFormat {
case "table":
if header {
Expand Down Expand Up @@ -165,7 +172,7 @@ func updateEstimatedVMCost(vm *types.VM) {
} else {
expireDuration, _ := time.ParseDuration(vm.TTL)
minutesRunning := int64(expireDuration.Minutes())
totalCredits := int64(minutesRunning) * vm.CreditsPerHourPerVM / 60.0
totalCredits := int64(minutesRunning) * vm.CreditsPerHour / 60.0
vm.EstimatedCost = vm.FlatFee + totalCredits
}
}
6 changes: 3 additions & 3 deletions pkg/types/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type VM struct {

TTL string `json:"ttl"`

CreditsPerHourPerVM int64 `json:"credits_per_hour_per_vm"`
FlatFee int64 `json:"flat_fee"`
TotalCredits int64 `json:"total_credits"`
CreditsPerHour int64 `json:"credits_per_hour"`
FlatFee int64 `json:"flat_fee"`
TotalCredits int64 `json:"total_credits"`

EstimatedCost int64 `json:"estimated_cost"` // Represents estimated credits for this vm based on the TTL

Expand Down
Loading