Skip to content

Add a count preview to maintenance task UI#1388

Open
mikee649 wants to merge 1 commit intomainfrom
rokas/count-preview
Open

Add a count preview to maintenance task UI#1388
mikee649 wants to merge 1 commit intomainfrom
rokas/count-preview

Conversation

@mikee649
Copy link

Add a count preview to maintenance task UI

Summary

  • Displays an estimated count of items to be processed before running a task, giving operators visibility into the scope of a task run
  • Fetches the count asynchronously via a new JSON endpoint so the main page load is not blocked
  • Automatically re-fetches the count when task parameters change, keeping the estimate in sync with the current input

Motivation

When running a maintenance task, operators had no way to gauge how many items would be processed beforehand. This made it difficult to assess the impact of a run or verify that parameters were configured correctly. A count preview solves this by showing a live estimate directly on the task show page.

What changed

New count endpoint (GET /tasks/:id/count)

  • Added a count action to TasksController that returns { count: <integer|null> } as JSON.
  • Errors are gracefully rescued, returning { count: null } so the UI can hide the indicator instead of showing a broken state.
  • Route added as a member action on the tasks resource.

TaskDataShow#count method

  • Computes the estimated item count by first trying the task's #count method, then falling back to collection.count.
  • Returns nil for CSV tasks (collection depends on uploaded file content), deleted tasks, and when the query times out.
  • Wrapped in a 15-second Timeout to prevent long-running count queries from blocking the endpoint.

TaskDataShow#no_collection? method

  • New predicate to detect tasks that have no collection (e.g. one-off tasks). These tasks skip the count preview since the indicator is not meaningful.

Frontend (show.html.erb)

  • Renders a #task-count div that starts with "Estimating expected items count..." placeholder text.
  • Inline <script> fetches the count on page load and displays it as "N items expected to be processed".
  • Listens on focusout and change events on the task form to re-fetch when parameters are modified.
  • Hidden for deleted tasks, CSV tasks, and no-collection tasks.
  • CSP updated with a new script hash for the inline script.

How it looks

  • Task with collection: Shows "42 items expected to be processed" below the parameters form.
  • Task with parameters: Count updates dynamically as parameters change (e.g. entering post IDs narrows the count).
  • CSV / no-collection / deleted tasks: Count indicator is not shown.
  • Timeout or error: Count indicator is silently hidden.
Screenshot 2026-02-12 at 2 56 16 PM

@mikee649 mikee649 requested a review from nvasilevski February 12, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant