Module: ThreeScale::Backend::Stats::Aggregators::Base

Included in:
ResponseCode, Usage
Defined in:
lib/3scale/backend/stats/aggregators/base.rb

Instance Method Summary collapse

Instance Method Details

#aggregate_values(value, timestamp, keys, cmd, bucket) ⇒ Object

Aggregates a value in a timestamp for all given keys using a specific Redis command to store them. If a bucket_key is specified, each key will be added to a Redis Set with that name.

Parameters:

  • value (Integer)
  • timestamp (Time)
  • keys (Array)

    array of => “key”

  • cmd (Symbol)
  • bucket (String, Nil)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/3scale/backend/stats/aggregators/base.rb', line 15

def aggregate_values(value, timestamp, keys, cmd, bucket)
  keys_for_bucket = []

  keys.each do |metric_type, prefix_key|
    granularities(metric_type).each do |granularity|
      key = counter_key(prefix_key, granularity.new(timestamp))
      expire_time = Stats::PeriodCommons.expire_time_for_granularity(granularity)

      # We don't need to store stats keys set to 0. It wastes Redis
      # memory because for rate-limiting and stats, a key of set to 0
      # is equivalent to a key that does not exist.
      if cmd == :set && value == 0
        storage.del(key)
      else
        store_key(cmd, key, value, expire_time)
      end

      unless Stats::PeriodCommons::EXCLUDED_FOR_BUCKETS.include?(granularity)
        keys_for_bucket << key
      end
    end
  end

  store_in_changed_keys(keys_for_bucket, bucket) if bucket
end

#storageObject



51
52
53
# File 'lib/3scale/backend/stats/aggregators/base.rb', line 51

def storage
  Backend::Storage.instance
end

#storage_cmd(raw_value) ⇒ Symbol

Return Redis command depending on raw_value. If raw_value is a string with a ‘#’ in the beginning, it returns ‘set’. Else, it returns ‘incrby’.

Parameters:

  • raw_value (String)

Returns:

  • (Symbol)

    the Redis command



47
48
49
# File 'lib/3scale/backend/stats/aggregators/base.rb', line 47

def storage_cmd(raw_value)
  Backend::Usage.is_set?(raw_value) ? :set : :incrby
end