From 43d99d266e0291f6c71242a54d89920b6936e2ff Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 29 Jul 2012 17:27:01 +0200 Subject: [PATCH] Improved caching, moved to websockets. --- .../controllers/agent_ticket_create.js.coffee | 80 ++++++++++--------- .../ticket_overviews_controller.rb | 27 +------ app/models/application_model.rb | 9 +-- app/models/ticket.rb | 37 +++++++++ app/models/user.rb | 25 +++--- config/environment.rb | 1 + config/initializers/cache.rb | 8 +- lib/cache.rb | 14 ++++ lib/web_socket.rb | 32 ++++++-- 9 files changed, 144 insertions(+), 89 deletions(-) create mode 100644 lib/cache.rb diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index c2cc9ca83..de13f1925 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -27,49 +27,57 @@ class Index extends App.Controller # get data / in case also ticket data for split fetch: (params) -> - App.Com.ajax( - id: 'ticket_create', - type: 'GET', - url: '/ticket_create', - data: { - ticket_id: params.ticket_id, - article_id: params.article_id, - }, - processData: true, - success: (data, status, xhr) => - - # get edit form attributes - @edit_form = data.edit_form - # load user collection - @loadCollection( type: 'User', data: data.users ) + # use cache + if window.LastRefresh[ 'ticket_create_attributes' ] && !params.ticket_id && !params.article_id - # render page - if !params.ticket_id && !params.article_id - @render() - else + # get edit form attributes + @edit_form = window.LastRefresh[ 'ticket_create_attributes' ].edit_form + + # load user collection + @loadCollection( type: 'User', data: window.LastRefresh[ 'ticket_create_attributes' ].users ) + + @render() + else + App.Com.ajax( + id: 'ticket_create', + type: 'GET', + url: '/ticket_create', + data: { + ticket_id: params.ticket_id, + article_id: params.article_id, + }, + processData: true, + success: (data, status, xhr) => + + # cache request + window.LastRefresh[ 'ticket_create_attributes' ] = data + + # get edit form attributes + @edit_form = data.edit_form # load user collection @loadCollection( type: 'User', data: data.users ) - + # load ticket collection - @loadCollection( type: 'Ticket', data: [data.ticket] ) - - # load article collections - @loadCollection( type: 'TicketArticle', data: data.articles || [] ) - - # render page - t = App.Ticket.find(params.ticket_id).attributes() - a = App.TicketArticle.find(params.article_id) - - # reset owner - t.owner_id = 0 - t.customer_id_autocompletion = a.from - t.subject = a.subject || t.title - t.body = a.body - @log '11111', t + if data.ticket && data.articles + @loadCollection( type: 'Ticket', data: [data.ticket] ) + + # load article collections + @loadCollection( type: 'TicketArticle', data: data.articles || [] ) + + # render page + t = App.Ticket.find(params.ticket_id).attributes() + a = App.TicketArticle.find(params.article_id) + + # reset owner + t.owner_id = 0 + t.customer_id_autocompletion = a.from + t.subject = a.subject || t.title + t.body = a.body + @log '11111', t @render( options: t ) - ) + ) render: (template = {}) -> diff --git a/app/controllers/ticket_overviews_controller.rb b/app/controllers/ticket_overviews_controller.rb index 1ee2efcdd..c72d6a1fa 100644 --- a/app/controllers/ticket_overviews_controller.rb +++ b/app/controllers/ticket_overviews_controller.rb @@ -86,29 +86,10 @@ class TicketOverviewsController < ApplicationController # GET /ticket_create/1 def ticket_create - # get related users - users = {} - - ticket_group_ids = [] - Group.where( :active => true ).each { |group| - ticket_group_ids.push group.id - } - ticket_owner_ids = [] - Ticket.agents.each { |user| - ticket_owner_ids.push user.id - if !users[user.id] - users[user.id] = user_data_full(user.id) - end - } - - ticket_state_ids = [] - Ticket::State.where( :active => true ).each { |state| - ticket_state_ids.push state.id - } - ticket_priority_ids = [] - Ticket::Priority.where( :active => true ).each { |priority| - ticket_priority_ids.push priority.id - } + # get attributes + (users, ticket_owner_ids, ticket_group_ids, ticket_state_ids, ticket_priority_ids) = Ticket.create_attributes( + :current_user_id => current_user.id, + ) # split data ticket = nil diff --git a/app/models/application_model.rb b/app/models/application_model.rb index 762f6f1f6..cec37a9db 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -8,18 +8,15 @@ class ApplicationModel < ActiveRecord::Base if o.respond_to?('cache_delete') then o.cache_delete end end def cache_delete -# puts 'cache_delete', self.id key = self.class.to_s + '::' + self.id.to_s - Rails.cache.delete( key.to_s ) + Cache.delete( key.to_s ) end def self.cache_set(data_id, data) -# puts 'cache_set', data_id key = self.to_s + '::' + data_id.to_s - Rails.cache.write( key.to_s, data ) + Cache.write( key.to_s, data ) end def self.cache_get(data_id) -# puts 'cache_get', data_id key = self.to_s + '::' + data_id.to_s - Rails.cache.read( key.to_s ) + Cache.get( key.to_s ) end end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 6cbe3fb9a..08e7cf175 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -240,6 +240,43 @@ class Ticket < ActiveRecord::Base end + +# Ticket.create_attributes( +# :current_user_id => 123, +# ) + def self.create_attributes (data) + + # get groups + ticket_group_ids = [] + Group.where( :active => true ).each { |group| + ticket_group_ids.push group.id + } + + # get related users + users = {} + ticket_owner_ids = [] + Ticket.agents.each { |user| + ticket_owner_ids.push user.id + if !users[user.id] + users[user.id] = User.user_data_full(user.id) + end + } + + # get states + ticket_state_ids = [] + Ticket::State.where( :active => true ).each { |state| + ticket_state_ids.push state.id + } + + # get priorities + ticket_priority_ids = [] + Ticket::Priority.where( :active => true ).each { |priority| + ticket_priority_ids.push priority.id + } + + return users, ticket_owner_ids, ticket_group_ids, ticket_state_ids, ticket_priority_ids + end + private def number_generate Ticket.new.number_adapter = Setting.get('ticket_number') diff --git a/app/models/user.rb b/app/models/user.rb index ca14d2c69..8d3dcb4c7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,24 +31,24 @@ class User < ApplicationModel end def self.authenticate( username, password ) - + # do not authenticate with nothing return if !username || username == '' return if !password || password == '' - + # try to find user based on login user = User.where( :login => username, :active => true ).first - + # try second lookup with email if !user user = User.where( :email => username, :active => true ).first end - + # no user found if !user return nil end - + # auth ok if user.password == password return user @@ -158,7 +158,8 @@ Your #{config.product_name} Team def self.find_fulldata(user_id) - return cache_get(user_id) if cache_get(user_id) + cache = self.cache_get(user_id) + return cache if cache # get user user = User.find(user_id) @@ -180,29 +181,31 @@ Your #{config.product_name} Team # set roles roles = [] user.roles.select('id, name').where( :active => true ).each { |role| - roles.push role + roles.push role.attributes } data['roles'] = roles data['role_ids'] = user.role_ids groups = [] user.groups.select('id, name').where( :active => true ).each { |group| - groups.push group + groups.push group.attributes } data['groups'] = groups data['group_ids'] = user.group_ids organization = user.organization - data['organization'] = organization + if organization + data['organization'] = organization.attributes + end organizations = [] user.organizations.select('id, name').where( :active => true ).each { |organization| - organizations.push organization + organizations.push organization.attributes } data['organizations'] = organizations data['organization_ids'] = user.organization_ids - cache_set(user.id, data) + self.cache_set(user.id, data) return data end diff --git a/config/environment.rb b/config/environment.rb index 80ed9c829..b2f8887db 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -16,6 +16,7 @@ require 'notification_factory' # load lib require 'gmaps' require 'rss' +require 'cache' require 'web_socket' diff --git a/config/initializers/cache.rb b/config/initializers/cache.rb index 9ffa80a34..672af8cff 100644 --- a/config/initializers/cache.rb +++ b/config/initializers/cache.rb @@ -1,9 +1,5 @@ # clear cache if Zammad::Application.config.cache_store[1] && File.directory?(Zammad::Application.config.cache_store[1]) + puts 'clear cache...' Rails.cache.clear -end - -# to get rails caching working, load models -Dir.foreach("#{Rails.root}/app/models") do |model_name| - require_dependency model_name unless model_name == '.' || model_name == '..' || model_name == '.gitkeep' -end \ No newline at end of file +end \ No newline at end of file diff --git a/lib/cache.rb b/lib/cache.rb new file mode 100644 index 000000000..3955b3e2d --- /dev/null +++ b/lib/cache.rb @@ -0,0 +1,14 @@ +module Cache + def self.delete( key ) + puts 'Cache.delete' + key.to_s + Rails.cache.delete( key.to_s ) + end + def self.write( key, data ) + puts 'Cache.write: ' + key.to_s + Rails.cache.write( key.to_s, data ) + end + def self.get( key ) + puts 'Cache.get: ' + key.to_s + Rails.cache.read( key.to_s ) + end +end \ No newline at end of file diff --git a/lib/web_socket.rb b/lib/web_socket.rb index f13cb3c30..cafed55c0 100644 --- a/lib/web_socket.rb +++ b/lib/web_socket.rb @@ -66,21 +66,23 @@ module Session next if !user_session[:id] user = User.find( user_session[:id] ) - # overviews - result = Ticket.overview( + # overview meta data + overview = Ticket.overview( :current_user_id => user.id, ) - if state_client_ids[client_id][:overview] != result - state_client_ids[client_id][:overview] = result + if state_client_ids[client_id][:overview] != overview + state_client_ids[client_id][:overview] = overview # send update to browser Session.transaction( client_id, { - :action => 'load', - :data => result, + :data => overview, :event => 'navupdate_ticket_overview', }) end + # ticket overview lists +# list = Ticket.overview_list() + # recent viewed recent_viewed = History.recent_viewed(user) if state_client_ids[client_id][:recent_viewed] != recent_viewed @@ -91,7 +93,6 @@ module Session # send update to browser Session.transaction( client_id, { - :action => 'load', :data => recent_viewed, :event => 'update_recent_viewed', }) @@ -110,6 +111,23 @@ module Session :collection => 'activity_stream', :data => activity_stream, }) + + # ticket create + ticket_create_attributes = Ticket.create_attributes( + :current_user_id => user.id, + ) + if state_client_ids[client_id][:ticket_create_attributes] != ticket_create_attributes + state_client_ids[client_id][:ticket_create_attributes] = ticket_create_attributes + + # send update to browser + Session.transaction( client_id, { + :data => ticket_create_attributes, + :collection => 'ticket_create_attributes', + }) + end + + # system settings + end # rss view