summaryallocation_json.go 5.1 KB

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