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.



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

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.



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

def data
  @data
end

#uuidObject

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Class Method Details

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



47
48
49
50
51
# File 'lib/process_metrics/base.rb', line 47

def self.work(name, parent=nil, &block)
  metric = ProcessMetrics::Base.new name, parent
  block.call metric
  metric.save
end

Instance Method Details

#attributesObject



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

def attributes
  {
    uuid: @uuid,
    parent_uuid: @parent_uuid,
    name: @name,
    data: @data,
    started_at: @started_at,
    finished_at: @finished_at
  }
end

#measure(name, &block) ⇒ Object



43
44
45
# File 'lib/process_metrics/base.rb', line 43

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

#saveObject



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

def save
  @finished_at = Time.now
  ProcessMetrics.config.persistence.save(self)
end

#serialized_attributesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/process_metrics/base.rb', line 27

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

#to_sObject



53
54
55
# File 'lib/process_metrics/base.rb', line 53

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