<commit_before>require 'rails_helper'

include FactoryGirl::Syntax::Methods

#RSpec.describe Evaluation, type: :model do
#  pending "add some examples to (or delete) #{__FILE__}"
#end

RSpec.describe Evaluation, type: :model do
  after do
    FactoryGirl.reload
  end

  it 'calculate alive rate by 1 evaluation is dead' do
    eval = Evaluation.new(:latest => true, :alive => false)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 0.0 # 0 / 1
  end

  it 'calculate alive rate by 1 evaluation is alive' do
    eval = Evaluation.new(:latest => true, :alive => true)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 100.0 # 1 / 1
  end

  it 'calculate alive rate by 2 evaluation that latest evaluation is dead and 1 old evaluation is alive' do
    endpoint_id = 1
    create(:evaluation, :endpoint_id => endpoint_id, :latest => false, :alive => true)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => false)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 50.0 # 1 / 2
  end

  it 'calculate alive rate by 2 evaluation that latest evaluation is alive and 1 old evaluation is dead' do
    endpoint_id = 1
    create(:evaluation, :endpoint_id => endpoint_id, :latest => false, :alive => false)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => true)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 50.0 # 1 / 2
  end

  it 'calculate alive rate by 30 evaluation that latest evaluation is dead and 29 old evaluation is alive' do
    endpoint_id = 1
    create_list(:evaluation, 29, :endpoint_id => endpoint_id, :latest => false, :alive => true)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => false)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 96.7 # 29 / 30
  end

  it 'calculate alive rate by 30 evaluation that latest evaluation is alive and 29 old evaluation is dead' do
    endpoint_id = 1
    create_list(:evaluation, 29, :endpoint_id => endpoint_id, :latest => false, :alive => false)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => true)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 3.3 # 1 / 30 
  end

  it 'calculate alive rate by 30 evaluation that last 15 evaluations are alive and the others are dead' do
    endpoint_id = 1
    create_list(:evaluation, 14, :endpoint_id => endpoint_id, :latest => false, :alive => true)
    create_list(:evaluation, 15, :endpoint_id => endpoint_id, :latest => false, :alive => false)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => true)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 50.0 # 15 / 30 
  end
  
end<commit_msg>add the test cases for calc_updated_intervals method<commit_after>require 'rails_helper'

include FactoryGirl::Syntax::Methods

#RSpec.describe Evaluation, type: :model do
#  pending "add some examples to (or delete) #{__FILE__}"
#end

RSpec.describe Evaluation, type: :model do
  after do
    FactoryGirl.reload
  end

  it 'calculate alive rate by 1 evaluation is dead' do
    eval = Evaluation.new(:latest => true, :alive => false)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 0.0 # 0 / 1
  end

  it 'calculate alive rate by 1 evaluation is alive' do
    eval = Evaluation.new(:latest => true, :alive => true)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 100.0 # 1 / 1
  end

  it 'calculate alive rate by 2 evaluation that latest evaluation is dead and 1 old evaluation is alive' do
    endpoint_id = 1
    create(:evaluation, :endpoint_id => endpoint_id, :latest => false, :alive => true)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => false)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 50.0 # 1 / 2
  end

  it 'calculate alive rate by 2 evaluation that latest evaluation is alive and 1 old evaluation is dead' do
    endpoint_id = 1
    create(:evaluation, :endpoint_id => endpoint_id, :latest => false, :alive => false)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => true)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 50.0 # 1 / 2
  end

  it 'calculate alive rate by 30 evaluation that latest evaluation is dead and 29 old evaluation is alive' do
    endpoint_id = 1
    create_list(:evaluation, 29, :endpoint_id => endpoint_id, :latest => false, :alive => true)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => false)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 96.7 # 29 / 30
  end

  it 'calculate alive rate by 30 evaluation that latest evaluation is alive and 29 old evaluation is dead' do
    endpoint_id = 1
    create_list(:evaluation, 29, :endpoint_id => endpoint_id, :latest => false, :alive => false)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => true)
    
    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 3.3 # 1 / 30 
  end

  it 'calculate alive rate by 30 evaluation that last 15 evaluations are alive and the others are dead' do
    endpoint_id = 1
    create_list(:evaluation, 14, :endpoint_id => endpoint_id, :latest => false, :alive => true)
    create_list(:evaluation, 15, :endpoint_id => endpoint_id, :latest => false, :alive => false)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :latest => true, :alive => true)

    alive_rate = Evaluation.calc_alive_rate(eval)

    expect(alive_rate).to eq 50.0 # 15 / 30
  end

  it 'should return 0 when evaluation dose not exist and last_updated is nil' do
    endpoint_id = 10000
    eval = Evaluation.new(:endpoint_id => endpoint_id, :last_updated => nil)

    update_interval = Evaluation.calc_update_interval(eval)

    expect(update_interval).to eq 0
  end

  it 'should return 1 when evaluation dose not exist and last_updated is not nil' do
    endpoint_id = 10000
    eval = Evaluation.new(:endpoint_id => endpoint_id, :last_updated => Time.now.strftime('%Y-%m-%d %H:%M:%s'))

    update_interval = Evaluation.calc_update_interval(eval)

    expect(update_interval).to eq 1
  end

  it 'should return 1 when the last updated fields are not same' do
    endpoint_id = 10000
    create(:evaluation, :endpoint_id => endpoint_id, :last_updated => 2.days.ago(Time.zone.now))
    create(:evaluation, :endpoint_id => endpoint_id, :last_updated => 1.days.ago(Time.zone.now))
    eval = Evaluation.new(:endpoint_id => endpoint_id, :last_updated => Time.zone.now)

    update_interval = Evaluation.calc_update_interval(eval)

    expect(update_interval).to eq 1
  end

  it 'should return 1 when the last updated fields are all same' do
    endpoint_id = 10000
    two_days_ago = 2.days.ago(Time.zone.now)
    create(:evaluation, :endpoint_id => endpoint_id, :last_updated => two_days_ago)
    create(:evaluation, :endpoint_id => endpoint_id, :last_updated => two_days_ago)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :last_updated => two_days_ago)

    update_interval = Evaluation.calc_update_interval(eval)

    expect(update_interval).to eq 3
  end

  it 'should return 2.3333333333333335 when endpoints is 3 times updated for 7 days' do
    endpoint_id = 10000
    six_days_ago = 6.days.ago(Time.zone.now)
    four_days_ago = 4.days.ago(Time.zone.now)
    one_days_ago = 1.days.ago(Time.zone.now)
    (4..6).each{create(:evaluation, :endpoint_id => endpoint_id, :last_updated => six_days_ago)}
    (2..3).each{create(:evaluation, :endpoint_id => endpoint_id, :last_updated => four_days_ago)}
    create(:evaluation, :endpoint_id => endpoint_id, :last_updated => one_days_ago)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :last_updated => one_days_ago)

    update_interval = Evaluation.calc_update_interval(eval)

    expect(update_interval).to eq 2.3333333333333335 # 7/3
  end

  it 'should return 3.5 when endpoint is 2 times updated and 2 times dead for 7 days' do
    endpoint_id = 10000
    four_days_ago = 4.days.ago(Time.zone.now)
    (4..6).each{create(:evaluation, :endpoint_id => endpoint_id, :last_updated => nil)}
    (2..3).each{create(:evaluation, :endpoint_id => endpoint_id, :last_updated => four_days_ago)}
    create(:evaluation, :endpoint_id => endpoint_id, :last_updated => nil)
    eval = Evaluation.new(:endpoint_id => endpoint_id, :last_updated => Time.zone.now)

    update_interval = Evaluation.calc_update_interval(eval)

    expect(update_interval).to eq 3.5
  end

  it 'should return 0 when endpoint is dead for 7 days' do
    endpoint_id = 10000
    (1..6).each{create(:evaluation, :endpoint_id => endpoint_id, :last_updated => nil)}
    eval = Evaluation.new(:endpoint_id => endpoint_id, :last_updated => nil)

    update_interval = Evaluation.calc_update_interval(eval)

    expect(update_interval).to eq 0
  end

end
{'fraction_non_alphanumeric': 0.10861710281397131, 'fraction_numerical': 0.02715427570349283, 'mean_word_length': 3.853347502656748, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5977424', 'n_tokens_mistral': 3174, 'n_tokens_neox': 2863, 'n_words': 862}