2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-09-06 22:42:11 +00:00
|
|
|
|
|
|
|
class StatsStore < ApplicationModel
|
2018-10-03 06:23:22 +00:00
|
|
|
include HasSearchIndexBackend
|
|
|
|
include StatsStore::SearchIndex
|
2020-06-08 07:17:29 +00:00
|
|
|
belongs_to :stats_store_object, class_name: 'ObjectLookup', optional: true
|
|
|
|
belongs_to :related_stats_store_object, class_name: 'ObjectLookup', optional: true
|
2018-10-03 06:23:22 +00:00
|
|
|
|
2015-09-06 22:42:11 +00:00
|
|
|
store :data
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
count = StatsStore.count_by_search(
|
|
|
|
object: 'User',
|
|
|
|
o_id: ticket.owner_id,
|
|
|
|
key: 'ticket:reopen',
|
|
|
|
start: Time.zone.now - 7.days,
|
|
|
|
end: Time.zone.now,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.count_by_search(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2018-10-03 06:23:22 +00:00
|
|
|
object_id = ObjectLookup.by_name(data[:object])
|
2015-09-06 22:42:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
StatsStore.where(stats_store_object_id: object_id, o_id: data[:o_id], key: data[:key])
|
2016-01-15 17:22:57 +00:00
|
|
|
.where('created_at > ? AND created_at < ?', data[:start], data[:end]).count
|
2015-09-06 22:42:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
item = StatsStore.search(
|
|
|
|
object: 'User',
|
|
|
|
o_id: current_user.id,
|
|
|
|
key: 'dashboard',
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.search(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2018-10-03 06:23:22 +00:00
|
|
|
data[:stats_store_object_id] = ObjectLookup.by_name(data[:object])
|
2015-09-06 22:42:11 +00:00
|
|
|
data.delete(:object)
|
|
|
|
end
|
|
|
|
|
|
|
|
find_by(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-10-06 16:59:40 +00:00
|
|
|
item = StatsStore.sync(
|
2015-09-06 22:42:11 +00:00
|
|
|
object: 'User',
|
|
|
|
o_id: current_user.id,
|
|
|
|
key: 'dashboard',
|
|
|
|
data: {some data},
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.sync(params)
|
|
|
|
|
|
|
|
data = params[:data]
|
|
|
|
params.delete(:data)
|
|
|
|
|
|
|
|
item = search(params)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2018-10-03 06:23:22 +00:00
|
|
|
data[:stats_store_object_id] = ObjectLookup.by_name(data[:object])
|
2015-09-06 22:42:11 +00:00
|
|
|
data.delete(:object)
|
|
|
|
end
|
|
|
|
|
|
|
|
params[:data] = data
|
|
|
|
params[:created_by_id] = 1
|
|
|
|
create(params)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
StatsStore.add(
|
|
|
|
object: 'User',
|
|
|
|
o_id: ticket.owner_id,
|
|
|
|
key: 'ticket:reopen',
|
|
|
|
data: { ticket_id: ticket.id },
|
|
|
|
created_at: Time.zone.now,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.add(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2018-10-03 06:23:22 +00:00
|
|
|
object_id = ObjectLookup.by_name(data[:object])
|
2015-09-06 22:42:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
|
|
|
record = {
|
|
|
|
stats_store_object_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:o_id],
|
|
|
|
key: data[:key],
|
|
|
|
data: data[:data],
|
|
|
|
created_at: data[:created_at],
|
|
|
|
created_by_id: data[:created_by_id],
|
2015-09-06 22:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StatsStore.create(record)
|
|
|
|
end
|
|
|
|
|
2018-10-03 06:23:22 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
StatsStore.remove(
|
|
|
|
object: 'User',
|
|
|
|
o_id: ticket.owner_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
|
|
|
object_id = ObjectLookup.by_name(data[:object])
|
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
|
|
|
record = {
|
|
|
|
stats_store_object_id: object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:o_id],
|
2018-10-03 06:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StatsStore.where(record).destroy_all
|
|
|
|
end
|
|
|
|
|
2015-09-06 22:42:11 +00:00
|
|
|
=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
|