allocation_json.go 9.1 KB

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