Module: SourceMonitor::Dashboard::TurboBroadcaster

Defined in:
lib/source_monitor/dashboard/turbo_broadcaster.rb

Constant Summary collapse

STREAM_NAME =
"source_monitor_dashboard"
STAT_CARDS =
[
  { key: "total_sources", label: "Sources", stat: :total_sources, caption: "Total registered" },
  { key: "active_sources", label: "Active", stat: :active_sources, caption: "Fetching on schedule" },
  { key: "failed_sources", label: "Failures", stat: :failed_sources, caption: "Require attention" },
  { key: "total_items", label: "Items", stat: :total_items, caption: "Stored entries" },
  { key: "fetches_today", label: "Fetches Today", stat: :fetches_today, caption: "Completed runs" }
].freeze

Class Method Summary collapse

Class Method Details

.broadcast_dashboard_updatesObject



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
# File 'lib/source_monitor/dashboard/turbo_broadcaster.rb', line 33

def broadcast_dashboard_updates
  return unless turbo_streams_available?

  queries = SourceMonitor::Dashboard::Queries.new
  url_helpers = SourceMonitor::Engine.routes.url_helpers
  stats = queries.stats

  STAT_CARDS.each do |card|
    Turbo::StreamsChannel.broadcast_replace_to(
      STREAM_NAME,
      target: "source_monitor_stat_#{card[:key]}",
      html: render_partial("source_monitor/dashboard/stat_card", stat_card: {
        key: card[:key],
        label: card[:label],
        value: stats[card[:stat]],
        caption: card[:caption]
      })
    )
  end

  Turbo::StreamsChannel.broadcast_replace_to(
    STREAM_NAME,
    target: "source_monitor_dashboard_recent_activity",
    html: render_partial(
      "source_monitor/dashboard/recent_activity",
      recent_activity: SourceMonitor::Dashboard::RecentActivityPresenter.new(
        queries.recent_activity,
        url_helpers:
      ).to_a
    )
  )

  fetch_schedule = queries.upcoming_fetch_schedule
  Turbo::StreamsChannel.broadcast_replace_to(
    STREAM_NAME,
    target: "source_monitor_dashboard_fetch_schedule",
    html: render_partial(
      "source_monitor/dashboard/fetch_schedule",
      groups: fetch_schedule.groups,
      reference_time: fetch_schedule.reference_time
    )
  )
rescue StandardError => error
  Rails.logger.error(
    "[SourceMonitor] Turbo stream broadcast failed: #{error.class}: #{error.message}"
  )
end

.fetch_callbackObject



17
18
19
# File 'lib/source_monitor/dashboard/turbo_broadcaster.rb', line 17

def fetch_callback
  @fetch_callback ||= lambda { |_event| broadcast_dashboard_updates }
end

.item_callbackObject



21
22
23
# File 'lib/source_monitor/dashboard/turbo_broadcaster.rb', line 21

def item_callback
  @item_callback ||= lambda { |_event| broadcast_dashboard_updates }
end

.setup!Object



10
11
12
13
14
15
# File 'lib/source_monitor/dashboard/turbo_broadcaster.rb', line 10

def setup!
  return unless turbo_streams_available?

  register_callback(:after_fetch_completed, fetch_callback)
  register_callback(:after_item_created, item_callback)
end