Class: ProcessMetrics::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/process_metrics/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
# File 'lib/process_metrics/base.rb', line 8

def initialize(name, parent=nil)
  @uuid        = SecureRandom.uuid
  @parent_uuid = parent ? parent.uuid : nil
  @data        = nil
  @name        = name
  @started_at  = Time.now
  @finished_at = nil
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/process_metrics/base.rb', line 6

def data
  @data
end

#parent_uuidObject

Returns the value of attribute parent_uuid.



6
7
8
# File 'lib/process_metrics/base.rb', line 6

def parent_uuid
  @parent_uuid
end

#uuidObject

Returns the value of attribute uuid.



6
7
8
# File 'lib/process_metrics/base.rb', line 6

def uuid
  @uuid
end

Class Method Details

.work(name, parent = nil, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/process_metrics/base.rb', line 44

def self.work(name, parent=nil, &block)
  metric = ProcessMetrics::Base.new name, parent
  result = nil
  begin
    result = block.call metric
    metric.finish
  ensure
    Thread.new(metric) { |_metric| _metric.save }.join
  end
  result
end

Instance Method Details

#attributesObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/process_metrics/base.rb', line 17

def attributes
  {
    uuid: @uuid,
    parent_uuid: @parent_uuid,
    name: @name,
    data: YAML::dump(@data),
    started_at: @started_at ? @started_at.strftime("%Y-%m-%d %H:%M:%S.%N") : nil,
    finished_at: @finished_at ? @finished_at.strftime("%Y-%m-%d %H:%M:%S.%N") : nil
  }
end

#finishObject



28
29
30
# File 'lib/process_metrics/base.rb', line 28

def finish
  @finished_at = Time.now
end

#measure(name, &block) ⇒ Object



40
41
42
# File 'lib/process_metrics/base.rb', line 40

def measure(name, &block)
  work(name, self, &block)
end

#saveObject



36
37
38
# File 'lib/process_metrics/base.rb', line 36

def save
  ProcessMetrics.config.persistence.save(self)
end

#to_sObject



56
57
58
# File 'lib/process_metrics/base.rb', line 56

def to_s
  self.class.name + " " + attributes.to_s
end