allocation_json.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package kubecost
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "time"
  6. )
  7. // MarshalJSON implements json.Marshaler interface
  8. func (a *Allocation) MarshalJSON() ([]byte, error) {
  9. buffer := bytes.NewBufferString("{")
  10. jsonEncodeString(buffer, "name", a.Name, ",")
  11. jsonEncode(buffer, "properties", a.Properties, ",")
  12. jsonEncode(buffer, "window", a.Window, ",")
  13. jsonEncodeString(buffer, "start", a.Start.Format(time.RFC3339), ",")
  14. jsonEncodeString(buffer, "end", a.End.Format(time.RFC3339), ",")
  15. jsonEncodeFloat64(buffer, "minutes", a.Minutes(), ",")
  16. jsonEncodeFloat64(buffer, "cpuCores", a.CPUCores(), ",")
  17. jsonEncodeFloat64(buffer, "cpuCoreRequestAverage", a.CPUCoreRequestAverage, ",")
  18. jsonEncodeFloat64(buffer, "cpuCoreUsageAverage", a.CPUCoreUsageAverage, ",")
  19. jsonEncodeFloat64(buffer, "cpuCoreHours", a.CPUCoreHours, ",")
  20. jsonEncodeFloat64(buffer, "cpuCost", a.CPUCost, ",")
  21. jsonEncodeFloat64(buffer, "cpuCostAdjustment", a.CPUCostAdjustment, ",")
  22. jsonEncodeFloat64(buffer, "cpuEfficiency", a.CPUEfficiency(), ",")
  23. jsonEncodeFloat64(buffer, "gpuCount", a.GPUs(), ",")
  24. jsonEncodeFloat64(buffer, "gpuHours", a.GPUHours, ",")
  25. jsonEncodeFloat64(buffer, "gpuCost", a.GPUCost, ",")
  26. jsonEncodeFloat64(buffer, "gpuCostAdjustment", a.GPUCostAdjustment, ",")
  27. jsonEncodeFloat64(buffer, "networkTransferBytes", a.NetworkTransferBytes, ",")
  28. jsonEncodeFloat64(buffer, "networkReceiveBytes", a.NetworkReceiveBytes, ",")
  29. jsonEncodeFloat64(buffer, "networkCost", a.NetworkCost, ",")
  30. jsonEncodeFloat64(buffer, "networkCrossZoneCost", a.NetworkCrossZoneCost, ",")
  31. jsonEncodeFloat64(buffer, "networkCrossRegionCost", a.NetworkCrossRegionCost, ",")
  32. jsonEncodeFloat64(buffer, "networkInternetCost", a.NetworkInternetCost, ",")
  33. jsonEncodeFloat64(buffer, "networkCostAdjustment", a.NetworkCostAdjustment, ",")
  34. jsonEncodeFloat64(buffer, "loadBalancerCost", a.LoadBalancerCost, ",")
  35. jsonEncodeFloat64(buffer, "loadBalancerCostAdjustment", a.LoadBalancerCostAdjustment, ",")
  36. jsonEncodeFloat64(buffer, "pvBytes", a.PVBytes(), ",")
  37. jsonEncodeFloat64(buffer, "pvByteHours", a.PVByteHours(), ",")
  38. jsonEncodeFloat64(buffer, "pvCost", a.PVCost(), ",")
  39. jsonEncode(buffer, "pvs", a.PVs, ",")
  40. jsonEncodeFloat64(buffer, "pvCostAdjustment", a.PVCostAdjustment, ",")
  41. jsonEncodeFloat64(buffer, "ramBytes", a.RAMBytes(), ",")
  42. jsonEncodeFloat64(buffer, "ramByteRequestAverage", a.RAMBytesRequestAverage, ",")
  43. jsonEncodeFloat64(buffer, "ramByteUsageAverage", a.RAMBytesUsageAverage, ",")
  44. jsonEncodeFloat64(buffer, "ramByteHours", a.RAMByteHours, ",")
  45. jsonEncodeFloat64(buffer, "ramCost", a.RAMCost, ",")
  46. jsonEncodeFloat64(buffer, "ramCostAdjustment", a.RAMCostAdjustment, ",")
  47. jsonEncodeFloat64(buffer, "ramEfficiency", a.RAMEfficiency(), ",")
  48. jsonEncodeFloat64(buffer, "sharedCost", a.SharedCost, ",")
  49. jsonEncodeFloat64(buffer, "externalCost", a.ExternalCost, ",")
  50. jsonEncodeFloat64(buffer, "totalCost", a.TotalCost(), ",")
  51. jsonEncodeFloat64(buffer, "totalEfficiency", a.TotalEfficiency(), ",")
  52. jsonEncode(buffer, "rawAllocationOnly", a.RawAllocationOnly, "")
  53. buffer.WriteString("}")
  54. return buffer.Bytes(), nil
  55. }
  56. // UnmarshalJSON prevent nil pointer on PVAllocations
  57. func (a *Allocation) UnmarshalJSON(b []byte) error {
  58. // initialize PV to prevent nil panic
  59. a.PVs = PVAllocations{}
  60. // Aliasing Allocation and casting to alias gives access to the default unmarshaller
  61. type alloc Allocation
  62. err := json.Unmarshal(b, (*alloc)(a))
  63. if err != nil {
  64. return err
  65. }
  66. // clear PVs if they are empty, it is not initialized when empty
  67. if len(a.PVs) == 0 {
  68. a.PVs = nil
  69. }
  70. return nil
  71. }
  72. // MarshalJSON marshals PVAllocation as map[*PVKey]*PVAllocation this allows PVKey to retain its values through marshalling
  73. func (pv PVAllocations) MarshalJSON() (b []byte, err error) {
  74. pointerMap := make(map[*PVKey]*PVAllocation)
  75. for pvKey, pvAlloc := range pv {
  76. kp := pvKey
  77. pointerMap[&kp] = pvAlloc
  78. }
  79. return json.Marshal(pointerMap)
  80. }
  81. // MarshalText converts PVKey to string to make it compatible with JSON Marshaller as an Object key
  82. // this function is required to have a value caller for the actual values to be saved
  83. func (pvk PVKey) MarshalText() (text []byte, err error) {
  84. return []byte(pvk.String()), nil
  85. }
  86. // UnmarshalText converts JSON key string to PVKey it compatible with JSON Unmarshaller from an Object key
  87. // this function is required to have a pointer caller for values to be pulled into marshalling struct
  88. func (pvk *PVKey) UnmarshalText(text []byte) error {
  89. return pvk.FromString(string(text))
  90. }
  91. // MarshalJSON JSON-encodes the AllocationSet
  92. func (as *AllocationSet) MarshalJSON() ([]byte, error) {
  93. if as == nil {
  94. return json.Marshal(map[string]*Allocation{})
  95. }
  96. return json.Marshal(as.Allocations)
  97. }
  98. // MarshalJSON JSON-encodes the range
  99. func (asr *AllocationSetRange) MarshalJSON() ([]byte, error) {
  100. if asr == nil {
  101. return json.Marshal([]*AllocationSet{})
  102. }
  103. return json.Marshal(asr.Allocations)
  104. }