<commit_before>require 'sinatra/base'
require 'mustache/sinatra'
require 'deployinator'
require 'deployinator/helpers'
require 'deployinator/helpers/deploy'
require 'deployinator/helpers/version'
require 'deployinator/helpers/git'
require 'deployinator/views/index'
require 'deployinator/views/log'
require 'deployinator/views/run_logs'
require 'deployinator/views/log_table'
require 'deployinator/views/deploys_status'

module Deployinator
  class DeployinatorApp < Sinatra::Base
    register Mustache::Sinatra
    helpers Deployinator::Helpers,
      Deployinator::Helpers,
      Deployinator::Helpers::DeployHelpers,
      Deployinator::Helpers::GitHelpers,
      Deployinator::Helpers::VersionHelpers

    def github_diff_url(params)
      stack = params[:stack].intern
      gh_info = git_info_for_stack[stack]
      "https://#{which_github_host(stack)}/#{gh_info[:user]}/#{gh_info[:repository]}/compare/#{params[:r1]}...#{params[:r2]}"
    end

    set :mustache, {
      :views     => Deployinator.root('views'),
      :templates => Deployinator.root('templates'),
      :namespace => Deployinator::Views
    }

    set :public_folder, Deployinator.root('public')
    set :static, true

    before do
      init(env)
      @disabled_override = params[:override].nil? ? false : true
    end

    get '/' do
        mustache Deployinator::Views::Index
    end

    get '/run_logs/view/:log' do
      template = open("#{File.dirname(__FILE__)}/templates/stream.mustache").read
      template.gsub!("{{ yield }}", "{{{yield}}}")
      Mustache.render(template, :yield => open("run_logs/" + params[:log]).read)
    end

    get '/run_logs/?' do
      @stack = "log"
      @params = params
      mustache Deployinator::Views::RunLogs
    end

    get '/run_logs/latest/:log_type' do
      run_logs = get_run_logs(:limit => 1, :type => params[:log_type])
      if run_logs.count == 1
        run_log = run_logs.first[:name]
        redirect "/run_logs/view/#{run_log}"
      else
        redirect "/"
      end
    end

    get '/log/?' do
      @stack = "log"
      @params = params
      mustache Deployinator::Views::LogTable
    end

    get '/:stack/can-deploy' do
      lock_info = push_lock_info(params["stack"]) || {}
      lock_info[:can_deploy] = lock_info.empty?
      content_type "application/json"
      lock_info.to_json
    end

    get '/diff/:stack/:r1/:r2/?' do
      @stack = params[:stack]
      diff(params["r1"], params["r2"], params["stack"], params[:time])
    end

    get '/diff/:stack/:r1/:r2/github/?' do
      redirect github_diff_url(params)
    end

    get '/head_rev/:stack' do
      git_head_rev(params[:stack]).chomp
    end

    get '/:thing' do
      @stack = params[:thing]
      @params = params
      begin
        mustache @stack
      rescue Errno::ENOENT
        pass
      end
    end

    get '/:stack/remove-lock' do
      stack = params[:stack]
      unlock_pushes(stack) if can_remove_stack_lock?
      redirect "/#{stack}"
    end

    # return a list of all deploys as JSON
    get '/deploys/?' do
      get_list_of_deploys.to_json
    end

    get '/css/style.css:version' do
      send_file "#{File.dirname(__FILE__)}/static/css/style.css"
    end

    get '/deploys_status' do
        mustache Deployinator::Views::DeploysStatus
    end

    get '/log.txt' do
      content_type :text
      `tac #{Deployinator.log_path}#{ " | head -n #{params[:limit]}" if params[:limit]}`
    end

    get '/timing_log.txt' do
      content_type :text
      `tac #{Deployinator.timing_log_path}`
    end

    get '/ti/:stack/:env' do
      @stack = params[:stack]
      average_duration(params["env"], params["stack"]).to_s
    end

    # this is the API endpoint to asynchronously start a deploy that runs in
    # the background.
    post '/deploys/?' do
      params[:username] = @username<commit_msg>fixing route<commit_after>require 'sinatra/base'
require 'mustache/sinatra'
require 'deployinator'
require 'deployinator/helpers'
require 'deployinator/helpers/deploy'
require 'deployinator/helpers/version'
require 'deployinator/helpers/git'
require 'deployinator/views/index'
require 'deployinator/views/log'
require 'deployinator/views/run_logs'
require 'deployinator/views/log_table'
require 'deployinator/views/deploys_status'

module Deployinator
  class DeployinatorApp < Sinatra::Base
    register Mustache::Sinatra
    helpers Deployinator::Helpers,
      Deployinator::Helpers,
      Deployinator::Helpers::DeployHelpers,
      Deployinator::Helpers::GitHelpers,
      Deployinator::Helpers::VersionHelpers

    def github_diff_url(params)
      stack = params[:stack].intern
      gh_info = git_info_for_stack[stack]
      "https://#{which_github_host(stack)}/#{gh_info[:user]}/#{gh_info[:repository]}/compare/#{params[:r1]}...#{params[:r2]}"
    end

    set :mustache, {
      :views     => Deployinator.root('views'),
      :templates => Deployinator.root('templates'),
      :namespace => Deployinator::Views
    }

    set :public_folder, Deployinator.root('public')
    set :static, true

    before do
      init(env)
      @disabled_override = params[:override].nil? ? false : true
    end

    get '/' do
        mustache Deployinator::Views::Index
    end

    get '/run_logs/view/:log' do
      template = open("#{File.dirname(__FILE__)}/templates/stream.mustache").read
      template.gsub!("{{ yield }}", "{{{yield}}}")
      Mustache.render(template, :yield => open("run_logs/" + params[:log]).read)
    end

    get '/run_logs/?' do
      @stack = "log"
      @params = params
      mustache Deployinator::Views::RunLogs
    end

    get '/run_logs/latest/:log_type' do
      run_logs = get_run_logs(:limit => 1, :type => params[:log_type])
      if run_logs.count == 1
        run_log = run_logs.first[:name]
        redirect "/run_logs/view/#{run_log}"
      else
        redirect "/"
      end
    end

    get '/log/?' do
      @stack = "log"
      @params = params
      mustache Deployinator::Views::LogTable
    end

    get '/:stack/can-deploy' do
      lock_info = push_lock_info(params["stack"]) || {}
      lock_info[:can_deploy] = lock_info.empty?
      content_type "application/json"
      lock_info.to_json
    end

    get '/diff/:stack/:r1/:r2/?' do
      @stack = params[:stack]
      diff(params["r1"], params["r2"], params["stack"], params[:time])
    end

    get '/diff/:stack/:r1/:r2/github/?' do
      redirect github_diff_url(params)
    end

    get '/head_rev/:stack' do
      git_head_rev(params[:stack]).chomp
    end

    get '/:thing' do
      @stack = params[:thing]
      @params = params
      begin
        mustache @stack
      rescue Errno::ENOENT
        pass
      end
    end

    get '/:stack/remove-lock' do
      stack = params[:stack]
      unlock_pushes(stack) if can_remove_stack_lock?
      redirect "/#{stack}"
    end

    # return a list of all deploys as JSON
    get '/deploys/?' do
      get_list_of_deploys.to_json
    end

    get '/css/style.css?:version' do
      send_file "#{File.dirname(__FILE__)}/static/css/style.css"
    end

    get '/deploys_status' do
        mustache Deployinator::Views::DeploysStatus
    end

    get '/log.txt' do
      content_type :text
      `tac #{Deployinator.log_path}#{ " | head -n #{params[:limit]}" if params[:limit]}`
    end

    get '/timing_log.txt' do
      content_type :text
      `tac #{Deployinator.timing_log_path}`
    end

    get '/ti/:stack/:env' do
      @stack = params[:stack]
      average_duration(params["env"], params["stack"]).to_s
    end

    # this is the API endpoint to asynchronously start a deploy that runs in
    # the background.
    post '/deploys/?' do
      params[:username] = @username
{'fraction_non_alphanumeric': 0.1377829820452771, 'fraction_numerical': 0.0026021337496747333, 'mean_word_length': 2.9059959349593494, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1967333', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2498, 'n_words': 512}