From 4a3136e54186a74178b8a563ebd17832fdfd5f92 Mon Sep 17 00:00:00 2001 From: Charlle Dias Date: Mon, 4 Jun 2018 17:05:41 -0300 Subject: [PATCH] create a cache to persiste the bigger ems_ref --- .../event_catcher/event.rb | 23 ----- .../event_catcher/stream.rb | 23 +++-- .../physical_infra_manager/event_parser.rb | 22 ++++- .../event_catcher/stream_spec.rb | 17 +++- .../event_parser_spec.rb | 5 +- .../event_catcher/stream.yml | 94 ++++++++++++++++++- 6 files changed, 141 insertions(+), 43 deletions(-) delete mode 100644 app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/event.rb diff --git a/app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/event.rb b/app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/event.rb deleted file mode 100644 index 14334bf3dc..0000000000 --- a/app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/event.rb +++ /dev/null @@ -1,23 +0,0 @@ -class ManageIQ::Providers::Lenovo::PhysicalInfraManager::EventCatcher::Event - def initialize(data) - @data = data - end - - def to_hash - { - :event_type => @data.msgID, - :ems_ref => @data.cn, - :source => "LenovoXclarity", - :message => @data.msg, - :timestamp => @data.timeStamp, - :component_id => @data.componentID, - :severity => @data.severity, - :severity_type => @data.severityText, - :sender_uuid => @data.senderUUID, - :sender_name => @data.systemName, - :sender_model => @data.systemTypeModelText, - :sender_type => @data.systemTypeText, - :type => @data.typeText - } - end -end diff --git a/app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.rb b/app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.rb index 5744c13d98..1c0d47f582 100755 --- a/app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.rb +++ b/app/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.rb @@ -4,6 +4,7 @@ class ManageIQ::Providers::Lenovo::PhysicalInfraManager::EventCatcher::Stream def initialize(ems) @ems = ems @collect_events = true + @last_event_ems_ref = last_event_ems_ref(@ems.id) end # Stop capturing events @@ -27,17 +28,23 @@ def filter_fields { :operation => 'NOT', :field => 'eventClass', :value => '200' }, { :operation => 'NOT', :field => 'eventClass', :value => '800' } ] - last_cn_event = get_last_ems_ref(@ems.id) - cn_operation = { :operation => 'GT', :field => 'cn', :value => last_cn_event.to_s } - fields.push(cn_operation) unless last_cn_event.nil? + + unless @last_event_ems_ref.nil? + cn_operation = { :operation => 'GT', :field => 'cn', :value => @last_event_ems_ref.to_s } + fields.push(cn_operation) + end fields end def parse_events(events) - events.collect do |data| - event = ManageIQ::Providers::Lenovo::PhysicalInfraManager::EventCatcher::Event.new(data).to_hash - ManageIQ::Providers::Lenovo::PhysicalInfraManager::EventParser.event_to_hash(event, @ems.id) + if events.any? + events = events.sort { |x, y| Integer(x.cn) <=> Integer(y.cn) }.collect do |event| + ManageIQ::Providers::Lenovo::PhysicalInfraManager::EventParser.event_to_hash(event, @ems.id) + end + # Update the @last_event_ems_ref with the new last ems_ref if to exist new events + @last_event_ems_ref = events.last[:ems_ref] end + events end def events @@ -61,7 +68,7 @@ def create_event_connection(ems) :port => ems.endpoints.first.port) end - def get_last_ems_ref(ems_id) - EventStream.where(:ems_id => ems_id).maximum('CAST(ems_ref AS int)') || 1 + def last_event_ems_ref(ems_id) + EventStream.where(:ems_id => ems_id).maximum('CAST(ems_ref AS int)') end end diff --git a/app/models/manageiq/providers/lenovo/physical_infra_manager/event_parser.rb b/app/models/manageiq/providers/lenovo/physical_infra_manager/event_parser.rb index b4b1357058..b3258aaf1e 100755 --- a/app/models/manageiq/providers/lenovo/physical_infra_manager/event_parser.rb +++ b/app/models/manageiq/providers/lenovo/physical_infra_manager/event_parser.rb @@ -1,5 +1,6 @@ module ManageIQ::Providers::Lenovo::PhysicalInfraManager::EventParser - def self.event_to_hash(event, ems_id) + def self.event_to_hash(data, ems_id) + event = filter_data(data) event_hash = { :event_type => event[:event_type], :ems_ref => event[:ems_ref], @@ -10,10 +11,27 @@ def self.event_to_hash(event, ems_id) :full_data => event, :ems_id => ems_id } - event_hash end + def self.filter_data(data) + { + :component_id => data.componentID, + :component_type => data.typeText, + :event_type => data.msgID, + :ems_ref => data.cn, + :message => data.msg, + :parent_uuid => data.senderUUID, + :parent_name => data.systemName, + :parent_model => data.systemTypeModelText, + :parent_type => data.systemTypeText, + :severity_id => data.severity, + :severity => data.severityText, + :source => 'LenovoXclarity', + :timestamp => data.timeStamp, + } + end + def self.get_physical_server_id(ems_ref) PhysicalServer.find_by(:ems_ref => ems_ref).try(:id) end diff --git a/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream_spec.rb b/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream_spec.rb index 0e60e20139..a11bdbfcea 100644 --- a/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream_spec.rb +++ b/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream_spec.rb @@ -16,13 +16,20 @@ it "yields a valid event" do VCR.use_cassette(described_class.name.underscore.to_s) do result = [] + pool = 2 stream.each_batch do |events| - result = events - stream.stop - expect($log).to receive(:info).with(/Stopping collect of LXCA events .../) + result.push(*events) + pool-=1 + if pool < 1 + stream.stop + expect(events.count).to be == 1 + expect($log).to receive(:info).with(/Stopping collect of LXCA events .../) + else + expect(events.count).to be == 20 + end end - expect(result.count).to be == 20 - expect(result.all? { |item| item[:full_data][:severity] == 200 }).to be true + expect(result.count).to be == 21 + expect(result.all? { |item| item[:full_data][:severity_id] == 200 }).to be true end end end diff --git a/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_parser_spec.rb b/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_parser_spec.rb index 08d8fe05b9..d3aad188d1 100644 --- a/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_parser_spec.rb +++ b/spec/models/manageiq/providers/lenovo/physical_infra_manager/event_parser_spec.rb @@ -13,10 +13,7 @@ } end - let(:event1) do - data = XClarityClient::Event.new(event_attrs1) - ManageIQ::Providers::Lenovo::PhysicalInfraManager::EventCatcher::Event.new(data).to_hash - end + let(:event1) { XClarityClient::Event.new(event_attrs1) } it 'will parse events' do event_hash = described_class.event_to_hash(event1, 3) diff --git a/spec/vcr_cassettes/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.yml b/spec/vcr_cassettes/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.yml index dbfcd9e699..aa49f12ad9 100644 --- a/spec/vcr_cassettes/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.yml +++ b/spec/vcr_cassettes/manageiq/providers/lenovo/physical_infra_manager/event_catcher/stream.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://10.243.9.123/events?filterWith=%7B%22filterType%22:%22FIELDNOTREGEXAND%22,%22fields%22:%5B%7B%22operation%22:%22NOT%22,%22field%22:%22eventClass%22,%22value%22:%22200%22%7D,%7B%22operation%22:%22NOT%22,%22field%22:%22eventClass%22,%22value%22:%22800%22%7D,%7B%22operation%22:%22GT%22,%22field%22:%22cn%22,%22value%22:%221%22%7D%5D%7D + uri: https://10.243.9.123/events?filterWith=%7B%22filterType%22:%22FIELDNOTREGEXAND%22,%22fields%22:%5B%7B%22operation%22:%22NOT%22,%22field%22:%22eventClass%22,%22value%22:%22200%22%7D,%7B%22operation%22:%22NOT%22,%22field%22:%22eventClass%22,%22value%22:%22800%22%7D%5D%7D body: encoding: US-ASCII string: '' @@ -114,4 +114,96 @@ http_interactions: Available","systemText":"IMM2-e41f13ed5a1e"}]' http_version: recorded_at: Wed, 15 Nov 2017 14:14:02 GMT +- request: + method: get + uri: https://10.243.9.123/events?filterWith=%7B%22filterType%22:%22FIELDNOTREGEXAND%22,%22fields%22:%5B%7B%22operation%22:%22NOT%22,%22field%22:%22eventClass%22,%22value%22:%22200%22%7D,%7B%22operation%22:%22NOT%22,%22field%22:%22eventClass%22,%22value%22:%22800%22%7D,%7B%22operation%22:%22GT%22,%22field%22:%22cn%22,%22value%22:%22684922%22%7D%5D%7D + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - LXCA via Ruby Client/0.5.7 + Authorization: + - Basic bHhjYzpQQVNTVzByRA== + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 15 Nov 2017 14:13:24 GMT + Set-Cookie: + - userAuthenticationMethod=local;Path=/;Secure + Expires: + - "-1" + - Thu, 01 Jan 1970 00:00:00 GMT + Strict-Transport-Security: + - max-age=86400; includeSubDomains; + X-Frame-Options: + - SAMEORIGIN + Cache-Control: + - no-store, no-cache, must-revalidate + Pragma: + - no-cache + Content-Security-Policy: + - 'default-src https:; script-src https: ''unsafe-inline'' ''unsafe-eval''; + style-src https: ''unsafe-inline''' + X-Content-Type-Options: + - nosniff + X-Xss-Protection: + - '1' + Content-Type: + - application/com.lenovo.lxca-v1.2.2+json; charset=UTF-8 + Vary: + - Accept-Encoding, User-Agent + Transfer-Encoding: + - chunked + body: + encoding: ASCII-8BIT + string: '[{ + "eventDate":"2017-11-08T21:50:42Z", + "systemTypeText":"Node", + "chassisText":"", + "timeStamp":"2017-11-08T17:49:14Z", + "location":"", + "args":[], + "originatorUUID":"", + "systemName":"XinYi-71", + "bayText":"Not Available", + "mtm":"546235Z","userid":"", + "fruSerialNumberText":"None", + "eventSourceText":"Hardware", + "serialnum":"KVX0171", + "sourceLogID":"PEL", + "action":100, + "parameters":{}, + "localLogSequence":"", + "localLogID":"", + "sourceID":"EADEBE8316174750A27FEC2E8226AC48", + "systemTypeModelText":"5462-35Z", + "flags":"", + "userIDIndex":"", + "failSNs":"", + "severityText":"Informational", + "severity":200, + "serviceabilityText":"Not Required", + "failFRUs":[""], + "msg":"Host Power has been turned on.", + "componentID":"EADEBE8316174750A27FEC2E8226AC48", + "eventClass":400, + "eventID":"816F00091301FFFF", + "sourceLogSequence":9852, + "systemSerialNumberText":"KVX0171", + "cn":"684923", + "service":100, + "typeText":"Power", + "msgID":"PLAT0107", + "systemFruNumberText":"None", + "systemText":"XinYi-71"}]' + http_version: + recorded_at: Wed, 15 Nov 2017 14:14:02 GMT recorded_with: VCR 3.0.3