Improved memory usage by using pluck.

This commit is contained in:
Martin Edenhofer 2016-08-31 09:10:25 +02:00
parent fa4e953423
commit c8230207ef
3 changed files with 18 additions and 17 deletions

View file

@ -19,11 +19,11 @@ returns
# get customer overviews # get customer overviews
if data[:current_user].role?('Customer') if data[:current_user].role?('Customer')
role = Role.find_by(name: 'Customer') role_id = Role.lookup(name: 'Customer').id
overviews = if data[:current_user].organization_id && data[:current_user].organization.shared overviews = if data[:current_user].organization_id && data[:current_user].organization.shared
Overview.where(role_id: role.id, active: true).order(:prio) Overview.where(role_id: role_id, active: true).order(:prio)
else else
Overview.where(role_id: role.id, organization_shared: false, active: true).order(:prio) Overview.where(role_id: role_id, organization_shared: false, active: true).order(:prio)
end end
overviews_list = [] overviews_list = []
overviews.each { |overview| overviews.each { |overview|
@ -36,8 +36,8 @@ returns
# get agent overviews # get agent overviews
return if !data[:current_user].role?('Agent') return if !data[:current_user].role?('Agent')
role = Role.find_by(name: 'Agent') role_id = Role.lookup(name: 'Agent').id
overviews = Overview.where(role_id: role.id, active: true).order(:prio) overviews = Overview.where(role_id: role_id, active: true).order(:prio)
overviews_list = [] overviews_list = []
overviews.each { |overview| overviews.each { |overview|
user_ids = overview.user_ids user_ids = overview.user_ids
@ -104,12 +104,13 @@ returns
.where(query_condition, *bind_condition) .where(query_condition, *bind_condition)
.order(order_by) .order(order_by)
.limit(500) .limit(500)
.pluck(:id, :updated_at)
tickets = [] tickets = []
ticket_result.each { |ticket| ticket_result.each { |ticket|
ticket_item = { ticket_item = {
id: ticket.id, id: ticket[0],
updated_at: ticket.updated_at, updated_at: ticket[1],
} }
tickets.push ticket_item tickets.push ticket_item
} }

View file

@ -333,9 +333,9 @@ returns
def permissions def permissions
list = {} list = {}
Object.const_get('Permission').joins(:roles).where('roles.id IN (?)', role_ids).each { |permission| Object.const_get('Permission').select('permissions.name, permissions.preferences').joins(:roles).where('roles.id IN (?)', role_ids).pluck(:name, :preferences).each { |permission|
next if permission.preferences[:selectable] == false next if permission[1]['selectable'] == false
list[permission.name] = true list[permission[0]] = true
} }
list list
end end
@ -374,13 +374,13 @@ returns
if local_key =~ /\.\*$/ if local_key =~ /\.\*$/
local_key.sub!('.*', '.%') local_key.sub!('.*', '.%')
permissions = Object.const_get('Permission').with_parents(local_key) permissions = Object.const_get('Permission').with_parents(local_key)
list = Object.const_get('Permission').joins(:roles).where('roles.id IN (?) AND (permissions.name IN (?) OR permissions.name LIKE ?)', role_ids, permissions, local_key) list = Object.const_get('Permission').select('preferences').joins(:roles).where('roles.id IN (?) AND (permissions.name IN (?) OR permissions.name LIKE ?)', role_ids, permissions, local_key).pluck(:preferences)
else else
permissions = Object.const_get('Permission').with_parents(local_key) permissions = Object.const_get('Permission').with_parents(local_key)
list = Object.const_get('Permission').joins(:roles).where('roles.id IN (?) AND permissions.name IN (?)', role_ids, permissions) list = Object.const_get('Permission').select('preferences').joins(:roles).where('roles.id IN (?) AND permissions.name IN (?)', role_ids, permissions).pluck(:preferences)
end end
list.each { |permission| list.each { |preferences|
next if permission.preferences[:selectable] == false next if preferences[:selectable] == false
Cache.write(key, true, expires_in: 20.seconds) Cache.write(key, true, expires_in: 20.seconds)
return true return true
} }
@ -419,8 +419,8 @@ returns
permission_ids.push permission.id permission_ids.push permission.id
} }
next if permission_ids.empty? next if permission_ids.empty?
Role.select('id').joins(:roles_permissions).where('permissions_roles.permission_id IN (?) AND roles.active = ?', permission_ids, true).uniq().each { |role| Role.joins(:roles_permissions).where('permissions_roles.permission_id IN (?) AND roles.active = ?', permission_ids, true).uniq().pluck(:id).each { |role_id|
role_ids.push role.id role_ids.push role_id
} }
total_role_ids.push role_ids total_role_ids.push role_ids
} }