allocation_json.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package kubecost
  2. import (
  3. "fmt"
  4. "math"
  5. "time"
  6. "github.com/opencost/opencost/pkg/util/json"
  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,omitEmpty"`
  54. ProportionalAssetResourceCosts *ProportionalAssetResourceCosts `json:"proportionalAssetResourceCosts,omitEmpty"`
  55. }
  56. func (aj *AllocationJSON) BuildFromAllocation(a *Allocation) {
  57. if aj == nil {
  58. return
  59. }
  60. aj.Name = a.Name
  61. aj.Properties = a.Properties
  62. aj.Window = a.Window
  63. aj.Start = a.Start.UTC().Format(time.RFC3339)
  64. aj.End = a.End.UTC().Format(time.RFC3339)
  65. aj.Minutes = formatFloat64ForResponse(a.Minutes())
  66. aj.CPUCores = formatFloat64ForResponse(a.CPUCores())
  67. aj.CPUCoreRequestAverage = formatFloat64ForResponse(a.CPUCoreRequestAverage)
  68. aj.CPUCoreUsageAverage = formatFloat64ForResponse(a.CPUCoreUsageAverage)
  69. aj.CPUCoreHours = formatFloat64ForResponse(a.CPUCoreHours)
  70. aj.CPUCost = formatFloat64ForResponse(a.CPUCost)
  71. aj.CPUCostAdjustment = formatFloat64ForResponse(a.CPUCostAdjustment)
  72. aj.CPUEfficiency = formatFloat64ForResponse(a.CPUEfficiency())
  73. aj.GPUCount = formatFloat64ForResponse(a.GPUs())
  74. aj.GPUHours = formatFloat64ForResponse(a.GPUHours)
  75. aj.GPUCost = formatFloat64ForResponse(a.GPUCost)
  76. aj.GPUCostAdjustment = formatFloat64ForResponse(a.GPUCostAdjustment)
  77. aj.NetworkTransferBytes = formatFloat64ForResponse(a.NetworkTransferBytes)
  78. aj.NetworkReceiveBytes = formatFloat64ForResponse(a.NetworkReceiveBytes)
  79. aj.NetworkCost = formatFloat64ForResponse(a.NetworkCost)
  80. aj.NetworkCrossZoneCost = formatFloat64ForResponse(a.NetworkCrossZoneCost)
  81. aj.NetworkCrossRegionCost = formatFloat64ForResponse(a.NetworkCrossRegionCost)
  82. aj.NetworkInternetCost = formatFloat64ForResponse(a.NetworkInternetCost)
  83. aj.NetworkCostAdjustment = formatFloat64ForResponse(a.NetworkCostAdjustment)
  84. aj.LoadBalancerCost = formatFloat64ForResponse(a.LoadBalancerCost)
  85. aj.LoadBalancerCostAdjustment = formatFloat64ForResponse(a.LoadBalancerCostAdjustment)
  86. aj.PVBytes = formatFloat64ForResponse(a.PVBytes())
  87. aj.PVByteHours = formatFloat64ForResponse(a.PVByteHours())
  88. aj.PVCost = formatFloat64ForResponse(a.PVCost())
  89. aj.PVs = a.PVs
  90. aj.PVCostAdjustment = formatFloat64ForResponse(a.PVCostAdjustment)
  91. aj.RAMBytes = formatFloat64ForResponse(a.RAMBytes())
  92. aj.RAMByteRequestAverage = formatFloat64ForResponse(a.RAMBytesRequestAverage)
  93. aj.RAMByteUsageAverage = formatFloat64ForResponse(a.RAMBytesUsageAverage)
  94. aj.RAMByteHours = formatFloat64ForResponse(a.RAMByteHours)
  95. aj.RAMCost = formatFloat64ForResponse(a.RAMCost)
  96. aj.RAMCostAdjustment = formatFloat64ForResponse(a.RAMCostAdjustment)
  97. aj.RAMEfficiency = formatFloat64ForResponse(a.RAMEfficiency())
  98. aj.SharedCost = formatFloat64ForResponse(a.SharedCost)
  99. aj.ExternalCost = formatFloat64ForResponse(a.ExternalCost)
  100. aj.TotalCost = formatFloat64ForResponse(a.TotalCost())
  101. aj.TotalEfficiency = formatFloat64ForResponse(a.TotalEfficiency())
  102. aj.RawAllocationOnly = a.RawAllocationOnly
  103. aj.ProportionalAssetResourceCosts = &a.ProportionalAssetResourceCosts
  104. }
  105. // formatFloat64ForResponse - take an existing float64, round it to 6 decimal places and return is possible, or return nil if invalid
  106. func formatFloat64ForResponse(f float64) *float64 {
  107. if math.IsNaN(f) || math.IsInf(f, 0) {
  108. return nil
  109. }
  110. // 6 digits of precision is the maximum the API should return
  111. result := math.Round(f*100000) / 100000.0
  112. return &result
  113. }
  114. // MarshalJSON implements json.Marshaler interface
  115. func (a *Allocation) MarshalJSON() ([]byte, error) {
  116. aj := &AllocationJSON{}
  117. aj.BuildFromAllocation(a)
  118. buffer, err := json.Marshal(aj)
  119. if err != nil {
  120. return nil, fmt.Errorf("unable to marshal allocation %s to JSON: %s", aj.Name, err)
  121. }
  122. return buffer, nil
  123. }
  124. // UnmarshalJSON prevent nil pointer on PVAllocations
  125. func (a *Allocation) UnmarshalJSON(b []byte) error {
  126. // initialize PV to prevent nil panic
  127. a.PVs = PVAllocations{}
  128. // Aliasing Allocation and casting to alias gives access to the default unmarshaller
  129. type alloc Allocation
  130. err := json.Unmarshal(b, (*alloc)(a))
  131. if err != nil {
  132. return err
  133. }
  134. // clear PVs if they are empty, it is not initialized when empty
  135. if len(a.PVs) == 0 {
  136. a.PVs = nil
  137. }
  138. return nil
  139. }
  140. // MarshalJSON marshals PVAllocation as map[*PVKey]*PVAllocation this allows PVKey to retain its values through marshalling
  141. func (pv PVAllocations) MarshalJSON() (b []byte, err error) {
  142. pointerMap := make(map[*PVKey]*PVAllocation)
  143. for pvKey, pvAlloc := range pv {
  144. kp := pvKey
  145. pointerMap[&kp] = pvAlloc
  146. }
  147. return json.Marshal(pointerMap)
  148. }
  149. // MarshalText converts PVKey to string to make it compatible with JSON Marshaller as an Object key
  150. // this function is required to have a value caller for the actual values to be saved
  151. func (pvk PVKey) MarshalText() (text []byte, err error) {
  152. return []byte(pvk.String()), nil
  153. }
  154. // UnmarshalText converts JSON key string to PVKey it compatible with JSON Unmarshaller from an Object key
  155. // this function is required to have a pointer caller for values to be pulled into marshalling struct
  156. func (pvk *PVKey) UnmarshalText(text []byte) error {
  157. return pvk.FromString(string(text))
  158. }
  159. // MarshalJSON JSON-encodes the AllocationSet
  160. func (as *AllocationSet) MarshalJSON() ([]byte, error) {
  161. if as == nil {
  162. return json.Marshal(map[string]*Allocation{})
  163. }
  164. return json.Marshal(as.Allocations)
  165. }
  166. // MarshalJSON JSON-encodes the range
  167. func (asr *AllocationSetRange) MarshalJSON() ([]byte, error) {
  168. if asr == nil {
  169. return json.Marshal([]*AllocationSet{})
  170. }
  171. return json.Marshal(asr.Allocations)
  172. }