From 20c7cf9502edc46077d755b92bef467e98d5be3d Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 18 Nov 2019 20:19:35 +0100 Subject: [PATCH] Performance: Don't check record associations if record is already present in assets structure. --- app/models/activity_stream/assets.rb | 18 ++-- app/models/channel/assets.rb | 36 ++++---- app/models/chat/session/assets.rb | 23 ++--- app/models/job/assets.rb | 11 +-- app/models/online_notification/assets.rb | 18 ++-- app/models/organization/assets.rb | 52 +++++------ app/models/overview/assets.rb | 22 ++--- app/models/recent_view/assets.rb | 16 ++-- app/models/role/assets.rb | 22 ++--- app/models/sla/assets.rb | 24 ++--- app/models/ticket/article/assets.rb | 15 ++-- app/models/ticket/assets.rb | 10 ++- app/models/trigger/assets.rb | 20 +++-- app/models/user/assets.rb | 108 +++++++++++------------ 14 files changed, 204 insertions(+), 191 deletions(-) diff --git a/app/models/activity_stream/assets.rb b/app/models/activity_stream/assets.rb index 1fd7a4fcd..ab99e386b 100644 --- a/app/models/activity_stream/assets.rb +++ b/app/models/activity_stream/assets.rb @@ -29,18 +29,18 @@ returns if !data[ app_model ] data[ app_model ] = {} end - if !data[ app_model ][ id ] - local_attributes = attributes_with_association_ids + return data if data[ app_model ][ id ] - local_attributes['object'] = ObjectLookup.by_id(local_attributes['activity_stream_object_id']) - local_attributes['type'] = TypeLookup.by_id(local_attributes['activity_stream_type_id']) + local_attributes = attributes_with_association_ids - # set temp. current attributes to assets pool to prevent - # loops, will be updated with lookup attributes later - data[ app_model ][ id ] = local_attributes + local_attributes['object'] = ObjectLookup.by_id(local_attributes['activity_stream_object_id']) + local_attributes['type'] = TypeLookup.by_id(local_attributes['activity_stream_type_id']) - ApplicationModel.assets_of_object_list([local_attributes], data) - end + # set temp. current attributes to assets pool to prevent + # loops, will be updated with lookup attributes later + data[ app_model ][ id ] = local_attributes + + ApplicationModel.assets_of_object_list([local_attributes], data) return data if !self['created_by_id'] diff --git a/app/models/channel/assets.rb b/app/models/channel/assets.rb index 316fa4c3e..4a210b5ac 100644 --- a/app/models/channel/assets.rb +++ b/app/models/channel/assets.rb @@ -29,27 +29,27 @@ returns if !data[ app_model ] data[ app_model ] = {} end - if !data[ app_model ][ id ] - attributes = attributes_with_association_ids + return data if data[ app_model ][ id ] - # remove passwords if use is no admin - access = false - if UserInfo.current_user_id - user = User.lookup(id: UserInfo.current_user_id) - if user.permissions?('admin.channel') - access = true - end - end - if !access - %w[inbound outbound].each do |key| - if attributes['options'] && attributes['options'][key] && attributes['options'][key]['options'] - attributes['options'][key]['options'].delete('password') - end - end - end + attributes = attributes_with_association_ids - data[ self.class.to_app_model ][ id ] = attributes + # remove passwords if use is no admin + access = false + if UserInfo.current_user_id + user = User.lookup(id: UserInfo.current_user_id) + if user.permissions?('admin.channel') + access = true + end end + if !access + %w[inbound outbound].each do |key| + if attributes['options'] && attributes['options'][key] && attributes['options'][key]['options'] + attributes['options'][key]['options'].delete('password') + end + end + end + + data[ self.class.to_app_model ][ id ] = attributes return data if !self['created_by_id'] && !self['updated_by_id'] diff --git a/app/models/chat/session/assets.rb b/app/models/chat/session/assets.rb index acb08409b..9001a086b 100644 --- a/app/models/chat/session/assets.rb +++ b/app/models/chat/session/assets.rb @@ -24,20 +24,20 @@ returns def assets(data) app_model_chat_session = Chat::Session.to_app_model - app_model_chat = Chat.to_app_model - app_model_user = User.to_app_model - data[ app_model_chat_session ] ||= {} - - if !data[ app_model_chat_session ][ id ] - data[ app_model_chat_session ][ id ] = attributes_with_association_ids - data[ app_model_chat_session ][ id ]['messages'] = [] - messages.each do |message| - data[ app_model_chat_session ][ id ]['messages'].push message.attributes - end - data[ app_model_chat_session ][ id ]['tags'] = tag_list + if !data[ app_model_chat_session ] + data[ app_model_chat_session ] = {} end + return data if data[ app_model_chat_session ][ id ] + data[ app_model_chat_session ][ id ] = attributes_with_association_ids + data[ app_model_chat_session ][ id ]['messages'] = [] + messages.each do |message| + data[ app_model_chat_session ][ id ]['messages'].push message.attributes + end + data[ app_model_chat_session ][ id ]['tags'] = tag_list + + app_model_chat = Chat.to_app_model if !data[ app_model_chat ] || !data[ app_model_chat ][ chat_id ] chat = Chat.lookup(id: chat_id) if chat @@ -45,6 +45,7 @@ returns end end + app_model_user = User.to_app_model %w[created_by_id updated_by_id].each do |local_user_id| next if !self[ local_user_id ] next if data[ app_model_user ] && data[ app_model_user ][ self[ local_user_id ] ] diff --git a/app/models/job/assets.rb b/app/models/job/assets.rb index 8d8ce0423..9852f8eca 100644 --- a/app/models/job/assets.rb +++ b/app/models/job/assets.rb @@ -29,14 +29,15 @@ returns if !data[ app_model ] data[ app_model ] = {} end + return data if data[ app_model ][ id ] + + data[ app_model ][ id ] = attributes_with_association_ids + data = assets_of_selector('condition', data) + data = assets_of_selector('perform', data) + if !data[ User.to_app_model ] data[ User.to_app_model ] = {} end - if !data[ app_model ][ id ] - data[ app_model ][ id ] = attributes_with_association_ids - data = assets_of_selector('condition', data) - data = assets_of_selector('perform', data) - end %w[created_by_id updated_by_id].each do |local_user_id| next if !self[ local_user_id ] next if data[ User.to_app_model ][ self[ local_user_id ] ] diff --git a/app/models/online_notification/assets.rb b/app/models/online_notification/assets.rb index 797bd04b8..f8d7f5ce8 100644 --- a/app/models/online_notification/assets.rb +++ b/app/models/online_notification/assets.rb @@ -29,18 +29,18 @@ returns if !data[ app_model ] data[ app_model ] = {} end - if !data[ app_model ][ id ] - local_attributes = attributes_with_association_ids + return data if data[ app_model ][ id ] - local_attributes['object'] = ObjectLookup.by_id(local_attributes['object_lookup_id']) - local_attributes['type'] = TypeLookup.by_id(local_attributes['type_lookup_id']) + local_attributes = attributes_with_association_ids - # set temp. current attributes to assets pool to prevent - # loops, will be updated with lookup attributes later - data[ app_model ][ id ] = local_attributes + local_attributes['object'] = ObjectLookup.by_id(local_attributes['object_lookup_id']) + local_attributes['type'] = TypeLookup.by_id(local_attributes['type_lookup_id']) - ApplicationModel.assets_of_object_list([local_attributes], data) - end + # set temp. current attributes to assets pool to prevent + # loops, will be updated with lookup attributes later + data[ app_model ][ id ] = local_attributes + + ApplicationModel.assets_of_object_list([local_attributes], data) return data if !self['created_by_id'] && !self['updated_by_id'] diff --git a/app/models/organization/assets.rb b/app/models/organization/assets.rb index d1bc00e4b..2804437a2 100644 --- a/app/models/organization/assets.rb +++ b/app/models/organization/assets.rb @@ -25,39 +25,41 @@ returns def assets(data) app_model_organization = Organization.to_app_model - app_model_user = User.to_app_model if !data[ app_model_organization ] data[ app_model_organization ] = {} end + return data if data[ app_model_organization ][ id ] + + local_attributes = attributes_with_association_ids + + # set temp. current attributes to assets pool to prevent + # loops, will be updated with lookup attributes later + data[ app_model_organization ][ id ] = local_attributes + + app_model_user = User.to_app_model + if local_attributes['member_ids'].present? + + # feature used for different purpose; do limit references + if local_attributes['member_ids'].count > 100 + local_attributes['member_ids'] = local_attributes['member_ids'].sort[0, 100] + end + local_attributes['member_ids'].each do |local_user_id| + next if data[ app_model_user ] && data[ app_model_user ][ local_user_id ] + + user = User.lookup(id: local_user_id) + next if !user + + data = user.assets(data) + end + end + + data[ app_model_organization ][ id ] = local_attributes + if !data[ app_model_user ] data[ app_model_user ] = {} end - if !data[ app_model_organization ][ id ] - local_attributes = attributes_with_association_ids - # set temp. current attributes to assets pool to prevent - # loops, will be updated with lookup attributes later - data[ app_model_organization ][ id ] = local_attributes - - if local_attributes['member_ids'].present? - - # feature used for different purpose; do limit references - if local_attributes['member_ids'].count > 100 - local_attributes['member_ids'] = local_attributes['member_ids'].sort[0, 100] - end - local_attributes['member_ids'].each do |local_user_id| - next if data[ app_model_user ] && data[ app_model_user ][ local_user_id ] - - user = User.lookup(id: local_user_id) - next if !user - - data = user.assets(data) - end - end - - data[ app_model_organization ][ id ] = local_attributes - end %w[created_by_id updated_by_id].each do |local_user_id| next if !self[ local_user_id ] next if data[ app_model_user ][ self[ local_user_id ] ] diff --git a/app/models/overview/assets.rb b/app/models/overview/assets.rb index ef8826cc2..d8447e125 100644 --- a/app/models/overview/assets.rb +++ b/app/models/overview/assets.rb @@ -25,26 +25,28 @@ returns def assets(data) app_model_overview = Overview.to_app_model - app_model_user = User.to_app_model if !data[ app_model_overview ] data[ app_model_overview ] = {} end + return data if data[ app_model_overview ][ id ] + + app_model_user = User.to_app_model if !data[ app_model_user ] data[ app_model_user ] = {} end - if !data[ app_model_overview ][ id ] - data[ app_model_overview ][ id ] = attributes_with_association_ids - user_ids&.each do |local_user_id| - next if data[ app_model_user ][ local_user_id ] - user = User.lookup(id: local_user_id) - next if !user + data[ app_model_overview ][ id ] = attributes_with_association_ids + user_ids&.each do |local_user_id| + next if data[ app_model_user ][ local_user_id ] - data = user.assets(data) - end - data = assets_of_selector('condition', data) + user = User.lookup(id: local_user_id) + next if !user + + data = user.assets(data) end + data = assets_of_selector('condition', data) + %w[created_by_id updated_by_id].each do |local_user_id| next if !self[ local_user_id ] next if data[ app_model_user ][ self[ local_user_id ] ] diff --git a/app/models/recent_view/assets.rb b/app/models/recent_view/assets.rb index 028485c23..74632a941 100644 --- a/app/models/recent_view/assets.rb +++ b/app/models/recent_view/assets.rb @@ -29,17 +29,17 @@ returns if !data[ app_model ] data[ app_model ] = {} end - if !data[ app_model ][ id ] - local_attributes = attributes_with_association_ids + return data if data[ app_model ][ id ] - local_attributes['object'] = ObjectLookup.by_id(local_attributes['recent_view_object_id']) + local_attributes = attributes_with_association_ids - # set temp. current attributes to assets pool to prevent - # loops, will be updated with lookup attributes later - data[ app_model ][ id ] = local_attributes + local_attributes['object'] = ObjectLookup.by_id(local_attributes['recent_view_object_id']) - ApplicationModel.assets_of_object_list([local_attributes], data) - end + # set temp. current attributes to assets pool to prevent + # loops, will be updated with lookup attributes later + data[ app_model ][ id ] = local_attributes + + ApplicationModel.assets_of_object_list([local_attributes], data) return data if !self['created_by_id'] diff --git a/app/models/role/assets.rb b/app/models/role/assets.rb index ad4ef97e4..13639de76 100644 --- a/app/models/role/assets.rb +++ b/app/models/role/assets.rb @@ -29,21 +29,21 @@ returns if !data[ app_model ] data[ app_model ] = {} end - if !data[ app_model ][ id ] - local_attributes = attributes_with_association_ids + return data if data[ app_model ][ id ] - # set temp. current attributes to assets pool to prevent - # loops, will be updated with lookup attributes later - data[ app_model ][ id ] = local_attributes + local_attributes = attributes_with_association_ids - local_attributes['group_ids'].each_key do |group_id| - next if data[:Group] && data[:Group][group_id] + # set temp. current attributes to assets pool to prevent + # loops, will be updated with lookup attributes later + data[ app_model ][ id ] = local_attributes - group = Group.lookup(id: group_id) - next if !group + local_attributes['group_ids'].each_key do |group_id| + next if data[:Group] && data[:Group][group_id] - data = group.assets(data) - end + group = Group.lookup(id: group_id) + next if !group + + data = group.assets(data) end return data if !self['created_by_id'] && !self['updated_by_id'] diff --git a/app/models/sla/assets.rb b/app/models/sla/assets.rb index 341451074..15dfec75c 100644 --- a/app/models/sla/assets.rb +++ b/app/models/sla/assets.rb @@ -25,24 +25,26 @@ returns def assets (data) app_model_sla = Sla.to_app_model - app_model_user = User.to_app_model if !data[ app_model_sla ] data[ app_model_sla ] = {} end + return data if data[ app_model_sla ][ id ] + + data[ app_model_sla ][ id ] = attributes_with_association_ids + data = assets_of_selector('condition', data) + if calendar_id + calendar = Calendar.lookup(id: calendar_id) + if calendar + data = calendar.assets(data) + end + end + + app_model_user = User.to_app_model if !data[ app_model_user ] data[ app_model_user ] = {} end - if !data[ app_model_sla ][ id ] - data[ app_model_sla ][ id ] = attributes_with_association_ids - data = assets_of_selector('condition', data) - if calendar_id - calendar = Calendar.lookup(id: calendar_id) - if calendar - data = calendar.assets(data) - end - end - end + %w[created_by_id updated_by_id].each do |local_user_id| next if !self[ local_user_id ] next if data[ app_model_user ][ self[ local_user_id ] ] diff --git a/app/models/ticket/article/assets.rb b/app/models/ticket/article/assets.rb index 9a6cc9dd1..dab042fa4 100644 --- a/app/models/ticket/article/assets.rb +++ b/app/models/ticket/article/assets.rb @@ -24,8 +24,14 @@ returns def assets(data) - app_model_ticket = Ticket.to_app_model app_model_article = Ticket::Article.to_app_model + + if !data[ app_model_article ] + data[ app_model_article ] = {} + end + return data if data[ app_model_article ][ id ] + + app_model_ticket = Ticket.to_app_model app_model_user = User.to_app_model if !data[ app_model_ticket ] @@ -38,12 +44,7 @@ returns end end - if !data[ app_model_article ] - data[ app_model_article ] = {} - end - if !data[ app_model_article ][ id ] - data[ app_model_article ][ id ] = attributes_with_association_ids - end + data[ app_model_article ][ id ] = attributes_with_association_ids %w[created_by_id updated_by_id origin_by_id].each do |local_user_id| next if !self[ local_user_id ] diff --git a/app/models/ticket/assets.rb b/app/models/ticket/assets.rb index 618f9c976..6c8a90945 100644 --- a/app/models/ticket/assets.rb +++ b/app/models/ticket/assets.rb @@ -24,14 +24,16 @@ returns def assets(data) app_model_ticket = Ticket.to_app_model - app_model_user = User.to_app_model if !data[ app_model_ticket ] data[ app_model_ticket ] = {} end - if !data[ app_model_ticket ][ id ] - data[ app_model_ticket ][ id ] = attributes_with_association_ids - end + return data if data[ app_model_ticket ][ id ] + + data[ app_model_ticket ][ id ] = attributes_with_association_ids + + app_model_user = User.to_app_model + %w[created_by_id updated_by_id owner_id customer_id].each do |local_user_id| next if !self[ local_user_id ] next if data[ app_model_user ] && data[ app_model_user ][ self[ local_user_id ] ] diff --git a/app/models/trigger/assets.rb b/app/models/trigger/assets.rb index 695cbfc14..389f9aebc 100644 --- a/app/models/trigger/assets.rb +++ b/app/models/trigger/assets.rb @@ -24,20 +24,22 @@ returns def assets(data) - app_model_overview = Trigger.to_app_model - app_model_user = User.to_app_model + app_model_trigger = Trigger.to_app_model - if !data[ app_model_overview ] - data[ app_model_overview ] = {} + if !data[ app_model_trigger ] + data[ app_model_trigger ] = {} end + return data if data[ app_model_trigger ][ id ] + + data[ app_model_trigger ][ id ] = attributes_with_association_ids + data = assets_of_selector('condition', data) + data = assets_of_selector('perform', data) + + app_model_user = User.to_app_model if !data[ app_model_user ] data[ app_model_user ] = {} end - if !data[ app_model_overview ][ id ] - data[ app_model_overview ][ id ] = attributes_with_association_ids - data = assets_of_selector('condition', data) - data = assets_of_selector('perform', data) - end + %w[created_by_id updated_by_id].each do |local_user_id| next if !self[ local_user_id ] next if data[ app_model_user ][ self[ local_user_id ] ] diff --git a/app/models/user/assets.rb b/app/models/user/assets.rb index 6747641c1..8c456b3d6 100644 --- a/app/models/user/assets.rb +++ b/app/models/user/assets.rb @@ -30,65 +30,65 @@ returns if !data[ app_model ] data[ app_model ] = {} end - if !data[ app_model ][ id ] - local_attributes = attributes_with_association_ids + return data if data[ app_model ][ id ] - # do not transfer crypted pw - local_attributes.delete('password') + local_attributes = attributes_with_association_ids - # set temp. current attributes to assets pool to prevent - # loops, will be updated with lookup attributes later - data[ app_model ][ id ] = local_attributes + # do not transfer crypted pw + local_attributes.delete('password') - # get linked accounts - local_attributes['accounts'] = {} - key = "User::authorizations::#{id}" - local_accounts = Cache.get(key) - if !local_accounts - local_accounts = {} - authorizations = self.authorizations() - authorizations.each do |authorization| - local_accounts[authorization.provider] = { - uid: authorization[:uid], - username: authorization[:username] - } - end - Cache.write(key, local_accounts) + # set temp. current attributes to assets pool to prevent + # loops, will be updated with lookup attributes later + data[ app_model ][ id ] = local_attributes + + # get linked accounts + local_attributes['accounts'] = {} + key = "User::authorizations::#{id}" + local_accounts = Cache.get(key) + if !local_accounts + local_accounts = {} + authorizations = self.authorizations() + authorizations.each do |authorization| + local_accounts[authorization.provider] = { + uid: authorization[:uid], + username: authorization[:username] + } end - local_attributes['accounts'] = local_accounts - - # get roles - local_attributes['role_ids']&.each do |role_id| - next if data[:Role] && data[:Role][role_id] - - role = Role.lookup(id: role_id) - next if !role - - data = role.assets(data) - end - - # get groups - local_attributes['group_ids']&.each do |group_id, _access| - next if data[:Group] && data[:Group][group_id] - - group = Group.lookup(id: group_id) - next if !group - - data = group.assets(data) - end - - # get organizations - local_attributes['organization_ids']&.each do |organization_id| - next if data[:Organization] && data[:Organization][organization_id] - - organization = Organization.lookup(id: organization_id) - next if !organization - - data = organization.assets(data) - end - - data[ app_model ][ id ] = local_attributes + Cache.write(key, local_accounts) end + local_attributes['accounts'] = local_accounts + + # get roles + local_attributes['role_ids']&.each do |role_id| + next if data[:Role] && data[:Role][role_id] + + role = Role.lookup(id: role_id) + next if !role + + data = role.assets(data) + end + + # get groups + local_attributes['group_ids']&.each do |group_id, _access| + next if data[:Group] && data[:Group][group_id] + + group = Group.lookup(id: group_id) + next if !group + + data = group.assets(data) + end + + # get organizations + local_attributes['organization_ids']&.each do |organization_id| + next if data[:Organization] && data[:Organization][organization_id] + + organization = Organization.lookup(id: organization_id) + next if !organization + + data = organization.assets(data) + end + + data[ app_model ][ id ] = local_attributes # add organization if self.organization_id