2
0

view.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. Names []string `json:"names,omitempty"`
  28. Token string `json:"token,omitempty"`
  29. Labels map[string]string `json:"labels"`
  30. KubernetesPercent float64 `json:"kubernetesPercent"`
  31. Cost float64 `json:"cost"`
  32. }
  33. func (vtr *ViewTableRow) Equal(that *ViewTableRow) bool {
  34. if vtr.Name != that.Name {
  35. return false
  36. }
  37. if len(vtr.Labels) != len(that.Labels) {
  38. return false
  39. }
  40. for key, value := range vtr.Labels {
  41. thatValue, ok := that.Labels[key]
  42. if !ok {
  43. return false
  44. }
  45. if value != thatValue {
  46. return false
  47. }
  48. }
  49. if !mathutil.Approximately(vtr.KubernetesPercent, that.KubernetesPercent) {
  50. return false
  51. }
  52. if !mathutil.Approximately(vtr.Cost, that.Cost) {
  53. return false
  54. }
  55. return true
  56. }
  57. type ViewGraphData []*ViewGraphDataSet
  58. func (vgd ViewGraphData) Equal(that ViewGraphData) bool {
  59. if len(vgd) != len(that) {
  60. return false
  61. }
  62. for i := 0; i < len(vgd); i++ {
  63. if !vgd[i].Equal(that[i]) {
  64. return false
  65. }
  66. }
  67. return true
  68. }
  69. type ViewGraphDataSet struct {
  70. Start time.Time `json:"start"`
  71. End time.Time `json:"end"`
  72. Items []ViewGraphDataSetItem `json:"items"`
  73. }
  74. // NOTE: does not compare start and end times, just that the items are equal
  75. func (vgds *ViewGraphDataSet) Equal(that *ViewGraphDataSet) bool {
  76. if len(vgds.Items) != len(that.Items) {
  77. return false
  78. }
  79. for i := 0; i < len(vgds.Items); i++ {
  80. if !vgds.Items[i].Equal(that.Items[i]) {
  81. return false
  82. }
  83. }
  84. return true
  85. }
  86. type ViewGraphDataSetItem struct {
  87. Name string `json:"name"`
  88. Value float64 `json:"value"`
  89. }
  90. func (vgdsi ViewGraphDataSetItem) Equal(that ViewGraphDataSetItem) bool {
  91. if vgdsi.Name != that.Name {
  92. return false
  93. }
  94. if !mathutil.Approximately(vgdsi.Value, that.Value) {
  95. return false
  96. }
  97. return true
  98. }
  99. type ViewTotals struct {
  100. NumResults int `json:"numResults"`
  101. Combined *ViewTableRow `json:"combined"`
  102. }