view.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package cloudcost
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/util/mathutil"
  5. )
  6. // View serves data to the Cloud Cost front end, in the
  7. // structure it requires (i.e. a graph and a table).
  8. type View struct {
  9. GraphData ViewGraphData `json:"graphData"`
  10. TableTotal *ViewTableRow `json:"tableTotal"`
  11. TableRows ViewTableRows `json:"tableRows"`
  12. }
  13. type ViewTableRows []*ViewTableRow
  14. func (vtrs ViewTableRows) Equal(that ViewTableRows) bool {
  15. if len(vtrs) != len(that) {
  16. return false
  17. }
  18. for i := 0; i < len(vtrs); i++ {
  19. if !vtrs[i].Equal(that[i]) {
  20. return false
  21. }
  22. }
  23. return true
  24. }
  25. type ViewTableRow struct {
  26. Name string `json:"name"`
  27. Labels map[string]string `json:"labels"`
  28. KubernetesPercent float64 `json:"kubernetesPercent"`
  29. Cost float64 `json:"cost"`
  30. }
  31. func (vtr *ViewTableRow) Equal(that *ViewTableRow) bool {
  32. if vtr.Name != that.Name {
  33. return false
  34. }
  35. if len(vtr.Labels) != len(that.Labels) {
  36. return false
  37. }
  38. for key, value := range vtr.Labels {
  39. thatValue, ok := that.Labels[key]
  40. if !ok {
  41. return false
  42. }
  43. if value != thatValue {
  44. return false
  45. }
  46. }
  47. if !mathutil.Approximately(vtr.KubernetesPercent, that.KubernetesPercent) {
  48. return false
  49. }
  50. if !mathutil.Approximately(vtr.Cost, that.Cost) {
  51. return false
  52. }
  53. return true
  54. }
  55. type ViewGraphData []*ViewGraphDataSet
  56. func (vgd ViewGraphData) Equal(that ViewGraphData) bool {
  57. if len(vgd) != len(that) {
  58. return false
  59. }
  60. for i := 0; i < len(vgd); i++ {
  61. if !vgd[i].Equal(that[i]) {
  62. return false
  63. }
  64. }
  65. return true
  66. }
  67. type ViewGraphDataSet struct {
  68. Start time.Time `json:"start"`
  69. End time.Time `json:"end"`
  70. Items []ViewGraphDataSetItem `json:"items"`
  71. }
  72. // NOTE: does not compare start and end times, just that the items are equal
  73. func (vgds *ViewGraphDataSet) Equal(that *ViewGraphDataSet) bool {
  74. if len(vgds.Items) != len(that.Items) {
  75. return false
  76. }
  77. for i := 0; i < len(vgds.Items); i++ {
  78. if !vgds.Items[i].Equal(that.Items[i]) {
  79. return false
  80. }
  81. }
  82. return true
  83. }
  84. type ViewGraphDataSetItem struct {
  85. Name string `json:"name"`
  86. Value float64 `json:"value"`
  87. }
  88. func (vgdsi ViewGraphDataSetItem) Equal(that ViewGraphDataSetItem) bool {
  89. if vgdsi.Name != that.Name {
  90. return false
  91. }
  92. if !mathutil.Approximately(vgdsi.Value, that.Value) {
  93. return false
  94. }
  95. return true
  96. }
  97. type ViewTotals struct {
  98. NumResults int `json:"numResults"`
  99. Combined *ViewTableRow `json:"combined"`
  100. }