<commit_before>require 'syster/reporters/base'
require 'syster/helpers/require'
require 'socket'

module Syster::Reporters
  class ElasticSearch < Base
    def self.runnable?
      Syster::Helpers::Require.can_require? %w[faraday json]
    end
  
    def initialize params={}
      require 'faraday'
      require 'json'

      @log = params[:logger] || Logger.new(STDERR)
      @update = {}
      @conn = Faraday.new :url => (params[:uri] || 'http://localhost:9200/')
      @hostname = Socket.gethostname

      begin
        @dry = get_dry
      rescue Exception => e
        @dry = {}
        @log.info "Couldn't get DRY (#{e}), creating documents"
        create_documents
      end
    end
  
    def report identifier, payload
      @update[identifier] = ::JSON::dump payload

      if @dry.has_key? identifier
        @dry[identifier][:last_run] = Time.now.to_i<commit_msg>Reporters::ElasticSearch: work around Puppet JSON being what it is<commit_after>require 'syster/reporters/base'
require 'syster/helpers/require'
require 'socket'
require 'iconv'

module Syster::Reporters
  class ElasticSearch < Base
    def self.runnable?
      Syster::Helpers::Require.can_require? %w[faraday json]
    end
  
    def initialize params={}
      require 'faraday'
      require 'json'

      @log = params[:logger] || Logger.new(STDERR)
      @update = {}
      @conn = Faraday.new :url => (params[:uri] || 'http://localhost:9200/')
      @hostname = Socket.gethostname

      begin
        @dry = get_dry
      rescue Exception => e
        @dry = {}
        @log.info "Couldn't get DRY (#{e}), creating documents"
        create_documents
      end
    end
  
    def report identifier, payload
      # Missing in Ruby 1.8: #encode ::Encoding::UTF_8, :undef => :replace
      @update[identifier] = ::Iconv.conv 'UTF-8//IGNORE', 'UTF-8',
                                         ::JSON::dump(payload)

      if @dry.has_key? identifier
        @dry[identifier][:last_run] = Time.now.to_i
{'fraction_non_alphanumeric': 0.12846347607052896, 'fraction_numerical': 0.00654911838790932, 'mean_word_length': 2.597826086956522, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3208081', 'n_tokens_mistral': 645, 'n_tokens_neox': 617, 'n_words': 147}