view.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package cloudcost
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/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. KubernetesPercent float64 `json:"kubernetesPercent"`
  28. Cost float64 `json:"cost"`
  29. }
  30. func (vtr *ViewTableRow) Equal(that *ViewTableRow) bool {
  31. if vtr.Name != that.Name {
  32. return false
  33. }
  34. if !mathutil.Approximately(vtr.KubernetesPercent, that.KubernetesPercent) {
  35. return false
  36. }
  37. if !mathutil.Approximately(vtr.Cost, that.Cost) {
  38. return false
  39. }
  40. return true
  41. }
  42. type ViewGraphData []*ViewGraphDataSet
  43. func (vgd ViewGraphData) Equal(that ViewGraphData) bool {
  44. if len(vgd) != len(that) {
  45. return false
  46. }
  47. for i := 0; i < len(vgd); i++ {
  48. if !vgd[i].Equal(that[i]) {
  49. return false
  50. }
  51. }
  52. return true
  53. }
  54. type ViewGraphDataSet struct {
  55. Start time.Time `json:"start"`
  56. End time.Time `json:"end"`
  57. Items []ViewGraphDataSetItem `json:"items"`
  58. }
  59. // NOTE: does not compare start and end times, just that the items are equal
  60. func (vgds *ViewGraphDataSet) Equal(that *ViewGraphDataSet) bool {
  61. if len(vgds.Items) != len(that.Items) {
  62. return false
  63. }
  64. for i := 0; i < len(vgds.Items); i++ {
  65. if !vgds.Items[i].Equal(that.Items[i]) {
  66. return false
  67. }
  68. }
  69. return true
  70. }
  71. type ViewGraphDataSetItem struct {
  72. Name string `json:"name"`
  73. Value float64 `json:"value"`
  74. }
  75. func (vgdsi ViewGraphDataSetItem) Equal(that ViewGraphDataSetItem) bool {
  76. if vgdsi.Name != that.Name {
  77. return false
  78. }
  79. if !mathutil.Approximately(vgdsi.Value, that.Value) {
  80. return false
  81. }
  82. return true
  83. }