Sha256: f544ce1295fc72e0441bf2559237ad92395fd79630f260d89249de92047ba08b

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

class FnordMetric::API

  @@opts = nil

  def initialize(opts={})
    @@opts = FnordMetric.default_options(opts)
    if @@opts[:redis]
      @redis = @@opts.delete :redis
    else
      connect
    end
  end

  def connect
    @redis = Redis.connect(:url => @@opts[:redis_url])
  end

  def event(event_data)
    begin
      if event_data.is_a?(Hash)
        event_data = event_data.to_json
      else
        JSON.parse(event_data) # void ;)
      end
    rescue JSON::ParserError
      FnordMetric.log("event_lost: can't parse json")
    else
      push_event(get_next_uuid, event_data)
    end
  end

  def disconnect
    @redis.quit
  end

  private

  def push_event(event_id, event_data)
    prefix = @@opts[:redis_prefix]
    @redis.hincrby "#{prefix}-stats",             "events_received", 1
    @redis.set     "#{prefix}-event-#{event_id}", event_data
    @redis.lpush   "#{prefix}-queue",             event_id
    @redis.expire  "#{prefix}-event-#{event_id}", @@opts[:event_queue_ttl]
    event_id
  end

  def get_next_uuid
    rand(8**32).to_s(36)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
johnf-fnordmetric-1.2.10 lib/fnordmetric/api.rb