class Fluent::Counter::Store
Public Class Methods
gen_key(scope, key)
click to toggle source
# File lib/fluent/counter/store.rb, line 25 def self.gen_key(scope, key) "#{scope}\t#{key}" end
new(opt = {})
click to toggle source
# File lib/fluent/counter/store.rb, line 29 def initialize(opt = {}) @log = opt[:log] || $log # Notice: This storage is not be implemented auto save. @storage = Plugin.new_storage('local', parent: DummyParent.new(@log)) conf = if opt[:path] {'persistent' => true, 'path' => opt[:path] } else {'persistent' => false } end @storage.configure(Fluent::Config::Element.new('storage', {}, conf, [])) end
Public Instance Methods
delete(key)
click to toggle source
# File lib/fluent/counter/store.rb, line 102 def delete(key) ret = @storage.delete(key) or raise UnknownKey.new("`#{key}` doesn't exist in counter") build_response(ret) end
get(key, raise_error: false, raw: false)
click to toggle source
# File lib/fluent/counter/store.rb, line 85 def get(key, raise_error: false, raw: false) ret = if raise_error @storage.get(key) or raise UnknownKey.new("`#{key}` doesn't exist in counter") else @storage.get(key) end if raw ret else ret && build_response(ret) end end
inc(key, data, force: false)
click to toggle source
# File lib/fluent/counter/store.rb, line 107 def inc(key, data, force: false) value = data.delete('value') init(key, data) if !key?(key) && force v = get(key, raise_error: true, raw: true) valid_type!(v, value) v['total'] += value v['current'] += value t = EventTime.now v['last_modified_at'] = [t.sec, t.nsec] @storage.put(key, v) build_response(v) end
init(key, data, ignore: false)
click to toggle source
# File lib/fluent/counter/store.rb, line 74 def init(key, data, ignore: false) ret = if v = get(key) raise InvalidParams.new("#{key} already exists in counter") unless ignore v else @storage.put(key, build_value(data)) end build_response(ret) end
key?(key)
click to toggle source
# File lib/fluent/counter/store.rb, line 98 def key?(key) !!@storage.get(key) end
reset(key)
click to toggle source
# File lib/fluent/counter/store.rb, line 122 def reset(key) v = get(key, raise_error: true, raw: true) success = false old_data = v.dup now = EventTime.now last_reset_at = EventTime.new(*v['last_reset_at']) # Does it need reset? if (last_reset_at + v['reset_interval']) <= now success = true v['current'] = initial_value(v['type']) t = [now.sec, now.nsec] v['last_reset_at'] = t v['last_modified_at'] = t @storage.put(key, v) end { 'elapsed_time' => now - last_reset_at, 'success' => success, 'counter_data' => build_response(old_data) } end
start()
click to toggle source
# File lib/fluent/counter/store.rb, line 66 def start @storage.load end
stop()
click to toggle source
# File lib/fluent/counter/store.rb, line 70 def stop @storage.save end
Private Instance Methods
build_response(d)
click to toggle source
# File lib/fluent/counter/store.rb, line 148 def build_response(d) { 'name' => d['name'], 'total' => d['total'], 'current' => d['current'], 'type' => d['type'], 'reset_interval' => d['reset_interval'], 'last_reset_at' => EventTime.new(*d['last_reset_at']), } end
build_value(data)
click to toggle source
value is Hash. value requires these fileds. :name, :total, :current, :type, :reset_interval, :last_reset_at, :last_modified_at
# File lib/fluent/counter/store.rb, line 161 def build_value(data) type = data['type'] || 'numeric' now = EventTime.now t = [now.sec, now.nsec] v = initial_value(type) data.merge( 'type' => type, 'last_reset_at' => t, 'last_modified_at' => t, 'current' => v, 'total' => v, ) end
initial_value(type)
click to toggle source
# File lib/fluent/counter/store.rb, line 177 def initial_value(type) case type when 'numeric', 'integer' then 0 when 'float' then 0.0 else raise InvalidParams.new('`type` should be integer, float, or numeric') end end
type_str(v)
click to toggle source
# File lib/fluent/counter/store.rb, line 191 def type_str(v) case v when Integer 'integer' when Float 'float' when Numeric 'numeric' else raise InvalidParams.new("`type` should be integer, float, or numeric") end end
valid_type!(v, value)
click to toggle source
# File lib/fluent/counter/store.rb, line 185 def valid_type!(v, value) type = v['type'] return unless (type != 'numeric') && (type_str(value) != type) raise InvalidParams.new("`type` is #{type}. You should pass #{type} value as a `value`") end