@@ -29,111 +29,117 @@ const (
2929 CPUCostCol = "CPU Cost"
3030 RAMCostCol = "RAM Cost"
3131
32- PredictColWorkload = "Workload"
33- PredictColReqCPU = "CPU"
34- PredictColReqMemory = "Mem"
35- PredictColReqGPU = "GPU"
36- PredictColMoCoreHours = "Mo. core-hrs"
37- PredictColMoGibHours = "Mo. GiB-hrs"
38- PredictColMoGPUHours = "Mo. GPU-hrs"
39- PredictColCostCoreHr = "Cost/core-hr"
40- PredictColCostGiBHr = "Cost/GiB-hr"
41- PredictColCostGPUHr = "Cost/GPU-hr"
42- PredictColMoCostCPU = "CPU/mo"
43- PredictColMoCostMemory = "Mem/mo"
44- PredictColMoCostGPU = "GPU/mo"
45- PredictColMoCostTotal = "Total/mo"
32+ PredictColWorkload = "Workload"
33+ PredictColReqCPU = "CPU"
34+ PredictColReqMemory = "Mem"
35+ PredictColReqGPU = "GPU"
36+ PredictColMoCoreHours = "Mo. core-hrs"
37+ PredictColMoGibHours = "Mo. GiB-hrs"
38+ PredictColMoGPUHours = "Mo. GPU-hrs"
39+ PredictColCostCoreHr = "Cost/core-hr"
40+ PredictColCostGiBHr = "Cost/GiB-hr"
41+ PredictColCostGPUHr = "Cost/GPU-hr"
42+ PredictColMoCostCPU = "CPU/mo"
43+ PredictColMoCostMemory = "Mem/mo"
44+ PredictColMoCostGPU = "GPU/mo"
45+ PredictColMoCostTotal = "Total/mo"
46+ PredictColMoCostDiffCPU = "Δ CPU/mo"
47+ PredictColMoCostDiffMemory = "Δ Mem/mo"
4648)
4749
4850func formatFloat (f float64 ) string {
4951 return fmt .Sprintf ("%.6f" , f )
5052}
5153
52- func writePredictionTable (out io.Writer , rowData []predictRowData , currencyCode string , showCostPerResourceHr bool ) {
53- t := makePredictionTable (rowData , currencyCode , showCostPerResourceHr )
54+ type predictionTableOptions struct {
55+ currencyCode string
56+ showCostPerResourceHr bool
57+ noDiff bool
58+ }
59+
60+ func writePredictionTable (out io.Writer , rowData []predictRowData , opts predictionTableOptions ) {
61+ t := makePredictionTable (rowData , opts )
5462 t .SetOutputMirror (out )
5563 t .Render ()
5664}
5765
58- func makePredictionTable (rowData []predictRowData , currencyCode string , showCostPerResourceHr bool ) table.Writer {
66+ func makePredictionTable (rowData []predictRowData , opts predictionTableOptions ) table.Writer {
5967 t := table .NewWriter ()
6068
61- columnConfigs := []table.ColumnConfig {
62- table. ColumnConfig {
69+ t . SetColumnConfigs ( []table.ColumnConfig {
70+ {
6371 Name : PredictColWorkload ,
6472 },
65- table. ColumnConfig {
73+ {
6674 Name : PredictColReqCPU ,
6775 },
68- table. ColumnConfig {
76+ {
6977 Name : PredictColReqMemory ,
7078 },
71- table. ColumnConfig {
79+ {
7280 Name : PredictColReqGPU ,
7381 },
74- }
75-
76- if showCostPerResourceHr {
77- columnConfigs = append (columnConfigs , []table.ColumnConfig {
78- table.ColumnConfig {
79- Name : PredictColCostCoreHr ,
80- },
81- table.ColumnConfig {
82- Name : PredictColCostGiBHr ,
83- },
84- table.ColumnConfig {
85- Name : PredictColCostGPUHr ,
86- },
87- }... )
88- }
89-
90- columnConfigs = append (columnConfigs , []table.ColumnConfig {
91- table.ColumnConfig {
82+ {
83+ Name : PredictColCostCoreHr ,
84+ Hidden : ! opts .showCostPerResourceHr ,
85+ },
86+ {
87+ Name : PredictColCostGiBHr ,
88+ Hidden : ! opts .showCostPerResourceHr ,
89+ },
90+ {
91+ Name : PredictColCostGPUHr ,
92+ Hidden : ! opts .showCostPerResourceHr ,
93+ },
94+ {
9295 Name : PredictColMoCostCPU ,
9396 Align : text .AlignRight ,
9497 AlignFooter : text .AlignRight ,
9598 },
96- table. ColumnConfig {
99+ {
97100 Name : PredictColMoCostMemory ,
98101 Align : text .AlignRight ,
99102 AlignFooter : text .AlignRight ,
100103 },
101- table. ColumnConfig {
104+ {
102105 Name : PredictColMoCostGPU ,
103106 Align : text .AlignRight ,
104107 AlignFooter : text .AlignRight ,
105108 },
106- table.ColumnConfig {
109+ {
110+ Name : PredictColMoCostDiffCPU ,
111+ Hidden : opts .noDiff ,
112+ Align : text .AlignRight ,
113+ AlignFooter : text .AlignRight ,
114+ },
115+ {
116+ Name : PredictColMoCostDiffMemory ,
117+ Hidden : opts .noDiff ,
118+ Align : text .AlignRight ,
119+ AlignFooter : text .AlignRight ,
120+ },
121+ {
107122 Name : PredictColMoCostTotal ,
108123 Align : text .AlignRight ,
109124 AlignFooter : text .AlignRight ,
110125 },
111- }... )
112- t .SetColumnConfigs (columnConfigs )
126+ })
113127
114- headerRow := table.Row {
128+ t . AppendHeader ( table.Row {
115129 PredictColWorkload ,
116130 PredictColReqCPU ,
117131 PredictColReqMemory ,
118132 PredictColReqGPU ,
119- }
120-
121- if showCostPerResourceHr {
122- headerRow = append (headerRow ,
123- PredictColCostCoreHr ,
124- PredictColCostGiBHr ,
125- PredictColCostGPUHr ,
126- )
127- }
128-
129- headerRow = append (headerRow ,
133+ PredictColCostCoreHr ,
134+ PredictColCostGiBHr ,
135+ PredictColCostGPUHr ,
130136 PredictColMoCostCPU ,
131137 PredictColMoCostMemory ,
132138 PredictColMoCostGPU ,
139+ PredictColMoCostDiffCPU ,
140+ PredictColMoCostDiffMemory ,
133141 PredictColMoCostTotal ,
134- )
135-
136- t .AppendHeader (headerRow )
142+ })
137143
138144 t .SortBy ([]table.SortBy {
139145 {
@@ -145,48 +151,52 @@ func makePredictionTable(rowData []predictRowData, currencyCode string, showCost
145151 var summedMonthlyCPU float64
146152 var summedMonthlyMem float64
147153 var summedMonthlyGPU float64
154+ var summedMonthlyDiffCPU float64
155+ var summedMonthlyDiffMemory float64
148156 var summedMonthlyTotal float64
149157
150158 for _ , rowDatum := range rowData {
151159 row := table.Row {}
152- row = append (row , fmt .Sprintf ("%s/%s" , rowDatum .workloadType , rowDatum .workloadName ))
153- row = append (row , rowDatum .cpuStr )
154- row = append (row , rowDatum .memStr )
155- row = append (row , rowDatum .gpuStr )
156-
157- if showCostPerResourceHr {
158- row = append (row , fmt .Sprintf ("%.4f %s" , rowDatum .prediction .DerivedCostPerCPUCoreHour , currencyCode ))
159- row = append (row , fmt .Sprintf ("%.4f %s" , rowDatum .prediction .DerivedCostPerMemoryByteHour * 1024 * 1024 * 1024 , currencyCode ))
160- row = append (row , fmt .Sprintf ("%.4f %s" , rowDatum .prediction .DerivedCostPerGPUHour , currencyCode ))
161- }
162-
163- row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .prediction .MonthlyCostCPU , currencyCode ))
164- row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .prediction .MonthlyCostMemory , currencyCode ))
165- row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .prediction .MonthlyCostGPU , currencyCode ))
166- row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .prediction .MonthlyCostTotal , currencyCode ))
167-
168- summedMonthlyCPU += rowDatum .prediction .MonthlyCostCPU
169- summedMonthlyMem += rowDatum .prediction .MonthlyCostMemory
170- summedMonthlyGPU += rowDatum .prediction .MonthlyCostGPU
171- summedMonthlyTotal += rowDatum .prediction .MonthlyCostTotal
160+ row = append (row , fmt .Sprintf ("%s/%s/%s" , rowDatum .workloadNamespace , rowDatum .workloadType , rowDatum .workloadName ))
161+ row = append (row , rowDatum .totalCPURequested )
162+ row = append (row , rowDatum .totalMemoryRequested )
163+ row = append (row , rowDatum .totalGPURequested )
164+
165+ row = append (row , fmt .Sprintf ("%.4f %s" , rowDatum .cpuCostMonthly / rowDatum .requestedCPUCoreHours , opts .currencyCode ))
166+ row = append (row , fmt .Sprintf ("%.4f %s" , (rowDatum .memoryCostMonthly / rowDatum .requestedMemoryByteHours )* 1024 * 1024 * 1024 , opts .currencyCode ))
167+ row = append (row , fmt .Sprintf ("%.4f %s" , rowDatum .gpuCostMonthly / rowDatum .requestedGPUHours , opts .currencyCode ))
168+
169+ row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .cpuCostMonthly , opts .currencyCode ))
170+ row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .memoryCostMonthly , opts .currencyCode ))
171+ row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .gpuCostMonthly , opts .currencyCode ))
172+ row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .cpuCostChangeMonthly , opts .currencyCode ))
173+ row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .memoryCostChangeMonthly , opts .currencyCode ))
174+ row = append (row , fmt .Sprintf ("%.2f %s" , rowDatum .totalCostMonthly , opts .currencyCode ))
175+
176+ summedMonthlyCPU += rowDatum .cpuCostMonthly
177+ summedMonthlyMem += rowDatum .memoryCostMonthly
178+ summedMonthlyGPU += rowDatum .gpuCostMonthly
179+ summedMonthlyDiffCPU += rowDatum .cpuCostChangeMonthly
180+ summedMonthlyDiffMemory += rowDatum .memoryCostChangeMonthly
181+ summedMonthlyTotal += rowDatum .totalCostMonthly
172182
173183 t .AppendRow (row )
174184 }
175185
176186 // A summary footer is redundant if there is only one row
177187 if len (rowData ) > 1 {
178188 footerRow := table.Row {}
179- blankRows := 4
180- if showCostPerResourceHr {
181- blankRows += 2
182- }
189+ blankRows := 7
190+
183191 for i := 0 ; i < blankRows ; i ++ {
184192 footerRow = append (footerRow , "" )
185193 }
186- footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyCPU , currencyCode ))
187- footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyMem , currencyCode ))
188- footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyGPU , currencyCode ))
189- footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyTotal , currencyCode ))
194+ footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyCPU , opts .currencyCode ))
195+ footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyMem , opts .currencyCode ))
196+ footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyGPU , opts .currencyCode ))
197+ footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyDiffCPU , opts .currencyCode ))
198+ footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyDiffMemory , opts .currencyCode ))
199+ footerRow = append (footerRow , fmt .Sprintf ("%.2f %s" , summedMonthlyTotal , opts .currencyCode ))
190200 t .AppendFooter (footerRow )
191201 }
192202
0 commit comments