<commit_before>module DclgStatsSelector
  class SelectorsController < PublishMyData::ApplicationController
    class InvalidCSVUploadError < StandardError; end

    rescue_from InvalidCSVUploadError, with: :invalid_upload
    rescue_from GeographyService::TooManyGSSCodesError, with: :too_many_gss_codes
    rescue_from GeographyService::TooManyGSSCodeTypesError, with: :mixed_gss_codes

    def new
    end

    def preview
      geography_service = GeographyService.new

      # The error handling used to live in the Selector, and I've preserved the
      # use of rescue_from for now, hence catching and raising an error within
      # the controller
      gss_code_candidates =
        begin
          CSV.read(params[:csv_upload].path).map(&:first)
        rescue ArgumentError
          raise InvalidCSVUploadError, "file upload does not contain .csv data"
        end

      data = geography_service.uris_and_geography_type_for_gss_codes(gss_code_candidates)

      @gss_resource_uris  = data.fetch(:gss_resource_uris)
      @non_gss_codes      = data.fetch(:non_gss_codes)
      @geography_type     = data.fetch(:geography_type)

      @gss_resource_uri_data = @gss_resource_uris.join(', ')

      respond_to do |format|
        format.html
      end
    end

    def create
      gss_resource_uris = params[:gss_resource_uri_data].split(', ')

      @selector = Selector.create(
        geography_type: params[:geography_type],
        row_uris:       gss_resource_uris
      )

      redirect_to selector_path(@selector)
    end

    def show
      @selector = Selector.find(params[:id])
      @snapshot = @selector.build_snapshot(row_limit: 20)
    end

    def download
      selector = Selector.find(params[:id])
      snapshot = selector.build_snapshot
      filename = "statistics"
      source_url = "[source link not currently stored]" # selector_url(selector)
      output_builder = CSVBuilder.build(
        site_name:  PublishMyData.local_domain,
        source_url: source_url,
        timestamp:  Time.now
      )
      snapshot.render(output_builder)

      response.headers['Content-Type'] = 'text/csv'
      response.headers['Content-Disposition'] = %'attachment; filename="#{filename}.csv"'
      render(text: output_builder.to_csv)
    end

    private

    def invalid_upload
      flash.now[:error] = 'The uploaded file did not contain valid CSV data, please check and try again.'
      render :new
    end

    def mixed_gss_codes
      flash.now[:error] = 'The uploaded file should contain GSS codes at either LSOA or Local Authority level.'
      render :new
    end<commit_msg>Use a smaller number of preview rows to fit on one screen<commit_after>module DclgStatsSelector
  class SelectorsController < PublishMyData::ApplicationController
    class InvalidCSVUploadError < StandardError; end

    rescue_from InvalidCSVUploadError, with: :invalid_upload
    rescue_from GeographyService::TooManyGSSCodesError, with: :too_many_gss_codes
    rescue_from GeographyService::TooManyGSSCodeTypesError, with: :mixed_gss_codes

    def new
    end

    def preview
      geography_service = GeographyService.new

      # The error handling used to live in the Selector, and I've preserved the
      # use of rescue_from for now, hence catching and raising an error within
      # the controller
      gss_code_candidates =
        begin
          CSV.read(params[:csv_upload].path).map(&:first)
        rescue ArgumentError
          raise InvalidCSVUploadError, "file upload does not contain .csv data"
        end

      data = geography_service.uris_and_geography_type_for_gss_codes(gss_code_candidates)

      @gss_resource_uris  = data.fetch(:gss_resource_uris)
      @non_gss_codes      = data.fetch(:non_gss_codes)
      @geography_type     = data.fetch(:geography_type)

      @gss_resource_uri_data = @gss_resource_uris.join(', ')

      respond_to do |format|
        format.html
      end
    end

    def create
      gss_resource_uris = params[:gss_resource_uri_data].split(', ')

      @selector = Selector.create(
        geography_type: params[:geography_type],
        row_uris:       gss_resource_uris
      )

      redirect_to selector_path(@selector)
    end

    def show
      @selector = Selector.find(params[:id])
      @snapshot = @selector.build_snapshot(row_limit: 7)
    end

    def download
      selector = Selector.find(params[:id])
      snapshot = selector.build_snapshot
      filename = "statistics"
      source_url = "[source link not currently stored]" # selector_url(selector)
      output_builder = CSVBuilder.build(
        site_name:  PublishMyData.local_domain,
        source_url: source_url,
        timestamp:  Time.now
      )
      snapshot.render(output_builder)

      response.headers['Content-Type'] = 'text/csv'
      response.headers['Content-Disposition'] = %'attachment; filename="#{filename}.csv"'
      render(text: output_builder.to_csv)
    end

    private

    def invalid_upload
      flash.now[:error] = 'The uploaded file did not contain valid CSV data, please check and try again.'
      render :new
    end

    def mixed_gss_codes
      flash.now[:error] = 'The uploaded file should contain GSS codes at either LSOA or Local Authority level.'
      render :new
    end
{'fraction_non_alphanumeric': 0.07749287749287749, 'fraction_numerical': 0.0005698005698005698, 'mean_word_length': 3.0885093167701863, 'pattern_counts': {'":': 0, '<': 7, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1887488', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1547, 'n_words': 370}