-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.rb
More file actions
559 lines (508 loc) · 18.1 KB
/
Copy pathapplication.rb
File metadata and controls
559 lines (508 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# frozen_string_literal: true
require "ratatui_ruby"
require "json"
module SolidQueueTui
class Application
VIEW_DASHBOARD = 0
VIEW_QUEUES = 1
VIEW_FAILED = 2
VIEW_IN_PROGRESS = 3
VIEW_BLOCKED = 4
VIEW_SCHEDULED = 5
VIEW_FINISHED = 6
VIEW_RECURRING = 7
VIEW_WORKERS = 8
VIEW_COUNT = 9
COMMAND_MAP = {
"dashboard" => VIEW_DASHBOARD,
"queues" => VIEW_QUEUES,
"failed" => VIEW_FAILED,
"inprogress" => VIEW_IN_PROGRESS,
"blocked" => VIEW_BLOCKED,
"scheduled" => VIEW_SCHEDULED,
"finished" => VIEW_FINISHED,
"recurring" => VIEW_RECURRING,
"workers" => VIEW_WORKERS
}.freeze
def initialize(dev: false)
@current_view = VIEW_DASHBOARD
@last_refresh = Time.at(0)
@stats = Data::Stats.empty
@show_help = false
@command_mode = false
@command_input = ""
@command_error = nil
@dev = dev
end
def run
@refresh_interval = SolidQueueTui.refresh_interval
setup_dev_reloader! if @dev
RatatuiRuby.run do |tui|
@tui = tui
init_views
refresh_data!
loop do
hot_reload! if @dev
render
event = @tui.poll_event
refresh_data_if_needed
break if handle_input(event)
end
end
end
private
def init_views
@views = {
VIEW_DASHBOARD => Views::DashboardView.new(@tui),
VIEW_QUEUES => Views::QueuesView.new(@tui),
VIEW_FAILED => Views::FailedView.new(@tui),
VIEW_IN_PROGRESS => Views::InProgressView.new(@tui),
VIEW_BLOCKED => Views::BlockedView.new(@tui),
VIEW_SCHEDULED => Views::ScheduledView.new(@tui),
VIEW_FINISHED => Views::FinishedView.new(@tui),
VIEW_RECURRING => Views::RecurringTasksView.new(@tui),
VIEW_WORKERS => Views::ProcessesView.new(@tui)
}
@job_detail = Views::JobDetailView.new(@tui)
end
def render
@tui.draw do |frame|
if @show_help
render_help_overlay(frame, frame.area)
return
end
# Layout: header (6) | content (fill) | help_bar (1)
header_area, content_area, help_area = @tui.layout_split(
frame.area,
direction: :vertical,
constraints: [
@tui.constraint_length(6),
@tui.constraint_fill(1),
@tui.constraint_length(1)
]
)
# Header
Components::Header.new(
@tui,
current_view: @current_view
).render(frame, header_area)
# Content — current view or detail overlay
if @job_detail.active?
@job_detail.render(frame, content_area)
else
current_view.render(frame, content_area)
end
# Help bar or command input
if @command_mode
render_command_bar(frame, help_area)
else
active_view = @job_detail.active? ? @job_detail : current_view
Components::HelpBar.new(
@tui,
breadcrumb: active_view.breadcrumb,
bindings: active_view.bindings,
status: status_message
).render(frame, help_area)
end
end
end
def handle_input(event)
return false unless event
# Job detail overlay gets priority
if @job_detail.active?
result = @job_detail.handle_input(event)
refresh_data! if result == :refresh
return false
end
# Help overlay
if @show_help
case event
in { type: :key, code: "esc" } | { type: :key, code: "?" } | { type: :key, code: "q" }
@show_help = false
else
nil
end
return false
end
# If view is in a modal state (filter, confirm, detail sub-view), it gets all input
if current_view.respond_to?(:capturing_input?) && current_view.capturing_input?
result = current_view.handle_input(event)
case result
when :refresh, :enter_queue, :exit_queue
refresh_data!
when :load_more
load_more_data!
when :open_detail
open_detail
end
return false
end
# Command mode input
if @command_mode
handle_command_input(event)
return @quit || false
end
# Global keybindings
case event
in { type: :key, code: "q" }
return true
in { type: :key, code: "c", modifiers: ["ctrl"] }
return true
in { type: :key, code: "esc" }
switch_view(VIEW_DASHBOARD) if @current_view != VIEW_DASHBOARD
return false
in { type: :key, code: "r" }
refresh_data!
return false
in { type: :key, code: "?" }
@show_help = true
return false
in { type: :key, code: "1" }
switch_view(VIEW_DASHBOARD)
return false
in { type: :key, code: "2" }
switch_view(VIEW_QUEUES)
return false
in { type: :key, code: "3" }
switch_view(VIEW_FAILED)
return false
in { type: :key, code: "4" }
switch_view(VIEW_IN_PROGRESS)
return false
in { type: :key, code: "5" }
switch_view(VIEW_BLOCKED)
return false
in { type: :key, code: "6" }
switch_view(VIEW_SCHEDULED)
return false
in { type: :key, code: "7" }
switch_view(VIEW_FINISHED)
return false
in { type: :key, code: "8" }
switch_view(VIEW_RECURRING)
return false
in { type: :key, code: "9" }
switch_view(VIEW_WORKERS)
return false
in { type: :key, code: "tab" }
switch_view((@current_view + 1) % VIEW_COUNT)
return false
in { type: :key, code: "back_tab" }
switch_view((@current_view - 1) % VIEW_COUNT)
return false
in { type: :key, code: "enter" }
open_detail
return false
in { type: :key, code: ":" }
@command_mode = true
@command_input = ""
@command_error = nil
return false
else
nil
end
# Pass to current view
result = current_view.handle_input(event)
if result == :refresh
refresh_data!
elsif result == :load_more
load_more_data!
end
false
end
def current_view
@views[@current_view]
end
def switch_view(index)
@current_view = index
refresh_data!
end
def open_detail
item = current_view.selected_item
return unless item
case @current_view
when VIEW_QUEUES
if current_view.detail_mode?
@job_detail.show(job: item) if item.respond_to?(:id)
else
result = current_view.handle_input({ type: :key, code: "enter" })
refresh_data! if result == :enter_queue
end
when VIEW_FAILED
failed_job = Data::FailedQuery.fetch_one(item.id) if item.respond_to?(:id)
@job_detail.show(failed_job: failed_job || item)
when VIEW_IN_PROGRESS, VIEW_BLOCKED, VIEW_SCHEDULED, VIEW_FINISHED
@job_detail.show(job: item) if item.respond_to?(:id)
when VIEW_WORKERS
running_jobs = Data::ProcessesQuery.fetch_running_jobs(process_id: item.id)
@job_detail.show(process: item, running_jobs: running_jobs)
end
end
def refresh_data_if_needed
return if Time.now - @last_refresh < @refresh_interval
refresh_data!
end
def refresh_data!
@last_refresh = Time.now
case @current_view
when VIEW_DASHBOARD
@stats = Data::Stats.fetch
current_view.update(stats: @stats)
when VIEW_QUEUES
if current_view.detail_mode?
q = current_view.selected_queue_name
f = current_view.filters
current_view.total_count = Data::JobsQuery.count_pending(queue: q, filter: f[:class_name])
jobs = Data::JobsQuery.fetch_pending(queue: q, filter: f[:class_name], limit: SolidQueueTui.page_size, offset: 0)
current_view.update_detail(jobs: jobs)
else
queues = Data::QueuesQuery.fetch
current_view.update(queues: queues)
end
when VIEW_FAILED
f = current_view.filters
current_view.total_count = Data::FailedQuery.count(filter: f[:class_name], queue: f[:queue])
failed_jobs = Data::FailedQuery.fetch(filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: 0)
current_view.update(failed_jobs: failed_jobs)
when VIEW_IN_PROGRESS
f = current_view.filters
current_view.total_count = Data::JobsQuery.count(status: "claimed", filter: f[:class_name], queue: f[:queue])
jobs = Data::JobsQuery.fetch(status: "claimed", filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: 0)
current_view.update(jobs: jobs)
when VIEW_BLOCKED
f = current_view.filters
current_view.total_count = Data::JobsQuery.count(status: "blocked", filter: f[:class_name], queue: f[:queue])
jobs = Data::JobsQuery.fetch(status: "blocked", filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: 0)
current_view.update(jobs: jobs)
when VIEW_SCHEDULED
f = current_view.filters
current_view.total_count = Data::JobsQuery.count(status: "scheduled", filter: f[:class_name], queue: f[:queue])
jobs = Data::JobsQuery.fetch(status: "scheduled", filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: 0)
current_view.update(jobs: jobs)
when VIEW_FINISHED
f = current_view.filters
since = current_view.date_range_start
current_view.total_count = Data::JobsQuery.count(status: "completed", filter: f[:class_name], queue: f[:queue], since: since)
jobs = Data::JobsQuery.fetch(status: "completed", filter: f[:class_name], queue: f[:queue], since: since, limit: SolidQueueTui.page_size, offset: 0)
current_view.update(jobs: jobs)
when VIEW_RECURRING
tasks = Data::RecurringTasksQuery.fetch
current_view.update(tasks: tasks)
when VIEW_WORKERS
processes = Data::ProcessesQuery.fetch
current_view.update(processes: processes)
end
rescue => e
Rails.logger.tagged("SQTUI") { Rails.logger.error("refresh_data! error: #{e.class}: #{e.message}\n#{e.backtrace&.first(5)&.join("\n")}") } if defined?(Rails) && Rails.logger
end
def load_more_data!
view = current_view
offset = view.current_offset
case @current_view
when VIEW_QUEUES
if view.detail_mode?
q = view.selected_queue_name
f = view.filters
more = Data::JobsQuery.fetch_pending(queue: q, filter: f[:class_name], limit: SolidQueueTui.page_size, offset: offset)
view.append(jobs: more)
end
when VIEW_FAILED
f = view.filters
more = Data::FailedQuery.fetch(filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: offset)
view.append(failed_jobs: more)
when VIEW_IN_PROGRESS
f = view.filters
more = Data::JobsQuery.fetch(status: "claimed", filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: offset)
view.append(jobs: more)
when VIEW_BLOCKED
f = view.filters
more = Data::JobsQuery.fetch(status: "blocked", filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: offset)
view.append(jobs: more)
when VIEW_SCHEDULED
f = view.filters
more = Data::JobsQuery.fetch(status: "scheduled", filter: f[:class_name], queue: f[:queue], limit: SolidQueueTui.page_size, offset: offset)
view.append(jobs: more)
when VIEW_FINISHED
f = view.filters
since = view.date_range_start
more = Data::JobsQuery.fetch(status: "completed", filter: f[:class_name], queue: f[:queue], since: since, limit: SolidQueueTui.page_size, offset: offset)
view.append(jobs: more)
end
rescue => e
Rails.logger.tagged("SQTUI") { Rails.logger.error("load_more_data! error: #{e.class}: #{e.message}") } if defined?(Rails) && Rails.logger
end
def setup_dev_reloader!
lib_dir = File.expand_path("../..", __FILE__)
@reloader = DevReloader.new(lib_dir)
end
# Check for file changes and re-instantiate views if code was reloaded.
def hot_reload!
return unless @reloader
if @reloader.check!
init_views
refresh_data!
end
end
def handle_command_input(event)
case event
in { type: :key, code: "enter" }
execute_command(@command_input.strip)
@command_mode = false
@command_input = ""
in { type: :key, code: "esc" }
@command_mode = false
@command_input = ""
@command_error = nil
in { type: :key, code: "tab" }
autocomplete_command
in { type: :key, code: "backspace" }
@command_input = @command_input[0...-1]
in { type: :key, code: /\A.\z/ => char }
@command_input += char
else
nil
end
end
def autocomplete_command
input = @command_input.strip
commands = COMMAND_MAP.keys
if input.empty?
@command_input = commands.first
return
end
matches = commands.select { |cmd| cmd.start_with?(input) }
return if matches.empty?
if matches.size == 1
@command_input = matches.first
elsif @command_input == matches.first
# Already on first match, cycle to next
idx = matches.index(@command_input) || 0
@command_input = matches[(idx + 1) % matches.size]
else
# Complete to first match
@command_input = matches.first
end
end
def execute_command(input)
return if input.empty?
if %w[quit exit].include?(input)
@quit = true
return
end
# Exact match first
if COMMAND_MAP.key?(input)
switch_view(COMMAND_MAP[input])
return
end
# Prefix match
matches = COMMAND_MAP.keys.select { |cmd| cmd.start_with?(input) }
if matches.size == 1
switch_view(COMMAND_MAP[matches.first])
end
end
def render_command_bar(frame, area)
# Show completions as hint
input = @command_input.strip
hint = if input.empty?
COMMAND_MAP.keys.uniq { |k| COMMAND_MAP[k] }.join(" ")
else
matches = COMMAND_MAP.keys.select { |cmd| cmd.start_with?(input) }
matches.empty? ? "no match" : matches.join(" ")
end
frame.render_widget(
@tui.paragraph(
text: @tui.text_line(spans: [
@tui.text_span(content: ":", style: @tui.style(fg: :cyan, modifiers: [:bold])),
@tui.text_span(content: @command_input, style: @tui.style(fg: :white)),
@tui.text_span(content: "\u2588", style: @tui.style(fg: :white)),
@tui.text_span(content: " #{hint}", style: @tui.style(fg: :dark_gray))
]),
style: @tui.style(fg: :white)
),
area
)
end
def status_message
elapsed = (Time.now - @last_refresh).to_i
"Last refresh: #{elapsed}s ago"
end
def render_help_overlay(frame, area)
help_text = [
@tui.text_line(spans: [
@tui.text_span(content: " SOLID QUEUE TUI — KEYBOARD SHORTCUTS", style: @tui.style(fg: :yellow, modifiers: [:bold]))
]),
empty_line,
help_section("Navigation"),
help_line("1-9", "Switch between views"),
help_line("Tab", "Next view"),
help_line("Shift + Tab", "Previous View"),
help_line(":", "Command mode (:queues, :failed, ...)"),
help_line("Esc", "Back to Dashboard"),
help_line("j / Up", "Move selection up"),
help_line("k / Down", "Move selection down"),
help_line("g", "Jump to top"),
help_line("G", "Jump to bottom"),
help_line("Enter", "View details"),
empty_line,
help_section("Actions"),
help_line("r", "Refresh data"),
help_line("/", "Filter (class, queue, date range)"),
help_line("c", "Clear active filter"),
help_line("R", "Retry failed job (in Failed view)"),
help_line("D", "Discard failed job (in Failed view)"),
help_line("A", "Retry all failed jobs"),
empty_line,
help_section("Views"),
help_line("1", "Dashboard — Overview with stats"),
help_line("2", "Queues — Per-queue breakdown"),
help_line("3", "Failed — Failed jobs with errors"),
help_line("4", "In Progress — Currently processing"),
help_line("5", "Blocked — Concurrency-blocked jobs"),
help_line("6", "Scheduled — Future scheduled jobs"),
help_line("7", "Finished — Completed jobs"),
help_line("8", "Recurring — Recurring tasks"),
help_line("9", "Workers — Active processes"),
empty_line,
help_section("General"),
help_line("?", "Toggle this help"),
help_line("q", "Quit"),
help_line("Ctrl+C", "Force quit"),
help_line("fn + select", "Select Text")
]
frame.render_widget(
@tui.paragraph(
text: help_text,
block: @tui.block(
title: " Help ",
title_style: @tui.style(fg: :cyan, modifiers: [:bold]),
titles: [
{ content: " Press ? or Esc to close ",
position: :bottom, alignment: :center }
],
borders: [:all],
border_type: :rounded,
border_style: @tui.style(fg: :cyan),
style: @tui.style(fg: :white)
)
),
area
)
end
def help_section(title)
@tui.text_line(spans: [
@tui.text_span(content: " ── #{title} ──", style: @tui.style(fg: :cyan, modifiers: [:bold]))
])
end
def help_line(key, desc)
@tui.text_line(spans: [
@tui.text_span(content: " #{key.ljust(14)}", style: @tui.style(fg: :green, modifiers: [:bold])),
@tui.text_span(content: desc, style: @tui.style(fg: :white))
])
end
def empty_line
@tui.text_line(spans: [
@tui.text_span(content: "", style: @tui.style(fg: :white))
])
end
end
end