boto-rb-management-ui 0.31.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 (106) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +22 -0
  4. data/db/migrations/20261201000001_create_boto_monitor_instances.rb +14 -0
  5. data/db/migrations/20261201000002_create_boto_monitor_series.rb +20 -0
  6. data/db/migrations/20261201000003_create_boto_monitor_errors.rb +19 -0
  7. data/db/migrations/20261201000004_create_boto_monitor_traces.rb +19 -0
  8. data/db/migrations/20261201000005_create_boto_monitor_runtime.rb +23 -0
  9. data/db/migrations/20261201000006_create_boto_monitor_n_plus_ones.rb +17 -0
  10. data/db/migrations/20261201000007_add_browser_data_to_boto_monitor.rb +31 -0
  11. data/db/migrations/20261203000001_create_boto_monitor_streams.rb +24 -0
  12. data/db/migrations/20261203000002_add_streams_open_to_boto_monitor_instances.rb +12 -0
  13. data/lib/boto/companions/management_ui.rb +8 -0
  14. data/lib/boto/management_ui/app.rb +84 -0
  15. data/lib/boto/management_ui/app_traits.rb +34 -0
  16. data/lib/boto/management_ui/assets/monitor/api.js +36 -0
  17. data/lib/boto/management_ui/assets/monitor/charts.js +212 -0
  18. data/lib/boto/management_ui/assets/monitor/constants.js +70 -0
  19. data/lib/boto/management_ui/assets/monitor/dom.js +70 -0
  20. data/lib/boto/management_ui/assets/monitor/format.js +76 -0
  21. data/lib/boto/management_ui/assets/monitor/header.js +2 -0
  22. data/lib/boto/management_ui/assets/monitor/main.js +33 -0
  23. data/lib/boto/management_ui/assets/monitor/panes.js +109 -0
  24. data/lib/boto/management_ui/assets/monitor/runtime.js +26 -0
  25. data/lib/boto/management_ui/assets/monitor/services.js +174 -0
  26. data/lib/boto/management_ui/assets/monitor/spa.js +203 -0
  27. data/lib/boto/management_ui/assets/monitor/traces.js +88 -0
  28. data/lib/boto/management_ui/assets/panel/base.css +87 -0
  29. data/lib/boto/management_ui/assets/panel/errors.css +36 -0
  30. data/lib/boto/management_ui/assets/panel/gateway.css +3 -0
  31. data/lib/boto/management_ui/assets/panel/layout.css +63 -0
  32. data/lib/boto/management_ui/assets/panel/monitor_layout.css +77 -0
  33. data/lib/boto/management_ui/assets/panel/monitor_tables.css +97 -0
  34. data/lib/boto/management_ui/assets/panel/service_view.css +102 -0
  35. data/lib/boto/management_ui/assets/panel/services.css +55 -0
  36. data/lib/boto/management_ui/assets/panel/spa.css +41 -0
  37. data/lib/boto/management_ui/assets/panel/tables.css +67 -0
  38. data/lib/boto/management_ui/assets/panel/tabs.css +71 -0
  39. data/lib/boto/management_ui/assets/panel/traces.css +45 -0
  40. data/lib/boto/management_ui/browser_telemetry.rb +87 -0
  41. data/lib/boto/management_ui/gateway_view.rb +57 -0
  42. data/lib/boto/management_ui/health_probe.rb +70 -0
  43. data/lib/boto/management_ui/in_process_receiver.rb +17 -0
  44. data/lib/boto/management_ui/monitor/api.rb +87 -0
  45. data/lib/boto/management_ui/monitor/batch_schema/browser_call_entry.rb +26 -0
  46. data/lib/boto/management_ui/monitor/batch_schema/browser_error_entry.rb +41 -0
  47. data/lib/boto/management_ui/monitor/batch_schema/checks.rb +90 -0
  48. data/lib/boto/management_ui/monitor/batch_schema/error_entry.rb +37 -0
  49. data/lib/boto/management_ui/monitor/batch_schema/n_plus_one_entry.rb +30 -0
  50. data/lib/boto/management_ui/monitor/batch_schema/page_entry.rb +25 -0
  51. data/lib/boto/management_ui/monitor/batch_schema/runtime.rb +33 -0
  52. data/lib/boto/management_ui/monitor/batch_schema/series.rb +41 -0
  53. data/lib/boto/management_ui/monitor/batch_schema/streams.rb +46 -0
  54. data/lib/boto/management_ui/monitor/batch_schema/trace_entry.rb +45 -0
  55. data/lib/boto/management_ui/monitor/batch_schema.rb +138 -0
  56. data/lib/boto/management_ui/monitor/ingest.rb +126 -0
  57. data/lib/boto/management_ui/monitor/memory_store/error_table.rb +53 -0
  58. data/lib/boto/management_ui/monitor/memory_store/instance_table.rb +45 -0
  59. data/lib/boto/management_ui/monitor/memory_store/n_plus_one_table.rb +44 -0
  60. data/lib/boto/management_ui/monitor/memory_store/runtime_table.rb +55 -0
  61. data/lib/boto/management_ui/monitor/memory_store/series_table.rb +49 -0
  62. data/lib/boto/management_ui/monitor/memory_store/staleness.rb +20 -0
  63. data/lib/boto/management_ui/monitor/memory_store/stream_table.rb +36 -0
  64. data/lib/boto/management_ui/monitor/memory_store/trace_table.rb +41 -0
  65. data/lib/boto/management_ui/monitor/memory_store.rb +118 -0
  66. data/lib/boto/management_ui/monitor/percentile.rb +38 -0
  67. data/lib/boto/management_ui/monitor/period.rb +48 -0
  68. data/lib/boto/management_ui/monitor/queries/browser_calls.rb +56 -0
  69. data/lib/boto/management_ui/monitor/queries/browser_view.rb +91 -0
  70. data/lib/boto/management_ui/monitor/queries/charts.rb +66 -0
  71. data/lib/boto/management_ui/monitor/queries/live_instances.rb +44 -0
  72. data/lib/boto/management_ui/monitor/queries/points.rb +61 -0
  73. data/lib/boto/management_ui/monitor/queries/route_view.rb +46 -0
  74. data/lib/boto/management_ui/monitor/queries/runtime_view.rb +80 -0
  75. data/lib/boto/management_ui/monitor/queries/service_cards.rb +65 -0
  76. data/lib/boto/management_ui/monitor/queries/streams.rb +39 -0
  77. data/lib/boto/management_ui/monitor/queries/trace_tree.rb +47 -0
  78. data/lib/boto/management_ui/monitor/queries/vitals.rb +50 -0
  79. data/lib/boto/management_ui/monitor/queries.rb +171 -0
  80. data/lib/boto/management_ui/monitor/retention.rb +62 -0
  81. data/lib/boto/management_ui/monitor/schema.rb +64 -0
  82. data/lib/boto/management_ui/monitor/sql_store/error_writer.rb +62 -0
  83. data/lib/boto/management_ui/monitor/sql_store/instance_writer.rb +63 -0
  84. data/lib/boto/management_ui/monitor/sql_store/n_plus_one_writer.rb +50 -0
  85. data/lib/boto/management_ui/monitor/sql_store/rows.rb +99 -0
  86. data/lib/boto/management_ui/monitor/sql_store/series_writer.rb +52 -0
  87. data/lib/boto/management_ui/monitor/sql_store/streams_writer.rb +34 -0
  88. data/lib/boto/management_ui/monitor/sql_store.rb +139 -0
  89. data/lib/boto/management_ui/monitor/store.rb +78 -0
  90. data/lib/boto/management_ui/monitor/stream_point.rb +45 -0
  91. data/lib/boto/management_ui/panel/env_rows.rb +54 -0
  92. data/lib/boto/management_ui/panel/html_view.rb +43 -0
  93. data/lib/boto/management_ui/panel/json_view.rb +43 -0
  94. data/lib/boto/management_ui/panel/masking.rb +43 -0
  95. data/lib/boto/management_ui/panel/page.rb +41 -0
  96. data/lib/boto/management_ui/panel/service_info.rb +62 -0
  97. data/lib/boto/management_ui/panel.html.erb +207 -0
  98. data/lib/boto/management_ui/panel.rb +115 -0
  99. data/lib/boto/management_ui/password_auth.rb +26 -0
  100. data/lib/boto/management_ui/service_list.rb +59 -0
  101. data/lib/boto/management_ui/setup.rb +103 -0
  102. data/lib/boto/management_ui/spas.rb +73 -0
  103. data/lib/boto/management_ui/tabs.rb +40 -0
  104. data/lib/boto/management_ui/version.rb +7 -0
  105. data/lib/boto/management_ui.rb +15 -0
  106. metadata +203 -0
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "time"
5
+
6
+ module Boto
7
+ module ManagementUi
8
+ module Monitor
9
+ # Store on a Sequel database (see Schema). Behaves like MemoryStore, but keeps rows until Retention purges them.
10
+ class SqlStore
11
+ include Store
12
+
13
+ SECONDS_PER_MINUTE = 60
14
+ # With --processes, gateway workers write together. SQLite takes the write lock up front, so the other
15
+ # worker waits out the busy timeout instead of failing when its read turns into a write; Postgres locks the
16
+ # rows it adds to (FOR UPDATE in the writers).
17
+ SQLITE_WRITE_LOCK = { mode: :immediate }.freeze
18
+
19
+ # clock is reserved for the instance expiry of the panel queries (task 9).
20
+ def initialize(database:, clock: Time)
21
+ @database = database
22
+ @clock = clock
23
+ end
24
+
25
+ def ingest(batch, received_at:)
26
+ @database.transaction(**write_lock) do
27
+ service = batch["service"]
28
+ minute = minute_of(batch["to"])
29
+ SeriesWriter.new(@database).write(service, minute, batch)
30
+ StreamsWriter.new(@database).write(service, minute, batch["streams"]) if batch["streams"]
31
+ writer = ErrorWriter.new(@database)
32
+ BatchSchema::ERROR_SOURCES.each { |key, source| writer.write(service, batch[key], source) }
33
+ NPlusOneWriter.new(@database).write(service, batch["n_plus_ones"])
34
+ batch["traces"].each { |trace| insert_trace(service, trace, received_at) }
35
+ InstanceWriter.new(@database).write(batch, minute, received_at)
36
+ end
37
+ nil
38
+ end
39
+
40
+ def services
41
+ @database[:boto_monitor_instances].distinct.select_map(:service)
42
+ end
43
+
44
+ def instances(service)
45
+ runtimes = runtime(service)
46
+ latest_instance_rows(service).map { |row| Rows.instance(row, runtimes[row[:instance]]) }
47
+ end
48
+
49
+ def series(service:, from:, to:)
50
+ rows = @database[:boto_monitor_series].where(service: service).where(Sequel[:minute] >= from)
51
+ .where(Sequel[:minute] < to).order(:minute).all
52
+ rows.map { |row| Rows.series(row) }
53
+ end
54
+
55
+ def streams(service:, from:, to:)
56
+ rows = @database[:boto_monitor_streams].where(service: service).where(Sequel[:minute] >= from)
57
+ .where(Sequel[:minute] < to).order(:minute).all
58
+ Rows.streams(rows)
59
+ end
60
+
61
+ def runtime_series(service:, from:, to:)
62
+ rows = @database[:boto_monitor_runtime].where(service: service).where(Sequel[:minute] >= from)
63
+ .where(Sequel[:minute] < to).order(:minute, :id).all
64
+ rows.map { |row| Rows.runtime_series(row) }
65
+ end
66
+
67
+ def errors(service, source: DEFAULT_ERROR_SOURCE)
68
+ rows = @database[:boto_monitor_errors].where(service: service, source: source).order(:id).all
69
+ rows.map { |row| Rows.error(row) }
70
+ end
71
+
72
+ def n_plus_ones(service)
73
+ @database[:boto_monitor_n_plus_ones].where(service: service).order(:id).all.map { |row| Rows.n_plus_one(row) }
74
+ end
75
+
76
+ def traces_by_id(trace_id)
77
+ rows = @database[:boto_monitor_traces].where(trace_id: trace_id).order(:id).all
78
+ rows.map { |row| Rows.trace(row).merge("service" => row[:service]) }
79
+ end
80
+
81
+ def trace(service, trace_id)
82
+ row = @database[:boto_monitor_traces].where(service: service, trace_id: trace_id).first
83
+ row && Rows.trace(row)
84
+ end
85
+
86
+ def traces(service, limit:)
87
+ rows = @database[:boto_monitor_traces].where(service: service).reverse(:received_at, :id).limit(limit).all
88
+ rows.map { |row| Rows.trace(row) }
89
+ end
90
+
91
+ def runtime(service)
92
+ latest_runtime_rows(service).to_h { |row| [row[:instance], Rows.runtime(row)] }
93
+ end
94
+
95
+ # Deletes what is older than the given cut-offs. Series and runtime use the metrics one; traces, errors and
96
+ # N+1 groups use the traces one, and errors and N+1 groups count by last_at.
97
+ def purge(before_metrics:, before_traces:)
98
+ @database[:boto_monitor_series].where(Sequel[:minute] < before_metrics).delete
99
+ @database[:boto_monitor_runtime].where(Sequel[:minute] < before_metrics).delete
100
+ @database[:boto_monitor_streams].where(Sequel[:minute] < before_metrics).delete
101
+ @database[:boto_monitor_instances].where(Sequel[:last_seen_at] < before_metrics).delete
102
+ @database[:boto_monitor_traces].where(Sequel[:received_at] < before_traces).delete
103
+ @database[:boto_monitor_errors].where(Sequel[:last_at] < before_traces).delete
104
+ @database[:boto_monitor_n_plus_ones].where(Sequel[:last_at] < before_traces).delete
105
+ nil
106
+ end
107
+
108
+ private
109
+
110
+ def write_lock = @database.database_type == :sqlite ? SQLITE_WRITE_LOCK : {}
111
+
112
+ def insert_trace(service, trace, received_at)
113
+ row = trace.slice(*Rows::TRACE_COLUMNS.map(&:to_s)).transform_keys(&:to_sym)
114
+ @database[:boto_monitor_traces].insert(row.merge(
115
+ service: service, received_at: received_at, started_at: Time.iso8601(trace["started_at"]),
116
+ spans: JSON.generate(trace["spans"])
117
+ ))
118
+ end
119
+
120
+ def minute_of(text)
121
+ seconds = Time.iso8601(text).to_i
122
+ Time.at(seconds - (seconds % SECONDS_PER_MINUTE)).utc
123
+ end
124
+
125
+ # One row per process; the latest one of each instance name, in the order the names first appeared.
126
+ def latest_instance_rows(service)
127
+ rows = @database[:boto_monitor_instances].where(service: service).order(:id).all
128
+ rows.group_by { |row| row[:instance] }.map { |_name, group| group.max_by { |row| row[:last_seen_at] } }
129
+ end
130
+
131
+ def latest_runtime_rows(service)
132
+ latest = @database[:boto_monitor_runtime].where(service: service).group(:instance)
133
+ .select(:instance, Sequel.function(:max, :minute).as(:minute))
134
+ @database[:boto_monitor_runtime].join(latest, %i[instance minute]).where(service: service).order(:id).all
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ # Interface of the monitor stores. MemoryStore follows it, and the SQL store will too.
7
+ # #ingest only receives batches already checked by BatchSchema.
8
+ module Store
9
+ DEFAULT_ERROR_SOURCE = "server"
10
+
11
+ # Stores a validated batch. received_at is the Time the gateway got it.
12
+ def ingest(batch, received_at:)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ # Names of the services with an instance in the window.
17
+ def services
18
+ raise NotImplementedError
19
+ end
20
+
21
+ # Hashes with "instance", "started_at", "last_seen", "process", "in_flight", "streams_open" and "runtime" for
22
+ # each instance. "streams_open" is nil when the app has no stream observer.
23
+ def instances(service)
24
+ raise NotImplementedError
25
+ end
26
+
27
+ # Per-minute rows in [from, to): "minute", "kind" ("routes", "operations", "page" or "browser_call"), the
28
+ # labels of the kind, and "count", "sum" and "buckets" summed across the instances.
29
+ def series(service:, from:, to:)
30
+ raise NotImplementedError
31
+ end
32
+
33
+ # Per-minute rows per route in [from, to): "minute", "route", the counters of the stream series ("opened",
34
+ # "resumed", "events_sent", ...), "rejected" and "closed" by reason, and "duration" (count, sum, buckets).
35
+ def streams(service:, from:, to:)
36
+ raise NotImplementedError
37
+ end
38
+
39
+ # One row per process and minute in [from, to): "minute", "instance", "process", "in_flight" and the
40
+ # runtime fields (cpu, memory, memory_limit, cpu_limit, memory_source, gc_count, dropped, pool).
41
+ def runtime_series(service:, from:, to:)
42
+ raise NotImplementedError
43
+ end
44
+
45
+ # Errors of the service and source, one per fingerprint, with "count", "first_at" and "last_at". Browser
46
+ # errors also carry "browsers", the count per browser.
47
+ def errors(service, source: DEFAULT_ERROR_SOURCE)
48
+ raise NotImplementedError
49
+ end
50
+
51
+ # The last `limit` traces of the service, newest first.
52
+ def traces(service, limit:)
53
+ raise NotImplementedError
54
+ end
55
+
56
+ # The kept trace with this id (with its spans), or nil.
57
+ def trace(service, trace_id)
58
+ raise NotImplementedError
59
+ end
60
+
61
+ # N+1 groups of the service, one per fingerprint, with "count", "max_repeats", "first_at", "last_at" and "sql".
62
+ def n_plus_ones(service)
63
+ raise NotImplementedError
64
+ end
65
+
66
+ # The kept traces with this id in every service (with their spans), each with its "service".
67
+ def traces_by_id(trace_id)
68
+ raise NotImplementedError
69
+ end
70
+
71
+ # Last runtime of each instance of the service, as { instance => runtime }.
72
+ def runtime(service)
73
+ raise NotImplementedError
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ module Monitor
6
+ # The sums of one route in one minute of the stream series. Both stores add the batches into it, and the
7
+ # queries add the stored points of a route, so the shape is the one BatchSchema::Streams gives per route.
8
+ module StreamPoint
9
+ module_function
10
+
11
+ def empty
12
+ { "rejected" => zeros(BatchSchema::STREAM_REJECTIONS), "closed" => zeros(BatchSchema::STREAM_CLOSINGS),
13
+ "duration" => { "count" => 0, "sum" => 0.0, "buckets" => Array.new(BatchSchema::BUCKET_COUNT, 0) } }
14
+ .merge(zeros(BatchSchema::STREAM_COUNTERS))
15
+ end
16
+
17
+ # entry is a route of a batch or a stored point; both have the same keys.
18
+ def add(point, entry)
19
+ BatchSchema::STREAM_COUNTERS.to_h { |key| [key, point[key] + entry[key]] }.merge(
20
+ "rejected" => sum_hash(point["rejected"], entry["rejected"]),
21
+ "closed" => sum_hash(point["closed"], entry["closed"]),
22
+ "duration" => sum_duration(point["duration"], entry["duration"])
23
+ )
24
+ end
25
+
26
+ def total(points)
27
+ points.reduce(empty) { |sum, point| add(sum, point) }
28
+ end
29
+
30
+ def zeros(keys)
31
+ keys.to_h { |key| [key, 0] }
32
+ end
33
+
34
+ def sum_hash(total, part)
35
+ total.to_h { |key, value| [key, value + part[key]] }
36
+ end
37
+
38
+ def sum_duration(total, part)
39
+ { "count" => total["count"] + part["count"], "sum" => total["sum"] + part["sum"],
40
+ "buckets" => total["buckets"].zip(part["buckets"]).map { |old, new| old + new } }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ class Panel
6
+ # The variables the panel lists: the solution's root files first, then what each project adds.
7
+ module EnvRows
8
+ Row = Data.define(:name, :value, :source, :masked)
9
+
10
+ PORT = "PORT"
11
+ PROCESS_SOURCE = "process"
12
+ BOTO_RUN_SOURCE = "boto run"
13
+ NO_ENV = Boto::Env.new({})
14
+
15
+ module_function
16
+
17
+ def solution(env)
18
+ env = normalize(env)
19
+ rows(env, solution_keys(env))
20
+ end
21
+
22
+ # Keys whose source is a file in the solution root: no "/" in the path and not a process-level source.
23
+ def solution_keys(env)
24
+ env = normalize(env)
25
+ env.file_keys.select { |key| root_file?(env.source(key)) }
26
+ end
27
+
28
+ def project(env, solution_keys)
29
+ env = normalize(env)
30
+ rows(env, [PORT] | (env.file_keys - solution_keys))
31
+ end
32
+
33
+ # An app without an env (no .env files loaded) lists nothing.
34
+ def normalize(env) = env || NO_ENV
35
+
36
+ def root_file?(source)
37
+ source && !source.include?("/") && ![PROCESS_SOURCE, BOTO_RUN_SOURCE].include?(source)
38
+ end
39
+
40
+ def rows(env, keys)
41
+ keys.filter_map { |key| row(env, key) }
42
+ end
43
+
44
+ def row(env, key)
45
+ value = env[key]
46
+ return if value.nil?
47
+
48
+ shown, masked = Masking.apply(key, value)
49
+ Row.new(name: key, value: shown, source: env.source(key), masked: masked)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+ require "json"
5
+
6
+ module Boto
7
+ module ManagementUi
8
+ class Panel
9
+ # Renders the panel page from panel.html.erb. Every value goes through h (ERB::Util.html_escape).
10
+ class HtmlView
11
+ include ERB::Util
12
+
13
+ HEALTH_LABELS = { ready: "Ready", failing: "Not ready", none: "No health check", unknown: "Unknown" }.freeze
14
+ KIND_LABELS = { GatewayView::SERVICE_KIND => "serviço", GatewayView::SPA_KIND => "spa" }.freeze
15
+
16
+ # Read as UTF-8 whatever the locale is: the page contains non-ASCII characters.
17
+ def self.template = ERB.new(File.read(Panel::TEMPLATE_FILE, encoding: Encoding::UTF_8), trim_mode: "-")
18
+
19
+ def initialize(template)
20
+ @template = template
21
+ end
22
+
23
+ def render(page)
24
+ @template.result(binding)
25
+ end
26
+
27
+ private
28
+
29
+ def plural(count, word) = "#{count} #{count == 1 ? word : "#{word}s"}"
30
+
31
+ def health_label(status) = HEALTH_LABELS.fetch(status)
32
+
33
+ def kind_label(kind) = KIND_LABELS.fetch(kind)
34
+
35
+ def tab_label(tab) = Tabs::LABELS.fetch(tab)
36
+
37
+ def subtabs_json(page) = JSON.generate(page.subtabs)
38
+
39
+ def verb_class(verb) = "verb verb--#{verb.downcase}"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ class Panel
8
+ # The panel's data as JSON, for /_boto/services.json. Same values as the HTML page.
9
+ module JsonView
10
+ module_function
11
+
12
+ def render(page)
13
+ JSON.generate(document(page))
14
+ end
15
+
16
+ def document(page)
17
+ { gateway: page.gateway, env: page.env_name, solution_env: page.solution_env.map { |row| row_hash(row) },
18
+ services: page.services.map { |service| service_hash(service, page) } }
19
+ end
20
+
21
+ # tabs is listed only for a run's services (the context); the gateway's own panel keeps its shape.
22
+ def service_hash(service, page)
23
+ hash = { name: service.name, prefix: service.prefix, kind: service.kind.to_s, port: service.port,
24
+ health: { status: service.health.status.to_s, detail: service.health.detail },
25
+ routes: service.routes.map { |route| route_hash(route) },
26
+ openapi: service.openapi && page.link(service.openapi),
27
+ swagger: service.swagger && page.link(service.swagger),
28
+ scalar: service.scalar && page.link(service.scalar),
29
+ env: service.env.map { |row| row_hash(row) } }
30
+ service.tabs ? hash.merge(tabs: service.tabs) : hash
31
+ end
32
+
33
+ def route_hash(route)
34
+ { method: route.verb, path: route.path, endpoint: route.endpoint }
35
+ end
36
+
37
+ def row_hash(row)
38
+ { name: row.name, value: row.value, source: row.source, masked: row.masked }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module Boto
6
+ module ManagementUi
7
+ class Panel
8
+ # Hides secrets before the panel or its JSON shows a variable. Pure: it returns the value to show and a flag.
9
+ module Masking
10
+ MASK = "••••••••"
11
+ SENSITIVE_WORDS = %w[PASSWORD SECRET TOKEN KEY DSN CREDENTIAL].freeze
12
+ SENSITIVE_NAME = Regexp.new(SENSITIVE_WORDS.join("|"), Regexp::IGNORECASE).freeze
13
+
14
+ module_function
15
+
16
+ # Returns [shown_value, masked]. The whole value goes for sensitive names; a URL keeps everything but its
17
+ # password.
18
+ def apply(name, value)
19
+ return [MASK, true] if SENSITIVE_NAME.match?(name)
20
+
21
+ url = mask_url_password(value)
22
+ return [url, true] if url
23
+
24
+ [value, false]
25
+ end
26
+
27
+ def mask_url_password(value)
28
+ password = url_password(value)
29
+ return if password.nil? || password.empty?
30
+
31
+ value.sub(":#{password}@") { ":#{MASK}@" }
32
+ end
33
+
34
+ # A value that is not a URI (spaces, stray %) has no password to hide.
35
+ def url_password(value)
36
+ URI.parse(value).password
37
+ rescue URI::InvalidURIError
38
+ nil
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ class Panel
6
+ # Everything the panel shows, built on each request so the health checks run at page load.
7
+ # base is the SCRIPT_NAME of the request, so links work behind a mount point.
8
+ # title: the name in the top bar; main_tabs: the tabs the run has (Tabs.main); gateway_view: the gateway's
9
+ # mount table, nil without a gateway.
10
+ Page = Data.define(:title, :gateway, :env_name, :host, :base, :solution_env, :services, :retention,
11
+ :main_tabs, :gateway_view, :paths) do
12
+ # A run's panel: every app of the run, and the gateway's table when there is one.
13
+ def self.for_context(context:, env_name:, host:, base:, paths:, retention: Setup::DEFAULT_RETENTION)
14
+ first_app = context.apps.each_value.first
15
+ new(title: context.solution.name, gateway: context.gateways.keys.first, env_name: env_name, host: host,
16
+ base: base, solution_env: EnvRows.solution(first_app&.env), retention: retention,
17
+ services: ServiceList.new(context).entries, main_tabs: Tabs.main(context),
18
+ gateway_view: GatewayView.new(context).entries, paths: paths)
19
+ end
20
+
21
+ # Absolute links (a service's own port, or the gateway address) are used as they are.
22
+ def link(path) = path.start_with?("http") ? path : "#{base}#{path}"
23
+
24
+ def production? = env_name == Setup::PRODUCTION
25
+
26
+ def default_main = Tabs.default_main(main_tabs)
27
+
28
+ # The subtabs per service, for the monitor script: only the services that list theirs.
29
+ def subtabs = services.to_h { |service| [service.name, service.tabs] }.compact
30
+
31
+ def service_count = services.size
32
+
33
+ def ready_count = services.count(&:ready?)
34
+
35
+ def failing_count = services.count(&:failing?)
36
+
37
+ def route_count = services.sum { |service| service.routes.size }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module ManagementUi
5
+ class Panel
6
+ # What the panel shows of one service. Route paths have no prefix; the prefix is added to the links.
7
+ # type: gateway, spa or service; via: the address behind a gateway; tabs: the subtabs.
8
+ ServiceInfo = Data.define(:name, :prefix, :kind, :port, :random_port, :health, :routes, :openapi, :swagger,
9
+ :scalar, :env, :type, :via, :tabs)
10
+
11
+ class ServiceInfo
12
+ Route = Data.define(:verb, :path, :endpoint)
13
+
14
+ OPENAPI_PATH = Boto::App::OPENAPI_PATH
15
+ OPENAPI_PLUGIN = :openapi
16
+ SWAGGER_PLUGIN = :swagger_ui
17
+ SCALAR_PLUGIN = :scalar
18
+ SERVICE = :service
19
+
20
+ # host: the panel request's host, so a service with :hosts accepts the probe as it accepts the panel.
21
+ # origin: where the doc links point (the panel's own mount by default, a gateway or the service's port otherwise).
22
+ def self.build(name, prefix, app, solution_keys, host: nil, origin: prefix)
23
+ new(name: name, prefix: prefix, kind: SERVICE, port: app.port, random_port: random_port?(app),
24
+ health: HealthProbe.check(app, host: host), routes: routes_of(app),
25
+ openapi: "#{origin}#{openapi_path(app)}", swagger: doc_link(app, origin, SWAGGER_PLUGIN),
26
+ scalar: doc_link(app, origin, SCALAR_PLUGIN),
27
+ env: EnvRows.project(app.env, solution_keys), type: nil, via: nil, tabs: nil)
28
+ end
29
+
30
+ def self.random_port?(app)
31
+ app.env&.source(EnvRows::PORT) == EnvRows::BOTO_RUN_SOURCE
32
+ end
33
+
34
+ def self.routes_of(app)
35
+ app.endpoints.map do |endpoint|
36
+ verb, path = app.route_for(endpoint)
37
+ Route.new(verb: verb, path: path, endpoint: endpoint.name)
38
+ end
39
+ end
40
+
41
+ # The OpenAPI plugin keeps its own path; without it the core serves the default in development.
42
+ def self.openapi_path(app)
43
+ plugin = plugin_named(app, OPENAPI_PLUGIN)
44
+ plugin ? plugin.path : OPENAPI_PATH
45
+ end
46
+
47
+ def self.doc_link(app, prefix, name)
48
+ plugin = plugin_named(app, name)
49
+ "#{prefix}#{plugin.path}" if plugin
50
+ end
51
+
52
+ def self.plugin_named(app, name)
53
+ app.plugins.find { |plugin| plugin.class.plugin_name == name }
54
+ end
55
+
56
+ def ready? = health.status == HealthProbe::READY
57
+
58
+ def failing? = health.status == HealthProbe::FAILING
59
+ end
60
+ end
61
+ end
62
+ end