Class: Element

Inherits:
Object
  • Object
show all
Defined in:
lib/gl_tail/element.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

Constructor Details

#initialize(block, name, color, start_position = nil) ⇒ Element

Returns a new instance of Element.



11
12
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
# File 'lib/gl_tail/element.rb', line 11

def initialize(block, name, color, start_position = nil)
  @block = block

  if name.nil?
    name = ''
  end

  if name =~ /^\d+.\d+.\d+.\d+$/
    @name = Resolver.resolv(name, self)
  else
    @name = name
  end

  @start_position = start_position.nil? ? -@block.top : start_position

  @queue = []
  @pending = []
  @activities = []
  @messages = 0
  @rate = 0
  @total = 0
  @sum = 0
  @average = 0.0
  @last_time = 0
  @step = 0, @updates = 0
  @active = false
  @color = color || [1.0, 1.0, 1.0, 1.0]
  @type = (@block.activity_type == "blobs" ? :blobs : :bars)
  @bar_color ||= @color.dup

end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



8
9
10
# File 'lib/gl_tail/element.rb', line 8

def active
  @active
end

#activitiesObject (readonly)

Returns the value of attribute activities.



9
10
11
# File 'lib/gl_tail/element.rb', line 9

def activities
  @activities
end

#averageObject (readonly)

Returns the value of attribute average.



9
10
11
# File 'lib/gl_tail/element.rb', line 9

def average
  @average
end

#average_sizeObject

Returns the value of attribute average_size.



8
9
10
# File 'lib/gl_tail/element.rb', line 8

def average_size
  @average_size
end

#colorObject

Returns the value of attribute color.



8
9
10
# File 'lib/gl_tail/element.rb', line 8

def color
  @color
end

#messagesObject (readonly)

Returns the value of attribute messages.



9
10
11
# File 'lib/gl_tail/element.rb', line 9

def messages
  @messages
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/gl_tail/element.rb', line 8

def name
  @name
end

#queueObject (readonly)

Returns the value of attribute queue.



9
10
11
# File 'lib/gl_tail/element.rb', line 9

def queue
  @queue
end

#rateObject (readonly)

Returns the value of attribute rate.



9
10
11
# File 'lib/gl_tail/element.rb', line 9

def rate
  @rate
end

#rightObject

Returns the value of attribute right.



8
9
10
# File 'lib/gl_tail/element.rb', line 8

def right
  @right
end

#totalObject (readonly)

Returns the value of attribute total.



9
10
11
# File 'lib/gl_tail/element.rb', line 9

def total
  @total
end

#updatesObject (readonly)

Returns the value of attribute updates.



9
10
11
# File 'lib/gl_tail/element.rb', line 9

def updates
  @updates
end

#wyObject

Returns the value of attribute wy.



8
9
10
# File 'lib/gl_tail/element.rb', line 8

def wy
  @wy
end

#yObject

Returns the value of attribute y.



8
9
10
# File 'lib/gl_tail/element.rb', line 8

def y
  @y
end

Instance Method Details

#<(b) ⇒ Object



305
306
307
# File 'lib/gl_tail/element.rb', line 305

def < (b)
  b.rate < self.rate
end

#<=(b) ⇒ Object



309
310
311
# File 'lib/gl_tail/element.rb', line 309

def <= (b)
  b.rate <= self.rate
end

#<=>(b) ⇒ Object



297
298
299
# File 'lib/gl_tail/element.rb', line 297

def <=>(b)
  b.rate <=> self.rate
end

#>(b) ⇒ Object



301
302
303
# File 'lib/gl_tail/element.rb', line 301

def > (b)
  b.rate > self.rate
end

#>=(b) ⇒ Object



313
314
315
# File 'lib/gl_tail/element.rb', line 313

def >= (b)
  b.rate >= self.rate
end

#add_activity(message, color, size, type, real_size) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gl_tail/element.rb', line 43

def add_activity(message, color, size,  type, real_size)
  @bar_color[0] = @bar_color[0] + (@color[0] - @bar_color[0]) / 5
  @bar_color[1] = @bar_color[1] + (@color[1] - @bar_color[1]) / 5
  @bar_color[2] = @bar_color[2] + (@color[2] - @bar_color[2]) / 5

  @pending.push Item.new(message, size, color, type) if(type != 3)
  @messages += 1
  @total += 1
  @sum += real_size
  @color = color

  if @rate == 0
    @rate = 1.0 / 60
    @messages = 0
  end
end

#add_event(message, color, update_stats) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gl_tail/element.rb', line 60

def add_event(message, color, update_stats)
  @pending.push Item.new(message, 0.01, color, 2)
  if update_stats
    @messages += 1
    @total += 1
    if @rate == 0
      @rate = 1.0 / 60
      @messages = 0
    end
  end
end

#render(engine, options = { }) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/gl_tail/element.rb', line 125

def render(engine, options = { })

  # this used to be in the constructor, couldnt leave it there without using globals
  if not defined? @x
    @x = (@block.is_right ? @block.alignment : (@block.alignment - (8.0 / (@block.config.screen.window_width / 2.0)) * (@block.width + 8)))
    @y = @start_position
    @z = 0
    @wy = @start_position

    @color = @block.color.dup if @color.nil? && @block.color
    @color ||= [1.0, 1.0, 1.0, 1.0]
    @size = 0.01
  end

  @wx = @block.is_right ? (@block.alignment - (@block.width+8)*8.0 / (engine.screen.window_width / 2.0)) : @block.alignment

  if(@y == -@block.top)
    @y = @wy
  end

  d = @wy - @y
  if d.abs < 0.001
    @y = @wy
  else
    @y += d / 20
  end

  d = @wx - @x
  if d.abs < 0.001
    @x = @wx
  else
    @x += d / 20
  end

  glPushMatrix()
  glTranslate(@x, @y, @z)


  if( rate > 0.0 )
    glBegin(GL_QUADS)

    if @x >= 0
      y2 = 0.0
      @x1 = 7*8.0 / (engine.screen.window_width / 2.0)
      y1 = engine.screen.line_size * 0.9
      x2 = @x1 + ((@block.width+1) * 8.0 / (engine.screen.window_width / 2.0) ) * (rate / @block.max_rate)

      @x2 ||= @x1
      d = (@x2 - x2)
      if d.abs < 0.001
        @x2 = x2
      else
        @x2 -= d / 40
      end
      glColor(@bar_color)
      glVertex3f(@x1, y1, @z)
      glColor(@bar_color)
      glVertex3f(@x2, y1, @z)
      glColor([0.0, 0.0, 0.0, 0.0])
      glVertex3f(@x2, y2, @z)
      glColor(@bar_color)
      glVertex3f(@x1, y2, @z)

    else
      @x2 = (@block.width+1)*8.0 / (engine.screen.window_width / 2.0)
      y2  = 0.0
      y1  = engine.screen.line_size * 0.9
      x1 = ((@block.width * 8.0) / (engine.screen.window_width / 2.0) ) * (1.0 - rate / @block.max_rate)

      @x1 ||= @x2
      d = (@x1 - x1)
      if d.abs < 0.001
        @x1 = x1
      else
        @x1 -= d / 20
      end

      glColor(@bar_color)
      glVertex3f(@x1, y1, @z)
      glColor(@bar_color)
      glVertex3f(@x2, y1, @z)
      glColor(@bar_color)
      glVertex3f(@x2, y2, @z)
      glColor([0.0, 0.0, 0.0, 0.0])
      glVertex3f(@x1, y2, @z)

    end


    glEnd

  end


#    glTranslate(@x, @y, @z)

  glColor( (@queue.size > 0 ? (engine.screen.highlight_color || [1.0, 0.0, 0.0, 1.0]) : @color ) )

  case @block.show
  when 0
    if @rate < 0.0001
      txt = "    r/m "
    else
      txt = "#{sprintf("%8.2f",@rate * 60)} "
    end
  when 1
    if @total == 0
      txt = "  total "
    else
      txt = "#{sprintf("%8d",@total)} "
    end
  when 2
    if @average == 0
      txt = "    avg "
    else
      txt = "#{sprintf("%8.2f",@average)} "
    end
  else
    raise "unknown block type #{self.inspect}"
  end

  if @x < 0
    engine.render_string(sprintf("%#{@block.width}s", @name.length > @block.width ? @name[-@block.width..-1] : @name), txt)
  else
    engine.render_string(txt[1..-1], @name[0..@block.width-1])
  end


  glPopMatrix()

  t = glutGet(GLUT_ELAPSED_TIME)
  while( (@queue.size > 0) && (@last_time < t ) )

    @last_time += @step
    item = @queue.pop
    url = item.message
    color = item.color
    size = item.size
    type = item.type

    if type == 2
      @activities.push Activity.new(url, 0.0 - (0.008 * url.length), engine.screen.top, 0.0, color, size, type)
    elsif type == 5
      a = Activity.new(url, 0.0, engine.screen.top, 0.0, color, size, type)
      a.wx = @wx
      a.wy = @wy + 0.05
      @activities.push a
    elsif type != 4
      if @x >= 0
        @activities.push Activity.new(url, (@block.alignment - (@block.width+8)*8.0 / (engine.screen.window_width / 2.0)), @y + engine.screen.line_size/2, @z, color, size, type)
      else
        @activities.push Activity.new(url, (@block.alignment + (@block.width+8)*8.0 / (engine.screen.window_width / 2.0) ), @y + engine.screen.line_size/2, @z, color, size, type)
      end
    end
  end

  @activities.each do |a|
    if a.x > 1.0 || a.x < -1.0 || a.y < -(engine.screen.aspect*1.5)
      @activities.delete a
    else
      a.wy = @wy + 0.005 if(a.type == 5 && @wy != a.wy)
      a.render(engine)
      engine.stats[1] += 1
    end
  end

  @bar_color[0] = @bar_color[0] + (0.15 - @bar_color[0]) / 100
  @bar_color[1] = @bar_color[1] + (0.15 - @bar_color[1]) / 100
  @bar_color[2] = @bar_color[2] + (0.15 - @bar_color[2]) / 100

end

#render_events(engine) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gl_tail/element.rb', line 95

def render_events(engine)
  @color ||= [1.0, 1.0, 1.0, 1.0]

  t = glutGet(GLUT_ELAPSED_TIME)
  while( (@queue.size > 0) && (@last_time < t ) )

    @last_time += @step
    item = @queue.pop
    url = item.message
    color = item.color
    size = item.size
    type = item.type

    if type == 2
      @activities.push Activity.new(url, 0.0 - (0.008 * url.length), engine.screen.top, 0.0, color, size, type)
    end
  end

  @activities.each do |a|
    if a.x > 1.0 || a.x < -1.0 || a.y < -(engine.screen.aspect*1.5)
      @activities.delete a
    else
      a.wy = @wy + 0.005 if(a.type == 5 && @wy != a.wy)
      a.render(engine)
      engine.stats[1] += 1
    end
  end

end

#updateObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gl_tail/element.rb', line 73

def update
  @rate = (@rate * 299.0 + @messages) / 300.0
  @updates += 1
  @messages = 0
  size = @pending.size
  if size > 0
    @average = @sum / @total.to_f

    @step = 1.0 / size * 1000.0
    @queue = @pending
    if size == 1
      @step = rand(1000) * 1.0
    end
    @pending = []
  else
    @step = 0
  end
  @last_time = glutGet(GLUT_ELAPSED_TIME)
  @last_time += @step if @queue.size == 1
  @rate
end