Class: SpeedGun::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/speed_gun/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Returns a new instance of Report.



35
36
37
38
39
40
# File 'lib/speed_gun/report.rb', line 35

def initialize
  @id = SecureRandom.uuid
  @name = nil
  @sources = []
  @events = []
end

Instance Attribute Details

#eventsArray<SpeedGun::Event> (readonly)

Returns Recorded events.

Returns:



15
16
17
# File 'lib/speed_gun/report.rb', line 15

def events
  @events
end

#idString (readonly)

Returns Report ID.

Returns:

  • (String)

    Report ID



7
8
9
# File 'lib/speed_gun/report.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/speed_gun/report.rb', line 9

def name
  @name
end

#sourcesArray<SpeedGun::Source> (readonly)

Returns Profiled source codes.

Returns:



12
13
14
# File 'lib/speed_gun/report.rb', line 12

def sources
  @sources
end

Class Method Details

.from_hash(hash) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/speed_gun/report.rb', line 17

def self.from_hash(hash)
  report = new

  hash['sources'].map! do |source_id, hash|
    SpeedGun::Source.from_hash(hash, source_id)
  end

  hash['events'].map! do |event_id, hash|
    SpeedGun::Event.from_hash(hash, event_id)
  end

  hash.each_pair do |key, val|
    report.instance_variable_set(:"@#{key}", val)
  end

  report
end

Instance Method Details

#durationObject



58
59
60
# File 'lib/speed_gun/report.rb', line 58

def duration
  latest_event_finished_at.to_f - nearlest_event_started_at.to_f
end

#latest_event_finished_atObject



54
55
56
# File 'lib/speed_gun/report.rb', line 54

def latest_event_finished_at
  @events.sort_by { |event| event.roughly_finished_at.to_f * -1 }.first.roughly_finished_at
end

#nearlest_event_started_atObject



50
51
52
# File 'lib/speed_gun/report.rb', line 50

def nearlest_event_started_at
  @events.sort_by(&:started_at).first.started_at
end

#record(event) ⇒ Object



42
43
44
# File 'lib/speed_gun/report.rb', line 42

def record(event)
  @events.push(event)
end

#source(source) ⇒ Object



46
47
48
# File 'lib/speed_gun/report.rb', line 46

def source(source)
  @sources.push(source)
end

#to_hashObject



62
63
64
65
66
67
68
# File 'lib/speed_gun/report.rb', line 62

def to_hash
  {
    name: name,
    sources: sources.map { |source| [ source.id, source.to_hash ] },
    events: events.map { |event| [event.id, event.to_hash] }
  }
end