Class: Railswatch::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/railswatch/utils.rb

Constant Summary collapse

DEFAULT_TIME_OFFSET =
1.minute

Class Method Summary collapse

Class Method Details

.days(duration = Railswatch.duration) ⇒ Object



19
20
21
# File 'lib/railswatch/utils.rb', line 19

def self.days(duration = Railswatch.duration)
  (duration / 1.day) + 1
end

.filter_params(params) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/railswatch/utils.rb', line 48

def self.filter_params(params)
  return {} if params.blank?
  return {} unless defined?(::Rails) && ::Rails.application

  filter = ActiveSupport::ParameterFilter.new(::Rails.application.config.filter_parameters)
  filter.filter(params.to_h)
rescue StandardError
  {}
end

.from_datetimei(datetimei) ⇒ Object



15
16
17
# File 'lib/railswatch/utils.rb', line 15

def self.from_datetimei(datetimei)
  Time.at(datetimei, in: '+00:00')
end

.kind_of_nowObject



11
12
13
# File 'lib/railswatch/utils.rb', line 11

def self.kind_of_now
  time + DEFAULT_TIME_OFFSET
end

.median(array) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/railswatch/utils.rb', line 23

def self.median(array)
  sorted = array.sort
  size = sorted.size
  center = size / 2

  if size.zero?
    nil
  elsif size.even?
    (sorted[center - 1] + sorted[center]) / 2.0
  else
    sorted[center]
  end
end

.percentile(values, percentile) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/railswatch/utils.rb', line 37

def self.percentile(values, percentile)
  return nil if values.empty?

  sorted = values.sort
  rank = (percentile.to_f / 100) * (sorted.size - 1)

  lower = sorted[rank.floor]
  upper = sorted[rank.ceil]
  lower + ((upper - lower) * (rank - rank.floor))
end

.timeObject



7
8
9
# File 'lib/railswatch/utils.rb', line 7

def self.time
  Time.now.utc
end