Module: GitLab::Exporter::Utils
- Defined in:
- lib/gitlab_exporter/util.rb
Overview
Helper methods, some stuff was copied from ActiveSupport
Class Method Summary collapse
- .camel_case_string(str) ⇒ Object
- .deep_symbolize_hash_keys(hash) ⇒ Object
- .deep_transform_keys_in_object(object, &block) ⇒ Object
- .exec_pgrep(pattern) ⇒ Object
- .pgrep(pattern) ⇒ Object
- .system_uptime ⇒ Object
- .wrap_in_array(object) ⇒ Object
Class Method Details
.camel_case_string(str) ⇒ Object
26 27 28 |
# File 'lib/gitlab_exporter/util.rb', line 26 def camel_case_string(str) str.gsub(/(?:_|^)([a-z\d]*)/i) { $1.capitalize } # rubocop:disable Style/PerlBackrefs end |
.deep_symbolize_hash_keys(hash) ⇒ Object
31 32 33 |
# File 'lib/gitlab_exporter/util.rb', line 31 def deep_symbolize_hash_keys(hash) deep_transform_keys_in_object(hash, &:to_sym) end |
.deep_transform_keys_in_object(object, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gitlab_exporter/util.rb', line 36 def deep_transform_keys_in_object(object, &block) case object when Hash object.keys.each do |key| value = object.delete(key) object[yield(key)] = deep_transform_keys_in_object(value, &block) end object when Array object.map! { |e| deep_transform_keys_in_object(e, &block) } else object end end |
.exec_pgrep(pattern) ⇒ Object
61 62 63 |
# File 'lib/gitlab_exporter/util.rb', line 61 def exec_pgrep(pattern) `pgrep -fl "#{pattern}"` end |
.pgrep(pattern) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/gitlab_exporter/util.rb', line 52 def pgrep(pattern) # pgrep will include the PID of the shell, so strip that out exec_pgrep(pattern).split("\n").each_with_object([]) do |line, arr| pid, name = line.split(" ") arr << pid if name != "sh" end end |
.system_uptime ⇒ Object
66 67 68 |
# File 'lib/gitlab_exporter/util.rb', line 66 def system_uptime File.read("/proc/uptime").split(" ")[0].to_f end |
.wrap_in_array(object) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/gitlab_exporter/util.rb', line 71 def wrap_in_array(object) if object.nil? [] elsif object.respond_to?(:to_ary) object.to_ary || [object] else [object] end end |