|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SolidQueueMonitor |
| 4 | + class SearchResultsPresenter < BasePresenter |
| 5 | + def initialize(query, results) |
| 6 | + @query = query |
| 7 | + @results = results |
| 8 | + end |
| 9 | + |
| 10 | + def render |
| 11 | + section_wrapper('Search Results', generate_content) |
| 12 | + end |
| 13 | + |
| 14 | + private |
| 15 | + |
| 16 | + def generate_content |
| 17 | + if @query.blank? |
| 18 | + generate_empty_query_message |
| 19 | + elsif total_count.zero? |
| 20 | + generate_no_results_message |
| 21 | + else |
| 22 | + generate_results_summary + generate_all_sections |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def generate_empty_query_message |
| 27 | + <<-HTML |
| 28 | + <div class="empty-state"> |
| 29 | + <p>Enter a search term in the header to find jobs across all categories.</p> |
| 30 | + </div> |
| 31 | + HTML |
| 32 | + end |
| 33 | + |
| 34 | + def generate_no_results_message |
| 35 | + <<-HTML |
| 36 | + <div class="empty-state"> |
| 37 | + <p>No results found for "#{escape_html(@query)}"</p> |
| 38 | + <p class="results-summary">0 results</p> |
| 39 | + </div> |
| 40 | + HTML |
| 41 | + end |
| 42 | + |
| 43 | + def generate_results_summary |
| 44 | + <<-HTML |
| 45 | + <div class="results-summary"> |
| 46 | + <p>Found #{total_count} #{total_count == 1 ? 'result' : 'results'} for "#{escape_html(@query)}"</p> |
| 47 | + </div> |
| 48 | + HTML |
| 49 | + end |
| 50 | + |
| 51 | + def generate_all_sections |
| 52 | + sections = [] |
| 53 | + sections << generate_ready_section if @results[:ready].any? |
| 54 | + sections << generate_scheduled_section if @results[:scheduled].any? |
| 55 | + sections << generate_failed_section if @results[:failed].any? |
| 56 | + sections << generate_in_progress_section if @results[:in_progress].any? |
| 57 | + sections << generate_completed_section if @results[:completed].any? |
| 58 | + sections << generate_recurring_section if @results[:recurring].any? |
| 59 | + sections.join |
| 60 | + end |
| 61 | + |
| 62 | + def generate_ready_section |
| 63 | + generate_section('Ready Jobs', @results[:ready]) do |execution| |
| 64 | + generate_job_row(execution.job, execution.queue_name, execution.created_at) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def generate_scheduled_section |
| 69 | + generate_section('Scheduled Jobs', @results[:scheduled]) do |execution| |
| 70 | + generate_job_row(execution.job, execution.queue_name, execution.scheduled_at, 'Scheduled for') |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def generate_failed_section |
| 75 | + generate_section('Failed Jobs', @results[:failed]) do |execution| |
| 76 | + generate_failed_row(execution) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def generate_in_progress_section |
| 81 | + generate_section('In Progress Jobs', @results[:in_progress]) do |execution| |
| 82 | + generate_job_row(execution.job, execution.job.queue_name, execution.created_at, 'Started at') |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + def generate_completed_section |
| 87 | + generate_section('Completed Jobs', @results[:completed]) do |job| |
| 88 | + generate_completed_row(job) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + def generate_recurring_section |
| 93 | + generate_section('Recurring Tasks', @results[:recurring]) do |task| |
| 94 | + generate_recurring_row(task) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + def generate_section(title, items, &block) |
| 99 | + <<-HTML |
| 100 | + <div class="search-results-section"> |
| 101 | + <h3>#{title} (#{items.size})</h3> |
| 102 | + <div class="table-container"> |
| 103 | + <table> |
| 104 | + <thead> |
| 105 | + <tr> |
| 106 | + #{section_headers(title)} |
| 107 | + </tr> |
| 108 | + </thead> |
| 109 | + <tbody> |
| 110 | + #{items.map(&block).join} |
| 111 | + </tbody> |
| 112 | + </table> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + HTML |
| 116 | + end |
| 117 | + |
| 118 | + def section_headers(title) |
| 119 | + case title |
| 120 | + when 'Recurring Tasks' |
| 121 | + '<th>Key</th><th>Class</th><th>Schedule</th><th>Queue</th>' |
| 122 | + when 'Failed Jobs' |
| 123 | + '<th>Job</th><th>Queue</th><th>Error</th><th>Failed At</th>' |
| 124 | + when 'Completed Jobs' |
| 125 | + '<th>Job</th><th>Queue</th><th>Arguments</th><th>Completed At</th>' |
| 126 | + else |
| 127 | + '<th>Job</th><th>Queue</th><th>Arguments</th><th>Time</th>' |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + def generate_job_row(job, queue_name, time, time_label = 'Created at') |
| 132 | + <<-HTML |
| 133 | + <tr> |
| 134 | + <td><a href="#{job_path(job)}" class="job-class-link">#{job.class_name}</a></td> |
| 135 | + <td>#{queue_link(queue_name)}</td> |
| 136 | + <td>#{format_arguments(job.arguments)}</td> |
| 137 | + <td> |
| 138 | + <span class="job-timestamp">#{time_label}: #{format_datetime(time)}</span> |
| 139 | + </td> |
| 140 | + </tr> |
| 141 | + HTML |
| 142 | + end |
| 143 | + |
| 144 | + def generate_failed_row(execution) |
| 145 | + job = execution.job |
| 146 | + <<-HTML |
| 147 | + <tr> |
| 148 | + <td><a href="#{job_path(job)}" class="job-class-link">#{job.class_name}</a></td> |
| 149 | + <td>#{queue_link(job.queue_name)}</td> |
| 150 | + <td><div class="error-message">#{escape_html(execution.error.to_s.truncate(100))}</div></td> |
| 151 | + <td> |
| 152 | + <span class="job-timestamp">#{format_datetime(execution.created_at)}</span> |
| 153 | + </td> |
| 154 | + </tr> |
| 155 | + HTML |
| 156 | + end |
| 157 | + |
| 158 | + def generate_completed_row(job) |
| 159 | + <<-HTML |
| 160 | + <tr> |
| 161 | + <td><a href="#{job_path(job)}" class="job-class-link">#{job.class_name}</a></td> |
| 162 | + <td>#{queue_link(job.queue_name)}</td> |
| 163 | + <td>#{format_arguments(job.arguments)}</td> |
| 164 | + <td> |
| 165 | + <span class="job-timestamp">#{format_datetime(job.finished_at)}</span> |
| 166 | + </td> |
| 167 | + </tr> |
| 168 | + HTML |
| 169 | + end |
| 170 | + |
| 171 | + def generate_recurring_row(task) |
| 172 | + <<-HTML |
| 173 | + <tr> |
| 174 | + <td><strong>#{task.key}</strong></td> |
| 175 | + <td>#{task.class_name || '-'}</td> |
| 176 | + <td><code>#{task.schedule}</code></td> |
| 177 | + <td>#{queue_link(task.queue_name)}</td> |
| 178 | + </tr> |
| 179 | + HTML |
| 180 | + end |
| 181 | + |
| 182 | + def total_count |
| 183 | + @total_count ||= @results.values.sum(&:size) |
| 184 | + end |
| 185 | + |
| 186 | + def escape_html(text) |
| 187 | + text.to_s.gsub('&', '&').gsub('<', '<').gsub('>', '>').gsub('"', '"') |
| 188 | + end |
| 189 | + end |
| 190 | +end |
0 commit comments