From 9ed408bf89e9de1fdd17bfdcb40f0e2b5616de19 Mon Sep 17 00:00:00 2001 From: Xinhui Date: Fri, 18 Nov 2016 16:50:50 +0100 Subject: Refactoring reflex & codifligne, add updated count message log Refs #1981 --- lib/stif/codif_line_synchronization.rb | 40 ++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'lib/stif/codif_line_synchronization.rb') diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb index deadad5ba..600415424 100644 --- a/lib/stif/codif_line_synchronization.rb +++ b/lib/stif/codif_line_synchronization.rb @@ -1,7 +1,28 @@ module Stif module CodifLineSynchronization class << self + attr_accessor :imported_count, :updated_count, :deleted_count + + def reset_counts + self.imported_count = 0 + self.updated_count = 0 + self.deleted_count = 0 + end + + def processed_counts + { + imported: self.imported_count, + updated: self.updated_count, + deleted: self.deleted_count + } + end + + def increment_counts prop_name, value + self.send("#{prop_name}=", self.send(prop_name) + value) + end + def synchronize + self.reset_counts start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) # Fetch Codifline data client = Codifligne::API.new @@ -47,10 +68,8 @@ module Stif # 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, - deleted: deleted_op + deleted_li + deleted_ne - } + + self.processed_counts end def create_or_update_company(api_operator) @@ -118,13 +137,14 @@ module Stif def delete_deprecated(objects, klass) ids = objects.map{ |o| o.stif_id }.to_a deprecated = klass.where.not(objectid: ids) - deprecated.destroy_all.length + increment_counts :deleted_count, deprecated.destroy_all.length end def delete_deprecated_lines(lines) ids = lines.map{ |l| l.stif_id }.to_a deprecated = Chouette::Line.where.not(objectid: ids).where(deactivated: false) deprecated.update_all deactivated: true + increment_counts :deleted_count, deprecated.update_all(deactivated: true) end def save_or_update(params, klass) @@ -132,10 +152,16 @@ module Stif object = klass.where(objectid: params[:objectid]).first if object object.assign_attributes(params) - object.save if object.changed? + if object.changed? + object.save + increment_counts :updated_count, 1 + end else object = klass.new(params) - object.save if object.valid? + if object.valid? + object.save + increment_counts :imported_count, 1 + end end object end -- cgit v1.2.3