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
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
Overview.where(role_id: role.id, active: true).order(:prio)
Overview.where(role_id: role_id, active: true).order(:prio)
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
overviews_list = []
overviews.each { |overview|
@ -36,8 +36,8 @@ returns
# get agent overviews
return if !data[:current_user].role?('Agent')
role = Role.find_by(name: 'Agent')
overviews = Overview.where(role_id: role.id, active: true).order(:prio)
role_id = Role.lookup(name: 'Agent').id
overviews = Overview.where(role_id: role_id, active: true).order(:prio)
overviews_list = []
overviews.each { |overview|
user_ids = overview.user_ids
@ -104,12 +104,13 @@ returns
.where(query_condition, *bind_condition)
.order(order_by)
.limit(500)
.pluck(:id, :updated_at)
tickets = []
ticket_result.each { |ticket|
ticket_item = {
id: ticket.id,
updated_at: ticket.updated_at,
id: ticket[0],
updated_at: ticket[1],
}
tickets.push ticket_item
}

View file

@ -333,9 +333,9 @@ returns
def permissions
list = {}
Object.const_get('Permission').joins(:roles).where('roles.id IN (?)', role_ids).each { |permission|
next if permission.preferences[:selectable] == false
list[permission.name] = true
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[1]['selectable'] == false
list[permission[0]] = true
}
list
end
@ -374,13 +374,13 @@ returns
if local_key =~ /\.\*$/
local_key.sub!('.*', '.%')
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
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
list.each { |permission|
next if permission.preferences[:selectable] == false
list.each { |preferences|
next if preferences[:selectable] == false
Cache.write(key, true, expires_in: 20.seconds)
return true
}
@ -419,8 +419,8 @@ returns
permission_ids.push permission.id
}
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_ids.push role.id
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
}
total_role_ids.push role_ids
}