af_elastic 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8dff2a5ab4f66ef59fa4623074c54b3363529513
4
+ data.tar.gz: 0098527cfe225d55958f76efbde163fe9151453c
5
+ SHA512:
6
+ metadata.gz: e86e17686153cda0cb599651f38e4a0b07d74930dd6f7b3804e7678e376397e8c326fd91b85ab31c403ce68b51000d00fd0040d08c5eacb21d405285962a205b
7
+ data.tar.gz: 2d39c3d1590a3dd5d82dc79f7bb25a10fe30a302e717dac90c8bef94390f9423d05ffb46ea8c59917cce6fed41cf5b3e3d7e9058563c60f7196bcb14b3dc5000
@@ -0,0 +1,3 @@
1
+ module AfElastic
2
+ VERSION = "1.1.0"
3
+ end
data/lib/af_elastic.rb ADDED
@@ -0,0 +1,109 @@
1
+ require "af_elastic/version"
2
+ require 'elasticsearch'
3
+
4
+ module AfElastic
5
+
6
+ # configurations
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ def self.configure
12
+ self.configuration ||= Configuration.new
13
+ yield(configuration)
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :es
18
+ attr_accessor :index
19
+ attr_accessor :capacity
20
+
21
+ def initialize
22
+ @es = Elasticsearch::Client.new
23
+ @index = 'gem-test'
24
+ @capacity = 1000
25
+ end
26
+ end
27
+
28
+ class EventStore
29
+ class << self
30
+ attr_accessor :events
31
+ attr_accessor :types
32
+ attr_accessor :big_map
33
+
34
+ def cache_event(uniqueKey, type, data={})
35
+ @big_map ||= {}
36
+ data[:eventType00] = type
37
+ @big_map[uniqueKey] = data
38
+ end
39
+
40
+ def update_event(finalized, uniqueKey, updated_data={})
41
+ if @big_map and @big_map.has_key? uniqueKey
42
+ updated_data.each do |k,v|
43
+ if k != :duration
44
+ @big_map[uniqueKey][k] = v
45
+ else
46
+ @big_map[uniqueKey][:duration] = updated_data[:duration] - @big_map[uniqueKey][:duration]
47
+ end
48
+ end
49
+ if finalized
50
+ type = @big_map[uniqueKey].delete(:eventType00)
51
+ add_event(type, @big_map[uniqueKey])
52
+ @big_map.delete(uniqueKey)
53
+ end
54
+ end
55
+ end
56
+
57
+ def add_event(type, data={})
58
+ begin
59
+ @types ||= Set.new
60
+ unless @types.include? type
61
+ puts "new type encountered"
62
+ # check mapping through network
63
+ unless AfElastic.configuration.es.indices.exists_type? index: AfElastic.configuration.index, type: type
64
+ dynamic = [{:simple_def=>{:match=>"*",:match_mapping_type=> "string",:mapping=> {:type=> "string",:index=> "not_analyzed"}}}]
65
+ map = {}
66
+ map[type] = {properties:{timestamp:{type:"date"}},dynamic_templates:dynamic}
67
+ unless AfElastic.configuration.es.indices.exists? index: AfElastic.configuration.index
68
+ AfElastic.configuration.es.indices.create index: AfElastic.configuration.index
69
+ end
70
+ AfElastic.configuration.es.indices.put_mapping index: AfElastic.configuration.index, type: type, body: map
71
+ end
72
+ @types.add type
73
+ end
74
+
75
+ @events ||= []
76
+ # add timestamp
77
+ data['timestamp'] = Time.now.to_i
78
+ @events << {index: {_index: AfElastic.configuration.index, _type: type, data: data}}
79
+ rescue Exception => e
80
+ puts e
81
+ puts "AfElastic::EventReporter::add_event: something wrong with elasticsearch"
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ class EventReporter
88
+ class << self
89
+
90
+ def report_events
91
+ events_to_report = AfElastic::EventStore.events || []
92
+ return if events_to_report.length < AfElastic.configuration.capacity
93
+ AfElastic::EventStore.events = []
94
+ puts "about to bulk"
95
+
96
+ th = Thread.new do
97
+ begin
98
+ AfElastic.configuration.es.bulk body: events_to_report
99
+ puts "bulk suceeded"
100
+ rescue Exception => e
101
+ puts "error bulk"
102
+ Rails.logger.error("AfElastic::EventReporter::report_events: error doing bulk inserting")
103
+ end
104
+ end
105
+ return th
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators/base'
2
+ module AfElastic
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+ desc "Creates AfElastic initializer for your application"
6
+
7
+ def copy_initializer
8
+ template "af_elastic_initializer.rb", "config/initializers/af_elastic.rb"
9
+
10
+ puts "Install complete, please go to config/initializers/af_elastic.rb to configure."
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ require 'elasticsearch'
2
+
3
+ AfElastic.configure do |config|
4
+ # Set the elastic search client to receive data
5
+ # config.es = Elasticsearch::Client.new host: '54.69.240.186'
6
+
7
+ # Set the database name to store the data
8
+ # config.index = 'gem-test'
9
+
10
+ # Set the capacity of events to store before pushing to elastic search
11
+ # config.capacity = 1000
12
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: af_elastic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Appfolio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: elasticsearch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/af_elastic.rb
34
+ - lib/af_elastic/version.rb
35
+ - lib/generators/af_elastic/install_generator.rb
36
+ - lib/generators/templates/af_elastic_initializer.rb
37
+ homepage:
38
+ licenses: []
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.2.3
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Get custom event data and send to elasticsearch.
60
+ test_files: []