Files
sure/app/models/rule.rb
Juan José Mata bf90cad9a0 Add Recent Runs visibility for rule executions (#376)
* Add Recent Runs visibility for rule executions

Adds a comprehensive tracking system for rule execution history with the following features:

- Creates RuleRun model to track execution metadata:
  * Date/time of execution
  * Execution type (manual/scheduled)
  * Success/failure status
  * Rule reference
  * Transaction counts (processed and modified)
  * Error messages for failed runs

- Updates RuleJob to automatically record execution results:
  * Captures transaction processing statistics
  * Handles success/failure states
  * Stores error details for debugging

- Adds "Recent Runs" section to rules index page:
  * Paginated display (20 runs per page)
  * Columnar layout similar to LLM usage page
  * Visual status indicators (success/failed badges)
  * Error tooltips for failed runs
  * Responsive design with design system tokens

- Includes i18n translations for all user-facing strings

This provides users with visibility into rule execution history, making it easier to debug issues and monitor rule performance.

* Update schema.rb with rule_runs table definition

* Linter noise

* Separate transaction counts into Queued, Processed, and Modified

Previously, the code eagerly reported transactions as "processed" when they
were only queued for processing. This commit separates the counts into three
distinct metrics:

- Transactions Queued: Count of transactions matching the rule's filter
  conditions before any processing begins
- Transactions Processed: Count of transactions that were actually processed
  and modified by the rule actions
- Transactions Modified: Count of transactions that had their values changed
  (currently same as Processed, but allows for future differentiation)

Changes:
- Add transactions_queued column to rule_runs table
- Update RuleJob to track all three counts separately
- Update action executors to return count of modified transactions
- Update Rule#apply to aggregate modification counts from actions
- Add transactions_queued label to locales
- Update Recent Runs view to display new column
- Add validation for transactions_queued in RuleRun model

The tracking now correctly reports:
1. How many transactions matched the filter (queued)
2. How many were actually modified (processed/modified)
3. Distinguishes between matching and modifying transactions

* Add Pending status to track async rule execution progress

Introduced a new "pending" status for rule runs to properly track async
AI operations. The system now:

- Tracks pending async jobs with a counter that decrements as jobs complete
- Updates transactions_modified incrementally as each job finishes
- Only counts transactions that were actually modified (not just queued)
- Displays pending status with yellow badge in the UI
- Automatically transitions from pending to success when all jobs complete

This provides better visibility into long-running AI categorization and
merchant detection operations, showing real-time progress as Sidekiq
processes the batches.

* Fix migration version to 7.2 as per project standards

* Consolidate rule_runs migrations into single migration file

Merged three separate migrations (create, add_transactions_queued,
add_pending_jobs_count) into a single CreateRuleRuns migration.
This provides better clarity and maintains a clean migration history.

Changes:
- Updated CreateRuleRuns migration to include all columns upfront
- Removed redundant add_column migrations
- Updated schema version to 2025_11_24_000000

* Linter and test fixes

* Space optimization

* LLM l10n is better than no l10n

* Fix implementation for tags/AI rules

* Fix tests

* Use batch_size

* Consider jobs "unknown" status sometimes

* Rabbit suggestion

* Rescue block for RuleRun.create!

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-07 16:30:02 +01:00

134 lines
4.0 KiB
Ruby

class Rule < ApplicationRecord
UnsupportedResourceTypeError = Class.new(StandardError)
belongs_to :family
has_many :conditions, dependent: :destroy
has_many :actions, dependent: :destroy
has_many :rule_runs, dependent: :destroy
accepts_nested_attributes_for :conditions, allow_destroy: true
accepts_nested_attributes_for :actions, allow_destroy: true
before_validation :normalize_name
validates :resource_type, presence: true
validates :name, length: { minimum: 1 }, allow_nil: true
validate :no_nested_compound_conditions
# Every rule must have at least 1 action
validate :min_actions
validate :no_duplicate_actions
def action_executors
registry.action_executors
end
def condition_filters
registry.condition_filters
end
def registry
@registry ||= case resource_type
when "transaction"
Rule::Registry::TransactionResource.new(self)
else
raise UnsupportedResourceTypeError, "Unsupported resource type: #{resource_type}"
end
end
def affected_resource_count
matching_resources_scope.count
end
def apply(ignore_attribute_locks: false, rule_run: nil)
total_modified = 0
total_async_jobs = 0
has_async = false
actions.each do |action|
result = action.apply(matching_resources_scope, ignore_attribute_locks: ignore_attribute_locks, rule_run: rule_run)
if result.is_a?(Hash) && result[:async]
has_async = true
total_async_jobs += result[:jobs_count] || 0
total_modified += result[:modified_count] || 0
elsif result.is_a?(Integer)
total_modified += result
else
# Log unexpected result type but don't fail
Rails.logger.warn("Rule#apply: Unexpected result type from action #{action.id}: #{result.class} (value: #{result.inspect})")
end
end
if has_async
{ modified_count: total_modified, async: true, jobs_count: total_async_jobs }
else
total_modified
end
end
def apply_later(ignore_attribute_locks: false)
RuleJob.perform_later(self, ignore_attribute_locks: ignore_attribute_locks)
end
def primary_condition_title
return "No conditions" if conditions.none?
first_condition = conditions.first
if first_condition.compound? && first_condition.sub_conditions.any?
first_sub_condition = first_condition.sub_conditions.first
"If #{first_sub_condition.filter.label.downcase} #{first_sub_condition.operator} #{first_sub_condition.value_display}"
else
"If #{first_condition.filter.label.downcase} #{first_condition.operator} #{first_condition.value_display}"
end
end
private
def matching_resources_scope
scope = registry.resource_scope
# 1. Prepare the query with joins required by conditions
conditions.each do |condition|
scope = condition.prepare(scope)
end
# 2. Apply the conditions to the query
conditions.each do |condition|
scope = condition.apply(scope)
end
scope
end
def min_actions
return if new_record? && actions.empty?
if actions.reject(&:marked_for_destruction?).empty?
errors.add(:base, "must have at least one action")
end
end
def no_duplicate_actions
action_types = actions.reject(&:marked_for_destruction?).map(&:action_type)
errors.add(:base, "Rule cannot have duplicate actions #{action_types.inspect}") if action_types.uniq.count != action_types.count
end
# Validation: To keep rules simple and easy to understand, we don't allow nested compound conditions.
def no_nested_compound_conditions
return true if conditions.none? { |condition| condition.compound? }
conditions.each do |condition|
if condition.compound?
if condition.sub_conditions.any? { |sub_condition| sub_condition.compound? }
errors.add(:base, "Compound conditions cannot be nested")
end
end
end
end
def normalize_name
self.name = nil if name.is_a?(String) && name.strip.empty?
end
end