Module: Karafka::Web::Ui::Helpers::FormattingHelper

Included in:
Base
Defined in:
lib/karafka/web/ui/helpers/formatting_helper.rb

Overview

Generic value, number and text formatting helpers used across the Web UI

Instance Method Summary collapse

Instance Method Details

#format_memory(mem_kb) ⇒ String

Returns formatted memory usage.

Parameters:

  • mem_kb (Integer)

    memory used in KB

Returns:

  • (String)

    formatted memory usage



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 21

def format_memory(mem_kb)
  return "0" if !mem_kb || mem_kb.zero?

  if mem_kb < 10_240
    "#{number_with_delimiter(mem_kb.round(4))} KB"
  elsif mem_kb < 1_000_000
    "#{number_with_delimiter((mem_kb / 1024.0).to_i)} MB"
  else
    "#{number_with_delimiter((mem_kb / (1024.0 * 1024.0)).round(1))} GB"
  end
end

#number_with_delimiter(number, delimiter = ",") ⇒ String

Converts number to a more friendly delimiter based version

Parameters:

  • number (Numeric)
  • delimiter (String) (defaults to: ",")

    delimiter (comma by default)

Returns:

  • (String)

    number with delimiter



37
38
39
40
41
42
43
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 37

def number_with_delimiter(number, delimiter = ",")
  return "" unless number

  parts = number.to_s.to_str.split(".")
  parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
  parts.join(".")
end

#object_value_to_s(object) ⇒ String

Converts object into a string and for objects that would anyhow return their stringified instance value, it replaces it with the class name instead. Useful for deserializers, etc presentation.

Parameters:

  • object (Object)

Returns:

  • (String)


15
16
17
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 15

def object_value_to_s(object)
  object.to_s.include?("#<") ? object.class.to_s : object.to_s
end

#truncate(string, length: 50, omission: "...", strategy: :default) ⇒ String

Truncates given text if it is too long and wraps it with a title with full text. Can use a middle-based strategy that keeps beginning and ending of a string instead of keeping just the beginning.

The :middle strategy is useful when we have strings such as really long process names that have important beginning and end but middle can be removed without risk of not allowing user to recognize the content.

Parameters:

  • string (String)

    string we want to truncate

  • length (Integer) (defaults to: 50)

    max length of the final string that we accept before truncating

  • omission (String) (defaults to: "...")

    truncation omission

  • strategy (Symbol) (defaults to: :default)

    :default or :middle how should we truncate

Returns:

  • (String)

    HTML span tag with truncated content and full content title



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 58

def truncate(string, length: 50, omission: "...", strategy: :default)
  return string if string.length <= length

  case strategy
  when :default
    truncated = string[0...(length - omission.length)] + omission
  when :middle
    part_length = (length - omission.length) / 2
    truncated = string[0...part_length] + omission + string[-part_length..]
  else
    raise Karafka::Errors::UnsupportedCaseError, "Unknown strategy: #{strategy}"
  end

  %(<span title="#{string}">#{truncated}</span>)
end