Skip to content

Commit 29d31e4

Browse files
Pere Urbon-Bayesjordansissel
authored andcommitted
ES loader cleanup, including some date format improvements and unique events control
Fixes elastic#1
1 parent 4c8eba3 commit 29d31e4

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

scripts/loader.rb

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'elasticsearch'
44
require 'csv'
5+
require 'active_support/core_ext'
56

67
class Loader
78

@@ -30,42 +31,50 @@ def client
3031

3132
def load_file!(client, file)
3233
puts file if @debug
34+
props = scrap_props(file)
3335
CSV.foreach(file, :headers => true) do |row|
3436
puts "#{row.class}, #{row.count}, #{row.headers}" if @debug
35-
match = /-(\d*.\d*)_\d*.csv/.match(file)
36-
clazzname = match[1]
37-
3837
row.headers.each do |header|
3938
next if row.headers.first == header
40-
41-
data = {
42-
"class" => clazzname,
43-
"type" => row[0].gsub(/\s|\//,'_'),
44-
"timestamp" => timestamp(file),
45-
"kpi" => header,
46-
"times" => row[header].to_i,
47-
"_source" => "script"
48-
}
49-
client.index(index: 'logstash-benchmark', type: 'bench', body: data) rescue puts "failure with #{row}"
39+
content = build_body(row, header, props)
40+
id = "#{props[:time]}#{content["type"]}#{content["kpi"]}".hash
41+
client.index(index: 'logstash-benchmark', type: 'bench', id: id, body: content) rescue puts "failure with #{row}"
5042
end
5143
end
5244
puts if @debug
5345
end
5446

55-
def timestamp(file)
56-
match = /(\d*).csv/.match(file)
57-
Time.at(match[1].to_i).utc.to_s.split(' ').first
47+
def build_body(row, header, props)
48+
type = row[0].gsub(/\s|\//,'_')
49+
kpi = header.gsub(/\s|\//,'_')
50+
{
51+
"class" => props[:class],
52+
"type" => type,
53+
"ts" => timestamp(props[:time]),
54+
"kpi" => kpi,
55+
"times" => row[header].to_i,
56+
"_source" => "script"
57+
}
58+
end
59+
60+
def timestamp(time)
61+
Time.at(time.to_i).strftime("%Y-%m-%dT%H:%M:%S.%3N%z")
62+
end
63+
64+
def scrap_props(file)
65+
match = /-(\d*.\d*)_(\d*).csv/.match(file)
66+
{:class => match[1], :time => match[2] }
5867
end
5968

6069
def build_index(client, params)
6170
client.indices.create index: 'logstash-benchmark', body: params
6271
end
6372

6473
def index_config
65-
props = { "name" => { "type" => "string" },
66-
"class" => { "type" => "string" },
67-
"kpi" => { "type" => "string" },
68-
"timestamp" => { "type" => "date", "index" => "analyzed" },
74+
props = { "name" => { "type" => "string" },
75+
"class" => { "type" => "string" },
76+
"kpi" => { "type" => "string" },
77+
"ts" => { "type" => "date", "format" => "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "index" => "analyzed" },
6978
"times" => { "type" => "integer" } }
7079
{ 'mappings' => { 'bench' => { '_source' => { 'enabled' => true }, 'properties' => props } } }
7180
end

0 commit comments

Comments
 (0)