|
2 | 2 |
|
3 | 3 | require 'elasticsearch'
|
4 | 4 | require 'csv'
|
| 5 | +require 'active_support/core_ext' |
5 | 6 |
|
6 | 7 | class Loader
|
7 | 8 |
|
@@ -30,42 +31,50 @@ def client
|
30 | 31 |
|
31 | 32 | def load_file!(client, file)
|
32 | 33 | puts file if @debug
|
| 34 | + props = scrap_props(file) |
33 | 35 | CSV.foreach(file, :headers => true) do |row|
|
34 | 36 | puts "#{row.class}, #{row.count}, #{row.headers}" if @debug
|
35 |
| - match = /-(\d*.\d*)_\d*.csv/.match(file) |
36 |
| - clazzname = match[1] |
37 |
| - |
38 | 37 | row.headers.each do |header|
|
39 | 38 | 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}" |
50 | 42 | end
|
51 | 43 | end
|
52 | 44 | puts if @debug
|
53 | 45 | end
|
54 | 46 |
|
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] } |
58 | 67 | end
|
59 | 68 |
|
60 | 69 | def build_index(client, params)
|
61 | 70 | client.indices.create index: 'logstash-benchmark', body: params
|
62 | 71 | end
|
63 | 72 |
|
64 | 73 | 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" }, |
69 | 78 | "times" => { "type" => "integer" } }
|
70 | 79 | { 'mappings' => { 'bench' => { '_source' => { 'enabled' => true }, 'properties' => props } } }
|
71 | 80 | end
|
|
0 commit comments