Refactor TelegramBotLog status handling and add helper

- Remove no_results status enum value, treat zero results as successful searches
- Add processing status for initial log creation
- Create TelegramBotLogsHelper#status_color_class to eliminate view duplication
- Refactor TelegramBotTask to use Stopwatch class for timing measurements
- Add total_request_time column to track end-to-end request duration
- Update factories, tests, and views to support new status model
- Add comprehensive helper tests and maintain full test coverage
This commit is contained in:
Dylan Knutson
2025-08-05 05:22:50 +00:00
parent 24a59d50f2
commit ff18b5f75c
10 changed files with 114 additions and 74 deletions

View File

@@ -124,7 +124,7 @@ RSpec.describe TelegramBotLogsController, type: :controller do
create(:telegram_bot_log, :successful, telegram_user_id: 456_789_123)
end
let!(:error_log) { create(:telegram_bot_log, :with_error) }
let!(:no_results_log) { create(:telegram_bot_log, :no_results) }
let!(:no_results_log) { create(:telegram_bot_log, :with_no_results) }
it "filters by telegram_user_id" do
get :index, params: { telegram_user_id: "123456789" }

View File

@@ -32,7 +32,7 @@ FactoryBot.define do
end
trait :with_no_results do
status { :no_results }
status { :success }
search_results_count { 0 }
download_time { 0.07 }
image_processing_time { 0.03 }
@@ -66,6 +66,18 @@ FactoryBot.define do
response_data { { error: "Unsupported format" } }
end
trait :processing do
status { :processing }
search_results_count { 0 }
download_time { nil }
image_processing_time { nil }
fingerprint_computation_time { nil }
search_computation_time { nil }
total_request_time { nil }
error_message { nil }
response_data { {} }
end
trait :minimal_user_info do
telegram_username { nil }
telegram_first_name { nil }

View File

@@ -0,0 +1,46 @@
# typed: false
require "rails_helper"
RSpec.describe TelegramBotLogsHelper, type: :helper do
describe "#status_color_class" do
it "returns blue classes for processing status" do
log = build(:telegram_bot_log, :processing)
expect(helper.status_color_class(log)).to eq("bg-blue-100 text-blue-800")
end
it "returns green classes for success status" do
log = build(:telegram_bot_log, :successful)
expect(helper.status_color_class(log)).to eq(
"bg-green-100 text-green-800",
)
end
it "returns red classes for error status" do
log = build(:telegram_bot_log, :with_error)
expect(helper.status_color_class(log)).to eq("bg-red-100 text-red-800")
end
it "returns orange classes for invalid_image status" do
log = build(:telegram_bot_log, :invalid_image)
expect(helper.status_color_class(log)).to eq(
"bg-orange-100 text-orange-800",
)
end
it "returns slate classes for unknown status" do
log = build(:telegram_bot_log)
allow(log).to receive(:status).and_return("unknown_status")
expect(helper.status_color_class(log)).to eq(
"bg-slate-100 text-slate-800",
)
end
it "handles success status with no results" do
log = build(:telegram_bot_log, :with_no_results)
expect(helper.status_color_class(log)).to eq(
"bg-green-100 text-green-800",
)
end
end
end

View File

@@ -37,9 +37,9 @@ RSpec.describe TelegramBotLog, type: :model do
it do
should define_enum_for(:status)
.with_values(
processing: "processing",
success: "success",
error: "error",
no_results: "no_results",
invalid_image: "invalid_image",
)
.backed_by_column_of_type(:string)
@@ -205,8 +205,13 @@ RSpec.describe TelegramBotLog, type: :model do
describe ".successful" do
it "returns only successful logs" do
successful_logs = TelegramBotLog.successful
expect(successful_logs).to include(user1_log1, user2_log, recent_log)
expect(successful_logs).not_to include(user1_log2, no_results_log)
expect(successful_logs).to include(
user1_log1,
user2_log,
recent_log,
no_results_log,
)
expect(successful_logs).not_to include(user1_log2)
end
end
@@ -401,6 +406,15 @@ RSpec.describe TelegramBotLog, type: :model do
expect(log.has_performance_metrics?).to be false
end
it "creates processing log with correct attributes" do
log = build(:telegram_bot_log, :processing)
expect(log.status).to eq("processing")
expect(log.search_results_count).to eq(0)
expect(log.error_message).to be_nil
expect(log.has_performance_metrics?).to be false
expect(log.total_request_time).to be_nil
end
it "creates log with image association" do
log = build(:telegram_bot_log, :with_image)
expect(log.processed_image).to be_present