<commit_before>class SupportController < ReportsController
  include ::Search
  skip_before_filter :check_reset_password, :check_eula, :check_username, :check_organization, :set_active_resource_headers
  before_filter :clear_active_resource_headers
  before_filter :authorize

  def show
  end

  def announcements
    @announcements = MiscDoc.announcements
    if request.put?
      if @announcements.update_attribute(:document, params[:document])
        flash[:notice] = 'Updated announcements.'
      else
        flash[:error] = "Could not update announcements: #{@announcements.errors.full_messages.join('; ')}"
      end
    else
      @announcements.document ||= {:notice => nil, :critical => nil}
    end

    respond_to do |format|
      format.html
      format.js
      format.json {render :json => @announcements}
    end
  end

  def compute_report
  end

  def organizations
    page_size = (params[:size].presence || (request.format.json? ? 100 : 9999999)).to_i
    offset    = (params[:offset].presence || 0).to_i
    sort      = params[:sort].presence || 'organizations.name ASC'
    name      = params[:name]

    scope = Organization
    scope = scope.where('organizations.name ILIKE ?', "%#{name}%") if name.present?

    total = scope.count
    if total > 0
      scope = scope.limit(page_size).offset(offset).order(sort)
      @organizations = scope.all

      if @organizations.present?
        org_owners = Cms::Ci.list(@organizations.map(&:cms_id)).to_map_with_value {|o| [o['ciId'], o['ciAttributes']['owner']]}

        counts = Organization.
          select('organizations.id').
          select('count(distinct teams.id) as team_count').
          select('count(distinct teams_users.user_id) as team_user_count').
          select('count(distinct groups_teams.group_id) as group_count').
          select('count(distinct group_members.user_id) as group_user_count').
          from("(#{scope.to_sql}) AS organizations").
          joins(:teams).
          joins('LEFT OUTER JOIN teams_users ON teams.id = teams_users.team_id').
          joins('LEFT OUTER JOIN groups_teams ON teams.id = groups_teams.team_id').
          joins('LEFT OUTER JOIN group_members ON groups_teams.group_id = group_members.group_id').
          group('organizations.id').all.to_map(&:id)


        admin_counts = Organization.
          select('organizations.id').
          select('count(teams_users.user_id) as admin_user_count').
          from("(#{scope.to_sql}) AS organizations").
          joins(:admin_users).
          group('organizations.id').all.to_map(&:id)

        admin_group_counts = Organization.
          select('organizations.id').
          select('count(distinct group_members.user_id) as admin_group_user_count').
          select('count(distinct group_members.group_id) as admin_group_count').
          from("(#{scope.to_sql}) AS organizations").
          joins(:admin_group_users).
          group('organizations.id').all.to_map(&:id)

        conditions = {'ciClassName.keyword' => '*.Compute'}
        conditions['nsPath' => name] if name.present?
        ci_counts = Search::Ci.compute_count_stats_by_org(conditions) || {}

        @organizations = @organizations.map do |o|
          org_counts             = counts[o.id]
          org_admin_counts       = admin_counts[o.id]
          org_admin_group_counts = admin_group_counts[o.id]
          org_ci_counts          = ci_counts[o.name]
          r                      = o.as_json

          r['owner']                  = org_owners[o.cms_id]
          r['team_count']             = org_counts.team_count
          r['team_user_count']        = org_counts.team_user_count
          r['group_count']            = org_counts.group_count
          r['group_user_count']       = org_counts.group_user_count
          r['admin_user_count']       = org_admin_counts ? org_admin_counts.admin_user_count : 0
          r['admin_group_count']      = org_admin_group_counts ? org_admin_group_counts.admin_group_count : 0
          r['admin_group_user_count'] = org_admin_group_counts ? org_admin_group_counts.admin_group_user_count : 0

          if org_ci_counts.present?
            [:assembly, :environment, :compute].each do |key|
              r["#{key}_count"]      = org_ci_counts[:all][key]
              r["prod_#{key}_count"] = org_ci_counts[:prod][key]
            end
          end
          r
        end
      end
    else
      @organizations = []
    end

    respond_to do |format|
      format.html

      format.js

      format.json do
        add_pagination_response_headers(total, @organizations.size, offset)
        render :json => @organizations.to_json(:methods => :owner)
      end

      format.any do
        add_pagination_response_headers(total, @organizations.size, offset)
        keys = %w(id name full_name created_at owner team_count team_user_count group_count group_user_count admin_user_count admin_group_count admin_group_user_count assembly_count prod_assembly_count environment_count prod_environment_count compute_count prod_compute_count)
        delimiter = params[:delimiter].presence || ','
        csv = keys.join(delimiter) << "\n"
        @organizations.each do |o|
          %w(id name full_name created_at owner).each {|f| o[f] = %()}
          o['full_name'] = %("#{o['full_name']}")
          csv << keys.inject([]) {|a, k| a << o[k]}.join(delimiter) << "\n"
        end
        render :text => csv<commit_msg>Fix for CSV version of org report (in support).<commit_after>class SupportController < ReportsController
  include ::Search
  skip_before_filter :check_reset_password, :check_eula, :check_username, :check_organization, :set_active_resource_headers
  before_filter :clear_active_resource_headers
  before_filter :authorize

  def show
  end

  def announcements
    @announcements = MiscDoc.announcements
    if request.put?
      if @announcements.update_attribute(:document, params[:document])
        flash[:notice] = 'Updated announcements.'
      else
        flash[:error] = "Could not update announcements: #{@announcements.errors.full_messages.join('; ')}"
      end
    else
      @announcements.document ||= {:notice => nil, :critical => nil}
    end

    respond_to do |format|
      format.html
      format.js
      format.json {render :json => @announcements}
    end
  end

  def compute_report
  end

  def organizations
    page_size = (params[:size].presence || (request.format.json? ? 100 : 9999999)).to_i
    offset    = (params[:offset].presence || 0).to_i
    sort      = params[:sort].presence || 'organizations.name ASC'
    name      = params[:name]

    scope = Organization
    scope = scope.where('organizations.name ILIKE ?', "%#{name}%") if name.present?

    total = scope.count
    if total > 0
      scope = scope.limit(page_size).offset(offset).order(sort)
      @organizations = scope.all

      if @organizations.present?
        org_owners = Cms::Ci.list(@organizations.map(&:cms_id)).to_map_with_value {|o| [o['ciId'], o['ciAttributes']['owner']]}

        counts = Organization.
          select('organizations.id').
          select('count(distinct teams.id) as team_count').
          select('count(distinct teams_users.user_id) as team_user_count').
          select('count(distinct groups_teams.group_id) as group_count').
          select('count(distinct group_members.user_id) as group_user_count').
          from("(#{scope.to_sql}) AS organizations").
          joins(:teams).
          joins('LEFT OUTER JOIN teams_users ON teams.id = teams_users.team_id').
          joins('LEFT OUTER JOIN groups_teams ON teams.id = groups_teams.team_id').
          joins('LEFT OUTER JOIN group_members ON groups_teams.group_id = group_members.group_id').
          group('organizations.id').all.to_map(&:id)


        admin_counts = Organization.
          select('organizations.id').
          select('count(teams_users.user_id) as admin_user_count').
          from("(#{scope.to_sql}) AS organizations").
          joins(:admin_users).
          group('organizations.id').all.to_map(&:id)

        admin_group_counts = Organization.
          select('organizations.id').
          select('count(distinct group_members.user_id) as admin_group_user_count').
          select('count(distinct group_members.group_id) as admin_group_count').
          from("(#{scope.to_sql}) AS organizations").
          joins(:admin_group_users).
          group('organizations.id').all.to_map(&:id)

        conditions = {'ciClassName.keyword' => '*.Compute'}
        conditions['nsPath' => name] if name.present?
        ci_counts = Search::Ci.compute_count_stats_by_org(conditions) || {}

        @organizations = @organizations.map do |o|
          org_counts             = counts[o.id]
          org_admin_counts       = admin_counts[o.id]
          org_admin_group_counts = admin_group_counts[o.id]
          org_ci_counts          = ci_counts[o.name]
          r                      = o.as_json

          r['owner']                  = org_owners[o.cms_id]
          r['team_count']             = org_counts.team_count
          r['team_user_count']        = org_counts.team_user_count
          r['group_count']            = org_counts.group_count
          r['group_user_count']       = org_counts.group_user_count
          r['admin_user_count']       = org_admin_counts ? org_admin_counts.admin_user_count : 0
          r['admin_group_count']      = org_admin_group_counts ? org_admin_group_counts.admin_group_count : 0
          r['admin_group_user_count'] = org_admin_group_counts ? org_admin_group_counts.admin_group_user_count : 0

          if org_ci_counts.present?
            [:assembly, :environment, :compute].each do |key|
              r["#{key}_count"]      = org_ci_counts[:all][key]
              r["prod_#{key}_count"] = org_ci_counts[:prod][key]
            end
          end
          r
        end
      end
    else
      @organizations = []
    end

    respond_to do |format|
      format.html

      format.js

      format.json do
        add_pagination_response_headers(total, @organizations.size, offset)
        render :json => @organizations.to_json(:methods => :owner)
      end

      format.any do
        add_pagination_response_headers(total, @organizations.size, offset)
        keys = %w(id name full_name created_at owner team_count team_user_count group_count group_user_count admin_user_count admin_group_count admin_group_user_count assembly_count prod_assembly_count environment_count prod_environment_count compute_count prod_compute_count)
        delimiter = params[:delimiter].presence || ','
        csv = keys.join(delimiter) << "\n"
        @organizations.each do |o|
          %w(id name full_name created_at owner).each {|f| o[f] = %("#{o[f]}")}
          csv << keys.inject([]) {|a, k| a << o[k]}.join(delimiter) << "\n"
        end
        render :text => csv   #, :content_type => 'text/data_string'
{'fraction_non_alphanumeric': 0.11742632253340655, 'fraction_numerical': 0.0027457440966501922, 'mean_word_length': 2.742123287671233, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4545902', 'n_tokens_mistral': 3405, 'n_tokens_neox': 3304, 'n_words': 612}