<commit_before>module BeaconControl
  module KontaktIoExtension
    class MappingService
      # @param [Admin] current_admin
      def initialize(current_admin)
        @admin = current_admin
      end

      # @param [Hash<String, Array<String>>] params
      def sync!(params)
        ActiveRecord::Base.transaction do
          sync_venues!(params)
          sync_beacons!(params)
        end
      end

      # @param [Hash<String, Array<String>>] params
      def sync_venues!(params)
        fetch_options!(params)
        venues.each do |data|
          zone = get_zone(data)
          if zone.kontakt_io_mapping.blank?
            zone.build_kontakt_io_mapping(kontakt_uid: data.id)
          end
          if !data.db? || update?
            zone.account = admin.account
            zone.save!
          end
        end
      end

      # @param [Hash<String, Array<String>>] params
      def sync_beacons!(params)
        fetch_options!(params)
        beacons.each do |data|
          beacon = get_beacon(data)
          if beacon.kontakt_io_mapping.blank?
            beacon.build_kontakt_io_mapping(kontakt_uid: data.unique_id)
          end
          if !data.db? || update?
            beacon.save!
            assign_to_zone!(data, beacon)
          end
        end
      end

      # private
      attr_reader :admin, :selected_beacons, :selected_venues

      # @param [::KontaktIo::Resource::Zone] data
      # @return [::Zone]
      def get_zone(data)
        find_zone(data) || build_zone(data)
      end

      # @param [::KontaktIo::Resource::Zone] data
      # @return [::Zone]
      def find_zone(data)
        ::Zone.kontakt_io.merge(KontaktIoMapping.with_uid(data.id)).first
      end

      # @param [::KontaktIo::Resource::Zone] data
      # @return [::Zone]
      def build_zone(data)
        ::Zone.new(name: data.name)
      end

      # @param [KontaktIo::Resource::Beacon] kontakt_beacon
      # @return [::Beacon]
      def get_beacon(kontakt_beacon)
        find_beacon(kontakt_beacon) || build_beacon(kontakt_beacon)
      end

      # @param [KontaktIo::Resource::Beacon] kontakt_beacon
      # @return [::Beacon]
      def find_beacon(kontakt_beacon)
        ::Beacon.kontakt_io.merge(KontaktIoMapping.with_uid(kontakt_beacon.unique_id)).first
      end

      # @param [KontaktIo::Resource::Beacon] kontakt_beacon
      # @return [::Beacon]
      def build_beacon(kontakt_beacon)
        ::Beacon::Factory.new(
          admin,
          name:  "#{kontakt_beacon.name} #{kontakt_beacon.unique_id}",
          uuid:  kontakt_beacon.proximity,
          major: kontakt_beacon.major,
          instance: kontakt_beacon.instance_id,
          namespace: kontakt_beacon.namespace,
          url: kontakt_beacon.url,
          vendor: 'Kontakt',
          protocol: (kontakt_beacon.eddystone? ? 'Eddystone' : 'iBeacon')
        ).build
      end<commit_msg>BOS-FF: Fix sync<commit_after>module BeaconControl
  module KontaktIoExtension
    class MappingService
      # @param [Admin] current_admin
      def initialize(current_admin)
        @admin = current_admin
      end

      # @param [Hash<String, Array<String>>] params
      def sync!(params)
        ActiveRecord::Base.transaction do
          sync_venues!(params)
          sync_beacons!(params)
        end
      end

      # @param [Hash<String, Array<String>>] params
      def sync_venues!(params)
        fetch_options!(params)
        venues.each do |data|
          zone = get_zone(data)
          if zone.kontakt_io_mapping.blank?
            zone.build_kontakt_io_mapping(kontakt_uid: data.id)
          end
          if !data.db? || update?
            zone.account = admin.account
            zone.save!
          end
        end
      end

      # @param [Hash<String, Array<String>>] params
      def sync_beacons!(params)
        fetch_options!(params)
        beacons.map do |data|
          beacon = get_beacon(data)
          update_beacon!(beacon, data) if update?
          if beacon.kontakt_io_mapping.blank?
            beacon.build_kontakt_io_mapping(kontakt_uid: data.unique_id)
          end
          if !data.db? || update?
            beacon.save!
            assign_to_zone!(data, beacon)
          end
          beacon
        end
      end

      # private
      attr_reader :admin, :selected_beacons, :selected_venues

      # @param [::KontaktIo::Resource::Zone] data
      # @return [::Zone]
      def get_zone(data)
        find_zone(data) || build_zone(data)
      end

      # @param [::KontaktIo::Resource::Zone] data
      # @return [::Zone]
      def find_zone(data)
        ::Zone.kontakt_io.merge(KontaktIoMapping.with_uid(data.id)).where(account_id: admin.account_id).first
      end

      # @param [::KontaktIo::Resource::Zone] data
      # @return [::Zone]
      def build_zone(data)
        ::Zone.new(name: data.name)
      end

      # @param [KontaktIo::Resource::Beacon] kontakt_beacon
      # @return [::Beacon]
      def get_beacon(kontakt_beacon)
        find_beacon(kontakt_beacon) || build_beacon(kontakt_beacon)
      end

      # @param [KontaktIo::Resource::Beacon] kontakt_beacon
      # @return [::Beacon]
      def find_beacon(kontakt_beacon)
        ::Beacon.kontakt_io.merge(KontaktIoMapping.with_uid(kontakt_beacon.unique_id)).where(account_id: admin.account_id).first
      end

      # @param [KontaktIo::Resource::Beacon] kontakt_beacon
      # @return [::Beacon]
      def build_beacon(kontakt_beacon)
        ::Beacon::Factory.new(
          admin,
          name:  "#{kontakt_beacon.name} #{kontakt_beacon.unique_id}",
          uuid:  kontakt_beacon.proximity,
          major: kontakt_beacon.major,
          minor: kontakt_beacon.minor,
          instance: kontakt_beacon.instance_id,
          namespace: kontakt_beacon.namespace,
          url: kontakt_beacon.url,
          vendor: 'Kontakt',
          protocol: (kontakt_beacon.eddystone? ? 'Eddystone' : 'iBeacon')
        ).build
      end

      def update_beacon!(beacon, kontakt_beacon)
        {
          name:  "#{kontakt_beacon.name} #{kontakt_beacon.unique_id}",
          uuid:  kontakt_beacon.proximity,
          major: kontakt_beacon.major,
          minor: kontakt_beacon.minor,
          instance: kontakt_beacon.instance_id,
          namespace: kontakt_beacon.namespace,
          url: kontakt_beacon.url,
          vendor: 'Kontakt',
          protocol: (kontakt_beacon.eddystone? ? 'Eddystone' : 'iBeacon')
        }.each_pair { |k, v| beacon.send("#{k}=", v) }
      end
{'fraction_non_alphanumeric': 0.10945655511206633, 'fraction_numerical': 0.0, 'mean_word_length': 2.292066700353714, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1701056', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2137, 'n_words': 362}