<commit_before>require "csv"

module Houston
  module Feedback
    class ProjectFeedbackController < ApplicationController
      attr_reader :project, :comments
      
      layout "houston/feedback/application"
      before_filter :find_project
      
      COMMON_SURVEY_FIELDS_TO_IGNORE = [
        "Time Started",
        "Date Submitted",
        "Status",
        "Language",
        "Referer",
        "Extended Referer",
        "SessionID",
        "User Agent",
        "Extended User Agent",
        "Longitude",
        "Latitude"
      ].freeze
      
      COMMON_SURVEY_RESPONSES_TO_IGNORE = [
        "",
        "Yes",
        "No"
      ].freeze
      
      def index
        authorize! :read, Comment
        
        @q = params.fetch(:q, "-#no -#addressed -#invalid")
        @comments = Comment \
          .for_project(project)
          .includes(:user)
          .with_flags_for(current_user)
          .search(@q)
        
        respond_to do |format|
          format.json do
            render json: CommentPresenter.new(current_ability, comments)
          end
          format.html do
            @tags = Comment.for_project(project).tags
          end
          format.xlsx do
            send_data CommentExcelPresenter.new(project, params[:q], comments),
              type: :xlsx,
              filename: "Comments.xlsx",
              disposition: "attachment"
          end
        end
      end
      
      def create
        comment = Comment.new(params.pick(:customer, :text, :tags))
        comment.project = project
        comment.user = current_user
        
        authorize! :create, comment
        
        if comment.save
          comment.read_by! current_user
          render json: CommentPresenter.new(current_ability, comment)
        else
          render json: comment.errors, status: :unprocessable_entity
        end
      end
      
      def upload_csv
        authorize! :create, Comment
        
        @target = params[:target]
        session[:csv_path] = params[:file].tempfile.path
        
        csv = CSV.open(session[:csv_path]).to_a
        headings = []
        csv.shift.each_with_index do |heading, i|
          next if COMMON_SURVEY_FIELDS_TO_IGNORE.member?(heading)
          next if csv.all? { |row| COMMON_SURVEY_RESPONSES_TO_IGNORE.member?(row[i]) }
          example = csv.lazy.map { |row| row[i] }.find { |value| !value.blank? }
          headings.push(text: heading, index: i, example: example)
        end
        
        @data = {
          headings: headings,
          filename: params[:file].original_filename }
        render layout: false
      end
      
      def import
        authorize! :create, Comment
        
        customer_fields = params.fetch(:customer_fields, []).map(&:to_i)
        feedback_fields = params.fetch(:feedback_fields, []).map(&:to_i)
        tags = params.fetch(:tags, [])
        
        import = SecureRandom.hex(16) # generates a 32-character string, naturally
        csv = CSV.open(session[:csv_path]).to_a
        comments = []
        headings = csv.shift
        csv.each do |row|
          customer = row.values_at(*customer_fields).map(&:strip).reject(&:blank?).join(", ")
          next if customer.blank?
          
          feedback_fields.each do |i|
            feedback, question = row[i], headings[i]
            next if feedback.blank?
            <commit_msg>[skip] Skipped showing survey headers for columns that contain only Likert values (8m)<commit_after>require "csv"

module Houston
  module Feedback
    class ProjectFeedbackController < ApplicationController
      attr_reader :project, :comments
      
      layout "houston/feedback/application"
      before_filter :find_project
      
      COMMON_SURVEY_FIELDS_TO_IGNORE = [
        "Time Started",
        "Date Submitted",
        "Status",
        "Language",
        "Referer",
        "Extended Referer",
        "SessionID",
        "User Agent",
        "Extended User Agent",
        "Longitude",
        "Latitude"
      ].freeze
      
      COMMON_SURVEY_RESPONSES_TO_IGNORE = [
        "",
        "Yes",
        "No",
        "Very dissatisfied",
        "Somewhat dissatisfied",
        "Somewhat satisfied",
        "Very satisfied",
        "Very unlikely",
        "Somewhat unlikely",
        "Somewhat likely",
        "Very likely"
      ].freeze
      
      def index
        authorize! :read, Comment
        
        @q = params.fetch(:q, "-#no -#addressed -#invalid")
        @comments = Comment \
          .for_project(project)
          .includes(:user)
          .with_flags_for(current_user)
          .search(@q)
        
        respond_to do |format|
          format.json do
            render json: CommentPresenter.new(current_ability, comments)
          end
          format.html do
            @tags = Comment.for_project(project).tags
          end
          format.xlsx do
            send_data CommentExcelPresenter.new(project, params[:q], comments),
              type: :xlsx,
              filename: "Comments.xlsx",
              disposition: "attachment"
          end
        end
      end
      
      def create
        comment = Comment.new(params.pick(:customer, :text, :tags))
        comment.project = project
        comment.user = current_user
        
        authorize! :create, comment
        
        if comment.save
          comment.read_by! current_user
          render json: CommentPresenter.new(current_ability, comment)
        else
          render json: comment.errors, status: :unprocessable_entity
        end
      end
      
      def upload_csv
        authorize! :create, Comment
        
        @target = params[:target]
        session[:csv_path] = params[:file].tempfile.path
        
        csv = CSV.open(session[:csv_path]).to_a
        headings = []
        csv.shift.each_with_index do |heading, i|
          next if COMMON_SURVEY_FIELDS_TO_IGNORE.member?(heading)
          next if csv.all? { |row| COMMON_SURVEY_RESPONSES_TO_IGNORE.member?(row[i].to_s.strip) }
          example = csv.lazy.map { |row| row[i] }.find { |value| !value.blank? }
          headings.push(text: heading, index: i, example: example)
        end
        
        @data = {
          headings: headings,
          filename: params[:file].original_filename }
        render layout: false
      end
      
      def import
        authorize! :create, Comment
        
        customer_fields = params.fetch(:customer_fields, []).map(&:to_i)
        feedback_fields = params.fetch(:feedback_fields, []).map(&:to_i)
        tags = params.fetch(:tags, [])
        
        import = SecureRandom.hex(16) # generates a 32-character string, naturally
        csv = CSV.open(session[:csv_path]).to_a
        comments = []
        headings = csv.shift
        csv.each do |row|
          customer = row.values_at(*customer_fields).map(&:strip).reject(&:blank?).join(", ")
          next if customer.blank?
          
          feedback_fields.each do |i|
            feedback, question = row[i].to_s.strip, headings[i]
            next if feedback.blank?
            
{'fraction_non_alphanumeric': 0.10183756487585917, 'fraction_numerical': 0.001262449151353626, 'mean_word_length': 1.8015717092337917, 'pattern_counts': {'":': 0, '<': 5, '<?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': '256646', 'n_tokens_mistral': 2083, 'n_tokens_neox': 2031, 'n_words': 436}