2
0

allocation_json.go 11 KB

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