diff options
| author | Xinhui | 2016-09-28 17:37:56 +0200 | 
|---|---|---|
| committer | Xinhui | 2016-09-28 17:37:56 +0200 | 
| commit | 1d84e1ded90688fee6ad80bb4f53c861552e3b68 (patch) | |
| tree | 4fd1102f114a5edaaaaef892733078b3f494194e | |
| parent | 4dbfc621e34d90438095139e72e841a584b44ff7 (diff) | |
| download | chouette-core-1d84e1ded90688fee6ad80bb4f53c861552e3b68.tar.bz2 | |
Templating line_referential_sync_message
Refs #1707
| -rw-r--r-- | app/models/line_referential.rb | 2 | ||||
| -rw-r--r-- | app/models/line_referential_sync.rb | 16 | ||||
| -rw-r--r-- | app/views/line_referentials/show.html.slim | 19 | ||||
| -rw-r--r-- | app/workers/line_referential_sync_worker.rb | 18 | ||||
| -rw-r--r-- | config/locales/line_referentials.en.yml | 9 | ||||
| -rw-r--r-- | config/locales/line_referentials.fr.yml | 8 | ||||
| -rw-r--r-- | lib/stif/codif_line_synchronization.rb | 102 | 
7 files changed, 94 insertions, 80 deletions
| diff --git a/app/models/line_referential.rb b/app/models/line_referential.rb index f33934dfc..eb519a727 100644 --- a/app/models/line_referential.rb +++ b/app/models/line_referential.rb @@ -7,7 +7,7 @@ class LineReferential < ActiveRecord::Base    has_many :companies, class_name: 'Chouette::Company'    has_many :networks, class_name: 'Chouette::Network' -  has_many :line_referential_syncs +  has_many :line_referential_syncs, -> { order(created_at: :desc) }    def add_member(organisation, options = {})      attributes = options.merge organisation: organisation diff --git a/app/models/line_referential_sync.rb b/app/models/line_referential_sync.rb index 996266b2e..187e69967 100644 --- a/app/models/line_referential_sync.rb +++ b/app/models/line_referential_sync.rb @@ -1,7 +1,7 @@  class LineReferentialSync < ActiveRecord::Base    include AASM    belongs_to :line_referential -  has_many :line_referential_sync_messages, :dependent => :destroy +  has_many :line_referential_sync_messages, -> { order(created_at: :desc) }, :dependent => :destroy    after_commit :perform_sync, :on => :create    validate :multiple_process_validation, :on => :create @@ -37,27 +37,27 @@ class LineReferentialSync < ActiveRecord::Base      end    end -  def create_sync_message criticity, key, attributes +  def create_sync_message criticity, key, message_attributs = {}      params = {        criticity: criticity,        message_key: key, -      message_attributs: attributes +      message_attributs: message_attributs      }      line_referential_sync_messages.create params    end    def log_pending      update_attribute(:started_at, Time.now) -    create_sync_message :info, :pending, self.attributes +    create_sync_message :info, :pending    end -  def log_successful +  def log_successful message_attributs      update_attribute(:ended_at, Time.now) -    create_sync_message :info, :successful, self.attributes +    create_sync_message :info, :successful, message_attributs    end -  def log_failed error +  def log_failed message_attributs      update_attribute(:ended_at, Time.now) -    create_sync_message :error, :failed, self.attributes.merge(error: error.message) +    create_sync_message :error, :failed, message_attributs    end  end diff --git a/app/views/line_referentials/show.html.slim b/app/views/line_referentials/show.html.slim index df0b2ab36..a3dcdf192 100644 --- a/app/views/line_referentials/show.html.slim +++ b/app/views/line_referentials/show.html.slim @@ -21,12 +21,21 @@        span.badge = @line_referential.lines.size        = link_to Referential.human_attribute_name("lines"), line_referential_lines_path(@line_referential) -/ - unless @line_referential.line_referential_sync.line_sync_operations.empty? -/   h3 Historique des synchronisations +- unless @line_referential.line_referential_syncs.empty? +  h3 Historique des synchronisations -/   ul.list-group width="75%" -/     - @line_referential.line_referential_sync.line_sync_operations.each do |sync| -/       li = "#{sync.created_at.to_formatted_s(:short)} - #{sync.message}" +  ul.list-group width="75%" +  - @line_referential.line_referential_syncs.each do |sync| +    - sync.line_referential_sync_messages.each do |log| +      li +        = log.criticity +        br +        = log.created_at +        br +        = t("line_referentials.synchronization.message.#{log.message_key}", log.message_attributs.symbolize_keys!) +        hr +    end +  end  - content_for :sidebar do    ul.actions diff --git a/app/workers/line_referential_sync_worker.rb b/app/workers/line_referential_sync_worker.rb index f5bf3b854..2069b1b01 100644 --- a/app/workers/line_referential_sync_worker.rb +++ b/app/workers/line_referential_sync_worker.rb @@ -2,14 +2,22 @@ class LineReferentialSyncWorker    include Sidekiq::Worker    sidekiq_options :retry => false +  def process_time +    Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) +  end +    def perform(lref_sync_id) -    lref_sync = LineReferentialSync.find lref_sync_id +    start_time = process_time +    lref_sync  = LineReferentialSync.find lref_sync_id      lref_sync.run      begin -      Stif::CodifLineSynchronization.synchronize -      lref_sync.successful -    rescue Exception => error -      lref_sync.failed error +      info = Stif::CodifLineSynchronization.synchronize +      lref_sync.successful info.merge({processing_time: process_time - start_time}) +    rescue Exception => e +      lref_sync.failed({ +        error: e.message, +        processing_time: process_time - start_time +      })      end    end  end diff --git a/config/locales/line_referentials.en.yml b/config/locales/line_referentials.en.yml index d7cd6c519..443a9a90d 100644 --- a/config/locales/line_referentials.en.yml +++ b/config/locales/line_referentials.en.yml @@ -5,12 +5,15 @@ en:      edit:        title: "Edit %{line_referential} referential"    synchronization: -    message: "Synchronization successful in %{time} seconds with %{imported} objects from Codifligne. %{deleted} objects were deleted." -    failure: "Synchronization interrupted after %{time} seconds." +    codifligne: +      message: +        pending: "Synchronization pending" +        successful: "Synchronization successful after %{processing_time} with %{imported} objects. %{deleted} objects were deleted.." +        failed: "Synchronization failed after %{processing_time} with error: %{error}."    activerecord:      models:        line_referential:          one:   "referential"      attributes:        line_referential: -        sync_interval: "Synchronisation frequency"
\ No newline at end of file +        sync_interval: "Synchronisation frequency" diff --git a/config/locales/line_referentials.fr.yml b/config/locales/line_referentials.fr.yml index adb96cbe0..da89fef63 100644 --- a/config/locales/line_referentials.fr.yml +++ b/config/locales/line_referentials.fr.yml @@ -4,11 +4,11 @@ fr:        edit: "Modifier ce référentiel"      edit:        title: "Modifier le référentiel %{line_referential}" -  synchronization: -    codifligne: +    synchronization:        message: -        success: "Synchronisation réussie après %{time} secondes avec %{imported} éléments importés de Codifligne. %{deleted} éléments ont été supprimés." -        failure: "Synchronisation interrompue après %{time} secondes." +        pending: "Synchronisation en attente" +        successful: "Synchronisation réussie après %{processing_time} secondes avec %{imported} éléments importés. %{deleted} éléments ont été supprimés." +        failed: "Synchronisation interrompue après %{processing_time} secondes avec l'erreur : %{error}."    activerecord:      models:        line_referential: diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb index fee785766..c3f0d81fa 100644 --- a/lib/stif/codif_line_synchronization.rb +++ b/lib/stif/codif_line_synchronization.rb @@ -3,60 +3,54 @@ module Stif      class << self        def synchronize          start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) -        begin -          # Fetch Codifline data -          client = Codifligne::API.new -          operators       = client.operators -          lines           = client.lines -          networks        = client.networks -          groups_of_lines = client.groups_of_lines - -          Rails.logger.info "Codifligne:sync - Codifligne request processed in #{elapsed_time_since start_time} seconds" - -          # Create or update Companies -          stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) -          operators.map       { |o| create_or_update_company(o) } -          log_create_or_update "Companies", operators.count, stime - -          # Create or update Lines -          stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) -          lines.map           { |l| create_or_update_line(l) } -          log_create_or_update "Lines", lines.count, stime - -          # Create or update Networks -          stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) -          networks.map        { |n| create_or_update_network(n) } -          log_create_or_update "Networks", networks.count, stime - -          # Create or update Group of lines -          stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) -          groups_of_lines.map { |g| create_or_update_group_of_lines(g) } -          log_create_or_update "Group of lines", groups_of_lines.count, stime - -          # Delete deprecated Group of lines -          deleted_gr = delete_deprecated(groups_of_lines, Chouette::GroupOfLine) -          log_deleted "Group of lines", deleted_gr unless deleted_gr == 0 - -          # Delete deprecated Networks -          deleted_ne = delete_deprecated(networks, Chouette::Network) -          log_deleted "Networks", deleted_ne unless deleted_ne == 0 - -          # Delete deprecated Lines -          deleted_li = delete_deprecated_lines(lines) -          log_deleted "Lines", deleted_li unless deleted_li == 0 - -          # Delete deprecated Operators -          deleted_op = delete_deprecated(operators, Chouette::Company) -          log_deleted "Operators", deleted_op unless deleted_op == 0 - -          # Building log message -          total_codifligne_elements = operators.count + lines.count + networks.count + groups_of_lines.count -          total_deleted = deleted_op + deleted_li + deleted_ne + deleted_gr -          total_time = elapsed_time_since start_time -        rescue Exception => e -          total_time = elapsed_time_since start_time -          Rails.logger.error "Codifligne:sync - Error: #{e}, ended after #{total_time} seconds" -        end +        # Fetch Codifline data +        client = Codifligne::API.new +        operators       = client.operators +        lines           = client.lines +        networks        = client.networks +        groups_of_lines = client.groups_of_lines + +        Rails.logger.info "Codifligne:sync - Codifligne request processed in #{elapsed_time_since start_time} seconds" + +        # Create or update Companies +        stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) +        operators.map       { |o| create_or_update_company(o) } +        log_create_or_update "Companies", operators.count, stime + +        # Create or update Lines +        stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) +        lines.map           { |l| create_or_update_line(l) } +        log_create_or_update "Lines", lines.count, stime + +        # Create or update Networks +        stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) +        networks.map        { |n| create_or_update_network(n) } +        log_create_or_update "Networks", networks.count, stime + +        # Create or update Group of lines +        stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) +        groups_of_lines.map { |g| create_or_update_group_of_lines(g) } +        log_create_or_update "Group of lines", groups_of_lines.count, stime + +        # Delete deprecated Group of lines +        deleted_gr = delete_deprecated(groups_of_lines, Chouette::GroupOfLine) +        log_deleted "Group of lines", deleted_gr unless deleted_gr == 0 + +        # Delete deprecated Networks +        deleted_ne = delete_deprecated(networks, Chouette::Network) +        log_deleted "Networks", deleted_ne unless deleted_ne == 0 + +        # Delete deprecated Lines +        deleted_li = delete_deprecated_lines(lines) +        log_deleted "Lines", deleted_li unless deleted_li == 0 + +        # Delete deprecated Operators +        deleted_op = delete_deprecated(operators, Chouette::Company) +        log_deleted "Operators", deleted_op unless deleted_op == 0 +        { +          imported: operators.count + lines.count + networks.count + groups_of_lines.count, +          deleted: deleted_op + deleted_li + deleted_ne + deleted_gr +        }        end        def create_or_update_company(api_operator) | 
