summaryallocation_json.go 4.8 KB

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