Browse Source

update CSV format: add network transfer column and re-arrange columns

Signed-off-by: r2k1 <yokree@gmail.com>
r2k1 3 years ago
parent
commit
a1466614b5
2 changed files with 25 additions and 13 deletions
  1. 18 8
      pkg/costmodel/csv_export.go
  2. 7 5
      pkg/costmodel/csv_export_test.go

+ 18 - 8
pkg/costmodel/csv_export.go

@@ -184,17 +184,21 @@ func (e *csvExporter) writeCSVToWriter(ctx context.Context, w io.Writer, dates [
 		"ControllerName",
 		"Pod",
 		"Container",
+
 		"CPUCoreUsageAverage",
 		"CPUCoreRequestAverage",
-		"CPUCost",
 		"RAMBytesUsageAverage",
 		"RAMBytesRequestAverage",
-		"RAMCost",
+		"NetworkReceiveBytes",
+		"NetworkTransferBytes",
 		"GPUs",
-		"GPUCost",
-		"NetworkCost",
 		"PVBytes",
+
+		"CPUCost",
+		"RAMCost",
+		"NetworkCost",
 		"PVCost",
+		"GPUCost",
 		"TotalCost",
 	})
 	if err != nil {
@@ -216,6 +220,8 @@ func (e *csvExporter) writeCSVToWriter(ctx context.Context, w io.Writer, dates [
 				return err
 			}
 
+			log.Infof("%f", alloc.TotalCost())
+
 			err := csvWriter.Write([]string{
 				date.Format("2006-01-02"),
 				alloc.Properties.Namespace,
@@ -223,17 +229,21 @@ func (e *csvExporter) writeCSVToWriter(ctx context.Context, w io.Writer, dates [
 				alloc.Properties.Controller,
 				alloc.Properties.Pod,
 				alloc.Properties.Container,
+
 				fmtFloat(alloc.CPUCoreUsageAverage),
 				fmtFloat(alloc.CPUCoreRequestAverage),
-				fmtFloat(alloc.CPUTotalCost()),
 				fmtFloat(alloc.RAMBytesUsageAverage),
 				fmtFloat(alloc.RAMBytesRequestAverage),
-				fmtFloat(alloc.RAMTotalCost()),
+				fmtFloat(alloc.NetworkReceiveBytes),
+				fmtFloat(alloc.NetworkTransferBytes),
 				fmtFloat(alloc.GPUs()),
-				fmtFloat(alloc.GPUCost),
-				fmtFloat(alloc.NetworkTotalCost()),
 				fmtFloat(alloc.PVBytes()),
+
+				fmtFloat(alloc.CPUTotalCost()),
+				fmtFloat(alloc.RAMTotalCost()),
+				fmtFloat(alloc.NetworkTotalCost()),
 				fmtFloat(alloc.PVCost()),
+				fmtFloat(alloc.GPUCost),
 				fmtFloat(alloc.TotalCost()),
 			})
 			if err != nil {

+ 7 - 5
pkg/costmodel/csv_export_test.go

@@ -43,6 +43,8 @@ func Test_UpdateCSV(t *testing.T) {
 							GPUHours:               48,
 							GPUCost:                0.8,
 							NetworkCost:            0.9,
+							NetworkTransferBytes:   10,
+							NetworkReceiveBytes:    11,
 							PVs: map[kubecost.PVKey]*kubecost.PVAllocation{
 								kubecost.PVKey{
 									Cluster: "test-cluster",
@@ -71,8 +73,8 @@ func Test_UpdateCSV(t *testing.T) {
 		assert.Len(t, model.ComputeAllocationCalls(), 1)
 		assert.Equal(t, time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].Start)
 		assert.Equal(t, time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].End)
-		assert.Equal(t, `Date,Namespace,ControllerKind,ControllerName,Pod,Container,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost,GPUs,GPUCost,NetworkCost,PVBytes,PVCost,TotalCost
-2021-01-01,test-namespace,test-controller-kind,test-controller-name,test-pod,test-container,0.1,0.2,0.3,0.4,0.5,0.6,2,0.8,0.9,2,2,4.6000000000000005
+		assert.Equal(t, `Date,Namespace,ControllerKind,ControllerName,Pod,Container,CPUCoreUsageAverage,CPUCoreRequestAverage,RAMBytesUsageAverage,RAMBytesRequestAverage,NetworkReceiveBytes,NetworkTransferBytes,GPUs,PVBytes,CPUCost,RAMCost,NetworkCost,PVCost,GPUCost,TotalCost
+2021-01-01,test-namespace,test-controller-kind,test-controller-name,test-pod,test-container,0.1,0.2,0.4,0.5,11,10,2,2,0.3,0.6,0.9,2,0.8,4.6000000000000005
 `, string(storage.WriteCalls()[0].Data))
 	})
 
@@ -116,9 +118,9 @@ func Test_UpdateCSV(t *testing.T) {
 		// 2021-01-01 is already in the export file, so we only compute for 2021-01-02
 		assert.Equal(t, time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].Start)
 		assert.Equal(t, time.Date(2021, 1, 3, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].End)
-		assert.Equal(t, `Date,Namespace,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost,ControllerKind,ControllerName,Pod,Container,GPUs,GPUCost,NetworkCost,PVBytes,PVCost,TotalCost
-2021-01-01,test-namespace,0.1,0.2,0.3,0.4,0.5,0.6,,,,,,,,,,
-2021-01-02,test-namespace,0,0,1,0,0,0,,,,,0,0,0,0,0,1
+		assert.Equal(t, `Date,Namespace,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost,ControllerKind,ControllerName,Pod,Container,NetworkReceiveBytes,NetworkTransferBytes,GPUs,PVBytes,NetworkCost,PVCost,GPUCost,TotalCost
+2021-01-01,test-namespace,0.1,0.2,0.3,0.4,0.5,0.6,,,,,,,,,,,,
+2021-01-02,test-namespace,0,0,1,0,0,0,,,,,0,0,0,0,0,0,0,1
 `, string(storage.WriteCalls()[0].Data))
 	})