Module: Gfspark::Graph

Included in:
App
Defined in:
lib/gfspark/graph.rb

Constant Summary collapse

BARS =
[" ",  "", "", "", "", "", "", "", "" ]
RANGES =
{
  "y"   => 'Year (1day avg)',
  "m"   => 'Month (2hour avg)',
  "w"   => 'Week (30min avg)',
  "3d"  => '3 Days (5min avg)',
  "s3d" => '3 Days (5min avg)',
  "d"   => 'Day (5min avg)',
  "sd"  => 'Day (1min avg)' ,
  "8h"  => '8 Hours (5min avg)',
  "s8h" => '8 Hours (1min avg)' ,
  "4h"  => '4 Hours (5min avg)',
  "s4h" => '4 Hours (1min avg)' ,
  "h"   => 'Hour (5min avg), ',
  "sh"  => 'Hour (1min avg)' ,
  "n"   => 'Half Day (5min avg)',
  "sn"  => 'Half Day (1min avg)',
  "c"   => "%s to $s",
  "sc"  => "%s to $s"
}

Instance Method Summary collapse

Instance Method Details

#bar(val, unit) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/gfspark/graph.rb', line 5

def bar(val, unit)
  n = (val.to_f/unit).ceil
  @height.times.map{|i|
    x = n - (i * 8)
    (x > 0) ?  (x < 8) ? BARS[x] : BARS.last : " "
  }
end

#period_titleObject



44
45
46
47
48
49
# File 'lib/gfspark/graph.rb', line 44

def period_title
  case @options[:t]
    when "c", "sc" then sprintf(RANGES[@options[:t]], @options[:from], @options[:to])
    else RANGES[@options[:t]]
  end
end

#range_arg?(t) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/gfspark/graph.rb', line 71

def range_arg?(t)
  RANGES.keys.include? t
end

#render(json, summary) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gfspark/graph.rb', line 13

def render(json, summary)
  rows = json['rows']
  max = rows.flatten.compact.max
  max_val = (max / 8).ceil * 8
  unit  = max_val / (@height * 8).to_f

  s = Time.at(json["start_timestamp"].to_i).strftime("%Y-%m-%d %H:%M:%S")
  e = Time.at(json["end_timestamp"].to_i).strftime("%Y-%m-%d %H:%M:%S")

  puts "  #{blue(json["column_names"].first)}"
  puts ""
  puts "    #{period_title}   #{s} - #{e}"
  puts ""

  result = []
  rows.flatten.map{|row| bar(row, unit)}.transpose.reverse.each_with_index do |row, i|
    i = (@height- i)
    label = i.even? ? sprintf("%.1f", unit * i * 8) : ""
    line = row.join
    if color = @options[:color]
      line = Term::ANSIColor.send(color, line)
    end
    result << "#{sprintf("%10s", label)} | #{line} |"
  end
  puts result.join("\n")
  puts ""

  sums = summary.first.last
  puts "    #{sprintf("cur: %.1f  ave: %.1f  max: %.1f  min %.1f", *sums)}"
end