Class: StompMessage::StompStatisticsServer

Inherits:
StompServer
  • Object
show all
Defined in:
lib/stomp_message/stomp_statistics_server.rb

Overview

statistics listening class. Updates the statistics at every message/event

Instance Attribute Summary collapse

Attributes inherited from StompServer

#conn, #exception_count, #host, #login, #msg_count, #mythreads, #password, #port, #queue, #thread_count, #topic

Instance Method Summary collapse

Methods inherited from StompServer

#close_topic, #connect_connection, #connect_topic, #disconnect_stomp, #method_missing, #monitor_queue_status, #run, #send_reply, #setup_auto_close, #stomp_DEBUG

Constructor Details

#initialize(options = {}) ⇒ StompStatisticsServer

Returns a new instance of StompStatisticsServer.



14
15
16
17
# File 'lib/stomp_message/stomp_statistics_server.rb', line 14

def initialize(options={})
  super(options)
  setup_var
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class StompMessage::StompServer

Instance Attribute Details

#statisticsObject

Returns the value of attribute statistics.



7
8
9
# File 'lib/stomp_message/stomp_statistics_server.rb', line 7

def statistics
  @statistics
end

#tagsObject

Returns the value of attribute tags.



7
8
9
# File 'lib/stomp_message/stomp_statistics_server.rb', line 7

def tags
  @tags
end

Instance Method Details

#handle_message(msg) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/stomp_message/stomp_statistics_server.rb', line 33

def handle_message(msg)
     puts "in handle message stomp stat server" if @debug
      m=StompMessage::Message.load_xml(msg.body)
     tag_increment(m.command)
     super(msg)
     tag_increment('msg_count')
     tag_increment('exception_count') if @old_exception_count!= self.exception_count
      @old_exception_count= self.exception_count
      
end

#increment_stats(tag, element) ⇒ Object

update the statistics by tag value (eg hour)



19
20
21
# File 'lib/stomp_message/stomp_statistics_server.rb', line 19

def increment_stats(tag, element)
   self.statistics[tag][element]+=1
end

#reset_element(tag, element) ⇒ Object

reset element to zero (eg reset hour to zero)



23
24
25
# File 'lib/stomp_message/stomp_statistics_server.rb', line 23

def reset_element(tag, element)
   self.statistics[tag][element]=0
end

#setup_varObject



8
9
10
11
12
13
# File 'lib/stomp_message/stomp_statistics_server.rb', line 8

def setup_var
 self.tags = %w(hour day week  month)
 self.statistics = Hash.new
 self.tags.each { |tagv| statistics[tagv] = Hash.new(0)    }
 @old_exception_count=0
end

#stomp_PING(msg, stomp_msg) ⇒ Object



43
44
45
46
# File 'lib/stomp_message/stomp_statistics_server.rb', line 43

def stomp_PING(msg, stomp_msg)
      puts "stomp PING: #{msg.body}" if @debug
      #do not reply as statistic servers do no respond to pings.  they respond to stomp_REPORT    
end

#stomp_REPORT(msg, stomp_msg) ⇒ Object

}

}

end



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stomp_message/stomp_statistics_server.rb', line 59

def stomp_REPORT(msg, stomp_msg)
  
   result= " Stomp Report #{Time.now}\n"
   result << " Messages processed: #{self.msg_count} \n"
   flag =   self.exception_count!=nil ? '' : 'IMPORTANT'
   result << "#{flag} Exceptions processed: #{self.exception_count}\n "
   self.tags.each { |tagv|  # run through each tag  (hour, day etc)  
       result << "#{tagv} Latests Statistics\n"
       self.statistics[tagv].each {|k,v| result <<  " ----- #{k}: #{v}\n"}
      }
   puts result if @debug
   reply_msg = StompMessage::Message.new('stomp_REPLY', "#{result}")
   send_reply(stomp_msg.headers,reply_msg) if stomp_msg.headers['reply-to']!=nil
   result
end

#stomp_RESET(msg, stomp_msg) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/stomp_message/stomp_statistics_server.rb', line 74

def stomp_RESET(msg, stomp_msg)
    puts "stomp reset: #{msg.body}" if @debug
    reset_tag = msg.body.to_s if self.tags.include?(msg.body.to_s)
    puts " --- RESET TAG: #{reset_tag}"
    self.statistics[reset_tag].each { |k,v| self.statistics[reset_tag][k]=0
                                   puts "RESET: tag #{k} value is zero"
                                    } if self.tags.include?(msg.body.to_s)
end

#tag_increment(element) ⇒ Object

increment all the tags with a new element value . Hash is zero if not found



27
28
29
30
31
# File 'lib/stomp_message/stomp_statistics_server.rb', line 27

def tag_increment(element)
  self.tags.each {|tagv|  puts "incrementing #{tagv}  element: #{element}" if @debug
                        increment_stats(tagv,element)}
  
end