2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2015-09-06 22:42:11 +00:00
|
|
|
|
|
|
|
class StatsStore < ApplicationModel
|
2018-10-03 06:23:22 +00:00
|
|
|
include HasSearchIndexBackend
|
|
|
|
|
2020-10-06 13:13:50 +00:00
|
|
|
belongs_to :stats_storable, polymorphic: true
|
2015-09-06 22:42:11 +00:00
|
|
|
|
2020-10-06 13:13:50 +00:00
|
|
|
store :data
|
2015-09-06 22:42:11 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-10-06 16:59:40 +00:00
|
|
|
item = StatsStore.sync(
|
2020-10-06 13:13:50 +00:00
|
|
|
stats_storable: current_user,
|
|
|
|
key: 'dashboard',
|
|
|
|
data: {some data},
|
2015-09-06 22:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.sync(params)
|
|
|
|
|
|
|
|
data = params[:data]
|
|
|
|
params.delete(:data)
|
|
|
|
|
2020-10-06 13:13:50 +00:00
|
|
|
item = find_by(params)
|
2015-09-06 22:42:11 +00:00
|
|
|
|
|
|
|
if item
|
|
|
|
item.data = data
|
|
|
|
item.save
|
2015-09-08 11:42:03 +00:00
|
|
|
return item
|
2015-09-06 22:42:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
params[:data] = data
|
|
|
|
params[:created_by_id] = 1
|
|
|
|
create(params)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
cleanup old stats store
|
|
|
|
|
|
|
|
StatsStore.cleanup
|
|
|
|
|
2016-08-17 11:24:51 +00:00
|
|
|
optional you can put the max oldest stats store entries as argument
|
2015-09-06 22:42:11 +00:00
|
|
|
|
2018-10-03 06:23:22 +00:00
|
|
|
StatsStore.cleanup(12.months)
|
2015-09-06 22:42:11 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-10-03 06:23:22 +00:00
|
|
|
def self.cleanup(diff = 12.months)
|
2015-09-08 12:11:03 +00:00
|
|
|
StatsStore.where('updated_at < ?', Time.zone.now - diff).delete_all
|
2015-09-06 22:42:11 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|