summaryallocation_json.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. return &SummaryAllocationResponse{
  36. Name: sa.Name,
  37. Start: sa.Start,
  38. End: sa.End,
  39. CPUCoreRequestAverage: formatutil.Float64ToResponse(sa.CPUCoreRequestAverage),
  40. CPUCoreUsageAverage: formatutil.Float64ToResponse(sa.CPUCoreUsageAverage),
  41. CPUCost: formatutil.Float64ToResponse(sa.CPUCost),
  42. GPURequestAverage: formatutil.Float64ToResponse(sa.GPURequestAverage),
  43. GPUUsageAverage: formatutil.Float64ToResponse(sa.GPUUsageAverage),
  44. GPUCost: formatutil.Float64ToResponse(sa.GPUCost),
  45. NetworkCost: formatutil.Float64ToResponse(sa.NetworkCost),
  46. LoadBalancerCost: formatutil.Float64ToResponse(sa.LoadBalancerCost),
  47. PVCost: formatutil.Float64ToResponse(sa.PVCost),
  48. RAMBytesRequestAverage: formatutil.Float64ToResponse(sa.RAMBytesRequestAverage),
  49. RAMBytesUsageAverage: formatutil.Float64ToResponse(sa.RAMBytesUsageAverage),
  50. RAMCost: formatutil.Float64ToResponse(sa.RAMCost),
  51. SharedCost: formatutil.Float64ToResponse(sa.SharedCost),
  52. ExternalCost: formatutil.Float64ToResponse(sa.ExternalCost),
  53. TotalEfficiency: formatutil.Float64ToResponse(sa.TotalEfficiency()),
  54. TotalCost: formatutil.Float64ToResponse(sa.TotalCost()),
  55. }
  56. }
  57. // SummaryAllocationSetResponse is a sanitized version of SummaryAllocationSet,
  58. // which formats fields and protects against issues like marshaling NaNs.
  59. type SummaryAllocationSetResponse struct {
  60. SummaryAllocations map[string]*SummaryAllocationResponse `json:"allocations"`
  61. Window Window `json:"window"`
  62. }
  63. // ToResponse converts a SummaryAllocationSet to a SummaryAllocationSetResponse,
  64. // protecting against NaN and null values.
  65. func (sas *SummaryAllocationSet) ToResponse() *SummaryAllocationSetResponse {
  66. if sas == nil {
  67. return nil
  68. }
  69. sars := make(map[string]*SummaryAllocationResponse, len(sas.SummaryAllocations))
  70. for k, v := range sas.SummaryAllocations {
  71. sars[k] = v.ToResponse()
  72. }
  73. return &SummaryAllocationSetResponse{
  74. SummaryAllocations: sars,
  75. Window: sas.Window.Clone(),
  76. }
  77. }
  78. // SummaryAllocationSetRangeResponse is a sanitized version of SummaryAllocationSetRange,
  79. // which formats fields and protects against issues like marshaling NaNs.
  80. type SummaryAllocationSetRangeResponse struct {
  81. Step time.Duration `json:"step"`
  82. SummaryAllocationSets []*SummaryAllocationSetResponse `json:"sets"`
  83. Window Window `json:"window"`
  84. }
  85. // ToResponse converts a SummaryAllocationSet to a SummaryAllocationSetResponse,
  86. // protecting against NaN and null values.
  87. func (sasr *SummaryAllocationSetRange) ToResponse() *SummaryAllocationSetRangeResponse {
  88. if sasr == nil {
  89. return nil
  90. }
  91. sasrr := make([]*SummaryAllocationSetResponse, len(sasr.SummaryAllocationSets))
  92. for i, v := range sasr.SummaryAllocationSets {
  93. sasrr[i] = v.ToResponse()
  94. }
  95. return &SummaryAllocationSetRangeResponse{
  96. Step: sasr.Step,
  97. SummaryAllocationSets: sasrr,
  98. Window: sasr.Window.Clone(),
  99. }
  100. }
  101. func EmptySummaryAllocationSetRangeResponse() *SummaryAllocationSetRangeResponse {
  102. return &SummaryAllocationSetRangeResponse{}
  103. }