Improved performance, reduced sql queries.

This commit is contained in:
Martin Edenhofer 2013-06-19 23:15:28 +02:00
parent 4538ed7a2e
commit 9fce89fe4f

View file

@ -13,23 +13,55 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
def user_ticket_counter_update(record) def user_ticket_counter_update(record)
return if !record.customer_id return if !record.customer_id
ticket_state_list_open = Ticket::State.where( # open ticket count
:state_type_id => Ticket::StateType.where( ticket_state_open_ids = Cache.get( 'ticket::state_ids::open' )
:name => ['new','open', 'pending reminder', 'pending action'] if !ticket_state_open_ids
) ticket_state_open_ids = self.state_ids( ['new','open', 'pending reminder', 'pending action'] )
) Cache.write( 'ticket::state_ids::open', ticket_state_open_ids, { :expires_in => 1.hour } )
ticket_state_list_closed = Ticket::State.where( end
:state_type_id => Ticket::StateType.where( tickets_open = Ticket.where(
:name => ['closed'] :customer_id => record.customer_id,
) :ticket_state_id => ticket_state_open_ids,
) ).count()
tickets_open = Ticket.where( :customer_id => record.customer_id, :ticket_state_id => ticket_state_list_open ).count() # closed ticket count
tickets_closed = Ticket.where( :customer_id => record.customer_id, :ticket_state_id => ticket_state_list_closed ).count() ticket_state_closed_ids = Cache.get( 'ticket::state_ids::closed' )
if !ticket_state_closed_ids
ticket_state_closed_ids = self.state_ids( ['closed'] )
Cache.write( 'ticket::state_ids::closed', ticket_state_closed_ids, { :expires_in => 1.hour } )
end
tickets_closed = Ticket.where(
:customer_id => record.customer_id,
:ticket_state_id => ticket_state_closed_ids,
).count()
customer = User.find( record.customer_id ) # check if update is needed
customer = User.lookup( :id => record.customer_id )
need_update = false
if customer[:preferences][:tickets_open] != tickets_open
need_update = true
customer[:preferences][:tickets_open] = tickets_open customer[:preferences][:tickets_open] = tickets_open
end
if customer[:preferences][:tickets_closed] != tickets_closed
need_update = true
customer[:preferences][:tickets_closed] = tickets_closed customer[:preferences][:tickets_closed] = tickets_closed
end
if need_update
customer.save customer.save
end end
end end
def state_ids(ticket_state_types)
ticket_state_types = Ticket::StateType.where(
:name => ticket_state_types,
)
ticket_states = Ticket::State.where( :state_type_id => ticket_state_types )
ticket_state_ids = []
ticket_states.each {|ticket_state|
ticket_state_ids.push ticket_state.id
}
ticket_state_ids
end
end