lib/hawkular/operations/operations_api.rb in hawkular-client-4.0.0 vs lib/hawkular/operations/operations_api.rb in hawkular-client-4.1.0
- old
+ new
@@ -26,18 +26,44 @@
# Client class to interact with the agent via websockets
class Client < Hawkular::BaseClient
include WebSocket::Client
include MonitorMixin
- attr_accessor :ws, :session_id, :logger
+ attr_accessor :ws, :logger
# helper for parsing the "OperationName=json_payload" messages
class WebSocket::Frame::Data
def to_msg_hash
- chunks = split('=', 2)
+ operation_name, json = split('=', 2)
- { operationName: chunks[0], data: JSON.parse(chunks[1]) }
+ # Check if there is a zip file following JSON.
+ # This check is done only in the first 100KB, hoping it's unlikely to
+ # have such large amount of JSON before a zip file is attached.
+ magic_bits = [
+ "\x50\x4B\x03\x04", # "PK" + 0x03 + 0x04 = Regular ZIP file
+ "\x50\x4B\x05\x06", # "PK" + 0x05 + 0x06 = Empty ZIP
+ "\x50\x4B\x07\x08" # "PK" + 0x07 + 0x08 = Spanned ZIP
+ ]
+ search_chunk = json[0, 102_400]
+ zip_file = nil
+
+ magic_bits.each do |bits|
+ idx = search_chunk.index(bits)
+
+ next unless idx
+
+ zip_file = json[idx..-1]
+ json = json[0, idx]
+ break
+ end
+
+ # Parse JSON and, if received, attach zip file
+ json = JSON.parse(json)
+ json[:attachments] = zip_file
+
+ # Return processed data
+ { operationName: operation_name, data: json }
rescue
{}
end
end
@@ -103,16 +129,12 @@
@ws = Simple.connect @url, ws_options do |client|
client.on(:message, once: true) do |msg|
parsed_message = msg.data.to_msg_hash
+ logger = Hawkular::Logger.new
logger.log("Sent WebSocket message: #{parsed_message}")
-
- case parsed_message[:operationName]
- when 'WelcomeResponse'
- @session_id = parsed_message[:data]['sessionId']
- end
end
end
Timeout.timeout(@wait_time) { sleep 0.1 until @ws.open? }
ensure
@@ -302,15 +324,19 @@
end
# Exports the JDR report
#
# @param [String] resource_path canonical path of the WildFly server
+ # @param [Boolean] delete_immediately specifies whether the temporary file at the remote
+ # server should be deleted. False, by default.
# @param callback [Block] callback that is run after the operation is done
- def export_jdr(resource_path, &callback)
+ def export_jdr(resource_path, delete_immediately = false, &callback)
fail Hawkular::ArgumentError, 'resource_path must be specified' if resource_path.nil?
check_pre_conditions(&callback)
- invoke_specific_operation({ resourcePath: resource_path }, 'ExportJdr', &callback)
+ invoke_specific_operation({ resourcePath: resource_path,
+ deleteImmediately: delete_immediately },
+ 'ExportJdr', &callback)
end
# Updates the collection intervals.
#
# @param [Hash] hash Arguments for update collection intervals