stats_store.rb 893 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class StatsStore < ApplicationModel
  3. include HasSearchIndexBackend
  4. belongs_to :stats_storable, polymorphic: true
  5. store :data
  6. =begin
  7. item = StatsStore.sync(
  8. stats_storable: current_user,
  9. key: 'dashboard',
  10. data: {some data},
  11. )
  12. =end
  13. def self.sync(params)
  14. data = params[:data]
  15. params.delete(:data)
  16. item = find_by(params)
  17. if item
  18. item.data = data
  19. item.save
  20. return item
  21. end
  22. params[:data] = data
  23. params[:created_by_id] = 1
  24. create(params)
  25. end
  26. =begin
  27. cleanup old stats store
  28. StatsStore.cleanup
  29. optional you can put the max oldest stats store entries as argument
  30. StatsStore.cleanup(12.months)
  31. =end
  32. def self.cleanup(diff = 12.months)
  33. where(updated_at: ...diff.ago)
  34. .delete_all
  35. true
  36. end
  37. end