summaryallocation_json.go 4.3 KB

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