mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
* 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>
109 lines
3.1 KiB
Ruby
109 lines
3.1 KiB
Ruby
class RulesController < ApplicationController
|
|
include StreamExtensions
|
|
|
|
before_action :set_rule, only: [ :edit, :update, :destroy, :apply, :confirm ]
|
|
|
|
def index
|
|
@sort_by = params[:sort_by] || "name"
|
|
@direction = params[:direction] || "asc"
|
|
|
|
allowed_columns = [ "name", "updated_at" ]
|
|
@sort_by = "name" unless allowed_columns.include?(@sort_by)
|
|
@direction = "asc" unless [ "asc", "desc" ].include?(@direction)
|
|
|
|
@rules = Current.family.rules.order(@sort_by => @direction)
|
|
|
|
# Fetch recent rule runs with pagination
|
|
recent_runs_scope = RuleRun
|
|
.joins(:rule)
|
|
.where(rules: { family_id: Current.family.id })
|
|
.recent
|
|
.includes(:rule)
|
|
|
|
@pagy, @recent_runs = pagy(recent_runs_scope, limit: params[:per_page] || 20, page_param: :runs_page)
|
|
|
|
render layout: "settings"
|
|
end
|
|
|
|
def new
|
|
@rule = Current.family.rules.build(
|
|
resource_type: params[:resource_type] || "transaction",
|
|
)
|
|
end
|
|
|
|
def create
|
|
@rule = Current.family.rules.build(rule_params)
|
|
|
|
if @rule.save
|
|
redirect_to confirm_rule_path(@rule, reload_on_close: true)
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def apply
|
|
@rule.update!(active: true)
|
|
@rule.apply_later(ignore_attribute_locks: true)
|
|
redirect_back_or_to rules_path, notice: "#{@rule.resource_type.humanize} rule activated"
|
|
end
|
|
|
|
def confirm
|
|
# Compute provider, model, and cost estimation for auto-categorize actions
|
|
if @rule.actions.any? { |a| a.action_type == "auto_categorize" }
|
|
# Use the same provider determination logic as Family::AutoCategorizer
|
|
llm_provider = Provider::Registry.get_provider(:openai)
|
|
|
|
if llm_provider
|
|
@selected_model = Provider::Openai.effective_model
|
|
@estimated_cost = LlmUsage.estimate_auto_categorize_cost(
|
|
transaction_count: @rule.affected_resource_count,
|
|
category_count: @rule.family.categories.count,
|
|
model: @selected_model
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
def edit
|
|
end
|
|
|
|
def update
|
|
if @rule.update(rule_params)
|
|
respond_to do |format|
|
|
format.html { redirect_back_or_to rules_path, notice: "Rule updated" }
|
|
format.turbo_stream { stream_redirect_back_or_to rules_path, notice: "Rule updated" }
|
|
end
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@rule.destroy
|
|
redirect_to rules_path, notice: "Rule deleted"
|
|
end
|
|
|
|
def destroy_all
|
|
Current.family.rules.destroy_all
|
|
redirect_to rules_path, notice: "All rules deleted"
|
|
end
|
|
|
|
private
|
|
def set_rule
|
|
@rule = Current.family.rules.find(params[:id])
|
|
end
|
|
|
|
def rule_params
|
|
params.require(:rule).permit(
|
|
:resource_type, :effective_date, :active, :name,
|
|
conditions_attributes: [
|
|
:id, :condition_type, :operator, :value, :_destroy,
|
|
sub_conditions_attributes: [ :id, :condition_type, :operator, :value, :_destroy ]
|
|
],
|
|
actions_attributes: [
|
|
:id, :action_type, :value, :_destroy
|
|
]
|
|
)
|
|
end
|
|
end
|