2
0

allocation_json.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, "networkCostAdjustment", a.NetworkCostAdjustment, ",")
  31. jsonEncodeFloat64(buffer, "loadBalancerCost", a.LoadBalancerCost, ",")
  32. jsonEncodeFloat64(buffer, "loadBalancerCostAdjustment", a.LoadBalancerCostAdjustment, ",")
  33. jsonEncodeFloat64(buffer, "pvBytes", a.PVBytes(), ",")
  34. jsonEncodeFloat64(buffer, "pvByteHours", a.PVByteHours(), ",")
  35. jsonEncodeFloat64(buffer, "pvCost", a.PVCost(), ",")
  36. jsonEncode(buffer, "pvs", a.PVs, ",")
  37. jsonEncodeFloat64(buffer, "pvCostAdjustment", a.PVCostAdjustment, ",")
  38. jsonEncodeFloat64(buffer, "ramBytes", a.RAMBytes(), ",")
  39. jsonEncodeFloat64(buffer, "ramByteRequestAverage", a.RAMBytesRequestAverage, ",")
  40. jsonEncodeFloat64(buffer, "ramByteUsageAverage", a.RAMBytesUsageAverage, ",")
  41. jsonEncodeFloat64(buffer, "ramByteHours", a.RAMByteHours, ",")
  42. jsonEncodeFloat64(buffer, "ramCost", a.RAMCost, ",")
  43. jsonEncodeFloat64(buffer, "ramCostAdjustment", a.RAMCostAdjustment, ",")
  44. jsonEncodeFloat64(buffer, "ramEfficiency", a.RAMEfficiency(), ",")
  45. jsonEncodeFloat64(buffer, "sharedCost", a.SharedCost, ",")
  46. jsonEncodeFloat64(buffer, "externalCost", a.ExternalCost, ",")
  47. jsonEncodeFloat64(buffer, "totalCost", a.TotalCost(), ",")
  48. jsonEncodeFloat64(buffer, "totalEfficiency", a.TotalEfficiency(), ",")
  49. jsonEncode(buffer, "rawAllocationOnly", a.RawAllocationOnly, "")
  50. buffer.WriteString("}")
  51. return buffer.Bytes(), nil
  52. }
  53. // UnmarshalJSON prevent nil pointer on PVAllocations
  54. func (a *Allocation) UnmarshalJSON(b []byte) error {
  55. // initialize PV to prevent nil panic
  56. a.PVs = PVAllocations{}
  57. // Aliasing Allocation and casting to alias gives access to the default unmarshaller
  58. type alloc Allocation
  59. err := json.Unmarshal(b, (*alloc)(a))
  60. if err != nil {
  61. return err
  62. }
  63. // clear PVs if they are empty, it is not initialized when empty
  64. if len(a.PVs) == 0 {
  65. a.PVs = nil
  66. }
  67. return nil
  68. }
  69. // MarshalJSON marshals PVAllocation as map[*PVKey]*PVAllocation this allows PVKey to retain its values through marshalling
  70. func (pv PVAllocations) MarshalJSON() (b []byte, err error) {
  71. pointerMap := make(map[*PVKey]*PVAllocation)
  72. for pvKey, pvAlloc := range pv {
  73. kp := pvKey
  74. pointerMap[&kp] = pvAlloc
  75. }
  76. return json.Marshal(pointerMap)
  77. }
  78. // MarshalText converts PVKey to string to make it compatible with JSON Marshaller as an Object key
  79. // this function is required to have a value caller for the actual values to be saved
  80. func (pvk PVKey) MarshalText() (text []byte, err error) {
  81. return []byte(pvk.String()), nil
  82. }
  83. // UnmarshalText converts JSON key string to PVKey it compatible with JSON Unmarshaller from an Object key
  84. // this function is required to have a pointer caller for values to be pulled into marshalling struct
  85. func (pvk *PVKey) UnmarshalText(text []byte) error {
  86. return pvk.FromString(string(text))
  87. }
  88. // MarshalJSON JSON-encodes the AllocationSet
  89. func (as *AllocationSet) MarshalJSON() ([]byte, error) {
  90. if as == nil {
  91. return json.Marshal(map[string]*Allocation{})
  92. }
  93. return json.Marshal(as.Allocations)
  94. }
  95. // MarshalJSON JSON-encodes the range
  96. func (asr *AllocationSetRange) MarshalJSON() ([]byte, error) {
  97. if asr == nil {
  98. return json.Marshal([]*AllocationSet{})
  99. }
  100. return json.Marshal(asr.Allocations)
  101. }