summaryallocation_json.go 4.5 KB

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