Class: AfElastic::EventStore

Inherits:
Object
  • Object
show all
Defined in:
lib/af_elastic.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.big_mapObject

Returns the value of attribute big_map.



32
33
34
# File 'lib/af_elastic.rb', line 32

def big_map
  @big_map
end

.eventsObject

Returns the value of attribute events.



30
31
32
# File 'lib/af_elastic.rb', line 30

def events
  @events
end

.typesObject

Returns the value of attribute types.



31
32
33
# File 'lib/af_elastic.rb', line 31

def types
  @types
end

Class Method Details

.add_event(type, data = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/af_elastic.rb', line 57

def add_event(type, data={})
    begin
  	@types ||= Set.new
  	unless @types.include? type
  		puts "new type encountered"
  	# check mapping through network
	  	unless AfElastic.configuration.es.indices.exists_type? index: AfElastic.configuration.index, type: type
	  		dynamic = [{:simple_def=>{:match=>"*",:match_mapping_type=> "string",:mapping=> {:type=> "string",:index=> "not_analyzed"}}}]
	  		map = {}
	  		map[type] = {properties:{timestamp:{type:"date"}},dynamic_templates:dynamic}
        unless AfElastic.configuration.es.indices.exists? index: AfElastic.configuration.index
        	AfElastic.configuration.es.indices.create index: AfElastic.configuration.index
		  	end
  			AfElastic.configuration.es.indices.put_mapping index: AfElastic.configuration.index, type: type, body: map
	  	end
	  	@types.add type
	  end

  	@events ||= []
  	# add timestamp
  	data['timestamp'] = Time.now.to_i
  	@events << {index: {_index: AfElastic.configuration.index, _type: type, data: data}}
    rescue Exception => e
      puts e
      puts "AfElastic::EventReporter::add_event: something wrong with elasticsearch"
    end
end

.cache_event(uniqueKey, type, data = {}) ⇒ Object



34
35
36
37
38
# File 'lib/af_elastic.rb', line 34

def cache_event(uniqueKey, type, data={})
  @big_map ||= {}
  data[:eventType00] = type
  @big_map[uniqueKey] = data
end

.update_event(finalized, uniqueKey, updated_data = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/af_elastic.rb', line 40

def update_event(finalized, uniqueKey, updated_data={})
  if @big_map and @big_map.has_key? uniqueKey
    updated_data.each do |k,v|
      if k != :duration
        @big_map[uniqueKey][k] = v
      else
        @big_map[uniqueKey][:duration] = updated_data[:duration] - @big_map[uniqueKey][:duration]
      end
    end
    if finalized
      type = @big_map[uniqueKey].delete(:eventType00)
      add_event(type, @big_map[uniqueKey])
      @big_map.delete(uniqueKey)
    end
  end
end