From 677dec2203ba24808326754983b4d02fbed6b9b3 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sun, 17 Nov 2024 17:07:36 +1100 Subject: [PATCH] Display metrics in human friendly units (#184) --- bin/outboxer_sidekiq_publisher | 2 +- lib/outboxer/web.rb | 38 ++++++++++++++++++++++++-- lib/outboxer/web/views/home.erb | 20 +++++++------- lib/outboxer/web/views/message.erb | 4 +-- lib/outboxer/web/views/messageable.erb | 2 +- lib/outboxer/web/views/messages.erb | 4 +-- lib/outboxer/web/views/publisher.erb | 12 ++++---- 7 files changed, 58 insertions(+), 24 deletions(-) diff --git a/bin/outboxer_sidekiq_publisher b/bin/outboxer_sidekiq_publisher index 9c54ac96..4b12d02e 100755 --- a/bin/outboxer_sidekiq_publisher +++ b/bin/outboxer_sidekiq_publisher @@ -37,6 +37,6 @@ Outboxer::Publisher.publish( EventCreatedJob.perform_async({ 'id' => message[:messageable_id] }) logger.info "Outboxer published message id=#{message[:id]} "\ - "messageable=#{message[:messageable_type]}/#{message[:messageable_id]}"\ + "messageable=#{message[:messageable_type]}::#{message[:messageable_id]}"\ end end diff --git a/lib/outboxer/web.rb b/lib/outboxer/web.rb index d244e258..6a144eb3 100755 --- a/lib/outboxer/web.rb +++ b/lib/outboxer/web.rb @@ -27,7 +27,41 @@ def outboxer_path(path) "#{request.script_name}#{path}" end - def human_readable_size(kilobytes) + def pretty_throughput(per_second: 0) + return "-" if per_second == 0 + + "#{per_second} /s" + end + + def pretty_duration_from_period(start_time:, end_time: ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)) + pretty_duration(seconds: end_time - start_time) + end + + def pretty_duration_from_seconds(seconds:) + return "-" if seconds <= 0 + + units = [ + { name: "y", scale: 1.0 / 31_536_000 }, # 1 year = 31,536,000 seconds (365 days) + { name: "mo", scale: 1.0 / 2_592_000 }, # 1 month = 2,592,000 seconds (30 days) + { name: "w", scale: 1.0 / 604_800 }, # 1 week = 604,800 seconds + { name: "d", scale: 1.0 / 86_400 }, # 1 day = 86,400 seconds + { name: "h", scale: 1.0 / 3_600 }, # 1 hour = 3,600 seconds + { name: "min", scale: 1.0 / 60 }, # 1 minute = 60 seconds + { name: "s", scale: 1 }, # 1 second = 1 second + { name: "ms", scale: 1_000 }, # 1 millisecond = 1/1,000 second + { name: "µs", scale: 1_000_000 }, # 1 microsecond = 1/1,000,000 second + { name: "ns", scale: 1_000_000_000 } # 1 nanosecond = 1/1,000,000,000 second + ].freeze + + units.each do |unit| + value = seconds * unit[:scale] + if value >= 1 || unit[:name] == "ns" + return "#{value.round(0)} #{unit[:name]}" + end + end + end + + def human_readable_size(kilobytes:) units = ['KB', 'MB', 'GB', 'TB'] size = kilobytes.to_f unit = units.shift @@ -37,7 +71,7 @@ def human_readable_size(kilobytes) unit = units.shift end - "#{size.round(2)} #{unit}" + "#{size.round(0)} #{unit}" end def time_ago_in_words(time) diff --git a/lib/outboxer/web/views/home.erb b/lib/outboxer/web/views/home.erb index 246be49a..c6f8744f 100644 --- a/lib/outboxer/web/views/home.erb +++ b/lib/outboxer/web/views/home.erb @@ -42,11 +42,11 @@ <%= time_ago_in_words(publisher[:created_at]) %> ago <%= time_ago_in_words(publisher[:updated_at]) %> ago <%= publisher[:status] %> - <%= publisher[:metrics]['throughput'] %> - <%= publisher[:metrics]['latency'] %> - <%= publisher[:metrics]['cpu'] %>% - <%= human_readable_size(publisher[:metrics]['rss']) %> - <%= (publisher[:metrics]['rtt'] * 1000).round(2) %> ms + <%= pretty_throughput(per_second: publisher[:metrics]['throughput']) %> + <%= pretty_duration_from_seconds(seconds: publisher[:metrics]['latency']) %> + <%= publisher[:metrics]['cpu'].round(0) %>% + <%= human_readable_size(kilobytes: publisher[:metrics]['rss']) %> + <%= pretty_duration_from_seconds(seconds: publisher[:metrics]['rtt']) %>
@@ -114,11 +114,11 @@ Status - Historic count Current count - Total count Throughput Latency + Historic count + Total count @@ -129,11 +129,11 @@ <%= status %> - <%= messages_metrics[status.to_sym][:count][:historic] || '-' %> <%= messages_metrics[status.to_sym][:count][:current] %> + <%= pretty_throughput(per_second: messages_metrics[status.to_sym][:throughput]) %> + <%= pretty_duration_from_seconds(seconds: messages_metrics[status.to_sym][:latency]) %> + <%= messages_metrics[status.to_sym][:count][:historic] || '-' %> <%= messages_metrics[status.to_sym][:count][:total] || '-' %> - <%= messages_metrics[status.to_sym][:throughput] %> - <%= messages_metrics[status.to_sym][:latency] %> <% end %> diff --git a/lib/outboxer/web/views/message.erb b/lib/outboxer/web/views/message.erb index 153cf789..d462e8d4 100644 --- a/lib/outboxer/web/views/message.erb +++ b/lib/outboxer/web/views/message.erb @@ -1,7 +1,7 @@
-

Message <%= message[:id] %>

+

Message::<%= message[:id] %>

" method="post" style="display: inline;"> @@ -43,7 +43,7 @@ Status - <%= message[:status] %> + <%= message[:status] %> Created diff --git a/lib/outboxer/web/views/messageable.erb b/lib/outboxer/web/views/messageable.erb index e3944aa5..e0f7c7c8 100644 --- a/lib/outboxer/web/views/messageable.erb +++ b/lib/outboxer/web/views/messageable.erb @@ -1,7 +1,7 @@
-

<%= messageable.class.name %> <%= messageable.id %>

+

<%= messageable.class.name %>::<%= messageable.id %>

diff --git a/lib/outboxer/web/views/messages.erb b/lib/outboxer/web/views/messages.erb index 16142067..fd815267 100644 --- a/lib/outboxer/web/views/messages.erb +++ b/lib/outboxer/web/views/messages.erb @@ -119,10 +119,10 @@ <%= message[:id] %> - + - + - + - + - + - +
<%= message[:status] %><%= message[:status] %> " class="custom-link"> - <%= message[:messageable_type] %>/<%= message[:messageable_id] %> + <%= message[:messageable_type] %>::<%= message[:messageable_id] %> diff --git a/lib/outboxer/web/views/publisher.erb b/lib/outboxer/web/views/publisher.erb index 4ef235fb..eef7d473 100644 --- a/lib/outboxer/web/views/publisher.erb +++ b/lib/outboxer/web/views/publisher.erb @@ -1,7 +1,7 @@
-

Publisher <%= publisher[:id] %>

+

Publisher::<%= publisher[:id] %>

" method="post" style="display: inline;"> @@ -102,23 +102,23 @@
Throughput<%= publisher[:metrics]['throughput'] %><%= pretty_throughput(per_second: publisher[:metrics]['throughput']) %>
Latency<%= publisher[:metrics]['latency'] %><%= pretty_duration_from_seconds(seconds: publisher[:metrics]['latency']) %>
CPU<%= publisher[:metrics]['cpu'] %>%<%= publisher[:metrics]['cpu'].round(0) %>%
RSS<%= human_readable_size(publisher[:metrics]['rss']) %><%= human_readable_size(kilobytes: publisher[:metrics]['rss']) %>
RTT<%= (publisher[:metrics]['rtt'] * 1000).round(2) %> ms<%= pretty_duration_from_seconds(seconds: publisher[:metrics]['rtt']) %>