Bulk insert TestrunMessages

With Sentry, we identified that all TestrunMessages are created one-by-one (besides passing them all in an array to #create!). Therefore, we now do the validations on our own first and then store all once, dramatically reducing the required time.

Fixes CODEOCEAN-HP
This commit is contained in:
Sebastian Serth
2023-02-15 22:48:04 +01:00
parent a558a6df3b
commit b375aed543

View File

@ -56,7 +56,7 @@ class TestrunMessage < ApplicationRecord
filtered_messages = filter_messages_by_size testrun, messages
# An array with hashes is passed, all are stored
TestrunMessage.create!(filtered_messages)
validate_and_store!(filtered_messages)
end
def self.filter_messages_by_size(testrun, messages)
@ -83,6 +83,20 @@ class TestrunMessage < ApplicationRecord
end
filtered_messages.select(&:present?)
end
private_class_method :filter_messages_by_size
def self.validate_and_store!(messages)
validated_messages = messages.map do |message|
testrun_message = TestrunMessage.new(message)
testrun_message.validate!
# We serialize the message without the ID, created_at and updated_at, as they are generated by the database.
testrun_message.serializable_hash(except: %w[id created_at updated_at])
end
# Now, we store all messages and skip validations (they are already done)
TestrunMessage.insert_all!(validated_messages) # rubocop:disable Rails/SkipsModelValidations
end
private_class_method :validate_and_store!
def either_data_or_log
if [data, log].count(&:present?) > 1