class SlackScratcher::Adapter::Elasticsearch

Elasticsearch adapter for storing slack logs.

@since 0.0.1 @attr_reader [Elasticsearch::Client] client elasticsearch client

Attributes

client[R]

Public Class Methods

new(hosts, metadata) click to toggle source

Initialize SlackScratcher::Adapter::Elasticsearch object.

@param [Array] hosts array of Elasticsearch hosts @param [Hash] metadata metadata for storing @option metadata [string] :index index for storing @option metadata [string] :type type for storing

@example return Elasticsearch adapter object

hosts = ['http://192.168.59.103:9200']
metadata = {index: 'slack', type: 'log'}
SlackScratcher::Adapter::Elastisearch.new(hosts, metadata)

@return [SlackScratcher::Adapter::Elasticsearh]

Elasticsearch adapter object
# File lib/slack_scratcher/adapter/elasticsearch.rb, line 26
def initialize(hosts, metadata)
  @client = ::Elasticsearch::Client.new hosts: hosts
  @metadata = metadata
end

Public Instance Methods

ready_index() click to toggle source

Create index and set mapping for saving slack log data

@example Create index and set mapping

adapter.ready_index

@return [Boolean] If there isn't any problem, it returns true

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 75
def ready_index
  unless index?
    create_index
    put_mapping
  end

  true
end
send(raw_data) click to toggle source

Send data into elastisearch host.

@see www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#bulk-instance_method @param [Array] raw_data slack logs which parsed by loader

@example Send log from loader to elastisearch adapter

loader.each { |data| adapter.send data }

@raise [Elasticsearch::Transport::Transport::Errors::BadRequest]

It raise when request is fail

@return [Hash] Deserialized Elasticsearch response @return [Boolean] If raw_data is empty, it returns false

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 44
def send(raw_data)
  data = format_bulk(raw_data)
  @client.bulk data unless raw_data.empty?
  false
rescue ::Elasticsearch::Transport::Transport::Errors::BadRequest => error
  puts error
end
timestamp_of_last_channel_log(channel_name) click to toggle source

Get last logs' timestamp of specific channel from saved data

@param [String] channel_name channel name

@example Get saved last log's timestamp of general channel

adapter.timestamp_of_last_log 'general' #=> '1426337804.820065'

@return [String] Timestamp of last log @return [String] If there isn't saved log, it returns '0'

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 61
def timestamp_of_last_channel_log(channel_name)
  request_body = create_body(query_for_last_log(channel_name))
  log = @client.search request_body

  return '0' if log['hits']['total'] == 0
  log['hits']['hits'][0]['_source']['ts']
end

Private Instance Methods

create_body(body = {}) click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 145
def create_body(body = {})
  {
    index: index,
    type: type,
    body: body
  }
end
create_index() click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 102
def create_index
  @client.indices.create(index: index)
end
format_bulk(data) click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 154
def format_bulk(data)
  { body: data.map { |log| format_log(log) } }
end
format_log(log) click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 159
def format_log(log)
  { index:
      {
        _index: index,
        _type: type,
        data: log
      }
  }
end
index() click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 87
def index
  @metadata[:index]
end
index?() click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 97
def index?
  @client.indices.exists(index: index)
end
mapping() click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 112
def mapping
  {
    type => {
      '_timestamp' => {
        'enabled' => true,
        'path' => 'dataetime'
      },
      '_id' => {
        'path' => 'uid'
      },
      '_routing' => {
        'required' => true,
        'path' => 'channel_id'
      }
    }
  }
end
put_mapping() click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 107
def put_mapping
  @client.indices.put_mapping create_body(mapping)
end
query_for_last_log(channel_name) click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 131
def query_for_last_log(channel_name)
  {
    size: 1,
    sort:
      [ { datetime: { order: 'desc' } }],
    query: {
      match: {
        channel: channel_name
      }
    }
  }
end
type() click to toggle source

@private

# File lib/slack_scratcher/adapter/elasticsearch.rb, line 92
def type
  @metadata[:type]
end