allocation_json.go 9.8 KB

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