Module: EventMeter::Rails

Defined in:
lib/event_meter/rails.rb

Class Method Summary collapse

Class Method Details

.auto_cleanup_error_handler(logger) ⇒ Object



47
48
49
50
51
# File 'lib/event_meter/rails.rb', line 47

def auto_cleanup_error_handler(logger)
  lambda do |error|
    logger.warn "EventMeter auto cleanup failed: #{error.class}: #{error.message}"
  end
end

.build_rollup_storage(storage, connection_class:, namespace:, table_prefix:) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/event_meter/rails.rb', line 59

def build_rollup_storage(storage, connection_class:, namespace:, table_prefix:)
  validate_storage!(storage, expected: :postgres, name: "rollup_storage")

  Stores::Rollup::ActiveRecordPostgres.new(
    connection_class: connection_class,
    namespace: namespace,
    table_prefix: table_prefix
  )
end

.build_stream_storage(storage, path:, sync:) ⇒ Object



53
54
55
56
57
# File 'lib/event_meter/rails.rb', line 53

def build_stream_storage(storage, path:, sync:)
  validate_storage!(storage, expected: :file, name: "stream_storage")

  Stores::Stream::File.new(path: path, sync: sync)
end

.configure(namespace:, stream_path:, stream_storage: :file, rollup_storage: :postgres, table_prefix: "event_meter", connection_class: nil, stream_sync: :flush, auto_cleanup_history: false, cleanup_history_retention: nil, cleanup_history_interval: nil, summary_key_limit: nil, logger: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/event_meter/rails.rb', line 8

def configure(namespace:, stream_path:, stream_storage: :file, rollup_storage: :postgres,
  table_prefix: "event_meter", connection_class: nil, stream_sync: :flush,
  auto_cleanup_history: false, cleanup_history_retention: nil,
  cleanup_history_interval: nil, summary_key_limit: nil, logger: nil)

  connection_class ||= Stores::Rollup::ActiveRecordPostgres.default_connection_class

  EventMeter.configure do |config|
    config.namespace = namespace
    config.stream_storage = build_stream_storage(
      stream_storage,
      path: stream_path,
      sync: stream_sync
    )
    config.rollup_storage = build_rollup_storage(
      rollup_storage,
      connection_class: connection_class,
      namespace: namespace,
      table_prefix: table_prefix
    )
    config.auto_cleanup_history = auto_cleanup_history
    config.cleanup_history_retention = cleanup_history_retention if cleanup_history_retention
    config.cleanup_history_interval = cleanup_history_interval if cleanup_history_interval
    config.summary_key_limit = summary_key_limit if summary_key_limit
    config.auto_cleanup_error_handler = auto_cleanup_error_handler(logger) if logger
  end
end

.install_postgres!(connection_class: nil, table_prefix: "event_meter") ⇒ Object



40
41
42
43
44
45
# File 'lib/event_meter/rails.rb', line 40

def install_postgres!(connection_class: nil, table_prefix: "event_meter")
  Stores::Rollup::ActiveRecordPostgres.install!(
    connection_class: connection_class || Stores::Rollup::ActiveRecordPostgres.default_connection_class,
    table_prefix: table_prefix
  )
end

.migration_sql(table_prefix: "event_meter") ⇒ Object



36
37
38
# File 'lib/event_meter/rails.rb', line 36

def migration_sql(table_prefix: "event_meter")
  Stores::Rollup::Postgres.schema_sql(table_prefix: table_prefix)
end

.storage_key(storage) ⇒ Object



75
76
77
# File 'lib/event_meter/rails.rb', line 75

def storage_key(storage)
  storage.respond_to?(:to_sym) ? storage.to_sym : storage
end

.validate_storage!(storage, expected:, name:) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
# File 'lib/event_meter/rails.rb', line 69

def validate_storage!(storage, expected:, name:)
  return if storage_key(storage) == expected

  raise ArgumentError, "#{name} must be #{expected.inspect}"
end