Class: Redtastic::Model
- Inherits:
-
Object
- Object
- Redtastic::Model
- Defined in:
- lib/redtastic/model.rb
Class Method Summary collapse
- .aggregate(params) ⇒ Object
- .decrement(params) ⇒ Object
-
.find(params) ⇒ Object
Retrieving.
-
.increment(params) ⇒ Object
Recording.
Class Method Details
.aggregate(params) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/redtastic/model.rb', line 77 def aggregate(params) key_data = fill_keys_and_dates(params) keys = key_data[0] # If interval is present, we return a hash including the total as well as a data point for each interval. # Example: Visits.aggregate(start_date: 2014-01-05, end_date: 2013-01-06, id: 1, interval: :days) # { # visits: 2 # days: [ # { # created_at: 2014-01-05, # visits: 1 # }, # { # created_at: 2014-01-06, # visits: 1 # } # ] # } if params[:interval].present? && @_resolution.present? if @_type == :unique argv = [] argv << key_data[1].shift # Only need the # of business ids (which is 1st element) from key_data[1] argv << temp_key if params[:attributes].present? attributes = param_to_array(params[:attributes]) attributes.each do |attribute| keys << attribute_key(attribute) argv << 1 end end data_points = Redtastic::ScriptManager.union_data_points_for_keys(keys, argv) else data_points = Redtastic::ScriptManager.data_points_for_keys(keys, key_data[1]) end result = HashWithIndifferentAccess.new dates = key_data[2] # The data_points_for_keys lua script returns an array of all the data points, with one exception: # the value at index 0 is the total across all the data points, so we pop it off of the data points array. result[model_name] = data_points.shift result[params[:interval]] = [] data_points.each_with_index do |data_point, index| point_hash = HashWithIndifferentAccess.new point_hash[model_name] = data_point point_hash[:date] = dates[index] result[params[:interval]] << point_hash end result else # If interval is not present, we just return the total as an integer if @_type == :unique argv = [] argv << temp_key if params[:attributes].present? attributes = param_to_array(params[:attributes]) attributes.each do |attribute| keys << attribute_key(attribute) argv << 1 end end Redtastic::ScriptManager.msunion(keys, argv) else key_data[1].shift # Remove the number of ids from the argv array (don't need it in the sum method) Redtastic::ScriptManager.sum(keys, key_data[1]).to_i end end end |
.decrement(params) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/redtastic/model.rb', line 22 def decrement(params) key_data = fill_keys_for_update(params) if @_type == :unique argv = [] argv << params[:unique_id] Redtastic::ScriptManager.msrem(key_data[0], argv) else if params[:by].present? decrement_by = params[:by] else decrement_by = 1 end Redtastic::ScriptManager.hmincrby(key_data[0], key_data[1].unshift(-1*decrement_by)) end end |
.find(params) ⇒ Object
Retrieving
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/redtastic/model.rb', line 40 def find(params) keys = [] argv = [] # Construct the key's timestamp from inputed date parameters = '' += "#{params[:year]}" += "-#{zeros(params[:month])}" if params[:month].present? += "-W#{params[:week]}" if params[:week].present? += "-#{zeros(params[:day])}" if params[:day].present? params.merge!(timestamp: ) # Handle multiple ids ids = param_to_array(params[:id]) ids.each do |id| params[:id] = id keys << key(params) argv << index(id) end if @_type == :unique unique_argv = [] unique_argv << params[:unique_id] result = Redtastic::ScriptManager.msismember(keys, unique_argv) else result = Redtastic::ScriptManager.hmfind(keys, argv) end # If only for a single id, just return the value rather than an array if result.size == 1 result[0] else result end end |
.increment(params) ⇒ Object
Recording
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/redtastic/model.rb', line 6 def increment(params) key_data = fill_keys_for_update(params) if @_type == :unique argv = [] argv << params[:unique_id] Redtastic::ScriptManager.msadd(key_data[0], argv) else if params[:by].present? increment_by = params[:by] else increment_by = 1 end Redtastic::ScriptManager.hmincrby(key_data[0], key_data[1].unshift(increment_by)) end end |