Improved performance of ticket overview backend job.

This commit is contained in:
Martin Edenhofer 2017-10-18 02:09:45 +02:00
parent 7bccc25d75
commit 80ab16a2d3
7 changed files with 142 additions and 34 deletions

View file

@ -44,7 +44,7 @@ class Sessions::Backend::ActivityStream
data = load data = load
return if !data || data.empty? return if data.blank?
if !@client if !@client
return { return {

View file

@ -9,25 +9,28 @@ class Sessions::Backend::Base
@last_change = nil @last_change = nil
end end
def asset_needed?(record) def asset_push(record, assets)
class_name = record.class.to_s class_name = record.class.to_s
if !@asset_lookup || !@asset_lookup[class_name] || !@asset_lookup[class_name][record.id] @asset_lookup[class_name] ||= {}
@asset_lookup[class_name] ||= {} @asset_lookup[class_name][record.id] = {
@asset_lookup[class_name][record.id] = { updated_at: record.updated_at,
updated_at: record.updated_at, pushed_at: Time.zone.now,
pushed_at: Time.zone.now, }
} record.assets(assets)
return true end
end
if (!@asset_lookup[class_name][record.id][:updated_at] || @asset_lookup[class_name][record.id][:updated_at] < record.updated_at) || def asset_needed?(record)
(!@asset_lookup[class_name][record.id][:pushed_at] || @asset_lookup[class_name][record.id][:pushed_at] > Time.zone.now - 45.seconds) return false if !asset_needed_by_updated_at?(record.class.to_s, record.id, record.updated_at)
@asset_lookup[class_name][record.id] = { true
updated_at: record.updated_at, end
pushed_at: Time.zone.now,
} def asset_needed_by_updated_at?(class_name, record_id, updated_at)
return true return true if @asset_lookup.blank?
end return true if @asset_lookup[class_name].blank?
return true if @asset_lookup[class_name][record_id].blank?
return true if @asset_lookup[class_name][record_id][:updated_at] < updated_at
return true if @asset_lookup[class_name][record_id][:pushed_at].blank?
return true if @asset_lookup[class_name][record_id][:pushed_at] < Time.zone.now - 1.hour
false false
end end

View file

@ -54,7 +54,7 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base
assets = {} assets = {}
items.each do |item| items.each do |item|
next if !asset_needed?(item) next if !asset_needed?(item)
assets = item.assets(assets) assets = asset_push(item, assets)
end end
if !@client if !@client
return { return {

View file

@ -98,12 +98,12 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
assets = {} assets = {}
overview = Overview.lookup(id: data[:overview][:id]) overview = Overview.lookup(id: data[:overview][:id])
if asset_needed?(overview) if asset_needed?(overview)
assets = overview.assets(assets) assets = asset_push(overview, assets)
end end
data[:tickets].each do |ticket_meta| data[:tickets].each do |ticket_meta|
next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
ticket = Ticket.lookup(id: ticket_meta[:id]) ticket = Ticket.lookup(id: ticket_meta[:id])
next if !asset_needed?(ticket) assets = asset_push(ticket, assets)
assets = ticket.assets(assets)
end end
data[:assets] = assets data[:assets] = assets

View file

@ -419,7 +419,7 @@ class AssetsTest < ActiveSupport::TestCase
assert_not(assets[:User][user5.id], 'check assets') assert_not(assets[:User][user5.id], 'check assets')
assert(assets[:TicketState][ticket_state1.id], 'check assets') assert(assets[:TicketState][ticket_state1.id], 'check assets')
assert_not(assets[:TicketState][ticket_state2.id], 'check assets') assert_not(assets[:TicketState][ticket_state2.id], 'check assets')
overview.destroy!
end end
test 'sla' do test 'sla' do

View file

@ -3,12 +3,12 @@ require 'test_helper'
class SessionBasicTicketTest < ActiveSupport::TestCase class SessionBasicTicketTest < ActiveSupport::TestCase
test 'b ticket_overview_List' do setup do
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
roles = Role.where(name: ['Agent']) roles = Role.where(name: ['Agent'])
groups = Group.all groups = Group.all
agent1 = User.create_or_update( @agent1 = User.create_or_update(
login: 'session-basic-ticket-agent-1', login: 'session-basic-ticket-agent-1',
firstname: 'Session', firstname: 'Session',
lastname: 'session basic ' + rand(99_999).to_s, lastname: 'session basic ' + rand(99_999).to_s,
@ -18,15 +18,90 @@ class SessionBasicTicketTest < ActiveSupport::TestCase
roles: roles, roles: roles,
groups: groups, groups: groups,
) )
assert(agent1.save!, 'create/update agent1')
Ticket.create(title: 'default overview test', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1) @agent2 = User.create_or_update(
login: 'session-basic-ticket-agent-2',
firstname: 'Session',
lastname: 'session basic ' + rand(99_999).to_s,
email: 'session-basic-ticket-agent-2@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
)
Overview.destroy_all
load "#{Rails.root}/db/seeds/overviews.rb"
end
user = User.lookup(id: agent1.id) test 'asset needed' do
client1 = Sessions::Backend::TicketOverviewList.new(user, {}, false, '123-1', 2)
client1 = Sessions::Backend::TicketOverviewList.new(@agent1, {}, false, '123-1', 2)
client2 = Sessions::Backend::TicketOverviewList.new(@agent2, {}, false, '123-2', 2)
ticket = Ticket.create!(title: 'default overview test', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
assert_not(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
assert_not(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
travel 30.minutes
assert_not(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
assert_not(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
travel 60.minutes
assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
assert_not(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
assert_not(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
ticket.touch
assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
assert_not(client1.asset_needed?(ticket))
assert_not(client2.asset_needed?(ticket))
travel 65.minutes
assert(client1.asset_needed?(ticket))
client1.asset_push(ticket, {})
assert(client2.asset_needed?(ticket))
client2.asset_push(ticket, {})
assert_not(client1.asset_needed?(ticket))
assert_not(client2.asset_needed?(ticket))
travel_back
end
test 'ticket_overview_List' do
ticket1 = Ticket.create!(title: 'default overview test 1', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
ticket2 = Ticket.create!(title: 'default overview test 2', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
client1 = Sessions::Backend::TicketOverviewList.new(@agent1, {}, false, '123-1', 2)
result1 = client1.push result1 = client1.push
assert(result1, 'check ticket_overview_List') assert(result1, 'check ticket_overview_List')
assert(result1[1][:data][:assets])
assert(result1[1][:data][:assets][:Overview])
assert(result1[1][:data][:assets][:User])
assert_equal(result1[1][:data][:assets][:Ticket][ticket1.id]['title'], ticket1.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket2.id]['title'], ticket2.title)
# next check should be empty / no changes # next check should be empty / no changes
result1 = client1.push result1 = client1.push
@ -38,33 +113,55 @@ class SessionBasicTicketTest < ActiveSupport::TestCase
assert(!result1, 'check ticket_overview_index - recall 2') assert(!result1, 'check ticket_overview_index - recall 2')
# create ticket # create ticket
ticket = Ticket.create(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1) ticket3 = Ticket.create!(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
travel 3.seconds travel 3.seconds
result1 = client1.push result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 3') assert(result1, 'check ticket_overview_index - recall 3')
assert(result1[1][:data][:assets])
assert_not(result1[1][:data][:assets][:Ticket][ticket1.id])
assert_not(result1[1][:data][:assets][:Ticket][ticket2.id])
assert_equal(result1[1][:data][:assets][:Ticket][ticket3.id]['title'], ticket3.title)
travel 3.seconds travel 3.seconds
# chnage overview # chnage overview
overviews = Ticket::Overviews.all( overviews = Ticket::Overviews.all(
current_user: user, current_user: @agent1,
) )
overviews.first.touch overviews.first.touch
result1 = client1.push result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 4') assert(result1, 'check ticket_overview_index - recall 4')
assert(result1[1][:data][:assets])
assert_not(result1[1][:data][:assets][:Ticket])
result1 = client1.push result1 = client1.push
assert(!result1, 'check ticket_overview_index - recall 5') assert(!result1, 'check ticket_overview_index - recall 5')
Sessions::Backend::TicketOverviewList.reset(user.id) Sessions::Backend::TicketOverviewList.reset(@agent1.id)
result1 = client1.push result1 = client1.push
assert(!result1, 'check ticket_overview_index - recall 6') assert(!result1, 'check ticket_overview_index - recall 6')
ticket = Ticket.create(title: '12323 - 2', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1) ticket4 = Ticket.create!(title: '12323 - 2', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
Sessions::Backend::TicketOverviewList.reset(user.id) Sessions::Backend::TicketOverviewList.reset(@agent1.id)
result1 = client1.push result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 7') assert(result1, 'check ticket_overview_index - recall 7')
assert(result1[1][:data][:assets])
assert_not(result1[1][:data][:assets][:Ticket][ticket1.id])
assert_not(result1[1][:data][:assets][:Ticket][ticket2.id])
assert_not(result1[1][:data][:assets][:Ticket][ticket3.id])
assert_equal(result1[1][:data][:assets][:Ticket][ticket4.id]['title'], ticket4.title)
travel 65.minutes
ticket1.touch
result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 8')
assert(result1[1][:data][:assets])
assert_equal(result1[1][:data][:assets][:Ticket][ticket1.id]['title'], ticket1.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket2.id]['title'], ticket2.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket3.id]['title'], ticket3.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket4.id]['title'], ticket4.title)
travel 10.seconds travel 10.seconds
Sessions.destroy_idle_sessions(3) Sessions.destroy_idle_sessions(3)

View file

@ -194,6 +194,14 @@ class SessionCollectionsTest < ActiveSupport::TestCase
assert(data[:assets][:Group][groups.first.id]) assert(data[:assets][:Group][groups.first.id])
travel 10.seconds travel 10.seconds
client1 = Sessions::Backend::Collections::Group.new(agent1, assets, false, '123-1', 4)
data = client1.push
assert_equal(data[:collection][:Group][0]['id'], groups[0].id)
assert(data[:assets])
assert_not(data[:assets][:Group])
travel 65.minutes
client1 = Sessions::Backend::Collections::Group.new(agent1, assets, false, '123-1', 4) client1 = Sessions::Backend::Collections::Group.new(agent1, assets, false, '123-1', 4)
data = client1.push data = client1.push
assert_equal(data[:collection][:Group][0]['id'], groups[0].id) assert_equal(data[:collection][:Group][0]['id'], groups[0].id)