<commit_before>require 'travis/api/app'

class Travis::Api::App
  class Endpoint
    class Jobs < Endpoint
      get '/' do
        respond_with service(:find_jobs, params)
      end

      get '/:id' do
        respond_with service(:find_job, params)
      end

      get '/:job_id/log' do
        resource = service(:find_log, params).run
        if !resource || resource.archived?
          archived_log_path = archive_url("/jobs/#{params[:job_id]}/log.txt")

          if params[:cors_hax]
            status 204
            headers['Access-Control-Expose-Headers'] = 'Location'
            headers['Location'] = archived_log_path
          else
            redirect archived_log_path, 307
          end
        else
          respond_with resource
        end
      end

      def archive_url(path)
        "https://s3.amazonaws.com/#{hostname('archive')}#{path}"
      end

      def hostname(name)
        "#{name}#{'-staging' if Travis.env == 'staging'}.#{Travis.config.host.split('.')[-2, 2].join('.')}"
      end
    end

    post '/:id/cancel' do
      service = self.service(:cancel_job, params)
      if !service.authorized?
        json = { error: {
          message: "You don't have access to cancel job(#{params[:id]})"
        } }

        Metriks.meter("api.request.cancel_job.unauthorized").mark
        status 403
        respond_with json
      elsif !service.can_cancel?
        json = { error: {
          message: "The job(#{params[:id]}) can't be canceled",
          code: 'cant_cancel'
        } }

        Metriks.meter("api.request.cancel_job.cant_cancel").mark
        status 422
        respond_with json
      else
        service.run

        Metriks.meter("api.request.cancel_job.success").mark
        status 204
      end

      Metriks.meter("api.request.cancel_job").mark
    end
  end
end<commit_msg>Measure cancel request on the beginning of the request<commit_after>require 'travis/api/app'

class Travis::Api::App
  class Endpoint
    class Jobs < Endpoint
      get '/' do
        respond_with service(:find_jobs, params)
      end

      get '/:id' do
        respond_with service(:find_job, params)
      end

      get '/:job_id/log' do
        resource = service(:find_log, params).run
        if !resource || resource.archived?
          archived_log_path = archive_url("/jobs/#{params[:job_id]}/log.txt")

          if params[:cors_hax]
            status 204
            headers['Access-Control-Expose-Headers'] = 'Location'
            headers['Location'] = archived_log_path
          else
            redirect archived_log_path, 307
          end
        else
          respond_with resource
        end
      end

      def archive_url(path)
        "https://s3.amazonaws.com/#{hostname('archive')}#{path}"
      end

      def hostname(name)
        "#{name}#{'-staging' if Travis.env == 'staging'}.#{Travis.config.host.split('.')[-2, 2].join('.')}"
      end
    end

    post '/:id/cancel' do
      Metriks.meter("api.request.cancel_job").mark

      service = self.service(:cancel_job, params)
      if !service.authorized?
        json = { error: {
          message: "You don't have access to cancel job(#{params[:id]})"
        } }

        Metriks.meter("api.request.cancel_job.unauthorized").mark
        status 403
        respond_with json
      elsif !service.can_cancel?
        json = { error: {
          message: "The job(#{params[:id]}) can't be canceled",
          code: 'cant_cancel'
        } }

        Metriks.meter("api.request.cancel_job.cant_cancel").mark
        status 422
        respond_with json
      else
        service.run

        Metriks.meter("api.request.cancel_job.success").mark
        status 204
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.1236226820747111, 'fraction_numerical': 0.009674818597151304, 'mean_word_length': 2.0284784377542717, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1669146', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1126, 'n_words': 235}