allocation_json.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package kubecost
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/pkg/util/json"
  5. "math"
  6. "time"
  7. )
  8. // AllocationJSON exists because there are expected JSON response fields
  9. // that are calculated values from methods on an annotation
  10. type AllocationJSON struct {
  11. Name string `json:"name"`
  12. Properties *AllocationProperties `json:"properties"`
  13. Window Window `json:"window"`
  14. Start string `json:"start"`
  15. End string `json:"end"`
  16. Minutes *float64 `json:"minutes"`
  17. CPUCores *float64 `json:"cpuCores"`
  18. CPUCoreRequestAverage *float64 `json:"cpuCoreRequestAverage"`
  19. CPUCoreUsageAverage *float64 `json:"cpuCoreUsageAverage"`
  20. CPUCoreHours *float64 `json:"cpuCoreHours"`
  21. CPUCost *float64 `json:"cpuCost"`
  22. CPUCostAdjustment *float64 `json:"cpuCostAdjustment"`
  23. CPUEfficiency *float64 `json:"cpuEfficiency"`
  24. GPUCount *float64 `json:"gpuCount"`
  25. GPUHours *float64 `json:"gpuHours"`
  26. GPUCost *float64 `json:"gpuCost"`
  27. GPUCostAdjustment *float64 `json:"gpuCostAdjustment"`
  28. NetworkTransferBytes *float64 `json:"networkTransferBytes"`
  29. NetworkReceiveBytes *float64 `json:"networkReceiveBytes"`
  30. NetworkCost *float64 `json:"networkCost"`
  31. NetworkCrossZoneCost *float64 `json:"networkCrossZoneCost"`
  32. NetworkCrossRegionCost *float64 `json:"networkCrossRegionCost"`
  33. NetworkInternetCost *float64 `json:"networkInternetCost"`
  34. NetworkCostAdjustment *float64 `json:"networkCostAdjustment"`
  35. LoadBalancerCost *float64 `json:"loadBalancerCost"`
  36. LoadBalancerCostAdjustment *float64 `json:"loadBalancerCostAdjustment"`
  37. PVBytes *float64 `json:"pvBytes"`
  38. PVByteHours *float64 `json:"pvByteHours"`
  39. PVCost *float64 `json:"pvCost"`
  40. PVs PVAllocations `json:"pvs"`
  41. PVCostAdjustment *float64 `json:"pvCostAdjustment"`
  42. RAMBytes *float64 `json:"ramBytes"`
  43. RAMByteRequestAverage *float64 `json:"ramByteRequestAverage"`
  44. RAMByteUsageAverage *float64 `json:"ramByteUsageAverage"`
  45. RAMByteHours *float64 `json:"ramByteHours"`
  46. RAMCost *float64 `json:"ramCost"`
  47. RAMCostAdjustment *float64 `json:"ramCostAdjustment"`
  48. RAMEfficiency *float64 `json:"ramEfficiency"`
  49. ExternalCost *float64 `json:"externalCost"`
  50. SharedCost *float64 `json:"sharedCost"`
  51. TotalCost *float64 `json:"totalCost"`
  52. TotalEfficiency *float64 `json:"totalEfficiency"`
  53. RawAllocationOnly *RawAllocationOnlyData `json:"rawAllocationOnly"`
  54. }
  55. func (aj *AllocationJSON) BuildFromAllocation(a *Allocation) {
  56. if aj == nil {
  57. return
  58. }
  59. aj.Name = a.Name
  60. aj.Properties = a.Properties
  61. aj.Window = a.Window
  62. aj.Start = a.Start.Format(time.RFC3339)
  63. aj.End = a.End.Format(time.RFC3339)
  64. aj.Minutes = formatFloat64ForResponse(a.Minutes())
  65. aj.CPUCores = formatFloat64ForResponse(a.CPUCores())
  66. aj.CPUCoreRequestAverage = formatFloat64ForResponse(a.CPUCoreRequestAverage)
  67. aj.CPUCoreUsageAverage = formatFloat64ForResponse(a.CPUCoreUsageAverage)
  68. aj.CPUCoreHours = formatFloat64ForResponse(a.CPUCoreHours)
  69. aj.CPUCost = formatFloat64ForResponse(a.CPUCost)
  70. aj.CPUCostAdjustment = formatFloat64ForResponse(a.CPUCostAdjustment)
  71. aj.CPUEfficiency = formatFloat64ForResponse(a.CPUEfficiency())
  72. aj.GPUCount = formatFloat64ForResponse(a.GPUs())
  73. aj.GPUHours = formatFloat64ForResponse(a.GPUHours)
  74. aj.GPUCost = formatFloat64ForResponse(a.GPUCost)
  75. aj.GPUCostAdjustment = formatFloat64ForResponse(a.GPUCostAdjustment)
  76. aj.NetworkTransferBytes = formatFloat64ForResponse(a.NetworkTransferBytes)
  77. aj.NetworkReceiveBytes = formatFloat64ForResponse(a.NetworkReceiveBytes)
  78. aj.NetworkCost = formatFloat64ForResponse(a.NetworkCost)
  79. aj.NetworkCrossZoneCost = formatFloat64ForResponse(a.NetworkCrossZoneCost)
  80. aj.NetworkCrossRegionCost = formatFloat64ForResponse(a.NetworkCrossRegionCost)
  81. aj.NetworkInternetCost = formatFloat64ForResponse(a.NetworkInternetCost)
  82. aj.NetworkCostAdjustment = formatFloat64ForResponse(a.NetworkCostAdjustment)
  83. aj.LoadBalancerCost = formatFloat64ForResponse(a.LoadBalancerCost)
  84. aj.LoadBalancerCostAdjustment = formatFloat64ForResponse(a.LoadBalancerCostAdjustment)
  85. aj.PVBytes = formatFloat64ForResponse(a.PVBytes())
  86. aj.PVByteHours = formatFloat64ForResponse(a.PVByteHours())
  87. aj.PVCost = formatFloat64ForResponse(a.PVCost())
  88. aj.PVs = a.PVs
  89. aj.PVCostAdjustment = formatFloat64ForResponse(a.PVCostAdjustment)
  90. aj.RAMBytes = formatFloat64ForResponse(a.RAMBytes())
  91. aj.RAMByteRequestAverage = formatFloat64ForResponse(a.RAMBytesRequestAverage)
  92. aj.RAMByteUsageAverage = formatFloat64ForResponse(a.RAMBytesUsageAverage)
  93. aj.RAMByteHours = formatFloat64ForResponse(a.RAMByteHours)
  94. aj.RAMCost = formatFloat64ForResponse(a.RAMCost)
  95. aj.RAMCostAdjustment = formatFloat64ForResponse(a.RAMCostAdjustment)
  96. aj.RAMEfficiency = formatFloat64ForResponse(a.RAMEfficiency())
  97. aj.SharedCost = formatFloat64ForResponse(a.SharedCost)
  98. aj.ExternalCost = formatFloat64ForResponse(a.ExternalCost)
  99. aj.TotalCost = formatFloat64ForResponse(a.TotalCost())
  100. aj.TotalEfficiency = formatFloat64ForResponse(a.TotalEfficiency())
  101. aj.RawAllocationOnly = a.RawAllocationOnly
  102. }
  103. // formatFloat64ForResponse - take an existing float64, round it to 6 decimal places and return is possible, or return nil if invalid
  104. func formatFloat64ForResponse(f float64) *float64 {
  105. if math.IsNaN(f) || math.IsInf(f, 0) {
  106. return nil
  107. }
  108. // 6 digits of precision is the maximum the API should return
  109. result := math.Round(f*100000) / 100000.0
  110. return &result
  111. }
  112. // MarshalJSON implements json.Marshaler interface
  113. func (a *Allocation) MarshalJSON() ([]byte, error) {
  114. aj := &AllocationJSON{}
  115. aj.BuildFromAllocation(a)
  116. buffer, err := json.Marshal(aj)
  117. if err != nil {
  118. return nil, fmt.Errorf("unable to marshal allocation %s to JSON: %s", aj.Name, err)
  119. }
  120. return buffer, nil
  121. }
  122. // UnmarshalJSON prevent nil pointer on PVAllocations
  123. func (a *Allocation) UnmarshalJSON(b []byte) error {
  124. // initialize PV to prevent nil panic
  125. a.PVs = PVAllocations{}
  126. // Aliasing Allocation and casting to alias gives access to the default unmarshaller
  127. type alloc Allocation
  128. err := json.Unmarshal(b, (*alloc)(a))
  129. if err != nil {
  130. return err
  131. }
  132. // clear PVs if they are empty, it is not initialized when empty
  133. if len(a.PVs) == 0 {
  134. a.PVs = nil
  135. }
  136. return nil
  137. }
  138. // MarshalJSON marshals PVAllocation as map[*PVKey]*PVAllocation this allows PVKey to retain its values through marshalling
  139. func (pv PVAllocations) MarshalJSON() (b []byte, err error) {
  140. pointerMap := make(map[*PVKey]*PVAllocation)
  141. for pvKey, pvAlloc := range pv {
  142. kp := pvKey
  143. pointerMap[&kp] = pvAlloc
  144. }
  145. return json.Marshal(pointerMap)
  146. }
  147. // MarshalText converts PVKey to string to make it compatible with JSON Marshaller as an Object key
  148. // this function is required to have a value caller for the actual values to be saved
  149. func (pvk PVKey) MarshalText() (text []byte, err error) {
  150. return []byte(pvk.String()), nil
  151. }
  152. // UnmarshalText converts JSON key string to PVKey it compatible with JSON Unmarshaller from an Object key
  153. // this function is required to have a pointer caller for values to be pulled into marshalling struct
  154. func (pvk *PVKey) UnmarshalText(text []byte) error {
  155. return pvk.FromString(string(text))
  156. }
  157. // MarshalJSON JSON-encodes the AllocationSet
  158. func (as *AllocationSet) MarshalJSON() ([]byte, error) {
  159. if as == nil {
  160. return json.Marshal(map[string]*Allocation{})
  161. }
  162. return json.Marshal(as.Allocations)
  163. }
  164. // MarshalJSON JSON-encodes the range
  165. func (asr *AllocationSetRange) MarshalJSON() ([]byte, error) {
  166. if asr == nil {
  167. return json.Marshal([]*AllocationSet{})
  168. }
  169. return json.Marshal(asr.Allocations)
  170. }