Class: Rhcf::Timeseries::Manager
- Inherits:
-
Object
- Object
- Rhcf::Timeseries::Manager
- Defined in:
- lib/rhcf/timeseries/manager.rb
Constant Summary collapse
- DEFAULT_STRATEGY =
RedisHgetallStrategy
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #connection_to_use ⇒ Object
- #crunch_values(subject, resolution_id, point, filter, limit = 1000) ⇒ Object
- #descend(path, do_descend = true, &block) ⇒ Object
- #find(subject, from, to = Time.now, filter = nil, limit = 1000) ⇒ Object
-
#initialize(options = {}) ⇒ Manager
constructor
A new instance of Manager.
- #on_connection(conn) {|_self| ... } ⇒ Object
- #resolution(id) ⇒ Object
- #resolution_value_at(moment, res_id) ⇒ Object
- #resolutions ⇒ Object
- #resolutions_of(moment) ⇒ Object
- #store(subject, event_point_hash, moment = Time.now, descend_subject = true, descend_event = true) ⇒ Object
- #store_point_value(subject_path, resolution_name, resolution_value, point_value, event_path) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Manager
Returns a new instance of Manager.
35 36 37 38 39 40 |
# File 'lib/rhcf/timeseries/manager.rb', line 35 def initialize( = {}) @strategy = ( [:strategy] || DEFAULT_STRATEGY).new @resolution_ids = [:resolutions] || DEFAULT_RESOLUTIONS @prefix = [([:prefix] || DEFAULT_PREFIX) , @strategy.id].join(NAMESPACE_SEPARATOR) @connection_to_use = [:connection] end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
34 35 36 |
# File 'lib/rhcf/timeseries/manager.rb', line 34 def prefix @prefix end |
Instance Method Details
#connection_to_use ⇒ Object
49 50 51 |
# File 'lib/rhcf/timeseries/manager.rb', line 49 def connection_to_use @connection_to_use || fail("No connection given") end |
#crunch_values(subject, resolution_id, point, filter, limit = 1000) ⇒ Object
115 116 117 |
# File 'lib/rhcf/timeseries/manager.rb', line 115 def crunch_values(subject, resolution_id, point, filter, limit = 1000) @strategy.crunch_values(self, subject, resolution_id, point, filter, limit) end |
#descend(path, do_descend = true, &block) ⇒ Object
91 92 93 94 95 |
# File 'lib/rhcf/timeseries/manager.rb', line 91 def descend(path, do_descend = true , &block) return if path.empty? or path == "." block.call(path) descend(File.dirname(path), do_descend, &block) if do_descend end |
#find(subject, from, to = Time.now, filter = nil, limit = 1000) ⇒ Object
101 102 103 |
# File 'lib/rhcf/timeseries/manager.rb', line 101 def find(subject, from, to = Time.now, filter = nil, limit = 1000) Rhcf::Timeseries::Query.new(subject, from, to, self, filter, limit) end |
#on_connection(conn) {|_self| ... } ⇒ Object
42 43 44 45 46 47 |
# File 'lib/rhcf/timeseries/manager.rb', line 42 def on_connection(conn) old_connection = @connection_to_use @connection_to_use = conn yield self @connection_to_use = old_connection end |
#resolution(id) ⇒ Object
105 106 107 108 109 |
# File 'lib/rhcf/timeseries/manager.rb', line 105 def resolution(id) res = DEFAULT_RESOLUTIONS_MAP[id] # TODO configurable fail ArgumentError, "Invalid resolution name #{id} for this time series" if res.nil? res.merge(:id => id) end |
#resolution_value_at(moment, res_id) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rhcf/timeseries/manager.rb', line 74 def resolution_value_at(moment, res_id) res_config = DEFAULT_RESOLUTIONS_MAP[res_id] # TODO configurable if res_config.nil? fail "No resolution config for id: #{res_id.class}:#{res_id}" end time_resolution_formater = res_config[:formatter] case time_resolution_formater when String moment.strftime(time_resolution_formater) when Proc time_resolution_formater.call(moment) else fail ArgumentError, "Unexpected moment formater type #{time_resolution_formater.class}" end end |
#resolutions ⇒ Object
111 112 113 |
# File 'lib/rhcf/timeseries/manager.rb', line 111 def resolutions @_resolutions ||= @resolution_ids.map { |id| resolution(id) } end |
#resolutions_of(moment) ⇒ Object
68 69 70 71 72 |
# File 'lib/rhcf/timeseries/manager.rb', line 68 def resolutions_of(moment) @resolution_ids.collect do |res_id| [res_id, resolution_value_at(moment, res_id)] end end |
#store(subject, event_point_hash, moment = Time.now, descend_subject = true, descend_event = true) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rhcf/timeseries/manager.rb', line 53 def store(subject, event_point_hash, moment = Time.now, descend_subject = true, descend_event = true) resolutions = resolutions_of(moment) descend(subject, descend_subject) do |subject_path| event_point_hash.each do |event, point_value| descend(event, descend_event) do |event_path| resolutions.each do |res| resolution_name, resolution_value = *res store_point_value(subject_path, resolution_name, resolution_value, point_value, event_path) end end end end end |
#store_point_value(subject_path, resolution_name, resolution_value, point_value, event_path) ⇒ Object
97 98 99 |
# File 'lib/rhcf/timeseries/manager.rb', line 97 def store_point_value( subject_path, resolution_name, resolution_value, point_value, event_path) @strategy.store_point_value(self, subject_path, resolution_name, resolution_value, point_value, event_path) end |