summaryallocation_json.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package opencost
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/util/formatutil"
  5. )
  6. // SummaryAllocationResponse is a sanitized version of SummaryAllocation, which
  7. // formats fields and protects against issues like mashaling NaNs.
  8. type SummaryAllocationResponse struct {
  9. Name string `json:"name"`
  10. Start time.Time `json:"start"`
  11. End time.Time `json:"end"`
  12. CPUCoreRequestAverage *float64 `json:"cpuCoreRequestAverage"`
  13. CPUCoreLimitAverage *float64 `json:"cpuCoreLimitAverage"`
  14. CPUCoreUsageAverage *float64 `json:"cpuCoreUsageAverage"`
  15. CPUCost *float64 `json:"cpuCost"`
  16. CPUCostIdle *float64 `json:"cpuCostIdle"`
  17. GPURequestAverage *float64 `json:"gpuRequestAverage"`
  18. GPUUsageAverage *float64 `json:"gpuUsageAverage"`
  19. GPUCost *float64 `json:"gpuCost"`
  20. GPUCostIdle *float64 `json:"gpuCostIdle"`
  21. NetworkCost *float64 `json:"networkCost"`
  22. LoadBalancerCost *float64 `json:"loadBalancerCost"`
  23. PVCost *float64 `json:"pvCost"`
  24. RAMBytesRequestAverage *float64 `json:"ramByteRequestAverage"`
  25. RAMBytesLimitAverage *float64 `json:"ramBytesLimitAverage"`
  26. RAMBytesUsageAverage *float64 `json:"ramByteUsageAverage"`
  27. RAMCost *float64 `json:"ramCost"`
  28. RAMCostIdle *float64 `json:"ramCostIdle"`
  29. SharedCost *float64 `json:"sharedCost"`
  30. ExternalCost *float64 `json:"externalCost"`
  31. TotalEfficiency *float64 `json:"totalEfficiency"`
  32. TotalCost *float64 `json:"totalCost"`
  33. }
  34. // ToResponse converts a SummaryAllocation to a SummaryAllocationResponse,
  35. // protecting against NaN and null values.
  36. func (sa *SummaryAllocation) ToResponse() *SummaryAllocationResponse {
  37. if sa == nil {
  38. return nil
  39. }
  40. // if the efficiency has already been set,
  41. // prefer that since it has been calculated elsewhere
  42. // and matches the sorting criteria more closely
  43. efficiency := sa.Efficiency
  44. if efficiency == 0 {
  45. // if efficiency has not been set by SQL or otherwise, calculate it
  46. // using the object method
  47. efficiency = sa.TotalEfficiency()
  48. }
  49. return &SummaryAllocationResponse{
  50. Name: sa.Name,
  51. Start: sa.Start,
  52. End: sa.End,
  53. CPUCoreRequestAverage: formatutil.Float64ToResponse(sa.CPUCoreRequestAverage),
  54. CPUCoreLimitAverage: formatutil.Float64ToResponse(sa.CPUCoreLimitAverage),
  55. CPUCoreUsageAverage: formatutil.Float64ToResponse(sa.CPUCoreUsageAverage),
  56. CPUCost: formatutil.Float64ToResponse(sa.CPUCost),
  57. CPUCostIdle: formatutil.Float64ToResponse(sa.CPUCostIdle),
  58. GPURequestAverage: sa.GPURequestAverage, // already in *float64
  59. GPUUsageAverage: sa.GPUUsageAverage, // already in *float64
  60. GPUCost: formatutil.Float64ToResponse(sa.GPUCost),
  61. GPUCostIdle: formatutil.Float64ToResponse(sa.GPUCostIdle),
  62. NetworkCost: formatutil.Float64ToResponse(sa.NetworkCost),
  63. LoadBalancerCost: formatutil.Float64ToResponse(sa.LoadBalancerCost),
  64. PVCost: formatutil.Float64ToResponse(sa.PVCost),
  65. RAMBytesRequestAverage: formatutil.Float64ToResponse(sa.RAMBytesRequestAverage),
  66. RAMBytesLimitAverage: formatutil.Float64ToResponse(sa.RAMBytesLimitAverage),
  67. RAMBytesUsageAverage: formatutil.Float64ToResponse(sa.RAMBytesUsageAverage),
  68. RAMCost: formatutil.Float64ToResponse(sa.RAMCost),
  69. RAMCostIdle: formatutil.Float64ToResponse(sa.RAMCostIdle),
  70. SharedCost: formatutil.Float64ToResponse(sa.SharedCost),
  71. ExternalCost: formatutil.Float64ToResponse(sa.ExternalCost),
  72. TotalEfficiency: formatutil.Float64ToResponse(efficiency),
  73. TotalCost: formatutil.Float64ToResponse(sa.TotalCost()),
  74. }
  75. }
  76. // SummaryAllocationSetResponse is a sanitized version of SummaryAllocationSet,
  77. // which formats fields and protects against issues like marshaling NaNs.
  78. type SummaryAllocationSetResponse struct {
  79. SummaryAllocations map[string]*SummaryAllocationResponse `json:"allocations"`
  80. Window Window `json:"window"`
  81. }
  82. // ToResponse converts a SummaryAllocationSet to a SummaryAllocationSetResponse,
  83. // protecting against NaN and null values.
  84. func (sas *SummaryAllocationSet) ToResponse() *SummaryAllocationSetResponse {
  85. if sas == nil {
  86. return nil
  87. }
  88. sars := make(map[string]*SummaryAllocationResponse, len(sas.SummaryAllocations))
  89. for k, v := range sas.SummaryAllocations {
  90. sars[k] = v.ToResponse()
  91. }
  92. return &SummaryAllocationSetResponse{
  93. SummaryAllocations: sars,
  94. Window: sas.Window.Clone(),
  95. }
  96. }
  97. // SummaryAllocationSetRangeResponse is a sanitized version of SummaryAllocationSetRange,
  98. // which formats fields and protects against issues like marshaling NaNs.
  99. type SummaryAllocationSetRangeResponse struct {
  100. Step time.Duration `json:"step"`
  101. SummaryAllocationSets []*SummaryAllocationSetResponse `json:"sets"`
  102. Window Window `json:"window"`
  103. }
  104. // ToResponse converts a SummaryAllocationSet to a SummaryAllocationSetResponse,
  105. // protecting against NaN and null values.
  106. func (sasr *SummaryAllocationSetRange) ToResponse() *SummaryAllocationSetRangeResponse {
  107. if sasr == nil {
  108. return nil
  109. }
  110. sasrr := make([]*SummaryAllocationSetResponse, len(sasr.SummaryAllocationSets))
  111. for i, v := range sasr.SummaryAllocationSets {
  112. sasrr[i] = v.ToResponse()
  113. }
  114. return &SummaryAllocationSetRangeResponse{
  115. Step: sasr.Step,
  116. SummaryAllocationSets: sasrr,
  117. Window: sasr.Window.Clone(),
  118. }
  119. }
  120. func EmptySummaryAllocationSetRangeResponse() *SummaryAllocationSetRangeResponse {
  121. return &SummaryAllocationSetRangeResponse{}
  122. }