<commit_before>require 'ostruct'
require 'set'
require 'active_support/core_ext/hash/indifferent_access'

module PagerBot
  module Models
    class Collection
      attr_reader :list

      def initialize(list, model_class=nil)
        @list = list
        unless model_class.nil?
          @list = @list.map {|el| model_class.new(el)}
        end
        index!
      end

      def get(index_field_value)
        @index[normalize(index_field_value)]
      end

      def ambiguous_aliases
        seen = Set.new
        ambiguous = Set.new
        @list.each do |member|
          member.aliases.each do |alias_|
            norm_alias = normalize(alias_['name'])
            if seen.include?(norm_alias)
              ambiguous << norm_alias
            end
            seen << norm_alias
          end
        end
        ambiguous
      end

      def serializable_list
        @list.map { |member| member.to_h }
      end

      private
      def normalize(string)
        PagerBot::Utilities.normalize(string.to_s)
      end

      def index!
        @index = {}
        ambiguous = ambiguous_aliases
        unless ambiguous.empty?
          PagerBot.log.warn(
            "Skipping the following ambiguous aliases: #{ambiguous.to_a}")
        end

        @list.each do |member|
          add_index(member.id, member)
          member.aliases = member.aliases.select do |alias_|
            norm_alias = normalize(alias_['name'])
            unless ambiguous.include? norm_alias
              add_index(norm_alias, member)
              true
            end
          end
        end
      end

      def add_index(key, value)
        @index[normalize(key)] = value
      end
    end

    class ModelBase < OpenStruct
      def initialize(hash)
        hash = hash.with_indifferent_access
        hash.delete('_id')
        hash['aliases'] ||= []
        marshal_load hash
      end

      def to_json
        table.to_json
      end

      def to_h
        table
      end<commit_msg>Add emails as an index for pagerbot users.<commit_after>require 'ostruct'
require 'set'
require 'active_support/core_ext/hash/indifferent_access'

module PagerBot
  module Models
    class Collection
      attr_reader :list

      def initialize(list, model_class=nil)
        @list = list
        unless model_class.nil?
          @list = @list.map {|el| model_class.new(el)}
        end
        index!
      end

      def get(index_field_value)
        @index[normalize(index_field_value)]
      end

      def ambiguous_aliases
        seen = Set.new
        ambiguous = Set.new
        @list.each do |member|
          member.aliases.each do |alias_|
            norm_alias = normalize(alias_['name'])
            if seen.include?(norm_alias)
              ambiguous << norm_alias
            end
            seen << norm_alias
          end
        end
        ambiguous
      end

      def serializable_list
        @list.map { |member| member.to_h }
      end

      private
      def normalize(string)
        PagerBot::Utilities.normalize(string.to_s)
      end

      def index!
        @index = {}
        ambiguous = ambiguous_aliases
        unless ambiguous.empty?
          PagerBot.log.warn(
            "Skipping the following ambiguous aliases: #{ambiguous.to_a}")
        end

        @list.each do |member|
          add_index(member.id, member)

          # add email as an index for users
          if member.is_a?(User)
            norm_email = member.email.split('@').first
            if has_index?(norm_email)
              PagerBot.log.warn("Adding ambiguous email alias: #{norm_email} for #{member.email}")
            end
            add_index(norm_email, member)
          end

          member.aliases = member.aliases.select do |alias_|
            norm_alias = normalize(alias_['name'])
            unless ambiguous.include? norm_alias
              add_index(norm_alias, member)
              true
            end
          end
        end
      end

      def add_index(key, value)
        @index[normalize(key)] = value
      end

      def has_index?(key)
        @index.key?(normalize(key))
      end
    end

    class ModelBase < OpenStruct
      def initialize(hash)
        hash = hash.with_indifferent_access
        hash.delete('_id')
        hash['aliases'] ||= []
        marshal_load hash
      end

      def to_json
        table.to_json
      end

      def to_h
        table
      end
{'fraction_non_alphanumeric': 0.0805505415162455, 'fraction_numerical': 0.0, 'mean_word_length': 1.6866666666666668, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '5794295', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1259, 'n_words': 296}