<commit_before>task :calc_cycles do
  calc_cycles
end

# This data can be loaded with the rake db:seed (or created alongside the db with db:setup).
root = '../..'

require_relative root + '/config/environment.rb'
require_relative root + '/lib/Docker'
require_relative root + '/lib/DockerTestRunner'
require_relative root + '/lib/DummyTestRunner'
require_relative root + '/lib/Folders'
require_relative root + '/lib/Git'
require_relative root + '/lib/HostTestRunner'
require_relative root + '/lib/OsDisk'
require_relative root + '/lib/ASTInterface/ASTInterface'


CYCLE_DIAG = true
ALLOWED_LANGS = Set["Java-1.8_JUnit"]


def root_path
  Rails.root.to_s + '/'
end



def calc_cycles
  pos = 0
  prev_outer = nil
  prev_cycle_end = nil
  test_change = false
  prod_change = false
  in_cycle = false
  cycle = ""
  cycle_lights = Array.new
  cycle_test_edits = 0
  cycle_code_edits = 0
  cycle_total_edits = 0
  cycle_test_change = 0
  cycle_code_change = 0
  cycle_reds = 0
  cycle_time = 0
  first_cycle = true
  curr_num_tests = 0
  new_test = false
  valid_red = false

# ALLOWED_LANGS = Set["Java-1.8_JUnit"]

  Cycle.delete_all
  Phase.delete_all


  #Get Session
  # Session.where("language_framework = ?", ALLOWED_LANGS).find_each do |curr_session|
  Session.where("id = ?", 2456).find_each do |curr_session|

    puts "CYCLE_DIAG: #{curr_session[0]}" if CYCLE_DIAG

    #New Cycle
    curr_cycle = Cycle.new(cycle_position: pos)

    #New Phase
    curr_phase = Phase.new(tdd_color: "red")

    last_light_color = "red"
    #For Each Light
    curr_session.compiles.each_with_index do |curr_compile, index|
      new_test = false
      #check for new test in compiles
      if !curr_compile.total_assert_count.nil?
        if curr_compile.total_assert_count > curr_num_tests
          new_test = true
          curr_num_tests = curr_compile.total_assert_count
        end
      end
      puts "CYCLE_DIAG CURR: #{curr_compile}" if CYCLE_DIAG
      puts "CYCLE_DIAG INDEX: #{index}" if CYCLE_DIAG


      puts "*************" if CYCLE_DIAG
      puts "{" if CYCLE_DIAG
      puts "Light color: " + curr_compile.light_color.to_s if CYCLE_DIAG
      puts "Test edit: " + curr_compile.test_change.to_s if CYCLE_DIAG
      puts "Production edit: " + curr_compile.prod_change.to_s if CYCLE_DIAG

      puts "Current Phase: " + curr_phase.tdd_color.to_s if CYCLE_DIAG
      puts "Current Phase Empty?: " + curr_phase.compiles.empty?.to_s if CYCLE_DIAG
      puts "New Test?: " + new_test.to_s if CYCLE_DIAG
      puts "valid red?: " + valid_red.to_s if CYCLE_DIAG

      if !curr_compile.test_change && !curr_compile.prod_change && (last_light_color == curr_compile.light_color.to_s)
          curr_phase.compiles << curr_compile
          curr_compile.save
          puts "Saved curr_compile to current phase" if CYCLE_DIAG
      else

puts "%%%%%%%%%%%  Start CASE  %%%%%%%%%%%"
        #cycle logic
        case curr_phase.tdd_color

        when "red"

          #check for new test in red phase, if there is one mark this as a valid red phase
          if new_test
            valid_red = true
          end
          
          if curr_compile.light_color.to_s == "red" || curr_compile.light_color.to_s == "amber"
            
            if curr_compile.test_change && !curr_compile.prod_change
              curr_phase.compiles << curr_compile
              curr_compile.save
              puts "Saved curr_compile to red phase" if CYCLE_DIAG
            
            elsif curr_compile.test_change && curr_compile.prod_change

              if valid_red
                #save phase before new curr_compile is added
                curr_phase.save
                curr_cycle.phases << curr_phase

                puts "Start Green Phase (both test and production changes and red or amber compile)" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "green")
                
                #new curr_compile is part of next phase, so save now
                puts "Saved curr_compile to green phase" if CYCLE_DIAG
                curr_phase.compiles << curr_compile
                curr_compile.save
              else
                puts "[!1!] NON - TDD >> no new test and production edits occured" if CYCLE_DIAG
            
                #NON TDD (no red phase occured)
                curr_cycle.valid_tdd = false
                curr_phase.tdd_color = "white"
            
                #save curr_compile to phase
                curr_phase.compiles << curr_compile
                curr_compile.save
              end
              #reset new_test
              valid_red = false
            
            else #only prod edits in red phase SHOULD indicate on to green
            
              if valid_red
                #save phase before new curr_compile is added
                curr_cycle.phases << curr_phase
                curr_phase.save

                puts "Start Green Phase (only production changes and red or amber compile)" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "green")
                
                #new curr_compile is part of next phase, so save now
                puts "Saved curr_compile to green phase" if CYCLE_DIAG
                curr_phase.compiles << curr_compile
              else  
                puts "[!2!] NON - TDD >> no new test for testing phase" if CYCLE_DIAG
            
                #NON TDD (no red phase occured)
                curr_cycle.valid_tdd = false
                curr_phase.tdd_color = "white"
            
                #save curr_compile to phase
                curr_phase.compiles << curr_compile
                curr_compile.save
              end
              #reset valid red marker
              valid_red = false

            end
          
          else #green curr_compile with valid_red indicates green phase
          
            if valid_red
                #save phase before new curr_compile is added
                curr_cycle.phases << curr_phase
                curr_phase.save
                
                puts "Start Green Phase (green compile during valid red phase)" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "green")
                
                #new curr_compile is part of next phase, so save now
                puts "Saved curr_compile to green phase and end green phase" if CYCLE_DIAG
                curr_phase.compiles << curr_compile
                curr_phase.save

                #on to blue
                puts "Start Blue Phase" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "blue")
            <commit_msg>no green phase saving fix<commit_after>task :calc_cycles do
  calc_cycles
end

# This data can be loaded with the rake db:seed (or created alongside the db with db:setup).
root = '../..'

require_relative root + '/config/environment.rb'
require_relative root + '/lib/Docker'
require_relative root + '/lib/DockerTestRunner'
require_relative root + '/lib/DummyTestRunner'
require_relative root + '/lib/Folders'
require_relative root + '/lib/Git'
require_relative root + '/lib/HostTestRunner'
require_relative root + '/lib/OsDisk'
require_relative root + '/lib/ASTInterface/ASTInterface'


CYCLE_DIAG = true
ALLOWED_LANGS = Set["Java-1.8_JUnit"]


def root_path
  Rails.root.to_s + '/'
end



def calc_cycles
  pos = 0
  prev_outer = nil
  prev_cycle_end = nil
  test_change = false
  prod_change = false
  in_cycle = false
  cycle = ""
  cycle_lights = Array.new
  cycle_test_edits = 0
  cycle_code_edits = 0
  cycle_total_edits = 0
  cycle_test_change = 0
  cycle_code_change = 0
  cycle_reds = 0
  cycle_time = 0
  first_cycle = true
  curr_num_tests = 0
  new_test = false
  valid_red = false

# ALLOWED_LANGS = Set["Java-1.8_JUnit"]

  Cycle.delete_all
  Phase.delete_all


  #Get Session
  # Session.where("language_framework = ?", ALLOWED_LANGS).find_each do |curr_session|
  Session.where("id = ?", 2456).find_each do |curr_session|

    puts "CYCLE_DIAG: #{curr_session[0]}" if CYCLE_DIAG

    #New Cycle
    curr_cycle = Cycle.new(cycle_position: pos)

    #New Phase
    curr_phase = Phase.new(tdd_color: "red")

    last_light_color = "red"
    #For Each Light
    curr_session.compiles.each_with_index do |curr_compile, index|
      new_test = false
      #check for new test in compiles
      if !curr_compile.total_assert_count.nil?
        if curr_compile.total_assert_count > curr_num_tests
          new_test = true
          curr_num_tests = curr_compile.total_assert_count
        end
      end
      puts "CYCLE_DIAG CURR: #{curr_compile}" if CYCLE_DIAG
      puts "CYCLE_DIAG INDEX: #{index}" if CYCLE_DIAG


      puts "*************" if CYCLE_DIAG
      puts "{" if CYCLE_DIAG
      puts "Light color: " + curr_compile.light_color.to_s if CYCLE_DIAG
      puts "Test edit: " + curr_compile.test_change.to_s if CYCLE_DIAG
      puts "Production edit: " + curr_compile.prod_change.to_s if CYCLE_DIAG

      puts "Current Phase: " + curr_phase.tdd_color.to_s if CYCLE_DIAG
      puts "Current Phase Empty?: " + curr_phase.compiles.empty?.to_s if CYCLE_DIAG
      puts "New Test?: " + new_test.to_s if CYCLE_DIAG
      puts "valid red?: " + valid_red.to_s if CYCLE_DIAG

      if !curr_compile.test_change && !curr_compile.prod_change && (last_light_color == curr_compile.light_color.to_s)
          curr_phase.compiles << curr_compile
          curr_compile.save
          puts "Saved curr_compile to current phase" if CYCLE_DIAG
      else

puts "%%%%%%%%%%%  Start CASE  %%%%%%%%%%%"
        #cycle logic
        case curr_phase.tdd_color

        when "red"

          #check for new test in red phase, if there is one mark this as a valid red phase
          if new_test
            valid_red = true
          end
          
          if curr_compile.light_color.to_s == "red" || curr_compile.light_color.to_s == "amber"
            
            if curr_compile.test_change && !curr_compile.prod_change
              curr_phase.compiles << curr_compile
              curr_compile.save
              puts "Saved curr_compile to red phase" if CYCLE_DIAG
            
            elsif curr_compile.test_change && curr_compile.prod_change

              if valid_red
                #save phase before new curr_compile is added
                curr_phase.save
                curr_cycle.phases << curr_phase

                puts "Start Green Phase (both test and production changes and red or amber compile)" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "green")
                
                #new curr_compile is part of next phase, so save now
                puts "Saved curr_compile to green phase" if CYCLE_DIAG
                curr_phase.compiles << curr_compile
                curr_compile.save
              else
                puts "[!1!] NON - TDD >> no new test and production edits occured" if CYCLE_DIAG
            
                #NON TDD (no red phase occured)
                curr_cycle.valid_tdd = false
                curr_phase.tdd_color = "white"
            
                #save curr_compile to phase
                curr_phase.compiles << curr_compile
                curr_compile.save
              end
              #reset new_test
              valid_red = false
            
            else #only prod edits in red phase SHOULD indicate on to green
            
              if valid_red
                #save phase before new curr_compile is added
                curr_cycle.phases << curr_phase
                curr_phase.save

                puts "Start Green Phase (only production changes and red or amber compile)" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "green")
                
                #new curr_compile is part of next phase, so save now
                puts "Saved curr_compile to green phase" if CYCLE_DIAG
                curr_phase.compiles << curr_compile
              else  
                puts "[!2!] NON - TDD >> no new test for testing phase" if CYCLE_DIAG
            
                #NON TDD (no red phase occured)
                curr_cycle.valid_tdd = false
                curr_phase.tdd_color = "white"
            
                #save curr_compile to phase
                curr_phase.compiles << curr_compile
                curr_compile.save
              end
              #reset valid red marker
              valid_red = false

            end
          
          else #green curr_compile with valid_red indicates green phase
          
            if valid_red
                #save phase before new curr_compile is added
                curr_cycle.phases << curr_phase
                curr_phase.save
                
                puts "Start Green Phase (green compile during valid red phase)" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "green")
                
                #new curr_compile is part of next phase, so save now
                puts "Saved curr_compile to green phase and end green phase" if CYCLE_DIAG
                curr_phase.compiles << curr_compile
                curr_phase.save

                #save phase since green phase was over in one compile
                curr_cycle.phases << curr_phase
                curr_phase.save
                
                #on to blue
                puts "Start Blue Phase" if CYCLE_DIAG
                curr_phase = Phase.new(tdd_color: "blue")
            
{'fraction_non_alphanumeric': 0.06966693987035243, 'fraction_numerical': 0.00298040384472096, 'mean_word_length': 1.9064530099610222, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 13, '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': '1176360', 'n_tokens_mistral': 4062, 'n_tokens_neox': 3961, 'n_words': 1105}