Skip to content

Commit

Permalink
CV2-5558: Downgrade Relationships based on last_seen, not created_at;…
Browse files Browse the repository at this point in the history
… add better logging (#2110)
  • Loading branch information
computermacgyver authored Nov 4, 2024
1 parent 6547324 commit 4629593
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
16 changes: 11 additions & 5 deletions app/models/bot/alegre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,17 @@ def self.set_relationship_type(pm, pm_id_scores, parent)
settings = tbi.nil? ? {} : tbi.alegre_settings
date_threshold = Time.now - settings['similarity_date_threshold'].to_i.months unless settings['similarity_date_threshold'].blank?
relationship_type = pm_id_scores[parent.id][:relationship_type]
if settings['date_similarity_threshold_enabled'] && !date_threshold.blank? && parent.created_at.to_i < date_threshold.to_i
relationship_type = Relationship.suggested_type
else
length_threshold = settings.blank? ? CheckConfig.get('text_length_matching_threshold').to_f : settings['text_length_matching_threshold'].to_f
relationship_type = Relationship.suggested_type if self.is_text_too_short?(pm, length_threshold)
if relationship_type != Relationship.suggested_type
if settings['date_similarity_threshold_enabled'] && !date_threshold.blank? && parent.last_seen.to_i < date_threshold.to_i
Rails.logger.info("[Alegre Bot] [ProjectMedia ##{pm.id}] [Relationships 6/6] Downgrading to suggestion due to parent age")
relationship_type = Relationship.suggested_type
else
length_threshold = settings.blank? ? CheckConfig.get('text_length_matching_threshold').to_f : settings['text_length_matching_threshold'].to_f
if self.is_text_too_short?(pm, length_threshold)
Rails.logger.info("[Alegre Bot] [ProjectMedia ##{pm.id}] [Relationships 6/6] Downgrading to suggestion due short text")
relationship_type = Relationship.suggested_type
end
end
end
relationship_type
end
Expand Down
22 changes: 15 additions & 7 deletions test/models/bot/alegre_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,32 @@ def teardown
}
]
})
# Set the TeamBotInstallation (tbi) for Alegre so that a query
# matched to an item seen more than 1 month ago is downgraded to suggestion
tbi = Bot::Alegre.get_alegre_tbi(@team.id)
tbi.set_date_similarity_threshold_enabled = true
tbi.set_similarity_date_threshold("1")
tbi.save!

# First verify a confirmed_type relationship is not downgraded.
# because last_seen will be now and not more than one month ago.
assert_difference 'Relationship.count' do
result = Bot::Alegre.relate_project_media_to_similar_items(pm2)
end
r = Relationship.last
assert_equal Relationship.confirmed_type, r.relationship_type
pm1.created_at = Time.now - 2.months
pm1.save!
tbi = Bot::Alegre.get_alegre_tbi(@team.id)
tbi.set_date_similarity_threshold_enabled = true
tbi.set_similarity_date_threshold("1")
tbi.save!
r.destroy
r.destroy!

# Now stub last_seen so that confirmed_type is downgraded to suggest_type
# because it is more than tbi.similarity_date_threshold months old
ProjectMedia.any_instance.stubs(:last_seen).returns(Time.now - 2.months)
assert_difference 'Relationship.count' do
result = Bot::Alegre.relate_project_media_to_similar_items(pm2)
end
r = Relationship.last
assert_equal Relationship.suggested_type, r.relationship_type
Bot::Alegre.unstub(:request)
ProjectMedia.any_instance.unstub(:last_seen)
end

test "should index report data" do
Expand Down

0 comments on commit 4629593

Please sign in to comment.