<commit_before>class Admin::SocialMediaController < Admin::AdminController
  helper_method :sort_column, :sort_direction, :current_page
  respond_to :html, :xml, :json, :csv, :xls

  before_action :user_has_agency
  before_action :set_outlet, only: [:show, :edit, :update, :destroy,
    :publish, :archive, :request_publish, :request_archive, :validate]

  # before_filter :require_admin, only: [:publish]
  # GET /outlets
  # GET /outlets.json
  def index
    if !current_user.admin?
      params[:status] = "Published"
    end
    respond_to do |format|
      format.html {
        @outlets = Outlet.includes(:official_tags,:agencies,:users).references(:official_tags,:agencies,:users)
        num_items = items_per_page_handler
        @total_outlets = @outlets.count
        if(params[:service] && !params[:service].blank?)
          @outlets = @outlets.where(service: params[:service])
        end
        @services = @outlets.group(:service).count
        @outlets = [] }
      format.json {
        @total_outlets = Outlet.count
        @outlets = Outlet.es_search(params, sort_column, sort_direction)
        @result_count = @outlets.total_count
        @outlets = @outlets.page(current_page).per(params["iDisplayLength"].to_i).results
      }
      format.csv {
        @outlets = Outlet.es_search(params, sort_column, sort_direction)
        @result_count = @outlets.total_count
        @outlets = @outlets.results
        render plain: @outlets.to_csv
      }
    end
  end

  def social_media_export
    if !params[:agency].blank?
      @outlets = Outlet.es_search(params, sort_column, sort_direction).per(1000).records
    else
      @outlets = Outlet.es_search(params, sort_column, sort_direction).per(1000).records
    end
    respond_to do |format|
      format.csv { render plain: @outlets.to_csv }
    end
  end

  def social_media_import
    @import = ""
  end

  def bulk_social_media_upload
    @import = params[:import]
  end

  def datatables<commit_msg>process csv and validate save<commit_after>class Admin::SocialMediaController < Admin::AdminController
  helper_method :sort_column, :sort_direction, :current_page
  respond_to :html, :xml, :json, :csv, :xls

  before_action :user_has_agency
  before_action :set_outlet, only: [:show, :edit, :update, :destroy,
    :publish, :archive, :request_publish, :request_archive, :validate]

  # before_filter :require_admin, only: [:publish]
  # GET /outlets
  # GET /outlets.json
  def index
    if !current_user.admin?
      params[:status] = "Published"
    end
    respond_to do |format|
      format.html {
        @outlets = Outlet.includes(:official_tags,:agencies,:users).references(:official_tags,:agencies,:users)
        num_items = items_per_page_handler
        @total_outlets = @outlets.count
        if(params[:service] && !params[:service].blank?)
          @outlets = @outlets.where(service: params[:service])
        end
        @services = @outlets.group(:service).count
        @outlets = [] }
      format.json {
        @total_outlets = Outlet.count
        @outlets = Outlet.es_search(params, sort_column, sort_direction)
        @result_count = @outlets.total_count
        @outlets = @outlets.page(current_page).per(params["iDisplayLength"].to_i).results
      }
      format.csv {
        @outlets = Outlet.es_search(params, sort_column, sort_direction)
        @result_count = @outlets.total_count
        @outlets = @outlets.results
        render plain: @outlets.to_csv
      }
    end
  end

  def social_media_export
    if !params[:agency].blank?
      @outlets = Outlet.es_search(params, sort_column, sort_direction).per(1000).records
    else
      @outlets = Outlet.es_search(params, sort_column, sort_direction).per(1000).records
    end
    respond_to do |format|
      format.csv { render plain: @outlets.to_csv }
    end
  end

  def social_media_import
    @import = ""
  end

  def bulk_social_media_upload
    import = params["import"]
    required_headers = ["service", "service_url", "organization", "short_description",
                       "language", "agency_tokens", "user_tokens"]
    unrequired_headers = ["long_description", "tag_tokens", "notes"]
    
    csv_data = CSV.read(import.tempfile, headers: true, header_converters: :symbol)
    csv_array = csv_data.map(&:to_h)
    invalid_count = 0
    csv_array.each do |obj|
      outlet = Outlet.new(obj)
      if not outlet.save
        invalid_count += 1
      else
        outlet.published!
      end
    end
    redirect_to social_media_import_admin_outlets_path, notice: "#{csv_array.length() - invalid_count} Social Media Accounts were successfully created and published." 
  end

  def datatables
{'fraction_non_alphanumeric': 0.11315508021390375, 'fraction_numerical': 0.0038502673796791446, 'mean_word_length': 3.3096774193548386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1453321', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1472, 'n_words': 292}