Class: GRI::Sgraph

Inherits:
Object show all
Defined in:
lib/gri/sgraph.rb

Constant Summary collapse

BARS =
[" ",  "", "", "", "", "", "", "", "" ]
TERMS =
{'sh'=>['Hour', 3600], 's8h'=>['8Hours', 8*3600],
'd'=>['Day', 24*3600], 'w'=>['Week', 7*24*3600],
'm'=>['Month', 31*24*3600], 'y'=>['Year', 366*24*3600]}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Sgraph

Returns a new instance of Sgraph.



6
7
8
# File 'lib/gri/sgraph.rb', line 6

def initialize options
  @options = options
end

Instance Method Details

#bar(val, unit, non_fullwidth_font = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gri/sgraph.rb', line 11

def bar(val, unit, non_fullwidth_font = false)
  n = (val.to_f/unit)
  @height.times.map{|i|
    x = n - (i * 8)
    if x <= 0
      BARS.first
    else
      bar_symbol = if x < 8
                     BARS[x]
                   else
                     BARS.last
                   end
      bar_symbol += " " if non_fullwidth_font
      bar_symbol
    end
  }
end

#render(json, summary, url = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
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/gri/sgraph.rb', line 29

def render(json, summary, url = nil)
  #@options = {:t=>'d', :non_fullwidth_font=>false}
  @height = 16
  rowss = json['data']
  meta = json['meta']
  step    = meta['step']
  start_timestamp = meta["start"]
  end_timestamp   = meta["end"].to_i
  s = Time.at(start_timestamp).localtime.strftime("%Y-%m-%d %H:%M:%S")
  e = Time.at(end_timestamp  ).localtime.strftime("%Y-%m-%d %H:%M:%S")

  rowss.transpose.each_with_index {|rows, i|
    #puts "i: #{i}"
    max     = rows.flatten.compact.max

    u0 = (max / @height)
    ex0 = (Math.log(u0) / Math.log(10)).floor
    unit = (u0 / 8 / (10**(ex0-1))).ceil * (10**(ex0-1))

    max_val = unit*@height
    #max_val = max
    #unit    = max_val / (@height * 8).to_f

    puts "    #{(url)}"
    puts "    #{s} -"
    puts "  #{meta['legend'][i].center(78)}"
    render_graph rows, unit
    render_x_axis_labels(rows, start_timestamp, step)
    puts ""

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

#render_graph(rows, unit) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gri/sgraph.rb', line 64

def render_graph rows, unit
  result = []

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

#render_x_axis_labels(rows, start_timestamp, step) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gri/sgraph.rb', line 108

def render_x_axis_labels(rows, start_timestamp, step)
  tm = rows.size * step

  x_axis_labels = rows.length.times.select{|n| n % 8 == 0}. map{|n|
    t = Time.at(start_timestamp + (n * step)).localtime
    sprintf("%-8s", to_axis_label(t, tm))
  }.join
  x_axis_arrows= rows.length.times.select{|n| n % 8 == 0}. map{|n|
    " /      "
  }.join

  case 'show'#@options[:x_axis_label]
  when "show"
    puts sprintf("%6s%s", "", x_axis_arrows)
    puts sprintf("%6s%s", "", x_axis_labels)
  when "simple"
    puts sprintf("%6s%s", "", x_axis_labels)
  end
end

#to_axis_label(t, tm) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gri/sgraph.rb', line 128

def to_axis_label t, tm
  f = if tm <= 3600
        '%M'
      elsif tm <= 48*3600
        '%H:%M'
      elsif tm <= 8*24*3600
        '%a'
      elsif tm <= 35*24*3600
        '%d'
      else
        '%m'
      end
  t.strftime f
end

#to_scalestr(v, base = 1000) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gri/sgraph.rb', line 90

def to_scalestr v, base=1000
  if v == nil or base == nil or base == 0
    return(v.to_i == v ? v.to_i : v)
  end
  v = v.to_f
  if v >= base ** 4
    "%gT" % (v / (base ** 4))
  elsif v >= base ** 3
    "%gG" % (v / (base ** 3))
  elsif v >= base ** 2
    "%gM" % (v / (base ** 2))
  elsif v >= base
    "%gK" % (v / base)
  else
    v.to_s
  end
end