Class: Block

Inherits:
Object
  • Object
show all
Includes:
GlTail::Configurable
Defined in:
lib/gl_tail/block.rb

Overview

gl_tail.rb - OpenGL visualization of your server traffic Copyright 2007 Erlend Simonsen <[email protected]>

Licensed under the GNU General Public License v2 (see LICENSE)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GlTail::Configurable

#config_rewrite_color, included

Constructor Details

#initialize(config, name) ⇒ Block

Returns a new instance of Block.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gl_tail/block.rb', line 22

def initialize(config, name)
  @config = config
  @name = name

  @size = 10
  @auto_clean = true
  @activity_type = "blobs"
  @order = 100

  @show = 0

  @header = Element.new(self, @name.upcase, [1.0, 1.0, 1.0, 1.0])

  @elements = { }
  @bottom_position = -@config.screen.top
  @max_rate = 1.0/599

  @sorted = []
  @updated = false
end

Instance Attribute Details

#bottom_positionObject (readonly)

Returns the value of attribute bottom_position.



10
11
12
# File 'lib/gl_tail/block.rb', line 10

def bottom_position
  @bottom_position
end

#columnObject

Returns the value of attribute column.



18
19
20
# File 'lib/gl_tail/block.rb', line 18

def column
  @column
end

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/gl_tail/block.rb', line 19

def config
  @config
end

#max_rateObject (readonly)

Returns the value of attribute max_rate.



20
21
22
# File 'lib/gl_tail/block.rb', line 20

def max_rate
  @max_rate
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/gl_tail/block.rb', line 10

def name
  @name
end

#showObject

Returns the value of attribute show.



53
54
55
# File 'lib/gl_tail/block.rb', line 53

def show
  @show
end

Instance Method Details

#add_activity(options = { }) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/gl_tail/block.rb', line 111

def add_activity(options = { })
  return unless options[:name]
  x = nil
  unless @elements[options[:name]]
    x = Element.new(self, options[:name], @color || options[:color] )
    @elements[options[:name]] = x
    if @sorted.size > @size
      @sorted.insert(@size,x)
    else
      @sorted << x
    end
  else
    x = @elements[options[:name]]
  end
  x.add_activity(options[:message], @color || options[:color], options[:size] || 0.01, options[:type] || 0 )
  @updated = true
end

#add_event(options = { }) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gl_tail/block.rb', line 129

def add_event(options = { })
  return unless options[:name]
  x = nil
  unless @elements[options[:name]]
    x = Element.new(self, options[:name], @color || options[:color] )
    @elements[options[:name]] = x
    if @sorted.size > @size
      @sorted.insert(@size,x)
    else
      @sorted << x
    end
  else
    x = @elements[options[:name]]
  end

  x.add_event(options[:message], options[:color] || @color, options[:update_stats] || false)
  @updated = true
end

#alignmentObject



67
68
69
# File 'lib/gl_tail/block.rb', line 67

def alignment
  column.alignment
end

#is_rightObject



63
64
65
# File 'lib/gl_tail/block.rb', line 63

def is_right
  column.is_right
end

#line_sizeObject



59
60
61
# File 'lib/gl_tail/block.rb', line 59

def line_size
  @config.screen.line_size
end

#positionObject



71
72
73
# File 'lib/gl_tail/block.rb', line 71

def position
  column.position
end

#render(engine, num) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gl_tail/block.rb', line 79

def render(engine, num)
  return num if @elements.size == 0 || @sorted.size == 0

  @header.wy = top - (num * line_size)
  #    @header.y = @header.wy if @header.y == -$CONFIG.top
  @header.render(engine)
  num += 1

  count = 0

  @sorted.each do |e|
    engine.stats[0] += 1
    if count < @size
      e.wy = top - (num * line_size)
      e.render(engine)
      num += 1
      @max_rate = e.rate if e.rate > @max_rate
    else
      e.render_events(engine)
    end

    if e.activities.size == 0 && (e.rate <= 0.001 || count > 100) && @auto_clean
      @elements.delete(e.name)
      @sorted.delete(e)
    end
    count += 1
  end

  @bottom_position = top - ((@sorted.size > 0 ? (num-1) : num) * line_size)
  num + 1
end

#topObject



55
56
57
# File 'lib/gl_tail/block.rb', line 55

def top
  @config.screen.top
end

#updateObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/gl_tail/block.rb', line 148

def update
  return if @sorted.size == 0

  @max_rate = @max_rate * 0.9999

  startTime = Time.now

  i = 1
  @ordered = [@sorted[0]]
  min = @sorted[0].update
  size = @sorted.size

  while i < size
    rate = @sorted[i].update
    if rate > min
      j = i - 1
      while @ordered[j-1].rate < rate && j > 0
        j -= 1
      end
      @ordered.insert(j, @sorted[i])
    else
      @ordered << @sorted[i]
      min = rate if i < @size
    end
    i += 1
  end

  @sorted = @ordered

#    puts "#{@name} [#{@sorted.size}]: [#{Time.now - startTime}]" if @name == "urls"

  return

  return unless @updated

  sortTime = Time.now
#    iSort( @sorted )

#    @sorted = case @show
#              when 0: @sorted.insertionSort
#              when 1: @sorted.sort! { |k,v| "#{sprintf('%05d',v.total)} #{v.rate}" <=> "#{sprintf('%05d',k.total)} #{k.rate}" }
#              when 2: @sorted.sort! { |k,v| "#{v.average} #{v.name}" <=> "#{k.average} #{k.name}" }
#              end

  puts "#{@name} [#{@sorted.size}]: [#{sortTime - startTime}] [#{Time.now - sortTime}]"

  @updated = false

end

#widthObject



75
76
77
# File 'lib/gl_tail/block.rb', line 75

def width
  column.size
end