Просмотр исходного кода

Merge branch 'feature/history_rewrite' into develop

Moved to new history API.
Martin Edenhofer 11 лет назад
Родитель
Сommit
b82a262de9

+ 1 - 1
app/controllers/tickets_controller.rb

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

+ 45 - 36
app/models/history.rb

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

+ 2 - 2
app/models/observer/history.rb

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

+ 2 - 4
app/models/observer/tag/ticket_history.rb

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

+ 1 - 2
app/models/observer/ticket/article/communicate_email.rb

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

+ 1 - 1
app/models/observer/ticket/notification.rb

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

+ 22 - 19
app/models/ticket.rb

@@ -713,11 +713,12 @@ class Ticket < ApplicationModel
       end
     end
 
+
   end
   def destroy_dependencies
 
-    # delete history
-    History.history_destroy( 'Ticket', self.id )
+      # delete history
+      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

+ 14 - 14
lib/import/otrs.rb

@@ -206,7 +206,7 @@ module Import::OTRS
     result = json(response)
     self._ticket_result(result)
   end
-  
+
   def self._ticket_result(result)
 #    puts result.inspect
     map = {
@@ -309,7 +309,7 @@ module Import::OTRS
         end
 
         record['Articles'].each { |article|
-    
+
           # get article values
           article_new = {
             :created_by_id => 1,
@@ -342,10 +342,10 @@ module Import::OTRS
               rescue
                 display_name = article_new[:from]
               end
-    
+
               # do extra decoding because we needed to use field.value
               display_name = Mail::Field.new( 'X-From', display_name ).to_s
-    
+
               roles = Role.lookup( :name => 'Customer' )
               user = User.create(
                 :login          => email,
@@ -361,7 +361,7 @@ module Import::OTRS
             end
             article_new[:created_by_id] = user.id
           end
-    
+
           if article_new[:ticket_article_sender] == 'customer'
             article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'Customer' ).id
             article_new.delete( :ticket_article_sender )
@@ -374,7 +374,7 @@ module Import::OTRS
             article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'System' ).id
             article_new.delete( :ticket_article_sender )
           end
-    
+
           if article_new[:ticket_article_type] == 'email-external'
             article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'email' ).id
             article_new[:internal] = false
@@ -410,14 +410,14 @@ module Import::OTRS
             article.id = article_new[:id]
             article.save
           end
-    
+
         }
-    
+
         record['History'].each { |history|
     #      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',
@@ -688,7 +688,7 @@ module Import::OTRS
       :UserLastname  => :lastname,
 #      :UserTitle     => 
       :UserLogin     => :login,
-      :UserPw        => :password, 
+      :UserPw        => :password,
     };
 
     result.each { |user|

+ 5 - 5
test/unit/history_test.rb

@@ -1,6 +1,6 @@
 # encoding: utf-8
 require 'test_helper'
- 
+
 class HistoryTest < ActiveSupport::TestCase
   test 'ticket' do
     tests = [
@@ -154,15 +154,15 @@ class HistoryTest < ActiveSupport::TestCase
           article.update_attributes( test[:ticket_update][:article] )
         end
       end
- 
-      # execute ticket events      
+
+      # execute ticket events
       Observer::Ticket::Notification.transaction
 
       # remember ticket
       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 '+++++++++++'
@@ -207,4 +207,4 @@ class HistoryTest < ActiveSupport::TestCase
       assert( !found, "Ticket destroyed")
     }
   end
-end
+end