<commit_before>require 'measure_evaluator'
require 'patient_zipper'
require 'get_dependencies'
require 'open-uri'
require 'prawnto'

class ProductTestsController < ApplicationController
  before_filter :authenticate_user!
  
  def show
    @test = ProductTest.find(params[:id])
    @product = @test.product
    @product.measure_map ||= Measure.default_map
    @vendor = @product.vendor
    @patients = Record.where(:test_id => @test.id)

    # Decide our current execution. Show the one requested, if any. Otherwise show the most recent, or a new one if none exist
    if !params[:execution_id].nil?
      @current_execution = TestExecution.find(params[:execution_id])   
    else
      @never_executed_before = true
      @current_execution = TestExecution.new({:product_test => @test, :execution_date => Time.now})
    end
    
    # Calculate and categorize the passing and failing measures
    passing_measures = @current_execution.passing_measures
    failing_measures = @current_execution.failing_measures
    @measures = { 'fail' => failing_measures, 'pass' => passing_measures }
    
    # Calculate the time remaining
    @loading_progress = {}

    # For the population to be cloned or imported
    
    # population_creation_job
    # And for the measures to be calculated
    @percent_completed = ((@test.measure_defs.size - @test.result_calculation_jobs.size).to_f / @test.measure_defs.size.to_f) * 100
    
    respond_to do |format|
      # Don't send tons of JSON until we have results. In the meantime, just update the client on our calculation status.
      format.json do
        if @percent_completed < 100
          render :json => { 'percent_completed' => @percent_completed }
        else
          render :json => {'test' => @test, 'results' => @current_execution.expected_results, 'percent_completed' => @percent_completed, 'patients' => @patients }
        end
      end
      
      format.html { render :action => "show" }
      
      format.pdf { render :layout => false }
      prawnto :filename => "#{@test.name}.pdf"
    end
  end
  
  def new
    @test = ProductTest.new
    @product = Product.find(params[:product])
    @vendor = @product.vendor
    @measures = Measure.top_level
    @patient_populations = PatientPopulation.installed
    @product.measure_map ||= Measure.default_map

    @measures_categories = @measures.select do |t|
      @product.measure_map.keys.include?(t[:id])
    end.group_by {|g| g.category}

    # TODO - Copied default from popHealth. This probably needs to change at some point. We also currently ignore the uploaded value anyway.
    @effective_date = Time.gm(2010, 12, 31)
    @period_start = 3.months.ago(Time.at(@effective_date))
  end
  
  def create
    # Create a new test and save here so id is made. We'll use it while cloning Records to associate them back to this ProductTest.
    test = current_user.product_tests.build(params[:product_test])
    month, day, year = params[:product_test][:effective_date_end].split('/')
    test.effective_date = Time.local(year.to_i, month.to_i, day.to_i).to_i
    test.save!

    if params[:byod] && Rails.env != 'production'
      # If the user brought their own data, kick off a PatientImportJob. Store the file temporarily in /tmp
      uploaded_file = params[:byod].tempfile
      byod_path = "/tmp/byod_#{test.id}_#{Time.now.to_i}"
      format = params[:product_test][:upload_format]
      File.rename(File.path(uploaded_file), byod_path)
      
      test.population_creation_job = Cypress::PatientImportJob.create(:zip_file_location => byod_path, :test_id => test.id, :format => format)
    elsif params[:patient_ids]
      if params[:population_description] && !params[:population_name].empty?
        # if the user has created a population using the minimal_set feature
        # and they want to save it for subsequent tests
        population = PatientPopulation.new({:product_test => test, :name => params[:population_name], :description => params[:population_description],
            :patient_ids => params[:patient_ids], :user => User.where({:email => current_user[:email]}).first })
        population.save!
        test.population_creation_job = Cypress::PopulationCloneJob.create(:subset_id => params[:population_name], :test_id => test.id)
        test.patient_population = population
      else
        test.population_creation_job = Cypress::PopulationCloneJob.create(:patient_ids => params[:patient_ids], :test_id => test.id)
      end
    else
      # Otherwise we're making a subset of the Test Deck
      test.population_creation_job = Cypress::PopulationCloneJob.create(:subset_id => params[:product_test][:patient_population], :test_id => test.id)
      test.patient_population = PatientPopulation.where(:name => params[:product_test][:patient_population]).first
    end
<commit_msg>Updating ProductTest creation to use GMT rather than local time<commit_after>require 'measure_evaluator'
require 'patient_zipper'
require 'get_dependencies'
require 'open-uri'
require 'prawnto'

class ProductTestsController < ApplicationController
  before_filter :authenticate_user!
  
  def show
    @test = ProductTest.find(params[:id])
    @product = @test.product
    @product.measure_map ||= Measure.default_map
    @vendor = @product.vendor
    @patients = Record.where(:test_id => @test.id)

    # Decide our current execution. Show the one requested, if any. Otherwise show the most recent, or a new one if none exist
    if !params[:execution_id].nil?
      @current_execution = TestExecution.find(params[:execution_id])   
    else
      @never_executed_before = true
      @current_execution = TestExecution.new({:product_test => @test, :execution_date => Time.now})
    end
    
    # Calculate and categorize the passing and failing measures
    passing_measures = @current_execution.passing_measures
    failing_measures = @current_execution.failing_measures
    @measures = { 'fail' => failing_measures, 'pass' => passing_measures }
    
    # Calculate the time remaining
    @loading_progress = {}

    # For the population to be cloned or imported
    
    # population_creation_job
    # And for the measures to be calculated
    @percent_completed = ((@test.measure_defs.size - @test.result_calculation_jobs.size).to_f / @test.measure_defs.size.to_f) * 100
    
    respond_to do |format|
      # Don't send tons of JSON until we have results. In the meantime, just update the client on our calculation status.
      format.json do
        if @percent_completed < 100
          render :json => { 'percent_completed' => @percent_completed }
        else
          render :json => {'test' => @test, 'results' => @current_execution.expected_results, 'percent_completed' => @percent_completed, 'patients' => @patients }
        end
      end
      
      format.html { render :action => "show" }
      
      format.pdf { render :layout => false }
      prawnto :filename => "#{@test.name}.pdf"
    end
  end
  
  def new
    @test = ProductTest.new
    @product = Product.find(params[:product])
    @vendor = @product.vendor
    @measures = Measure.top_level
    @patient_populations = PatientPopulation.installed
    @product.measure_map ||= Measure.default_map

    @measures_categories = @measures.select do |t|
      @product.measure_map.keys.include?(t[:id])
    end.group_by {|g| g.category}

    # TODO - Copied default from popHealth. This probably needs to change at some point. We also currently ignore the uploaded value anyway.
    @effective_date = Time.gm(2010, 12, 31)
    @period_start = 3.months.ago(Time.at(@effective_date))
  end
  
  def create
    # Create a new test and save here so id is made. We'll use it while cloning Records to associate them back to this ProductTest.
    test = current_user.product_tests.build(params[:product_test])
    month, day, year = params[:product_test][:effective_date_end].split('/')
    test.effective_date = Time.gm(year.to_i, month.to_i, day.to_i).to_i
    test.save!

    if params[:byod] && Rails.env != 'production'
      # If the user brought their own data, kick off a PatientImportJob. Store the file temporarily in /tmp
      uploaded_file = params[:byod].tempfile
      byod_path = "/tmp/byod_#{test.id}_#{Time.now.to_i}"
      format = params[:product_test][:upload_format]
      File.rename(File.path(uploaded_file), byod_path)
      
      test.population_creation_job = Cypress::PatientImportJob.create(:zip_file_location => byod_path, :test_id => test.id, :format => format)
    elsif params[:patient_ids]
      if params[:population_description] && !params[:population_name].empty?
        # if the user has created a population using the minimal_set feature
        # and they want to save it for subsequent tests
        population = PatientPopulation.new({:product_test => test, :name => params[:population_name], :description => params[:population_description],
            :patient_ids => params[:patient_ids], :user => User.where({:email => current_user[:email]}).first })
        population.save!
        test.population_creation_job = Cypress::PopulationCloneJob.create(:subset_id => params[:population_name], :test_id => test.id)
        test.patient_population = population
      else
        test.population_creation_job = Cypress::PopulationCloneJob.create(:patient_ids => params[:patient_ids], :test_id => test.id)
      end
    else
      # Otherwise we're making a subset of the Test Deck
      test.population_creation_job = Cypress::PopulationCloneJob.create(:subset_id => params[:product_test][:patient_population], :test_id => test.id)
      test.patient_population = PatientPopulation.where(:name => params[:product_test][:patient_population]).first
    end

{'fraction_non_alphanumeric': 0.10832559579077684, 'fraction_numerical': 0.0030950170225936243, 'mean_word_length': 3.6560999039385207, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, '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': '6334385', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2771, 'n_words': 725}