2
0

memoryrepository.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cloudcost
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/opencost"
  7. "golang.org/x/exp/maps"
  8. )
  9. // MemoryRepository is an implementation of Repository that uses a map keyed on config key and window start along with a
  10. // RWMutex to make it threadsafe
  11. type MemoryRepository struct {
  12. rwLock sync.RWMutex
  13. data map[string]map[time.Time]*opencost.CloudCostSet
  14. }
  15. func NewMemoryRepository() *MemoryRepository {
  16. return &MemoryRepository{
  17. data: make(map[string]map[time.Time]*opencost.CloudCostSet),
  18. }
  19. }
  20. func (m *MemoryRepository) Has(startTime time.Time, billingIntegration string) (bool, error) {
  21. m.rwLock.RLock()
  22. defer m.rwLock.RUnlock()
  23. billingIntegrationData, ok := m.data[billingIntegration]
  24. if !ok {
  25. return false, nil
  26. }
  27. _, ook := billingIntegrationData[startTime.UTC()]
  28. return ook, nil
  29. }
  30. func (m *MemoryRepository) Get(startTime time.Time, billingIntegration string) (*opencost.CloudCostSet, error) {
  31. m.rwLock.RLock()
  32. defer m.rwLock.RUnlock()
  33. billingIntegrationData, ok := m.data[billingIntegration]
  34. if !ok {
  35. return nil, nil
  36. }
  37. ccs, ook := billingIntegrationData[startTime.UTC()]
  38. if !ook {
  39. return nil, nil
  40. }
  41. return ccs.Clone(), nil
  42. }
  43. func (m *MemoryRepository) Keys() ([]string, error) {
  44. m.rwLock.RLock()
  45. defer m.rwLock.RUnlock()
  46. keys := maps.Keys(m.data)
  47. return keys, nil
  48. }
  49. func (m *MemoryRepository) Put(ccs *opencost.CloudCostSet) error {
  50. m.rwLock.Lock()
  51. defer m.rwLock.Unlock()
  52. if ccs == nil {
  53. return fmt.Errorf("MemoryRepository: Put: cannot save nil")
  54. }
  55. if ccs.Window.IsOpen() {
  56. return fmt.Errorf("MemoryRepository: Put: cloud cost set has invalid window %s", ccs.Window.String())
  57. }
  58. if ccs.Integration == "" {
  59. return fmt.Errorf("MemoryRepository: Put: cloud cost set does not have an integration value")
  60. }
  61. if _, ok := m.data[ccs.Integration]; !ok {
  62. m.data[ccs.Integration] = make(map[time.Time]*opencost.CloudCostSet)
  63. }
  64. m.data[ccs.Integration][ccs.Window.Start().UTC()] = ccs
  65. return nil
  66. }
  67. // Expire deletes all items in the map with a start time before the given limit
  68. func (m *MemoryRepository) Expire(limit time.Time) error {
  69. m.rwLock.Lock()
  70. defer m.rwLock.Unlock()
  71. for key, integration := range m.data {
  72. for startTime := range integration {
  73. if startTime.Before(limit) {
  74. delete(integration, startTime)
  75. }
  76. }
  77. // remove integration if it is now empty
  78. if len(integration) == 0 {
  79. delete(m.data, key)
  80. }
  81. }
  82. return nil
  83. }