<commit_before>class AmiEvent

  include Mongoid::Document

  field :target_call_id, type: String
  field :timestamp,      type: String
  field :name,           type: String
  field :headers,        type: Hash

  index(timestamp: 1)
  default_scope -> { asc(:timestamp) }

  ChannelRegex = /^SIP\/(\d+)/


  class << self

    # TODO The "data" is not an AmiEvent, but plain JSON.
    #      Should we convert it?
    #
    def handle_update(data)
      handle_agent_update(data) || handle_call_update(data)
    end


    def channel_is_active?(data)
      data['name'] == 'Newstate' && data['headers']['ChannelState'] == '5'
    end


    def get_peer_from(data)
      hdr = data['headers']

      if data['name'] == 'PeerStatus'
        hdr['Peer'][ChannelRegex, 1]
      elsif channel_is_active?(data)
        hdr['Channel'][ChannelRegex, 1]
      elsif data['name'] == 'Hangup'
        hdr['Channel'][ChannelRegex, 1]
      end
    end


    def handle_agent_update(data)
      if (peer = get_peer_from data)
        user = User.where(name: peer).first
        user.send_update_notification_to_clients if user
        return true
      end
    end


    def agent_takes_call?(data)
      hdr = data['headers']
      data['name'] == 'Newstate' && hdr['ChannelState'] == '6' &&
        !hdr['ConnectedLineNum'].blank?
    end


    def send_updates_for_call(data)
      yield_to_call(data) do |call|
        call.create_history_entry_for_mailbox
        call.send_update_notification_to_clients
      end
    end


    def create_history_for(data)
      yield_to_call(data) do |call|
        agent = data['headers']['Channel'][ChannelRegex, 1]
        call.create_customer_history_entry(agent)
      end
    end


    def yield_to_call(data, &block)
      if (tcid = data['target_call_id'])
        if (call = Call.find tcid)
          block.call(call)
          return tcid
        end
      end
    end


    def handle_call_update(data)
      if data['name'] == 'CallState'
        send_updates_for_call(data)
      elsif agent_takes_call?(data)
        create_history_for(data)
      end
    end
  end
end<commit_msg>keep ami events local to AHN<commit_after>class AmiEvent

  include Mongoid::Document

  field :target_call_id, type: String
  field :timestamp,      type: String
  field :name,           type: String
  field :headers,        type: Hash

  index(timestamp: 1)
  default_scope -> { asc(:timestamp) }


  class << self

    # TODO The "data" is not an AmiEvent, but plain JSON.
    #      Should we convert it?
    #
    def handle_update(data)
      handle_agent_update(data) || handle_call_update(data)
    end


    def get_agent_from(data)
      if data['name'] == 'AgentEvent'
        data['headers']['Extension']
      end
    end


    def handle_agent_update(data)
      if (agent = get_agent_from data)
        if user = User.where(name: agent).first
          create_history_for(data, agent)
          user.send_update_notification_to_clients
        end

        return true
      end
    end


    def agent_takes_call?(data)
      data['headers']['AgentUp']
    end


    def send_updates_for_call(data)
      yield_to_call(data) do |call|
        call.create_history_entry_for_mailbox
        call.send_update_notification_to_clients
      end
    end


    def create_history_for(data, agent)
      if agent_takes_call?(data)
        yield_to_call(data) do |call|
          call.create_customer_history_entry(agent)
        end
      end
    end


    def yield_to_call(data, &block)
      if (tcid = data['target_call_id'])
        if (call = Call.find tcid)
          block.call(call)
          return tcid
        end
      end
    end


    def handle_call_update(data)
      if data['name'] == 'CallState'
        send_updates_for_call(data)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09921259842519685, 'fraction_numerical': 0.002099737532808399, 'mean_word_length': 2.4026785714285714, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1966696', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1213, 'n_words': 275}