allocation_json.go 9.4 KB

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