Skip to content

Commit

Permalink
Performance: User preference store YAML parsing for notification sett…
Browse files Browse the repository at this point in the history
…ings is slow.
  • Loading branch information
rolfschmidt authored and thorsteneckel committed Jan 17, 2020
1 parent b468134 commit f9a26d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/notification_factory/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ def self.notification_settings(user, ticket, type)
type = map[type]
end

return if !user.preferences
return if !user.preferences['notification_config']
# this cache will optimize the preference catch performance
# because of the yaml deserialization its pretty slow
# on many tickets you we cache it.
user_preferences = Cache.get("NotificationFactory::Mailer.notification_settings::#{user.id}")
if user_preferences.blank?
user_preferences = user.preferences

Cache.write("NotificationFactory::Mailer.notification_settings::#{user.id}", user_preferences, expires_in: 20.seconds)
end

return if !user_preferences
return if !user_preferences['notification_config']

matrix = user.preferences['notification_config']['matrix']
matrix = user_preferences['notification_config']['matrix']
return if !matrix

owned_by_nobody = false
Expand All @@ -60,7 +70,7 @@ def self.notification_settings(user, ticket, type)

# check if group is in selected groups
if !owned_by_me
selected_group_ids = user.preferences['notification_config']['group_ids']
selected_group_ids = user_preferences['notification_config']['group_ids']
if selected_group_ids.is_a?(Array)
hit = nil
if selected_group_ids.blank?
Expand Down
8 changes: 8 additions & 0 deletions test/unit/notification_factory_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase

agent1.preferences[:notification_config][:group_ids] = nil
agent1.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand All @@ -180,6 +181,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase

agent2.preferences[:notification_config][:group_ids] = nil
agent2.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand All @@ -198,6 +200,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase
# no group selection
agent1.preferences[:notification_config][:group_ids] = []
agent1.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand All @@ -217,6 +220,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase

agent2.preferences[:notification_config][:group_ids] = []
agent2.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand All @@ -234,6 +238,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase

agent1.preferences[:notification_config][:group_ids] = ['-']
agent1.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand All @@ -253,6 +258,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase

agent2.preferences[:notification_config][:group_ids] = ['-']
agent2.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand All @@ -271,6 +277,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase
# dedecated group selection
agent1.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
agent1.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand All @@ -289,6 +296,7 @@ class NotificationFactoryMailerTest < ActiveSupport::TestCase

agent2.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
agent2.save
travel 30.seconds

result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
assert_equal(true, result[:channels][:online])
Expand Down

0 comments on commit f9a26d7

Please sign in to comment.