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

ONE_HOUR =
60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PeriodicSync

#log_internal, #pool, #prepare_data, #start_periodic_sync, #sync

Constructor Details

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

Returns a new instance of ExampleContextsAggregator.



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

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.



13
14
15
# File 'lib/prefab/example_contexts_aggregator.rb', line 13

def cache
  @cache
end

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/prefab/example_contexts_aggregator.rb', line 13

def data
  @data
end

Instance Method Details

#record(contexts) ⇒ Object



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

def record(contexts)
  key = contexts.grouped_key

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

  @cache.set(key)

  @data.push(contexts)
end