<commit_before>require 'akane/storages/abstract_storage'
require 'elasticsearch'

module Akane
  module Storages
    class Elasticsearch < AbstractStorage
      def initialize(*)
        super

        @es = ::Elasticsearch::Client.new(
          hosts: [@config["host"]],
          logger: @config["enable_es_log"] ? @logger : nil
        )
        @index_name = @config["index"] || 'akane'
        set_elasticsearch_up
      end

      def record_tweet(account, tweet)
        tweet_hash = tweet.attrs
        tweet_hash['deleted'] = false
        @es.index(index: @index_name, type: 'tweet', id: tweet_hash['id_str'], body: tweet_hash)
      end

      def mark_as_deleted(account, user_id, tweet_id)
        @es.update(index: @index_name, type: 'tweet', ignore: 404,
                   id: tweet_id.to_s, body: {doc: {deleted: true}})
      end

      def record_event(account, event)
        case event["event"]
        when 'favorite'
        when 'unfavorite'
        when 'block'
        when 'unblock'
        when 'follow'
        when 'unfollow'
        end
      end

      def record_message(account, message)
        @es.index(index: @index_name, type: 'message', id: message['id_str'], body: message.attrs)
      end

      private

      def set_elasticsearch_up
        begin
          @es.indices.get_mapping(index: @index_name)
        rescue ::Elasticsearch::Transport::Transport::Errors::NotFound => e
          raise e unless /IndexMissingException/ === e.message

          @logger.info 'elasticsearch.setup: creating index'

          date_format = "EE MMM d HH:mm:ss Z yyyy"
          user_properties = {
                      notifications: {type: 'boolean', store: 'no', index: 'no'},
                      follow_request_sent: {type: 'boolean', store: 'no', index: 'no'},
                      following: {type: 'boolean', store: 'no', index: 'no'},
                      default_profile_image: {type: 'boolean', store: 'no', index: 'no'},
                      default_profile: {type: 'boolean', store: 'no', index: 'no'},
                      geo_enabled: {type: 'boolean', store: 'no', index: 'no'},
                      time_zone: {type: 'string', index: 'not_analyzed'},
                      utc_offset: {type: 'integer', store: 'yes', index: 'no'},
                      favourites_count: {type: 'integer', store: 'no', index: 'no'},
                      created_at: {type: 'date', format: date_format, store: 'yes', index: 'no'},
                      listed_count: {type: 'integer', store: 'no', index: 'no'},
                      friends_count: {type: 'integer', store: 'no', index: 'no'},
                      followers_count: {type: 'integer', store: 'no', index: 'no'},
                      id: {type: 'long'},
                      id_str: {type: 'string', index: 'not_analyzed'},<commit_msg>tweet_hash keys seem like Symbol<commit_after>require 'akane/storages/abstract_storage'
require 'elasticsearch'

module Akane
  module Storages
    class Elasticsearch < AbstractStorage
      def initialize(*)
        super

        @es = ::Elasticsearch::Client.new(
          hosts: [@config["host"]],
          logger: @config["enable_es_log"] ? @logger : nil
        )
        @index_name = @config["index"] || 'akane'
        set_elasticsearch_up
      end

      def record_tweet(account, tweet)
        tweet_hash = tweet.attrs
        tweet_hash[:deleted] = false
        @es.index(index: @index_name, type: 'tweet', id: tweet_hash[:id_str], body: tweet_hash)
      end

      def mark_as_deleted(account, user_id, tweet_id)
        @es.update(index: @index_name, type: 'tweet', ignore: 404,
                   id: tweet_id.to_s, body: {doc: {deleted: true}})
      end

      def record_event(account, event)
        case event["event"]
        when 'favorite'
        when 'unfavorite'
        when 'block'
        when 'unblock'
        when 'follow'
        when 'unfollow'
        end
      end

      def record_message(account, message)
        @es.index(index: @index_name, type: 'message', id: message[:id_str], body: message.attrs)
      end

      private

      def set_elasticsearch_up
        begin
          @es.indices.get_mapping(index: @index_name)
        rescue ::Elasticsearch::Transport::Transport::Errors::NotFound => e
          raise e unless /IndexMissingException/ === e.message

          @logger.info 'elasticsearch.setup: creating index'

          date_format = "EE MMM d HH:mm:ss Z yyyy"
          user_properties = {
                      notifications: {type: 'boolean', store: 'no', index: 'no'},
                      follow_request_sent: {type: 'boolean', store: 'no', index: 'no'},
                      following: {type: 'boolean', store: 'no', index: 'no'},
                      default_profile_image: {type: 'boolean', store: 'no', index: 'no'},
                      default_profile: {type: 'boolean', store: 'no', index: 'no'},
                      geo_enabled: {type: 'boolean', store: 'no', index: 'no'},
                      time_zone: {type: 'string', index: 'not_analyzed'},
                      utc_offset: {type: 'integer', store: 'yes', index: 'no'},
                      favourites_count: {type: 'integer', store: 'no', index: 'no'},
                      created_at: {type: 'date', format: date_format, store: 'yes', index: 'no'},
                      listed_count: {type: 'integer', store: 'no', index: 'no'},
                      friends_count: {type: 'integer', store: 'no', index: 'no'},
                      followers_count: {type: 'integer', store: 'no', index: 'no'},
                      id: {type: 'long'},
                      id_str: {type: 'string', index: 'not_analyzed'},
{'fraction_non_alphanumeric': 0.1385182584269663, 'fraction_numerical': 0.001053370786516854, 'mean_word_length': 2.1148168398031713, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5881943', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1641, 'n_words': 435}