allocation_json.go 10 KB

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