<commit_before># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

class Chat < ApplicationModel
  validates :name, presence: true
  store     :preferences

  def customer_state(session_id = nil)

    # reconnect
    if session_id
      chat_session = Chat::Session.find_by(session_id: session_id, state: %w(waiting running))

      if chat_session
        if chat_session.state == 'running'
          user = nil
          if chat_session.user_id
            chat_user = User.find(chat_session.user_id)
            url = nil
            if chat_user.image && chat_user.image != 'none'
              url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/users/image/#{chat_user.image}"
            end
            user = {
              name: chat_user.fullname,
              avatar: url,
            }

            # get queue postion if needed
            session = Chat::Session.messages_by_session_id(session_id)
            if session
              return {
                state: 'reconnect',
                session: session,
                agent: user,
              }
            end
          end
        elsif chat_session.state == 'waiting'
          return {
            state: 'reconnect',
            position: chat_session.position,
          }
        end
      end
    end

    # check if agents are available
    available_agents = Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - 2.minutes).count
    if available_agents == 0
      return { state: 'offline' }
    end

    # if all seads are used
    if Chat.waiting_chat_count >= max_queue
      return {
        state: 'no_seats_available',
        queue: Chat.waiting_chat_count,
      }
    end

    # seads are available
    { state: 'online' }
  end

  def self.agent_state(user_id)
    assets = {}
    Chat.where(active: true).each {|chat|
      assets = chat.assets(assets)
    }
    {
      waiting_chat_count: waiting_chat_count,
      running_chat_count: running_chat_count,
      active_sessions: Chat::Session.active_chats_by_user_id(user_id),
      active_agents: active_agents,
      seads_available: seads_available,
      seads_total: seads_total,
      active: Chat::Agent.state(user_id),
      assets: assets,
    }
  end

  def self.waiting_chat_count
    Chat::Session.where(state: ['waiting']).count
  end

  def self.running_chat_count
    Chat::Session.where(state: ['running']).count
  end

  def self.active_chat_count
    Chat::Session.where(state: %w(waiting running)).count<commit_msg>Revert "Removed not longer needed checks."<commit_after># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

class Chat < ApplicationModel
  validates :name, presence: true
  store     :preferences

  def customer_state(session_id = nil)
    return { state: 'chat_disabled' } if !Setting.get('chat')

    # reconnect
    if session_id
      chat_session = Chat::Session.find_by(session_id: session_id, state: %w(waiting running))

      if chat_session
        if chat_session.state == 'running'
          user = nil
          if chat_session.user_id
            chat_user = User.find(chat_session.user_id)
            url = nil
            if chat_user.image && chat_user.image != 'none'
              url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/users/image/#{chat_user.image}"
            end
            user = {
              name: chat_user.fullname,
              avatar: url,
            }

            # get queue postion if needed
            session = Chat::Session.messages_by_session_id(session_id)
            if session
              return {
                state: 'reconnect',
                session: session,
                agent: user,
              }
            end
          end
        elsif chat_session.state == 'waiting'
          return {
            state: 'reconnect',
            position: chat_session.position,
          }
        end
      end
    end

    # check if agents are available
    available_agents = Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - 2.minutes).count
    if available_agents == 0
      return { state: 'offline' }
    end

    # if all seads are used
    if Chat.waiting_chat_count >= max_queue
      return {
        state: 'no_seats_available',
        queue: Chat.waiting_chat_count,
      }
    end

    # seads are available
    { state: 'online' }
  end

  def self.agent_state(user_id)
    return { state: 'chat_disabled' } if !Setting.get('chat')
    assets = {}
    Chat.where(active: true).each {|chat|
      assets = chat.assets(assets)
    }
    {
      waiting_chat_count: waiting_chat_count,
      running_chat_count: running_chat_count,
      active_sessions: Chat::Session.active_chats_by_user_id(user_id),
      active_agents: active_agents,
      seads_available: seads_available,
      seads_total: seads_total,
      active: Chat::Agent.state(user_id),
      assets: assets,
    }
  end

  def self.waiting_chat_count
    Chat::Session.where(state: ['waiting']).count
  end

  def self.running_chat_count
    Chat::Session.where(state: ['running']).count
  end

  def self.active_chat_count
    Chat::Session.where(state: %w(waiting running)).count
{'fraction_non_alphanumeric': 0.10204081632653061, 'fraction_numerical': 0.004196070951745184, 'mean_word_length': 2.1724137931034484, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7203749', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1576, 'n_words': 332}