Class: Prefab::ExampleContextsAggregator

Inherits:
Object
  • Object
show all
Includes:
PeriodicSync
Defined in:
lib/prefab/example_contexts_aggregator.rb

Overview

This class aggregates example contexts. It dedupes based on the concatenation of the keys of the contexts.

It shouldn’t send the same context more than once per hour.

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(ExampleContextsAggregator)
ONE_HOUR =
60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PeriodicSync

#pool, #post, #prepare_data, #start_periodic_sync, #sync

Constructor Details

#initialize(client:, max_contexts:, sync_interval:) ⇒ ExampleContextsAggregator

Returns a new instance of ExampleContextsAggregator.



19
20
21
22
23
24
25
26
27
28
# File 'lib/prefab/example_contexts_aggregator.rb', line 19

def initialize(client:, max_contexts:, sync_interval:)
  @client = client
  @max_contexts = max_contexts
  @name = 'example_contexts_aggregator'

  @data = Concurrent::Array.new
  @cache = Prefab::RateLimitCache.new(ONE_HOUR)

  start_periodic_sync(sync_interval)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



15
16
17
# File 'lib/prefab/example_contexts_aggregator.rb', line 15

def cache
  @cache
end

#dataObject (readonly)

Returns the value of attribute data.



15
16
17
# File 'lib/prefab/example_contexts_aggregator.rb', line 15

def data
  @data
end

Instance Method Details

#record(contexts) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/prefab/example_contexts_aggregator.rb', line 30

def record(contexts)
  key = contexts.grouped_key

  return unless @data.size < @max_contexts && !@cache.fresh?(key)

  @cache.set(key)

  @data.push(contexts)
end