Improved OnlineNotification.cleanup, just delete notification after 35 min. if user read it by him self. If auto seen is set (by some other user), just delete the online notification after 8 hours.

This commit is contained in:
Martin Edenhofer 2016-02-21 12:43:16 +01:00
parent 08bd5186b7
commit ccc61277f8
8 changed files with 138 additions and 16 deletions

View file

@ -11,7 +11,7 @@ class App.OnlineNotificationWidget extends App.Controller
@bind 'OnlineNotification::changed', => @bind 'OnlineNotification::changed', =>
@delay( @delay(
=> @fetch() => @fetch()
1600 2600
'online-notification-changed' 'online-notification-changed'
) )
@ -38,12 +38,12 @@ class App.OnlineNotificationWidget extends App.Controller
if @access() if @access()
@createContainer() @createContainer()
@subscribeId = App.OnlineNotification.subscribe( @updateContent ) @subscribeId = App.OnlineNotification.subscribe(@updateContent)
release: -> release: ->
@removeContainer() @removeContainer()
$(window).off 'click.notifications' $(window).off 'click.notifications'
App.OnlineNotification.unsubscribe( @subscribeId ) App.OnlineNotification.unsubscribe(@subscribeId)
access: -> access: ->
return false if !@Session.get() return false if !@Session.get()
@ -105,12 +105,12 @@ class App.OnlineNotificationWidget extends App.Controller
fetch: => fetch: =>
load = (items) => load = (items) =>
@fetchedData = true @fetchedData = true
App.OnlineNotification.refresh( items, { clear: true } ) App.OnlineNotification.refresh(items, { clear: true })
@updateContent() @updateContent()
App.OnlineNotification.fetchFull(load) App.OnlineNotification.fetchFull(load)
updateContent: => updateContent: =>
items = App.OnlineNotification.search( sortBy: 'created_at', order: 'DESC' ) items = App.OnlineNotification.search(sortBy: 'created_at', order: 'DESC')
counter = 0 counter = 0
for item in items for item in items
if !item.seen if !item.seen

View file

@ -48,11 +48,11 @@ curl http://localhost/api/v1/online_notifications.json -v -u #{login}:#{password
def index def index
if params[:full] if params[:full]
render json: OnlineNotification.list_full(current_user, 50) render json: OnlineNotification.list_full(current_user, 75)
return return
end end
notifications = OnlineNotification.list(current_user, 50) notifications = OnlineNotification.list(current_user, 75)
model_index_render_result(notifications) model_index_render_result(notifications)
end end

View file

@ -23,6 +23,6 @@ class Observer::Ticket::OnlineNotificationSeen < ActiveRecord::Observer
# set all online notifications to seen # set all online notifications to seen
# send background job # send background job
Delayed::Job.enqueue( Observer::Ticket::OnlineNotificationSeen::BackgroundJob.new( record.id ) ) Delayed::Job.enqueue( Observer::Ticket::OnlineNotificationSeen::BackgroundJob.new( record.id, record.updated_by_id ) )
end end
end end

View file

@ -1,6 +1,7 @@
class Observer::Ticket::OnlineNotificationSeen::BackgroundJob class Observer::Ticket::OnlineNotificationSeen::BackgroundJob
def initialize(id) def initialize(ticket_id, user_id)
@ticket_id = id @ticket_id = ticket_id
@user_id = user_id || 1
end end
def perform def perform
@ -14,6 +15,7 @@ class Observer::Ticket::OnlineNotificationSeen::BackgroundJob
next if !seen next if !seen
next if seen == notification.seen next if seen == notification.seen
notification.seen = true notification.seen = true
notification.updated_by_id = @user_id
notification.save notification.save
} }
end end

View file

@ -18,8 +18,11 @@ add a new online notification for this user
object: 'Ticket', object: 'Ticket',
o_id: ticket.id, o_id: ticket.id,
seen: false, seen: false,
created_by_id: 1,
user_id: 2, user_id: 2,
created_by_id: 1,
updated_by_id: 1,
created_at: Time.zone.now,
updated_at: Time.zone.now,
) )
=end =end
@ -40,7 +43,10 @@ add a new online notification for this user
type_lookup_id: type_id, type_lookup_id: type_id,
seen: data[:seen], seen: data[:seen],
user_id: data[:user_id], user_id: data[:user_id],
created_by_id: data[:created_by_id] created_by_id: data[:created_by_id],
updated_by_id: data[:updated_by_id],
created_at: data[:created_at],
updated_at: data[:updated_at],
} }
OnlineNotification.create(record) OnlineNotification.create(record)
@ -203,12 +209,28 @@ cleanup old online notifications
OnlineNotification.cleanup OnlineNotification.cleanup
with dedicated times
max_age = Time.zone.now - 9.months
max_own_seen = Time.zone.now - 35.minutes
max_auto_seen = Time.zone.now - 8.hours
OnlineNotification.cleanup(max_age, max_own_seen, max_auto_seen)
=end =end
def self.cleanup def self.cleanup(max_age = Time.zone.now - 9.months, max_own_seen = Time.zone.now - 35.minutes, max_auto_seen = Time.zone.now - 8.hours)
OnlineNotification.where('created_at < ?', Time.zone.now - 6.months).delete_all OnlineNotification.where('created_at < ?', max_age).delete_all
OnlineNotification.where('seen = ? AND created_at < ?', true, Time.zone.now - 4.hours).delete_all OnlineNotification.where('seen = ? AND updated_at < ?', true, max_own_seen).each {|notification|
OnlineNotification.where('seen = ? AND updated_at < ?', true, Time.zone.now - 1.hour).delete_all
# delete own "seen" notificatons after 1 hour
next if notification.user_id == notification.updated_by_id && notification.updated_at > max_own_seen
# delete notificatons which are set to "seen" by somebody else after 8 hour
next if notification.user_id != notification.updated_by_id && notification.updated_at > max_auto_seen
notification.delete
}
# notify all agents # notify all agents
User.of_role('Agent').each {|user| User.of_role('Agent').each {|user|

View file

@ -394,6 +394,7 @@ class CreateBase < ActiveRecord::Migration
t.integer :type_lookup_id, null: false t.integer :type_lookup_id, null: false
t.integer :user_id, null: false t.integer :user_id, null: false
t.boolean :seen, null: false, default: false t.boolean :seen, null: false, default: false
t.integer :updated_by_id, null: false
t.integer :created_by_id, null: false t.integer :created_by_id, null: false
t.timestamps null: false t.timestamps null: false
end end

View file

@ -0,0 +1,6 @@
class AddUpdatedByOnlineNotification < ActiveRecord::Migration
def up
return if OnlineNotification.column_names.include?('updated_by_id')
add_column :online_notifications, :updated_by_id, :integer
end
end

View file

@ -440,6 +440,97 @@ class OnlineNotificationTest < ActiveSupport::TestCase
end end
test 'cleanup check' do
online_notification1 = OnlineNotification.add(
type: 'create',
object: 'Ticket',
o_id: 123,
seen: false,
user_id: agent_user1.id,
created_by_id: 1,
updated_by_id: 1,
created_at: Time.zone.now - 10.months,
updated_at: Time.zone.now - 10.months,
)
online_notification2 = OnlineNotification.add(
type: 'create',
object: 'Ticket',
o_id: 123,
seen: true,
user_id: agent_user1.id,
created_by_id: 1,
updated_by_id: 1,
created_at: Time.zone.now - 10.months,
updated_at: Time.zone.now - 10.months,
)
online_notification3 = OnlineNotification.add(
type: 'create',
object: 'Ticket',
o_id: 123,
seen: false,
user_id: agent_user1.id,
created_by_id: 1,
updated_by_id: 1,
created_at: Time.zone.now - 2.days,
updated_at: Time.zone.now - 2.days,
)
online_notification4 = OnlineNotification.add(
type: 'create',
object: 'Ticket',
o_id: 123,
seen: true,
user_id: agent_user1.id,
created_by_id: agent_user1.id,
updated_by_id: agent_user1.id,
created_at: Time.zone.now - 2.days,
updated_at: Time.zone.now - 2.days,
)
online_notification5 = OnlineNotification.add(
type: 'create',
object: 'Ticket',
o_id: 123,
seen: true,
user_id: agent_user1.id,
created_by_id: agent_user2.id,
updated_by_id: agent_user2.id,
created_at: Time.zone.now - 2.days,
updated_at: Time.zone.now - 2.days,
)
online_notification6 = OnlineNotification.add(
type: 'create',
object: 'Ticket',
o_id: 123,
seen: true,
user_id: agent_user1.id,
created_by_id: agent_user1.id,
updated_by_id: agent_user1.id,
created_at: Time.zone.now - 10.minutes,
updated_at: Time.zone.now - 10.minutes,
)
online_notification7 = OnlineNotification.add(
type: 'create',
object: 'Ticket',
o_id: 123,
seen: true,
user_id: agent_user1.id,
created_by_id: agent_user2.id,
updated_by_id: agent_user2.id,
created_at: Time.zone.now - 10.minutes,
updated_at: Time.zone.now - 10.minutes,
)
OnlineNotification.cleanup
assert(!OnlineNotification.find_by(id: online_notification1.id))
assert(!OnlineNotification.find_by(id: online_notification2.id))
assert(OnlineNotification.find_by(id: online_notification3.id))
assert(!OnlineNotification.find_by(id: online_notification4.id))
assert(!OnlineNotification.find_by(id: online_notification5.id))
assert(OnlineNotification.find_by(id: online_notification6.id))
assert(OnlineNotification.find_by(id: online_notification7.id))
end
def notification_check(online_notifications, checks) def notification_check(online_notifications, checks)
checks.each { |check_item| checks.each { |check_item|
hit = false hit = false