Refactor Telegram bot logs with performance enhancements and view improvements

- Add comprehensive file information display (content type, size, dimensions)
- Move complex image dimension logic from views to TelegramBotLogsHelper
- Add percentage calculations to performance metrics section
- Use Rails associations instead of manual BlobFile lookups
- Update slow requests filtering to use total_request_time column
- Enhance search result thumbnails and improve post linking
- Add comprehensive test coverage for helper methods
- Improve error handling and type safety throughout
This commit is contained in:
Dylan Knutson
2025-08-05 06:22:37 +00:00
parent ff18b5f75c
commit e78baa6594
10 changed files with 276 additions and 87 deletions

View File

@@ -179,6 +179,7 @@ RSpec.describe TelegramBotLogsController, type: :controller do
:successful,
fingerprint_computation_time: 0.1,
search_computation_time: 0.2,
total_request_time: 0.3,
)
slow_log =
create(
@@ -186,6 +187,7 @@ RSpec.describe TelegramBotLogsController, type: :controller do
:successful,
fingerprint_computation_time: 0.8,
search_computation_time: 0.9,
total_request_time: 6.0,
)
get :index, params: { slow_requests: "true" }

View File

@@ -43,4 +43,84 @@ RSpec.describe TelegramBotLogsHelper, type: :helper do
)
end
end
describe "#image_dimensions_for_blob_file" do
it "returns N/A for nil blob_file" do
expect(helper.image_dimensions_for_blob_file(nil)).to eq("N/A")
end
it "returns N/A for non-image content type" do
blob_file = build(:blob_file, content_type: "text/plain")
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq("N/A")
end
it "returns dimensions for static image" do
blob_file = build(:blob_file, content_type: "image/jpeg")
mock_vips_image = double("Vips::Image", width: 1920, height: 1080)
mock_media = instance_double(LoadedMedia::StaticImage)
allow(LoadedMedia).to receive(:from_file).with(
blob_file.content_type,
blob_file.absolute_file_path,
).and_return(mock_media)
allow(mock_media).to receive(:is_a?).with(
LoadedMedia::StaticImage,
).and_return(true)
allow(mock_media).to receive(:instance_variable_get).with(
:@vips_image,
).and_return(mock_vips_image)
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq(
"1920×1080",
)
end
it "returns dimensions for GIF" do
blob_file = build(:blob_file, content_type: "image/gif")
mock_media = instance_double(LoadedMedia::Gif)
allow(LoadedMedia).to receive(:from_file).with(
blob_file.content_type,
blob_file.absolute_file_path,
).and_return(mock_media)
allow(mock_media).to receive(:is_a?).with(
LoadedMedia::StaticImage,
).and_return(false)
allow(mock_media).to receive(:is_a?).with(LoadedMedia::Gif).and_return(
true,
)
allow(mock_media).to receive(:instance_variable_get).with(
:@width,
).and_return(800)
allow(mock_media).to receive(:instance_variable_get).with(
:@height,
).and_return(600)
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq("800×600")
end
it "returns N/A for unsupported media type" do
blob_file = build(:blob_file, content_type: "image/webp")
mock_media = double("UnsupportedMedia")
allow(LoadedMedia).to receive(:from_file).with(
blob_file.content_type,
blob_file.absolute_file_path,
).and_return(mock_media)
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq("N/A")
end
it "returns 'Unable to determine' when exception occurs" do
blob_file = build(:blob_file, content_type: "image/jpeg")
allow(LoadedMedia).to receive(:from_file).and_raise(
StandardError.new("File not found"),
)
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq(
"Unable to determine",
)
end
end
end

View File

@@ -85,7 +85,7 @@ describe Domain::Fa::Job::BrowsePageJob do
end
unless expect_to_enqueue
it "does not enqueue user page job", quiet: false do
it "does not enqueue user page job" do
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::UserPageJob)).to eq([])
end
end

View File

@@ -126,8 +126,7 @@ RSpec.describe Domain::Inkbunny::Job::UserGalleryJob do
end
let(:args) { { user: user, caused_by_entry: nil } }
it "correctly handles posts with a null last_file_update_datetime",
quiet: false do
it "correctly handles posts with a null last_file_update_datetime" do
expect { perform_now(args) }.to(
change(Domain::Post::InkbunnyPost, :count).by(1),
)