Class: StepStats::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/step_stats/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Stats

Returns a new instance of Stats.



5
6
7
8
9
# File 'lib/step_stats/stats.rb', line 5

def initialize(*args)
  @stats = {}
  @step_counter = "STEP-000"
  @steps = {}
end

Instance Attribute Details

#statsObject

Returns the value of attribute stats.



3
4
5
# File 'lib/step_stats/stats.rb', line 3

def stats
  @stats
end

Instance Method Details

#add_stat(step_definition, duration, status, location) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/step_stats/stats.rb', line 11

def add_stat step_definition, duration, status, location
  step_number = get_step_number step_definition.regexp_source
  if @stats[step_number].nil?
    @stats[step_number] = Step.new(step_definition.regexp_source, step_definition.file_colon_line)
  end
  step_entry = {duration: duration, status: status, location: location}
  @stats[step_number].add step_entry
end

#get_step_number(step_regx) ⇒ Object



20
21
22
23
# File 'lib/step_stats/stats.rb', line 20

def get_step_number step_regx
  @steps[step_regx] = @step_counter.next!.dup if @steps[step_regx].nil?
  @steps[step_regx]
end

#percent(step, rounding = 'ceil') ⇒ Object



25
26
27
# File 'lib/step_stats/stats.rb', line 25

def percent step,rounding='ceil'
  ((step.total/total_time)*100).send(rounding)
end

#to_chart_dataObject



49
50
51
# File 'lib/step_stats/stats.rb', line 49

def to_chart_data
  @stats.values.each.map{|s|[s.name,s.total,s.location]}.to_s
end

#top30Object



41
42
43
# File 'lib/step_stats/stats.rb', line 41

def top30
  (@stats.values.count*0.3).ceil
end

#top30countObject



33
34
35
# File 'lib/step_stats/stats.rb', line 33

def top30count
  @stats.values.sort_by{|step| step.count}.reverse[0..top30]
end

#top30timeObject



37
38
39
# File 'lib/step_stats/stats.rb', line 37

def top30time
  @stats.values.sort_by{|step| step.avg}.reverse[0..top30]
end

#total_timeObject



45
46
47
# File 'lib/step_stats/stats.rb', line 45

def total_time
  @stats.values.inject(0){|accum,step| accum+step.total}.to_f
end