summaryallocation_json.go 4.2 KB

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