Merge branch 'feature/history_rewrite' into develop

Moved to new history API.
This commit is contained in:
Martin Edenhofer 2013-06-14 10:05:42 +02:00
commit b82a262de9
9 changed files with 93 additions and 84 deletions

View file

@ -149,7 +149,7 @@ class TicketsController < ApplicationController
return if !ticket_permission( ticket )
# get history of ticket
history = History.history_list( 'Ticket', params[:id], 'Ticket::Article' )
history = History.list( 'Ticket', params[:id], 'Ticket::Article' )
# get related users
users = {}

View file

@ -12,7 +12,27 @@ class History < ApplicationModel
@@cache_object = {}
@@cache_attribute = {}
def self.history_create(data)
=begin
add a new history entry for an object
History.add(
:history_type => 'updated',
:history_object => 'Ticket',
:history_attribute => 'ticket_state',
:o_id => ticket.id,
:id_to => 3,
:id_from => 2,
:value_from => 'open',
:value_to => 'pending',
:created_by_id => 1,
:created_at => '2013-06-04 10:00:00',
:updated_at => '2013-06-04 10:00:00'
)
=end
def self.add(data)
# lookups
if data[:history_type]
@ -63,13 +83,31 @@ class History < ApplicationModel
end
end
def self.history_destroy( requested_object, requested_object_id )
History.where( :history_object_id => History::Object.where( :name => requested_object ) ).
where( :o_id => requested_object_id ).
destroy_all
=begin
remove whole history entries of an object
History.remove( 'Ticket', 123 )
=end
def self.remove( requested_object, requested_object_id )
history_object = History::Object.where( :name => requested_object ).first
History.where(
:history_object_id => history_object.id,
:o_id => requested_object_id,
).destroy_all
end
def self.history_list( requested_object, requested_object_id, related_history_object = nil )
=begin
return all histoy entries of an object
history_list = History.list( 'Ticket', 123 )
=end
def self.list( requested_object, requested_object_id, related_history_object = nil )
if !related_history_object
history_object = self.history_object_lookup( requested_object )
history = History.where( :history_object_id => history_object.id ).
@ -90,36 +128,7 @@ class History < ApplicationModel
order('created_at ASC, id ASC')
end
list = []
history.each { |item|
item_tmp = item.attributes
item_tmp['history_type'] = item.history_type.name
item_tmp['history_object'] = item.history_object.name
if item.history_attribute
item_tmp['history_attribute'] = item.history_attribute.name
end
item_tmp.delete( 'history_attribute_id' )
item_tmp.delete( 'history_object_id' )
item_tmp.delete( 'history_type_id' )
item_tmp.delete( 'o_id' )
item_tmp.delete( 'updated_at' )
if item_tmp['id_to'] == nil && item_tmp['id_from'] == nil
item_tmp.delete( 'id_to' )
item_tmp.delete( 'id_from' )
end
if item_tmp['value_to'] == nil && item_tmp['value_from'] == nil
item_tmp.delete( 'value_to' )
item_tmp.delete( 'value_from' )
end
if item_tmp['related_history_object_id'] == nil
item_tmp.delete( 'related_history_object_id' )
end
if item_tmp['related_o_id'] == nil
item_tmp.delete( 'related_o_id' )
end
list.push item_tmp
}
return list
return history
end
def self.activity_stream( user, limit = 10 )

View file

@ -21,7 +21,7 @@ class Observer::History < ActiveRecord::Observer
related_o_id = record.ticket_id
related_history_object = 'Ticket'
end
History.history_create(
History.add(
:o_id => record.id,
:history_type => 'created',
:history_object => record.class.name,
@ -145,7 +145,7 @@ class Observer::History < ActiveRecord::Observer
related_o_id = record.ticket_id
related_history_object_id = 'Ticket'
end
History.history_create(
History.add(
:o_id => current.id,
:history_type => 'updated',
:history_object => record.class.name,

View file

@ -11,13 +11,12 @@ class Observer::Tag::TicketHistory < ActiveRecord::Observer
return true if record.tag_object.name != 'Ticket';
# add ticket history
History.history_create(
History.add(
:o_id => record.o_id,
:history_type => 'added',
:history_object => 'Ticket',
:history_attribute => 'Tag',
:value_from => record.tag_item.name,
:created_by_id => record.created_by_id || UserInfo.current_user_id || 1
)
end
def after_destroy(record)
@ -26,13 +25,12 @@ class Observer::Tag::TicketHistory < ActiveRecord::Observer
return true if record.tag_object.name != 'Ticket';
# add ticket history
History.history_create(
History.add(
:o_id => record.o_id,
:history_type => 'removed',
:history_object => 'Ticket',
:history_attribute => 'Tag',
:value_from => record.tag_item.name,
:created_by_id => record.created_by_id || UserInfo.current_user_id || 1
)
end
end

View file

@ -57,7 +57,7 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer
end
}
if recipient_list != ''
History.history_create(
History.add(
:o_id => record.id,
:history_type => 'email',
:history_object => 'Ticket::Article',
@ -65,7 +65,6 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer
:related_history_object => 'Ticket',
:value_from => record.subject,
:value_to => recipient_list,
:created_by_id => record.created_by_id,
)
end
end

View file

@ -237,7 +237,7 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
# add history record
if recipient_list != ''
History.history_create(
History.add(
:o_id => ticket.id,
:history_type => 'notification',
:history_object => 'Ticket',

View file

@ -713,11 +713,12 @@ class Ticket < ApplicationModel
end
end
end
def destroy_dependencies
# delete history
History.history_destroy( 'Ticket', self.id )
History.remove( 'Ticket', self.id )
# delete articles
self.articles.destroy_all
@ -731,7 +732,7 @@ class Ticket < ApplicationModel
total_time_without_pending = 0
total_time = 0
#get history for ticket
history_list = History.history_list( 'Ticket', self.id, 'Ticket' )
history_list = History.list( 'Ticket', self.id, 'Ticket' )
#loop through hist. changes and get time
last_state = nil
@ -740,51 +741,53 @@ class Ticket < ApplicationModel
history_list.each { |history_item|
# ignore if it isn't a state change
next if history_item['history_attribute'] != 'ticket_state'
next if !history_item.history_attribute_id
history_attribute = History::Attribute.lookup( :id => history_item.history_attribute_id );
next if history_attribute.name != 'ticket_state'
# ignore all older state changes after end_time
next if last_state_change && last_state_change > end_time
# if created_at is later then end_time, use end_time as last time
if history_item['created_at'] > end_time
history_item['created_at'] = end_time
if history_item.created_at > end_time
history_item.created_at = end_time
end
# get initial state and time
if !last_state
last_state = history_item['value_from']
last_state = history_item.value_from
last_state_change = start_time
end
# use time if ticket got from e. g. open to pending
if history_item['value_from'] != 'pending' && history_item['value_to'] == 'pending'
diff = escalation_time_diff( last_state_change, history_item['created_at'], sla_selected )
puts "Diff count !=pending -> ==pending #{diff.to_s} - #{last_state_change} - #{history_item['created_at']}"
if history_item.value_from != 'pending' && history_item.value_to == 'pending'
diff = escalation_time_diff( last_state_change, history_item.created_at, sla_selected )
puts "Diff count !=pending -> ==pending #{diff.to_s} - #{last_state_change} - #{history_item.created_at}"
total_time_without_pending = total_time_without_pending + diff
total_time = total_time + diff
last_state_is_pending = true
# use time if ticket got from e. g. open to open
elsif history_item['value_from'] != 'pending' && history_item['value_to'] != 'pending'
diff = escalation_time_diff( last_state_change, history_item['created_at'], sla_selected )
puts "Diff count !=pending -> !=pending #{diff.to_s} - #{last_state_change} - #{history_item['created_at']}"
elsif history_item.value_from != 'pending' && history_item.value_to != 'pending'
diff = escalation_time_diff( last_state_change, history_item.created_at, sla_selected )
puts "Diff count !=pending -> !=pending #{diff.to_s} - #{last_state_change} - #{history_item.created_at}"
total_time_without_pending = total_time_without_pending + diff
total_time = total_time + diff
last_state_is_pending = false
elsif history_item['value_from'] == 'pending' && history_item['value_to'] != 'pending'
diff = escalation_time_diff( last_state_change, history_item['created_at'], sla_selected )
puts "Diff not count ==pending -> !=pending #{diff.to_s} - #{last_state_change} - #{history_item['created_at']}"
elsif history_item.value_from == 'pending' && history_item.value_to != 'pending'
diff = escalation_time_diff( last_state_change, history_item.created_at, sla_selected )
puts "Diff not count ==pending -> !=pending #{diff.to_s} - #{last_state_change} - #{history_item.created_at}"
total_time = total_time + diff
last_state_is_pending = false
# no pending state, do not count
else
puts "Diff do not count #{history_item['value_from']}->#{history_item['value_to']} -> #{history_item['created_at']}"
puts "Diff do not count #{history_item.value_from}->#{history_item.value_to} -> #{history_item.created_at}"
last_state_is_pending = false
end
# remember for next loop last state
last_state = history_item['value_to']
last_state_change = history_item['created_at']
last_state = history_item.value_to
last_state_change = history_item.created_at
}
# if last state isnt pending, count rest

View file

@ -417,7 +417,7 @@ module Import::OTRS
# puts '-------'
# puts history.inspect
if history['HistoryType'] == 'NewTicket'
History.history_create(
History.add(
:id => history['HistoryID'],
:o_id => history['TicketID'],
:history_type => 'created',
@ -444,7 +444,7 @@ module Import::OTRS
end
end
# puts "STATE UPDATE (#{history['HistoryID']}): -> #{from}->#{to}"
History.history_create(
History.add(
:id => history['HistoryID'],
:o_id => history['TicketID'],
:history_type => 'updated',
@ -469,7 +469,7 @@ module Import::OTRS
to = $3
to_id = $4
end
History.history_create(
History.add(
:id => history['HistoryID'],
:o_id => history['TicketID'],
:history_type => 'updated',
@ -494,7 +494,7 @@ module Import::OTRS
to = $3
to_id = $4
end
History.history_create(
History.add(
:id => history['HistoryID'],
:o_id => history['TicketID'],
:history_type => 'updated',
@ -509,7 +509,7 @@ module Import::OTRS
)
end
if history['ArticleID'] && history['ArticleID'] != 0
History.history_create(
History.add(
:id => history['HistoryID'],
:o_id => history['ArticleID'],
:history_type => 'created',

View file

@ -162,7 +162,7 @@ class HistoryTest < ActiveSupport::TestCase
tickets.push ticket
# get history
history_list = History.history_list( 'Ticket', ticket.id, 'Ticket::Article' )
history_list = History.list( 'Ticket', ticket.id, 'Ticket::Article' )
puts history_list.inspect
test[:history_check].each { |check_item|
# puts '+++++++++++'