Class: ThreeScale::Backend::Stats::BucketReader

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale/backend/stats/bucket_reader.rb

Overview

This class allows us to read the buckets that we are creating in Redis to store the stats keys that change. It also allows us to keep track of the ones that are pending to be read.

Constant Summary collapse

InvalidInterval =
Class.new(ThreeScale::Backend::Error)

Instance Method Summary collapse

Constructor Details

#initialize(bucket_create_interval, bucket_storage, events_storage) ⇒ BucketReader

Returns a new instance of BucketReader.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/3scale/backend/stats/bucket_reader.rb', line 50

def initialize(bucket_create_interval, bucket_storage, events_storage)
  # This is needed because ThreeScale::Backend::TimeHacks.beginning_of_bucket
  if 60%bucket_create_interval != 0 || bucket_create_interval <= 0
    raise InvalidInterval, 'Bucket create interval needs to divide 60'
  end

  @bucket_create_interval = bucket_create_interval
  @bucket_storage = bucket_storage
  @events_storage = events_storage
  @latest_bucket_read_marker = LatestBucketReadMarker.new(bucket_storage.storage)
end

Instance Method Details

#latest_bucket_read=(latest_bucket_read) ⇒ Object



75
76
77
# File 'lib/3scale/backend/stats/bucket_reader.rb', line 75

def latest_bucket_read=(latest_bucket_read)
  latest_bucket_read_marker.latest_bucket_read = latest_bucket_read
end

#pending_events_in_buckets(end_time_utc: Time.now.utc, max_buckets: nil) ⇒ Object

Returns the pending events and the bucket of the most recent of the events sent. This allows the caller to call latest_bucket_read= when it has processed all the events.



65
66
67
68
69
70
71
72
73
# File 'lib/3scale/backend/stats/bucket_reader.rb', line 65

def pending_events_in_buckets(end_time_utc: Time.now.utc, max_buckets: nil)
  buckets = if max_buckets
              pending_buckets(end_time_utc).take(max_buckets)
            else
              pending_buckets(end_time_utc).to_a
            end

  { events: events(buckets), latest_bucket: buckets.last }
end