2016-03-03 08:56:13 +00:00
|
|
|
class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
|
2016-03-03 01:51:24 +00:00
|
|
|
|
|
|
|
def self.reset(user_id)
|
|
|
|
key = "TicketOverviewPull::#{user_id}"
|
|
|
|
Cache.write(key, { needed: true })
|
|
|
|
end
|
|
|
|
|
2016-03-03 08:56:13 +00:00
|
|
|
def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 8)
|
2016-03-03 01:51:24 +00:00
|
|
|
@user = user
|
|
|
|
@client = client
|
|
|
|
@client_id = client_id
|
|
|
|
@ttl = ttl
|
2016-03-03 08:56:13 +00:00
|
|
|
@asset_lookup = asset_lookup
|
2016-03-03 01:51:24 +00:00
|
|
|
@last_change = nil
|
|
|
|
@last_overview = {}
|
|
|
|
@last_overview_change = nil
|
|
|
|
@last_ticket_change = nil
|
2014-07-13 18:52:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# get whole collection
|
2016-03-03 01:51:24 +00:00
|
|
|
index_and_lists = Ticket::Overviews.index(@user)
|
2015-05-10 19:54:57 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# no data exists
|
2017-10-16 13:45:04 +00:00
|
|
|
return if index_and_lists.blank?
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# no change exists
|
2016-03-03 01:51:24 +00:00
|
|
|
return if @last_change == index_and_lists
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# remember last state
|
2016-03-03 01:51:24 +00:00
|
|
|
@last_change = index_and_lists
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
index_and_lists
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
def client_key
|
2015-07-03 15:18:01 +00:00
|
|
|
"as::load::#{self.class}::#{@user.id}::#{@client_id}"
|
2014-07-13 18:52:32 +00:00
|
|
|
end
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
def work_needed?
|
|
|
|
key = "TicketOverviewPull::#{@user.id}"
|
|
|
|
if Cache.get(key)
|
|
|
|
Cache.delete(key)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false if Sessions::CacheIn.get(client_key)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
def push
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
return if !work_needed?
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2015-02-25 20:52:14 +00:00
|
|
|
# reset check interval
|
2016-03-01 14:26:46 +00:00
|
|
|
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
# check if min one ticket or overview has changed
|
|
|
|
last_overview_change = Overview.latest_change
|
2015-02-25 20:52:14 +00:00
|
|
|
last_ticket_change = Ticket.latest_change
|
2016-03-03 01:51:24 +00:00
|
|
|
return if last_ticket_change == @last_ticket_change && last_overview_change == @last_overview_change
|
|
|
|
@last_overview_change = last_overview_change
|
2015-02-25 20:52:14 +00:00
|
|
|
@last_ticket_change = last_ticket_change
|
|
|
|
|
|
|
|
# load current data
|
2016-03-03 01:51:24 +00:00
|
|
|
index_and_lists = load
|
|
|
|
return if !index_and_lists
|
|
|
|
|
|
|
|
# push overview index
|
|
|
|
indexes = []
|
2017-10-01 12:25:52 +00:00
|
|
|
index_and_lists.each do |index|
|
2016-03-03 01:51:24 +00:00
|
|
|
assets = {}
|
|
|
|
overview = Overview.lookup(id: index[:overview][:id])
|
|
|
|
meta = {
|
|
|
|
name: overview.name,
|
|
|
|
prio: overview.prio,
|
|
|
|
link: overview.link,
|
|
|
|
count: index[:count],
|
|
|
|
}
|
|
|
|
indexes.push meta
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-03-03 01:51:24 +00:00
|
|
|
if @client
|
|
|
|
@client.log "push overview_index for user #{@user.id}"
|
|
|
|
@client.send(
|
|
|
|
event: 'ticket_overview_index',
|
|
|
|
data: indexes,
|
|
|
|
)
|
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# push overviews
|
2014-07-14 05:26:22 +00:00
|
|
|
results = []
|
2017-10-01 12:25:52 +00:00
|
|
|
index_and_lists.each do |data|
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
# do not deliver unchanged lists
|
2016-09-27 22:05:46 +00:00
|
|
|
next if @last_overview[data[:overview][:id]] == data
|
|
|
|
@last_overview[data[:overview][:id]] = data
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
assets = {}
|
2016-09-27 22:05:46 +00:00
|
|
|
overview = Overview.lookup(id: data[:overview][:id])
|
2016-03-03 08:56:13 +00:00
|
|
|
if asset_needed?(overview)
|
2017-10-18 00:09:45 +00:00
|
|
|
assets = asset_push(overview, assets)
|
2016-03-03 08:56:13 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
data[:tickets].each do |ticket_meta|
|
2017-10-18 00:09:45 +00:00
|
|
|
next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
|
2016-03-03 01:51:24 +00:00
|
|
|
ticket = Ticket.lookup(id: ticket_meta[:id])
|
2017-10-18 00:09:45 +00:00
|
|
|
assets = asset_push(ticket, assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-09-27 22:05:46 +00:00
|
|
|
data[:assets] = assets
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
if !@client
|
2014-07-14 05:26:22 +00:00
|
|
|
result = {
|
2016-03-03 01:51:24 +00:00
|
|
|
event: 'ticket_overview_list',
|
2016-09-27 22:05:46 +00:00
|
|
|
data: data,
|
2013-09-21 22:40:19 +00:00
|
|
|
}
|
2014-07-14 05:26:22 +00:00
|
|
|
results.push result
|
|
|
|
else
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
@client.log "push overview_list #{overview.link} for user #{@user.id}"
|
2014-07-14 05:26:22 +00:00
|
|
|
|
|
|
|
# send update to browser
|
2015-04-27 15:21:17 +00:00
|
|
|
@client.send(
|
2016-03-03 01:51:24 +00:00
|
|
|
event: 'ticket_overview_list',
|
2016-09-27 22:05:46 +00:00
|
|
|
data: data,
|
2015-04-27 15:21:17 +00:00
|
|
|
)
|
2014-07-14 05:26:22 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-07-14 05:26:22 +00:00
|
|
|
return results if !@client
|
|
|
|
nil
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|