<commit_before>class WikiTools < Sinatra::Application
  CONTRIBS_FETCH_SIZE = 500
  CONTRIBS_PAGE_SIZE = 50

  namespace '/musikanimal' do
    before '/*' do
      params.delete_if { |_k, v| v == '' }
    end

    get '/nonautomated_edits' do
      namespace_id = params[:namespace].to_s.empty? ? nil : params[:namespace].to_i
      namespace_text = namespaces[namespace_id] || 'All'

      haml :nonautomated_edits, locals: {
        app_name: 'Nonautomated edit counter',
        namespace: namespace_id,
        namespaces: namespaces,
        namespace_text: namespace_text,
        username: params[:username]
      }
    end

    get '/nonautomated_edits/about' do
      haml :'nonautomated_edits/about', locals: {
        app_name: 'Nonautomated edit counter',
        tools: repl_client.getTools.to_a
      }
    end

    namespace '/api/nonautomated_edits' do
      get '' do
        content_type :json

        if params['username'].to_s.empty?
          status 400
          return {
            error: 'Bad request! username parameter is required'
          }.to_json
        end

        params['namespace'] = params['namespace'] == '' ? nil : params['namespace']

        unless params['totalCountOnly']
          count_data = repl_call(:countEdits, {
            username: params['username'],
            namespace: params['namespace'],
            nonAutomated: true,
            includeRedirects: !!params['redirects']
          }).to_i
        end

        if params['namespace']
          total_edits = repl_call(:countEdits, {
            username: params['username'],
            namespace: params['namespace']
          }).to_i
        else
          total_edits = repl_call(:countAllEdits, params['username']).to_i
        end

        res = {
          username: params['username'],
          namespace: params['namespace'],
          namespace_text: namespaces[params['namespace'].to_s.empty? ? nil : params['namespace'].to_i],
          total_count: total_edits,
          automated_count: ((total_edits - count_data) rescue nil),
          nonautomated_count: (count_data rescue nil)
        }

        if params['contribs']
          if total_edits > 50_000
            status 501
            return normalize_data(res.merge({
              contribs: [],
              error: 'Query too large! Unable to retrieve non-automated contributions. User has over 50,000 edits. Batch querying will be implemented soon.'
            }))
          else
            offset = params['offset'].to_i || 0
            range_offset = offset % CONTRIBS_FETCH_SIZE
            end_range_offset = range_offset + CONTRIBS_PAGE_SIZE - 1

            res[:contribs] = repl_call(:getEdits, {
              username: params['username'],
              namespace: params['namespace'],
              nonAutomated: true,
              offset: (offset / CONTRIBS_FETCH_SIZE.to_f).floor * CONTRIBS_FETCH_SIZE,
              limit: CONTRIBS_FETCH_SIZE
            })[range_offset.to_i..end_range_offset.to_i]
          end
        end

        status 200
        normalize_data(res)
      end

      get '/tools' do
        content_type :json

        res = repl_client.getTools

        status 200
        normalize_data(res)
      end

      get '/tools/:id' do<commit_msg>no .to_i, apparently<commit_after>class WikiTools < Sinatra::Application
  CONTRIBS_FETCH_SIZE = 500
  CONTRIBS_PAGE_SIZE = 50

  namespace '/musikanimal' do
    before '/*' do
      params.delete_if { |_k, v| v == '' }
    end

    get '/nonautomated_edits' do
      namespace_id = params[:namespace].to_s.empty? ? nil : params[:namespace].to_i
      namespace_text = namespaces[namespace_id] || 'All'

      haml :nonautomated_edits, locals: {
        app_name: 'Nonautomated edit counter',
        namespace: namespace_id,
        namespaces: namespaces,
        namespace_text: namespace_text,
        username: params[:username]
      }
    end

    get '/nonautomated_edits/about' do
      haml :'nonautomated_edits/about', locals: {
        app_name: 'Nonautomated edit counter',
        tools: repl_client.getTools.to_a
      }
    end

    namespace '/api/nonautomated_edits' do
      get '' do
        content_type :json

        if params['username'].to_s.empty?
          status 400
          return {
            error: 'Bad request! username parameter is required'
          }.to_json
        end

        params['namespace'] = params['namespace'] == '' ? nil : params['namespace']

        unless params['totalCountOnly']
          count_data = repl_call(:countEdits, {
            username: params['username'],
            namespace: params['namespace'],
            nonAutomated: true,
            includeRedirects: !!params['redirects']
          }).to_i
        end

        if params['namespace']
          total_edits = repl_call(:countEdits, {
            username: params['username'],
            namespace: params['namespace']
          }).to_i
        else
          total_edits = repl_call(:countAllEdits, params['username']).to_i
        end

        res = {
          username: params['username'],
          namespace: params['namespace'],
          namespace_text: namespaces[params['namespace'].to_s.empty? ? nil : params['namespace'].to_i],
          total_count: total_edits,
          automated_count: ((total_edits - count_data) rescue nil),
          nonautomated_count: (count_data rescue nil)
        }

        if params['contribs']
          if total_edits > 50_000
            status 501
            return normalize_data(res.merge({
              contribs: [],
              error: 'Query too large! Unable to retrieve non-automated contributions. User has over 50,000 edits. Batch querying will be implemented soon.'
            }))
          else
            offset = params['offset'].to_i || 0
            range_offset = offset % CONTRIBS_FETCH_SIZE
            end_range_offset = range_offset + CONTRIBS_PAGE_SIZE - 1

            res[:contribs] = repl_call(:getEdits, {
              username: params['username'],
              namespace: params['namespace'],
              nonAutomated: true,
              offset: (offset / CONTRIBS_FETCH_SIZE.to_f).floor * CONTRIBS_FETCH_SIZE,
              limit: CONTRIBS_FETCH_SIZE
            })[range_offset..end_range_offset]
          end
        end

        status 200
        normalize_data(res)
      end

      get '/tools' do
        content_type :json

        res = repl_client.getTools

        status 200
        normalize_data(res)
      end

      get '/tools/:id' do
{'fraction_non_alphanumeric': 0.10059623910717015, 'fraction_numerical': 0.008867145696376701, 'mean_word_length': 2.0399628252788102, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5803146', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1812, 'n_words': 411}