-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport_xml_data_elastic.rb
64 lines (50 loc) · 1.56 KB
/
export_xml_data_elastic.rb
1
2
3
4
5
6
7
8
9
10
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
require 'rexml/document'
require "nokogiri"
require "uri"
require "net/http"
ELASTIC_URL = ENV["ELASTIC_URL"]
ELASTIC_API_KEY = ENV["ELASTIC_API_KEY"]
PROJECT = ARGV[0]
TEST_RESULT_DIR = ARGV[1]
TEST_TYPE = ARGV[2]
TIME_NOW = Time.now.strftime('%Y-%m-%dT%H:%M:%S')
def export_data()
# some test produce more than one result file
Dir.glob("#{TEST_RESULT_DIR}/*.xml") do |xml_file|
doc = File.open(xml_file) { |f| Nokogiri::XML(f) }
doc.search("testsuite").each do |test_result|
send_data test_result
end
end
end
def send_data(line)
data = {}
status = "success"
if line.attr("failures").to_i > 0
status = "failed"
elsif line.attr("errors").to_i > 0
status = "error"
end
timestamp = TIME_NOW
data.store("testProject", "#{PROJECT}-#{TEST_TYPE}-test")
data.store("tests", line.attr("tests").to_i)
data.store("name", line.attr("name"))
data.store("time", line.attr("time").to_i)
data.store("errors", line.attr("errors").to_i)
data.store("failures", line.attr("failures").to_i)
data.store("timeString", timestamp + "M")
data.store("analysedAt", timestamp)
data.store("success", line.attr("tests").to_i - (line.attr("errors").to_i + line.attr("failures").to_i))
url = URI(ELASTIC_URL)
http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Post.new(url)
request["api_key"] = ELASTIC_API_KEY
request["Content-Type"] = "application/json"
request.body = data.to_s.gsub("=>", ":")
response = http.request(request)
print(" - new data exported: #{response.code} data: #{data}\n")
end
if __FILE__ == $0
export_data()
end