cosmonats 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +73 -22
  3. data/lib/cosmo/active_job/adapter.rb +2 -0
  4. data/lib/cosmo/active_job/executor.rb +7 -0
  5. data/lib/cosmo/active_job/options.rb +6 -0
  6. data/lib/cosmo/api/batch.rb +26 -0
  7. data/lib/cosmo/api/counter.rb +52 -4
  8. data/lib/cosmo/api/cron/entry.rb +34 -20
  9. data/lib/cosmo/api/cron.rb +24 -10
  10. data/lib/cosmo/api/job.rb +15 -1
  11. data/lib/cosmo/api/kv.rb +141 -12
  12. data/lib/cosmo/api/registry.rb +47 -6
  13. data/lib/cosmo/api/stats/jobs.rb +84 -0
  14. data/lib/cosmo/api/stats/metrics.rb +20 -9
  15. data/lib/cosmo/api/stats/processes.rb +27 -11
  16. data/lib/cosmo/api/stats/totals.rb +1 -0
  17. data/lib/cosmo/api/stats.rb +58 -7
  18. data/lib/cosmo/api/stream.rb +73 -24
  19. data/lib/cosmo/batch/callback.rb +5 -0
  20. data/lib/cosmo/batch/dispatcher.rb +4 -4
  21. data/lib/cosmo/batch.rb +34 -11
  22. data/lib/cosmo/cli.rb +6 -0
  23. data/lib/cosmo/client.rb +217 -23
  24. data/lib/cosmo/config/cosmo.yml +1 -0
  25. data/lib/cosmo/config/settings.rb +4 -0
  26. data/lib/cosmo/config.rb +58 -10
  27. data/lib/cosmo/engine.rb +28 -1
  28. data/lib/cosmo/heartbeat.rb +12 -3
  29. data/lib/cosmo/http_server/handler.rb +5 -0
  30. data/lib/cosmo/http_server/health.rb +2 -0
  31. data/lib/cosmo/http_server/ping.rb +2 -0
  32. data/lib/cosmo/http_server.rb +8 -0
  33. data/lib/cosmo/job/data.rb +12 -1
  34. data/lib/cosmo/job/enqueuer.rb +100 -1
  35. data/lib/cosmo/job/limit.rb +5 -1
  36. data/lib/cosmo/job/processor.rb +56 -21
  37. data/lib/cosmo/job/setter.rb +53 -0
  38. data/lib/cosmo/job.rb +71 -2
  39. data/lib/cosmo/logger.rb +37 -0
  40. data/lib/cosmo/middleware/chain.rb +12 -0
  41. data/lib/cosmo/middleware/limit.rb +12 -3
  42. data/lib/cosmo/middleware/metrics.rb +6 -4
  43. data/lib/cosmo/middleware/sentry.rb +6 -4
  44. data/lib/cosmo/middleware/totals.rb +5 -11
  45. data/lib/cosmo/middleware.rb +0 -1
  46. data/lib/cosmo/processor.rb +60 -3
  47. data/lib/cosmo/publisher.rb +8 -0
  48. data/lib/cosmo/services.rb +7 -2
  49. data/lib/cosmo/stream/message.rb +5 -0
  50. data/lib/cosmo/stream/processor.rb +7 -0
  51. data/lib/cosmo/stream/serializer.rb +4 -0
  52. data/lib/cosmo/stream.rb +35 -1
  53. data/lib/cosmo/utils/duration.rb +3 -0
  54. data/lib/cosmo/utils/hash.rb +12 -0
  55. data/lib/cosmo/utils/json.rb +8 -0
  56. data/lib/cosmo/utils/signal.rb +9 -0
  57. data/lib/cosmo/utils/stopwatch.rb +1 -0
  58. data/lib/cosmo/utils/string.rb +4 -0
  59. data/lib/cosmo/utils/thread_pool.rb +8 -0
  60. data/lib/cosmo/utils/ttl_cache.rb +10 -0
  61. data/lib/cosmo/utils.rb +0 -1
  62. data/lib/cosmo/version.rb +1 -1
  63. data/lib/cosmo/web/assets/app.css +273 -629
  64. data/lib/cosmo/web/assets/app.js +158 -0
  65. data/lib/cosmo/web/assets/fonts/OFL.txt +92 -0
  66. data/lib/cosmo/web/assets/fonts/geist-mono.woff2 +0 -0
  67. data/lib/cosmo/web/assets/fonts/geist.woff2 +0 -0
  68. data/lib/cosmo/web/assets/logo.svg +10 -0
  69. data/lib/cosmo/web/cache.rb +77 -0
  70. data/lib/cosmo/web/chart.rb +63 -39
  71. data/lib/cosmo/web/circuit.rb +56 -0
  72. data/lib/cosmo/web/context.rb +6 -4
  73. data/lib/cosmo/web/controllers/application.rb +42 -0
  74. data/lib/cosmo/web/controllers/batches.rb +8 -1
  75. data/lib/cosmo/web/controllers/crons.rb +24 -3
  76. data/lib/cosmo/web/controllers/jobs.rb +88 -68
  77. data/lib/cosmo/web/controllers/metrics.rb +6 -0
  78. data/lib/cosmo/web/controllers/overview.rb +88 -0
  79. data/lib/cosmo/web/controllers/processes.rb +36 -0
  80. data/lib/cosmo/web/controllers/streams.rb +38 -28
  81. data/lib/cosmo/web/helpers/application.rb +101 -0
  82. data/lib/cosmo/web/renderer.rb +36 -6
  83. data/lib/cosmo/web/throughput.rb +104 -0
  84. data/lib/cosmo/web/views/batches/_table.erb +42 -54
  85. data/lib/cosmo/web/views/batches/index.erb +3 -10
  86. data/lib/cosmo/web/views/crons/_table.erb +36 -55
  87. data/lib/cosmo/web/views/crons/index.erb +2 -9
  88. data/lib/cosmo/web/views/jobs/_args.erb +5 -0
  89. data/lib/cosmo/web/views/jobs/_busy.erb +25 -121
  90. data/lib/cosmo/web/views/jobs/_dead.erb +42 -82
  91. data/lib/cosmo/web/views/jobs/_enqueued.erb +33 -113
  92. data/lib/cosmo/web/views/jobs/_panel.erb +34 -0
  93. data/lib/cosmo/web/views/jobs/_queue_row.erb +18 -0
  94. data/lib/cosmo/web/views/jobs/_queues.erb +20 -0
  95. data/lib/cosmo/web/views/jobs/_scheduled.erb +20 -54
  96. data/lib/cosmo/web/views/jobs/index.erb +4 -17
  97. data/lib/cosmo/web/views/layout.erb +73 -22
  98. data/lib/cosmo/web/views/metrics/_chart.erb +37 -32
  99. data/lib/cosmo/web/views/metrics/_panel.erb +3 -3
  100. data/lib/cosmo/web/views/metrics/_table.erb +24 -40
  101. data/lib/cosmo/web/views/metrics/index.erb +8 -16
  102. data/lib/cosmo/web/views/overview/_badges.erb +3 -0
  103. data/lib/cosmo/web/views/overview/_kpis.erb +19 -0
  104. data/lib/cosmo/web/views/overview/_running.erb +18 -0
  105. data/lib/cosmo/web/views/overview/_throughput.erb +50 -0
  106. data/lib/cosmo/web/views/overview/_workers.erb +25 -0
  107. data/lib/cosmo/web/views/overview/index.erb +28 -0
  108. data/lib/cosmo/web/views/processes/_table.erb +67 -0
  109. data/lib/cosmo/web/views/processes/index.erb +1 -0
  110. data/lib/cosmo/web/views/shared/_pager.erb +22 -0
  111. data/lib/cosmo/web/views/shared/unavailable.erb +7 -0
  112. data/lib/cosmo/web/views/streams/_info.erb +29 -86
  113. data/lib/cosmo/web/views/streams/_list.erb +7 -0
  114. data/lib/cosmo/web/views/streams/_pause_banner.erb +8 -16
  115. data/lib/cosmo/web/views/streams/_stream.erb +53 -0
  116. data/lib/cosmo/web/views/streams/index.erb +3 -11
  117. data/lib/cosmo/web/views/streams/info.erb +3 -11
  118. data/lib/cosmo/web.rb +73 -35
  119. data/lib/cosmo.rb +33 -0
  120. data/sig/cosmo/active_job/adapter.rbs +3 -3
  121. data/sig/cosmo/active_job/options.rbs +4 -2
  122. data/sig/cosmo/api/batch.rbs +5 -3
  123. data/sig/cosmo/api/counter.rbs +16 -6
  124. data/sig/cosmo/api/cron/entry.rbs +6 -3
  125. data/sig/cosmo/api/cron.rbs +10 -4
  126. data/sig/cosmo/api/kv.rbs +21 -4
  127. data/sig/cosmo/api/registry.rbs +10 -0
  128. data/sig/cosmo/api/stats/jobs.rbs +34 -0
  129. data/sig/cosmo/api/stats/metrics.rbs +4 -1
  130. data/sig/cosmo/api/stats/processes.rbs +9 -1
  131. data/sig/cosmo/api/stats.rbs +12 -2
  132. data/sig/cosmo/api/stream.rbs +12 -5
  133. data/sig/cosmo/batch/callback.rbs +1 -1
  134. data/sig/cosmo/batch/dispatcher.rbs +1 -1
  135. data/sig/cosmo/batch.rbs +5 -5
  136. data/sig/cosmo/cli.rbs +2 -0
  137. data/sig/cosmo/client.rbs +24 -4
  138. data/sig/cosmo/config/settings.rbs +6 -0
  139. data/sig/cosmo/config.rbs +5 -5
  140. data/sig/cosmo/engine.rbs +5 -0
  141. data/sig/cosmo/heartbeat.rbs +2 -0
  142. data/sig/cosmo/job/data.rbs +3 -1
  143. data/sig/cosmo/job/enqueuer.rbs +12 -0
  144. data/sig/cosmo/job/processor.rbs +18 -3
  145. data/sig/cosmo/job/setter.rbs +20 -0
  146. data/sig/cosmo/job.rbs +10 -4
  147. data/sig/cosmo/logger.rbs +5 -3
  148. data/sig/cosmo/middleware.rbs +0 -8
  149. data/sig/cosmo/processor.rbs +14 -2
  150. data/sig/cosmo/publisher.rbs +1 -0
  151. data/sig/cosmo/stream/processor.rbs +4 -0
  152. data/sig/cosmo/stream.rbs +4 -3
  153. data/sig/cosmo/utils/hash.rbs +2 -1
  154. data/sig/cosmo/utils/signal.rbs +5 -5
  155. data/sig/cosmo/utils/string.rbs +2 -2
  156. data/sig/cosmo/utils/thread_pool.rbs +1 -1
  157. data/sig/cosmo/web/cache.rbs +35 -0
  158. data/sig/cosmo/web/chart.rbs +48 -0
  159. data/sig/cosmo/web/circuit.rbs +29 -0
  160. data/sig/cosmo/web/context.rbs +15 -0
  161. data/sig/cosmo/web/controllers/application.rbs +32 -0
  162. data/sig/cosmo/web/controllers/batches.rbs +11 -0
  163. data/sig/cosmo/web/controllers/crons.rbs +23 -0
  164. data/sig/cosmo/web/controllers/jobs.rbs +47 -0
  165. data/sig/cosmo/web/controllers/metrics.rbs +21 -0
  166. data/sig/cosmo/web/controllers/overview.rbs +31 -0
  167. data/sig/cosmo/web/controllers/processes.rbs +13 -0
  168. data/sig/cosmo/web/controllers/streams.rbs +27 -0
  169. data/sig/cosmo/web/helpers/application.rbs +61 -0
  170. data/sig/cosmo/web/renderer.rbs +33 -0
  171. data/sig/cosmo/web/throughput.rbs +45 -0
  172. data/sig/cosmo/web.rbs +25 -0
  173. data/sig/cosmo.rbs +19 -0
  174. metadata +53 -23
  175. data/lib/cosmo/api/stats/busy.rb +0 -63
  176. data/lib/cosmo/middleware/busy.rb +0 -15
  177. data/lib/cosmo/utils/overrides.rb +0 -206
  178. data/lib/cosmo/utils/warnings.rb +0 -17
  179. data/lib/cosmo/web/controllers/actions.rb +0 -16
  180. data/lib/cosmo/web/views/actions/index.erb +0 -7
  181. data/lib/cosmo/web/views/jobs/_processes.erb +0 -145
  182. data/lib/cosmo/web/views/jobs/_stats.erb +0 -77
  183. data/lib/cosmo/web/views/jobs/_tabs.erb +0 -14
  184. data/lib/cosmo/web/views/jobs/busy.erb +0 -18
  185. data/lib/cosmo/web/views/jobs/dead.erb +0 -18
  186. data/lib/cosmo/web/views/jobs/enqueued.erb +0 -18
  187. data/lib/cosmo/web/views/jobs/scheduled.erb +0 -18
  188. data/lib/cosmo/web/views/streams/_stream_row.erb +0 -42
  189. data/lib/cosmo/web/views/streams/_table.erb +0 -25
  190. data/sig/cosmo/api/stats/busy.rbs +0 -31
@@ -6,36 +6,47 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Crons < Application
9
+ # Renders the crons page, or 404 while scheduling is disabled.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
13
  return not_found unless Config.scheduled.enabled
11
14
 
12
15
  content_for :title, "Crons"
16
+ content_for :subtitle, "Schedules parked in NATS. NATS keeps the time."
13
17
  ok render("crons/index", layout: true)
14
18
  end
15
19
 
20
+ # Renders the crons table.
21
+ #
22
+ # @return [Array] the Rack response
16
23
  def _table
17
24
  return not_found unless Config.scheduled.enabled
18
25
 
19
- ok render("crons/_table", { schedules: cron.all })
26
+ ok render("crons/_table", { groups: groups })
20
27
  end
21
28
 
22
29
  # Dispatch the job immediately, bypassing the schedule timer.
23
30
  # Expects params["subject"] = the schedule subject stored in NATS.
31
+ #
32
+ # @return [Array] the Rack response
24
33
  def run_now
25
34
  return not_found unless Config.scheduled.enabled
26
35
 
27
36
  subject = Rack::Utils.unescape(params["subject"].to_s)
28
37
  cron.run_now!(subject)
29
- ok
38
+ ok flash("#{label(subject)} enqueued now")
30
39
  end
31
40
 
32
41
  # Purge the schedule from NATS so it stops firing.
42
+ #
43
+ # @return [Array] the Rack response
33
44
  def delete
34
45
  return not_found unless Config.scheduled.enabled
35
46
 
36
47
  subject = Rack::Utils.unescape(params["subject"].to_s)
37
48
  cron.delete!(subject)
38
- _table
49
+ ok render("crons/_table", { groups: groups }) + flash("#{label(subject)} deleted")
39
50
  end
40
51
 
41
52
  private
@@ -43,6 +54,16 @@ module Cosmo
43
54
  def cron
44
55
  @cron ||= API::Cron.instance
45
56
  end
57
+
58
+ def groups
59
+ cron.all.group_by { _1[:stream] }
60
+ end
61
+
62
+ # "cosmo.cron.default.report_job.monthly" → "monthly", "cosmo.cron.default.report_job" → "report_job"
63
+ def label(subject)
64
+ parts = subject.split(".")
65
+ parts.size > 4 ? parts.drop(4).join(".") : parts.last
66
+ end
46
67
  end
47
68
  end
48
69
  end
@@ -6,135 +6,155 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Jobs < Application
9
+ # Renders the jobs page on the busy tab.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
- content_for :title, "Jobs"
11
- processes_query = params.slice("page", "limit").transform_values(&:to_i).select { _2.positive? }
12
- ok render("jobs/index", { processes_query: }, layout: true)
13
+ tab("busy")
13
14
  end
14
15
 
16
+ # Renders the busy tab.
17
+ #
18
+ # @return [Array] the Rack response
15
19
  def busy
16
- return _busy if hx_request?
17
-
18
- content_for :title, "Busy Jobs"
19
- ok render("jobs/busy", layout: true)
20
+ tab("busy")
20
21
  end
21
22
 
23
+ # Renders the enqueued tab.
24
+ #
25
+ # @return [Array] the Rack response
22
26
  def enqueued
23
- return _enqueued if hx_request?
24
-
25
- content_for :title, "Enqueued Jobs"
26
- stream_name, _stream_names = streams
27
- ok render("jobs/enqueued", { stream_name: }, layout: true)
27
+ tab("enqueued")
28
28
  end
29
29
 
30
+ # Renders the scheduled tab, or 404 while scheduling is disabled.
31
+ #
32
+ # @return [Array] the Rack response
30
33
  def scheduled
31
34
  return not_found unless Config.scheduled.enabled
32
35
 
33
- return _scheduled if hx_request?
34
-
35
- content_for :title, "Scheduled Jobs"
36
- ok render("jobs/scheduled", layout: true)
36
+ tab("scheduled")
37
37
  end
38
38
 
39
+ # Renders the dead tab, or 404 while the dead stream is disabled.
40
+ #
41
+ # @return [Array] the Rack response
39
42
  def dead
40
43
  return not_found unless Config.dead.enabled
41
44
 
42
- return _dead if hx_request?
45
+ tab("dead")
46
+ end
43
47
 
44
- content_for :title, "Dead Jobs"
45
- ok render("jobs/dead", layout: true)
48
+ # Renders the queues table.
49
+ #
50
+ # @return [Array] the Rack response
51
+ def _queues
52
+ ok render("jobs/_queues", { queues: API::Stats.queues, compact: params["compact"].to_s == "1" })
46
53
  end
47
54
 
55
+ # Sends a dead job back to its queue.
56
+ #
57
+ # @return [Array] the Rack response
48
58
  def retry
49
59
  return not_found unless Config.dead.enabled
50
60
 
51
61
  seq = path.split("/").last.to_i
52
- stream = API::Stream.new("dead")
53
- stream.retry(seq)
54
- ok
62
+ API::Stream.new("dead").retry(seq)
63
+ ok flash("Dead job ##{seq} sent back to its queue")
55
64
  end
56
65
 
66
+ # Deletes a dead job.
67
+ #
68
+ # @return [Array] the Rack response
57
69
  def delete
58
70
  return not_found unless Config.dead.enabled
59
71
 
60
72
  seq = path.split("/").last.to_i
61
- stream = API::Stream.new("dead")
62
- stream.delete(seq)
63
- ok
73
+ API::Stream.new("dead").delete(seq)
74
+ ok flash("Dead job ##{seq} deleted")
64
75
  end
65
76
 
77
+ # Removes an enqueued message from its stream and renders the enqueued tab.
78
+ #
79
+ # @return [Array] the Rack response
66
80
  def delete_enqueued
67
81
  seq = path.split("/").last.to_i
68
- stream_name, = streams
82
+ stream_name = params["stream_name"]
69
83
  API::Stream.new(stream_name).delete(seq)
70
- _enqueued
84
+ ok panel("enqueued") + flash("Message ##{seq} removed from #{stream_name}")
71
85
  end
72
86
 
73
- def _scheduled
74
- return not_found unless Config.scheduled.enabled
87
+ private
75
88
 
76
- stream = API::Stream.new("scheduled")
77
- jobs = stream.messages(page: params["page"], limit: params["limit"])
78
- ok render("jobs/_scheduled", { jobs: jobs, total: stream.total })
79
- end
89
+ def tab(name)
90
+ return ok(panel(name)) if hx_request?
80
91
 
81
- def _dead
82
- return not_found unless Config.dead.enabled
92
+ content_for :title, "Jobs"
93
+ content_for :subtitle, "Queues, in-flight work and everything waiting its turn."
94
+ ok render("jobs/index", { panel: panel(name) }, layout: true)
95
+ end
83
96
 
84
- stream = API::Stream.new("dead")
85
- jobs = stream.messages(page: params["page"], limit: params["limit"])
86
- ok render("jobs/_dead", { jobs: jobs, total: stream.total })
97
+ def panel(name)
98
+ render("jobs/_panel", send("#{name}_locals").merge(tab: name, counts: counts))
87
99
  end
88
100
 
89
- def _busy
90
- busy = API::Stats::Busy.instance
91
- total = busy.size
92
- page, limit, total_pages = paginate(total, API::Stats::Busy::LIMIT)
93
- polling = params["poll"].to_s != "0"
94
- ok render("jobs/_busy", { jobs: busy.list(page:, limit:), total:, page:, limit:, total_pages:, polling: })
101
+ def counts
102
+ counts = { busy: API::Stats.busy, enqueued: API::Stats.enqueued }
103
+ counts[:scheduled] = API::Stats.scheduled if Config.scheduled.enabled
104
+ counts[:dead] = API::Stats.dead if Config.dead.enabled
105
+ counts
95
106
  end
96
107
 
97
- def _processes
98
- processes = API::Stats::Processes.instance
99
- total = processes.size
100
- page, limit, total_pages = paginate(total, API::Stats::Processes::LIMIT)
108
+ def busy_locals
109
+ total = API::Stats.busy
110
+ page, limit, total_pages = paginate(total, API::Registry::LIMIT)
101
111
  polling = params["poll"].to_s != "0"
102
- groups = processes.list(page:, limit:).chunk { API::Stats::Processes.kind(_1) }.to_a
103
- ok render("jobs/_processes", { groups:, total:, page:, limit:, total_pages:, polling: })
112
+ longest = params["order"] == "longest"
113
+ jobs = API::Stats.running_page(page:, limit:, longest:)
114
+ { jobs:, total:, page:, limit:, total_pages:, polling:,
115
+ query: { limit:, poll: (0 unless polling), order: ("longest" if longest) }.compact }
104
116
  end
105
117
 
106
- def _enqueued # rubocop:disable Metrics/AbcSize
107
- stream_name, stream_names = streams
108
- limit = (params["limit"] || API::Stream::LIMIT).to_i
118
+ def enqueued_locals # rubocop:disable Metrics/AbcSize
119
+ stream_name, stream_sizes = streams
120
+ limit = limit(API::Stream::LIMIT)
109
121
  page = [params["page"].to_i, 1].max
122
+ locals = { stream_name:, stream_sizes:, page:, limit:, total: 0, total_pages: 0, jobs: nil, polling: false,
123
+ query: { stream_name: Rack::Utils.escape(stream_name.to_s), limit: } }
124
+ return locals if stream_name.to_s.empty?
110
125
 
111
- unless stream_name.to_s.empty?
112
- stream = API::Stream.new(stream_name)
113
- total = stream.total
114
- total_pages = (total.to_f / limit).ceil
115
- page = page.clamp(1, [total_pages, 1].max)
116
- jobs = stream.messages(page:, limit:)
117
- end
126
+ stream = API::Stream.new(stream_name)
127
+ total = stream_sizes.fetch(stream_name) { stream.total }
128
+ total_pages = (total.to_f / limit).ceil
129
+ page = page.clamp(1, [total_pages, 1].max)
130
+ locals.merge(total:, total_pages:, page:, jobs: stream.messages(page:, limit:))
131
+ end
118
132
 
119
- ok render("jobs/_enqueued", { jobs:, total:, stream_name:, stream_names:, page:, limit:, total_pages: })
133
+ def scheduled_locals
134
+ stored_locals("scheduled").merge(polling: true)
120
135
  end
121
136
 
122
- def _stats
123
- ok render("jobs/_stats", API::Stats.summary)
137
+ def dead_locals
138
+ stored_locals("dead", order: :desc).merge(polling: false)
124
139
  end
125
140
 
126
- private
141
+ def stored_locals(name, order: :asc)
142
+ stream = API::Stream.new(name)
143
+ total = stream.total
144
+ page, limit, total_pages = paginate(total, API::Stream::LIMIT)
145
+ { jobs: stream.messages(page:, limit:, order:), total:, page:, limit:, total_pages:, query: { limit: } }
146
+ end
127
147
 
128
148
  def paginate(total, default_limit)
129
- limit = (params["limit"] || default_limit).to_i
149
+ limit = limit(default_limit)
130
150
  total_pages = (total.to_f / limit).ceil
131
151
  [params["page"].to_i.clamp(1, [total_pages, 1].max), limit, total_pages]
132
152
  end
133
153
 
134
154
  def streams
135
- stream_names = API::Stream.jobs.map(&:name)
136
- stream_name = params.fetch("stream_name", stream_names.first)
137
- [stream_name, stream_names]
155
+ stream_sizes = API::Stream.jobs.to_h { [_1.name, _1.size] }
156
+ stream_name = params.fetch("stream_name") { stream_sizes.find { |_, size| size.positive? }&.first || stream_sizes.keys.first }
157
+ [stream_name, stream_sizes]
138
158
  end
139
159
  end
140
160
  end
@@ -9,14 +9,20 @@ module Cosmo
9
9
  class Metrics < Application
10
10
  RANGES = { 1 => "Today", 7 => "7 days", 30 => "30 days" }.freeze
11
11
 
12
+ # Renders the metrics page, or 404 while metrics are disabled.
13
+ #
14
+ # @return [Array] the Rack response
12
15
  def index
13
16
  return not_found unless Config.metrics.enabled
14
17
 
15
18
  content_for :title, "Metrics"
19
+ content_for :subtitle, "Runs, failures, execution and wait time per job class."
16
20
  ok render("metrics/index", { days:, ranges: RANGES }, layout: true)
17
21
  end
18
22
 
19
23
  # The chart and the table together, so the charted job stays highlighted across refreshes.
24
+ #
25
+ # @return [Array] the Rack response
20
26
  def _panel
21
27
  return not_found unless Config.metrics.enabled
22
28
 
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cosmo/web/controllers/application"
4
+ require "cosmo/web/throughput"
5
+
6
+ module Cosmo
7
+ class Web
8
+ module Controllers
9
+ class Overview < Application
10
+ RANGES = [7, 30].freeze
11
+ CELLS = 64
12
+ RUNNING = 9
13
+ PROCESSES = 3
14
+ BATCHES = 100
15
+
16
+ # Renders the overview page.
17
+ #
18
+ # @return [Array] the Rack response
19
+ def index
20
+ content_for :title, "Overview"
21
+ content_for :subtitle, "Everything beamed through NATS, live."
22
+ ok render("overview/index", { metrics: Config.metrics.enabled }, layout: true)
23
+ end
24
+
25
+ # Renders the overview's KPI cards.
26
+ #
27
+ # @return [Array] the Rack response
28
+ def _kpis
29
+ ok render("overview/_kpis", API::Stats.summary)
30
+ end
31
+
32
+ # Renders the throughput chart for the requested range, or 404 while metrics are disabled.
33
+ #
34
+ # @return [Array] the Rack response
35
+ def _throughput
36
+ return not_found unless Config.metrics.enabled
37
+
38
+ days = RANGES.include?(params["days"].to_i) ? params["days"].to_i : RANGES.first
39
+ chart = Throughput.new(API::Stats::Metrics.instance.daily(days:))
40
+ ok render("overview/_throughput", { chart:, days:, ranges: RANGES })
41
+ end
42
+
43
+ # Renders the worker processes and a cell per thread, busy or idle.
44
+ #
45
+ # @return [Array] the Rack response
46
+ def _workers
47
+ registry = API::Stats::Processes.instance
48
+ threads = registry.threads
49
+ cells, busy = worker_cells(threads)
50
+ ok render("overview/_workers", { processes: registry.list(limit: PROCESSES), total: registry.size,
51
+ cells:, hidden: [threads - CELLS, 0].max, busy:, threads: })
52
+ end
53
+
54
+ # Renders a sample of the running jobs.
55
+ #
56
+ # @return [Array] the Rack response
57
+ def _running
58
+ ok render("overview/_running", { jobs: API::Stats.running(RUNNING), total: API::Stats.busy })
59
+ end
60
+
61
+ # Renders the sidebar's count badges. Batches are only counted up to BATCHES; past that the badge
62
+ # reads "100+".
63
+ #
64
+ # @return [Array] the Rack response
65
+ def _badges
66
+ badges = { jobs: API::Stats.enqueued, streams: visible_streams.size }
67
+ badges[:crons] = API::Cron.instance.all.size if Config.scheduled.enabled
68
+ batches = API::Batch.count(limit: BATCHES + 1)
69
+ badges[:batches] = batches > BATCHES ? "#{BATCHES}+" : batches
70
+ ok render("overview/_badges", { badges: })
71
+ end
72
+
73
+ private
74
+
75
+ def worker_cells(threads)
76
+ busy = API::Stats.busy_streams.select { _2.positive? }
77
+ cells = busy.flat_map { |stream, count| Array.new([count, CELLS].min, stream) }.first(CELLS)
78
+ cells += Array.new((threads - busy.values.sum).clamp(0, CELLS - cells.size))
79
+ [cells, busy.values.sum]
80
+ end
81
+
82
+ def visible_streams
83
+ API::Stream.all.reject { _1.name.start_with?("KV_", "_cosmo") }
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cosmo/web/controllers/application"
4
+
5
+ module Cosmo
6
+ class Web
7
+ module Controllers
8
+ class Processes < Application
9
+ # Renders the processes page, or only its table for htmx.
10
+ #
11
+ # @return [Array] the Rack response
12
+ def index
13
+ return ok(table) if hx_request?
14
+
15
+ content_for :title, "Processes"
16
+ content_for :subtitle, "Every worker process heartbeating into NATS."
17
+ ok render("processes/index", { table: table }, layout: true)
18
+ end
19
+
20
+ private
21
+
22
+ def table
23
+ registry = API::Stats::Processes.instance
24
+ total = registry.size
25
+ limit = limit(API::Stats::Processes::LIMIT)
26
+ total_pages = (total.to_f / limit).ceil
27
+ page = params["page"].to_i.clamp(1, [total_pages, 1].max)
28
+ polling = params["poll"].to_s != "0"
29
+ groups = registry.list(page:, limit:).chunk { API::Stats::Processes.kind(_1) }.to_a
30
+ render("processes/_table", { groups:, total:, page:, limit:, total_pages:, polling:,
31
+ query: { limit:, poll: (0 unless polling) }.compact })
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -6,63 +6,73 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Streams < Application
9
+ # Renders the streams page, or only the stream list for htmx.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
- return _table if hx_request?
13
+ return ok(list) if hx_request?
11
14
 
12
15
  content_for :title, "Streams"
16
+ content_for :subtitle, "JetStream streams and the consumers reading them."
13
17
  ok render("streams/index", layout: true)
14
18
  end
15
19
 
20
+ # Renders a stream's configuration.
21
+ #
22
+ # @return [Array] the Rack response
16
23
  def info
17
- name = Rack::Utils.unescape(@request.params["name"])
18
- return _info if hx_request?
24
+ return ok(render("streams/_info", info_locals(API::Stream.new(name)))) if hx_request?
19
25
 
20
26
  content_for :title, "Streams"
21
- ok render("streams/info", { name: name }, layout: true)
27
+ content_for :subtitle, "Configuration of #{name}."
28
+ ok render("streams/info", { name: }, layout: true)
22
29
  end
23
30
 
31
+ # Pauses a stream, so workers stop fetching from it.
32
+ #
33
+ # @return [Array] the Rack response
24
34
  def pause
25
- name = Rack::Utils.unescape(@request.params["name"])
26
35
  stream = API::Stream.new(name)
27
36
  stream.pause!
28
- return ok render("streams/_pause_banner", banner_locals(stream)) if @request.params["banner"]
29
-
30
- ok render("streams/_stream_row", { stream: row_locals(stream) })
37
+ toggled(stream, "paused")
31
38
  end
32
39
 
40
+ # Resumes a paused stream.
41
+ #
42
+ # @return [Array] the Rack response
33
43
  def unpause
34
- name = Rack::Utils.unescape(@request.params["name"])
35
44
  stream = API::Stream.new(name)
36
45
  stream.unpause!
37
- return ok render("streams/_pause_banner", banner_locals(stream)) if @request.params["banner"]
38
-
39
- ok render("streams/_stream_row", { stream: row_locals(stream) })
46
+ toggled(stream, "resumed")
40
47
  end
41
48
 
42
- def _table
43
- streams = API::Stream.all.reject { |s| s.name.start_with?("KV_") || s.name.start_with?("_cosmo") }.map { row_locals(_1) }
44
- ok render("streams/_table", { streams: streams })
49
+ private
50
+
51
+ def name
52
+ Rack::Utils.unescape(params["name"].to_s)
45
53
  end
46
54
 
47
- def _info
48
- name = Rack::Utils.unescape(@request.params["name"])
49
- stream = API::Stream.new(name)
50
- ok render("streams/_info", stream.info.merge(name:, paused: stream.paused?))
55
+ def list
56
+ streams = API::Stream.all.reject { _1.name.start_with?("KV_", "_cosmo") }.map { card(_1) }
57
+ render("streams/_list", { streams: })
51
58
  end
52
59
 
53
- private
60
+ def toggled(stream, verb)
61
+ body = case params["view"] || (params["banner"] && "banner")
62
+ when "banner" then render("streams/_pause_banner", { name: stream.name, paused: stream.paused? })
63
+ when "queue" then render("jobs/_queue_row", { queue: API::Stats.queue(stream), compact: false })
64
+ else render("streams/_stream", { stream: card(stream) })
65
+ end
66
+ ok body + flash("#{stream.name} #{verb}")
67
+ end
54
68
 
55
- def row_locals(stream)
69
+ def card(stream)
56
70
  state, config = stream.info.values
57
- { name: stream.name, messages: state.messages, bytes: state.bytes,
58
- first_seq: state.first_seq, last_seq: state.last_seq,
59
- consumer_count: state.consumer_count,
60
- subjects: config.subjects,
61
- paused: stream.paused? }
71
+ { name: stream.name, state:, config:, consumers: stream.consumers, paused: stream.paused? }
62
72
  end
63
73
 
64
- def banner_locals(stream)
65
- { name: stream.name, paused: stream.paused? }
74
+ def info_locals(stream)
75
+ stream.info.merge(name: stream.name, paused: stream.paused?)
66
76
  end
67
77
  end
68
78
  end