aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/line_referential_sync.rb4
-rw-r--r--app/views/line_referentials/show.html.slim2
-rw-r--r--config/locales/line_referentials.en.yml3
-rw-r--r--config/locales/line_referentials.fr.yml4
-rw-r--r--db/migrate/20160818090010_add_message_to_line_sync_operation.rb5
-rw-r--r--lib/stif/codif_line_synchronization.rb39
-rw-r--r--spec/models/line_referential_sync_spec.rb4
7 files changed, 43 insertions, 18 deletions
diff --git a/app/models/line_referential_sync.rb b/app/models/line_referential_sync.rb
index 163d87f8e..763936f3b 100644
--- a/app/models/line_referential_sync.rb
+++ b/app/models/line_referential_sync.rb
@@ -3,8 +3,8 @@ class LineReferentialSync < ActiveRecord::Base
has_many :line_sync_operations, dependent: :destroy
- def record_status status
- line_sync_operations << LineSyncOperation.new(status: status)
+ def record_status status, message
+ line_sync_operations << LineSyncOperation.new(status: status, message: message)
line_sync_operations.first.destroy while line_sync_operations.count > 30
end
end
diff --git a/app/views/line_referentials/show.html.slim b/app/views/line_referentials/show.html.slim
index 9e8d5b314..ddd82950a 100644
--- a/app/views/line_referentials/show.html.slim
+++ b/app/views/line_referentials/show.html.slim
@@ -18,7 +18,7 @@
ul.list-group width="75%"
- @line_referential.line_referential_sync.line_sync_operations.each do |sync|
- li = "#{sync.created_at} - #{sync.status}"
+ li = "#{sync.created_at} - #{sync.message}"
= semantic_form_for @line_referential, :url => line_referential_path do |form|
= form.inputs do
diff --git a/config/locales/line_referentials.en.yml b/config/locales/line_referentials.en.yml
index d1b164ea3..ad4cfa45c 100644
--- a/config/locales/line_referentials.en.yml
+++ b/config/locales/line_referentials.en.yml
@@ -1,4 +1,7 @@
en:
+ synchronization:
+ message: "Synchronization successful in %{time} seconds with %{imported} objects from Codifligne. %{deleted} objects were deleted."
+ failure: "Synchronization interrupted after %{time} seconds."
activerecord:
attributes:
line_referential:
diff --git a/config/locales/line_referentials.fr.yml b/config/locales/line_referentials.fr.yml
index 5b5e0419c..6378087d7 100644
--- a/config/locales/line_referentials.fr.yml
+++ b/config/locales/line_referentials.fr.yml
@@ -1,4 +1,8 @@
fr:
+ 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."
activerecord:
attributes:
line_referential:
diff --git a/db/migrate/20160818090010_add_message_to_line_sync_operation.rb b/db/migrate/20160818090010_add_message_to_line_sync_operation.rb
new file mode 100644
index 000000000..37b3e5af6
--- /dev/null
+++ b/db/migrate/20160818090010_add_message_to_line_sync_operation.rb
@@ -0,0 +1,5 @@
+class AddMessageToLineSyncOperation < ActiveRecord::Migration
+ def change
+ add_column :line_sync_operations, :message, :string
+ end
+end
diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb
index 65d82dc71..77b9d1c02 100644
--- a/lib/stif/codif_line_synchronization.rb
+++ b/lib/stif/codif_line_synchronization.rb
@@ -1,13 +1,14 @@
module Stif
module CodifLineSynchronization
class << self
- # Don't check last synchronizations date and lines last update if first_sync
- def synchronize first_sync = false
+ # Don't check last synchronizations if force_sync
+ def synchronize force_sync = false
# Check last synchronization and synchronization interval
date = DateTime.now.to_date - LineReferential.first.sync_interval.days
- last_sync = LineReferential.first.line_referential_sync.line_sync_operations.where(status: 'ok').last.try(:created_at)
- return if (last_sync.nil? || last_sync.to_date > date) && !first_sync
+ last_sync = LineReferential.first.line_referential_sync.line_sync_operations.where(status: :ok).last.try(:created_at)
+ return if last_sync.present? && last_sync.to_date > date && !force_sync
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
# TODO Check exceptions and status messages
begin
# Fetch Codifline data
@@ -22,15 +23,21 @@ module Stif
networks.map { |n| create_or_update_network(n) }
groups_of_lines.map { |g| create_or_update_group_of_lines(g) }
- delete_deprecated(operators, Chouette::Company)
- delete_deprecated_lines(lines)
- delete_deprecated(networks, Chouette::Network)
- delete_deprecated(groups_of_lines, Chouette::GroupOfLine)
+ total_deleted = delete_deprecated(operators, Chouette::Company)
+ total_deleted += delete_deprecated_lines(lines)
+ total_deleted += delete_deprecated(networks, Chouette::Network)
+ total_deleted += delete_deprecated(groups_of_lines, Chouette::GroupOfLine)
- LineReferential.first.line_referential_sync.record_status "OK"
+ # Building log message
+ total_codifligne_elements = operators.count + lines.count + networks.count + groups_of_lines.count
+ total_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - start_time
+
+ LineReferential.first.line_referential_sync.record_status :ok, I18n.t('synchronization.message.success', time: total_time, imported: total_codifligne_elements, deleted: total_deleted)
rescue Exception => e
- ap e.message
- LineReferential.first.line_referential_sync.record_status "Error"
+ Rails.logger.error "#{e} #{e.backtrace}"
+
+ total_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - start_time
+ LineReferential.first.line_referential_sync.record_status :ko, I18n.t('synchronization.message.failure', time: total_time)
end
end
@@ -100,12 +107,18 @@ module Stif
def delete_deprecated(objects, klass)
ids = objects.map{ |o| o.stif_id }.to_a
- klass.where.not(objectid: ids).destroy_all
+ deprecated = klass.where.not(objectid: ids)
+ count = deprecated.count
+ deprecated.destroy_all
+ count
end
def delete_deprecated_lines(lines)
ids = lines.map{ |l| l.stif_id }.to_a
- Chouette::Line.where.not(objectid: ids).map { |l| l.deactivated = true ; l.save }
+ deprecated = Chouette::Line.where.not(objectid: ids).where(deactivated: false)
+ count = deprecated.count
+ deprecated.map { |l| l.deactivated = true ; l.save }
+ count
end
def save_or_update(params, klass)
diff --git a/spec/models/line_referential_sync_spec.rb b/spec/models/line_referential_sync_spec.rb
index 8befc216a..a237f4d24 100644
--- a/spec/models/line_referential_sync_spec.rb
+++ b/spec/models/line_referential_sync_spec.rb
@@ -12,12 +12,12 @@ RSpec.describe LineReferentialSync, :type => :model do
let!(:line_ref_sync_with_records) { create(:line_referential_sync_with_record, line_sync_operations_count: 30) }
it 'should add a new record' do
- line_ref_sync.record_status "OK"
+ line_ref_sync.record_status :ok, "message"
expect(line_ref_sync.line_sync_operations.count).to eq(1)
end
it 'should not have more than 30 records' do
- line_ref_sync_with_records.record_status "OK"
+ line_ref_sync_with_records.record_status :ok, "message"
expect(line_ref_sync_with_records.line_sync_operations.count).to eq(30)
end
end