<commit_before>require 'zip/zip'
require 'zip/zipfilesystem'
require 'yaml'

require 'scalarm/database/core/mongo_active_record'
require 'scalarm/service_core/utils'

class LogBankController < ApplicationController
  before_filter :authenticate, except: [
                                 :status, :get_simulation_output_size,
                                 :get_experiment_output_size, :get_simulation_stdout_size
                             ]

  before_filter :load_log_bank, except: [ :status ]
  before_filter :authorize_get, only: [ :get_simulation_output, :get_experiment_output, :get_simulation_stdout ]
  before_filter :authorize_put, only: [ :put_simulation_output, :put_simulation_stdout ]
  before_filter :authorize_delete, only: [ :delete_simulation_output, :delete_experiment_output, :delete_simulation_stdout ]

  @@experiment_size_threshold = 1024*1024*1024*300 # 300 MB

  def status
    tests = Scalarm::ServiceCore::Utils.parse_json_if_string(params[:tests])

    status = 'ok'
    message = ''

    unless tests.nil?
      failed_tests = tests.select { |t_name| not send("status_test_#{t_name}") }

      unless failed_tests.empty?
        status = 'failed'
        message = "Failed tests: #{failed_tests.join(', ')}"
      end
    end

    http_status = (status == 'ok' ? :ok : :internal_server_error)

    respond_to do |format|
      format.html do
        render text: message, status: http_status
      end
      format.json do
        render json: {status: status, message: message}, status: http_status
      end
    end
  end

  def get_simulation_output
    # just stream previously save binary data from the backend using included module
    sim_record = SimulationOutputRecord.where(
        experiment_id: @experiment_id,
        simulation_idx: @simulation_idx,
        type: 'binary'
    ).first
    file_object = sim_record.nil? ? nil : sim_record.file_object

    if file_object.nil?
      render inline: 'Required file not found', status: 404
    else
      file_name = "experiment_#{@experiment_id}_simulation_#{@simulation_idx}.tar.gz"
      response.headers['Content-Type'] = 'Application/octet-stream'
      response.headers['Content-Disposition'] = 'attachment; filename="' + file_name + '"'

      response.stream.write file_object
      # file_object.each do |data_chunk|
      #   response.stream.write data_chunk
      # end
      response.stream.close
    end

  end

  def get_simulation_output_size
    sim_record = SimulationOutputRecord.where(experiment_id: @experiment_id, simulation_idx: @simulation_idx, type: 'binary').first
    file_object = sim_record.nil? ? nil : sim_record.file_object

    if file_object.nil?
      render inline: 'Required file not found', status: 404
    else
      render json: { size: file_object.file_length }
    end
  end

  ##
  # PUT, parameters: file - binary simulation output
  def put_simulation_output
    unless params[:file] && (tmpfile = params[:file].tempfile)
      render inline: 'No file provided', status: 400
    else
      sim_record = SimulationOutputRecord.new(
          experiment_id: experiment_id,
          simulation_idx: simulation_idx,
          type: 'binary'
      )
      sim_record.set_file_object(tmpfile)

      sim_record.save

      render inline: 'Upload completed'
    end
  end<commit_msg>Minor fixes.<commit_after>require 'zip/zip'
require 'zip/zipfilesystem'
require 'yaml'

require 'scalarm/database/core/mongo_active_record'
require 'scalarm/service_core/utils'

class LogBankController < ApplicationController
  before_filter :authenticate, except: [
                                 :status, :get_simulation_output_size,
                                 :get_experiment_output_size, :get_simulation_stdout_size
                             ]

  before_filter :load_log_bank, except: [ :status ]
  before_filter :authorize_get, only: [ :get_simulation_output, :get_experiment_output, :get_simulation_stdout ]
  before_filter :authorize_put, only: [ :put_simulation_output, :put_simulation_stdout ]
  before_filter :authorize_delete, only: [ :delete_simulation_output, :delete_experiment_output, :delete_simulation_stdout ]

  @@experiment_size_threshold = 1024*1024*1024*300 # 300 MB

  def status
    tests = Scalarm::ServiceCore::Utils.parse_json_if_string(params[:tests])

    status = 'ok'
    message = ''

    unless tests.nil?
      failed_tests = tests.select { |t_name| not send("status_test_#{t_name}") }

      unless failed_tests.empty?
        status = 'failed'
        message = "Failed tests: #{failed_tests.join(', ')}"
      end
    end

    http_status = (status == 'ok' ? :ok : :internal_server_error)

    respond_to do |format|
      format.html do
        render text: message, status: http_status
      end
      format.json do
        render json: {status: status, message: message}, status: http_status
      end
    end
  end

  def get_simulation_output
    # just stream previously save binary data from the backend using included module
    sim_record = SimulationOutputRecord.where(
        experiment_id: @experiment_id,
        simulation_idx: @simulation_idx,
        type: 'binary'
    ).first
    file_object = sim_record.nil? ? nil : sim_record.file_object

    if file_object.nil?
      render inline: 'Required file not found', status: 404
    else
      file_name = "experiment_#{@experiment_id}_simulation_#{@simulation_idx}.tar.gz"
      response.headers['Content-Type'] = 'Application/octet-stream'
      response.headers['Content-Disposition'] = 'attachment; filename="' + file_name + '"'

      response.stream.write file_object
      # file_object.each do |data_chunk|
      #   response.stream.write data_chunk
      # end
      response.stream.close
    end

  end

  def get_simulation_output_size
    sim_record = SimulationOutputRecord.where(experiment_id: @experiment_id, simulation_idx: @simulation_idx, type: 'binary').first
    file_object = sim_record.nil? ? nil : sim_record.file_object

    if file_object.nil?
      render inline: 'Required file not found', status: 404
    else
      render json: { size: file_object.file_length }
    end
  end

  ##
  # PUT, parameters: file - binary simulation output
  def put_simulation_output
    unless params[:file] && (tmpfile = params[:file].tempfile)
      render inline: 'No file provided', status: 400
    else
      sim_record = SimulationOutputRecord.new(
          experiment_id: @experiment_id,
          simulation_idx: @simulation_idx,
          type: 'binary'
      )
      sim_record.set_file_object(tmpfile)

      sim_record.save

      render inline: 'Upload completed'
    end
  end
{'fraction_non_alphanumeric': 0.09217076482124001, 'fraction_numerical': 0.008146025041484387, 'mean_word_length': 3.193548387096774, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1771796', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1974, 'n_words': 442}