diff options
| author | Xinhui | 2016-11-18 16:50:50 +0100 | 
|---|---|---|
| committer | Xinhui | 2016-11-18 16:51:15 +0100 | 
| commit | 9ed408bf89e9de1fdd17bfdcb40f0e2b5616de19 (patch) | |
| tree | f028c4c7e14f254aede86528c2bb2f44b87d46f2 /lib/stif/codif_line_synchronization.rb | |
| parent | 10869063be27b5e0d64da62fa09ed31b26782486 (diff) | |
| download | chouette-core-9ed408bf89e9de1fdd17bfdcb40f0e2b5616de19.tar.bz2 | |
Refactoring reflex & codifligne, add updated count message log
Refs #1981
Diffstat (limited to 'lib/stif/codif_line_synchronization.rb')
| -rw-r--r-- | lib/stif/codif_line_synchronization.rb | 40 | 
1 files changed, 33 insertions, 7 deletions
| 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 | 
