Class: RateThrottleClient::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/rate_throttle_client/chart.rb

Instance Method Summary collapse

Constructor Details

#initialize(log_dir:, name:, time_scale:) ⇒ Chart

Returns a new instance of Chart.



3
4
5
6
7
8
9
# File 'lib/rate_throttle_client/chart.rb', line 3

def initialize(log_dir:, name:, time_scale:)
  @log_dir = log_dir
  @time_scale = time_scale
  @name = name
  @label_hash = nil
  @log_files = nil
end

Instance Method Details

#call(open_file = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rate_throttle_client/chart.rb', line 40

def call(open_file = false)
  require 'gruff'

  graph = Gruff::Line.new()
  graph.title_font_size = 24

  graph.hide_legend = true if log_files.length > 10
  graph.title = "#{@name}\nSleep Values for #{log_files.count} clients"
  graph.x_axis_label = "Time duration in hours"
  graph.y_axis_label = "Sleep time in seconds"

  log_files.each do |entry|
    graph.data entry.basename.to_s.gsub("-chart-data.txt", ""), entry.each_line.map(&:to_f)
  end

  graph.labels = label_hash

  graph.write(file)

  `open #{file}` if open_file

  file
end

#fileObject



64
65
66
# File 'lib/rate_throttle_client/chart.rb', line 64

def file
  @log_dir.join('chart.png')
end

#get_line_countObject



19
20
21
# File 'lib/rate_throttle_client/chart.rb', line 19

def get_line_count
  log_files.first.each_line.count
end

#label_hash(line_count = get_line_count) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rate_throttle_client/chart.rb', line 23

def label_hash(line_count = get_line_count)
  return @label_hash if @label_hash
  @label_hash = {}

  lines_per_hour = (3600.0 / @time_scale).floor

  line_tick = (line_count / 5.0).floor

  @label_hash[0] = "0"
  1.upto(5).each do |i|
    line_number = i * line_tick
    @label_hash[line_number - 1] = "%.2f" % (line_number.to_f / lines_per_hour)
  end

  @label_hash
end

#log_filesObject



11
12
13
14
15
16
17
# File 'lib/rate_throttle_client/chart.rb', line 11

def log_files
 @log_files ||= @log_dir.entries.map do |entry|
    @log_dir.join(entry)
  end.select do |file|
    file.basename.to_s.end_with?("-chart-data.txt")
  end
end