Improved caching, moved to websockets.

This commit is contained in:
Martin Edenhofer 2012-07-29 17:27:01 +02:00
parent 66137dcf7b
commit 43d99d266e
9 changed files with 144 additions and 89 deletions

View file

@ -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 = {}) ->

View file

@ -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

View file

@ -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

View file

@ -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')

View file

@ -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

View file

@ -16,6 +16,7 @@ require 'notification_factory'
# load lib
require 'gmaps'
require 'rss'
require 'cache'
require 'web_socket'

View file

@ -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
end

14
lib/cache.rb Normal file
View file

@ -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

View file

@ -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