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(self)
ONE_HOUR =
60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PeriodicSync

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

Constructor Details

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

Returns a new instance of ExampleContextsAggregator.



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

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.



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

def cache
  @cache
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Instance Method Details

#record(contexts) ⇒ Object



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

def record(contexts)
  key = contexts.grouped_key

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

  @cache.set(key)

  @data.push(contexts)
end