Class: Chawk::Models::Node
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Chawk::Models::Node
- Defined in:
- lib/node.rb
Overview
The Node, where most Chawk:Node information is persisted..
Instance Attribute Summary collapse
-
#access ⇒ Object
Returns the value of attribute access.
-
#agent ⇒ Object
Returns the value of attribute agent.
Instance Method Summary collapse
- #_add(args, type, options = {}) ⇒ Object
- #_insert_point(val, ts, options = {}) ⇒ Object
- #_insert_point_array(item, options) ⇒ Object
- #_insert_point_hash(item, ts, options) ⇒ Object
- #_insert_point_string(item, ts, options) ⇒ Object
- #_insert_value(val, ts, options = {}) ⇒ Object
- #_prepare_insert(val, ts, options) ⇒ Object
- #_range(dt_from, dt_to, coll, options = {}) ⇒ Object
- #_unravel(items) ⇒ Object
-
#add_points(args, options = {}) ⇒ Object
Add an item or an array of items (one at a time) to the datastore.
-
#add_values(args, options = {}) ⇒ Object
Add an item or an array of items (one at a time) to the datastore.
- #check_admin_access ⇒ Object
- #check_read_access ⇒ Object
- #check_write_access ⇒ Object
- #clear_points! ⇒ Object
- #clear_values! ⇒ Object
- #decrement(value = 1, options = {}) ⇒ Object
- #increment(value = 1, options = {}) ⇒ Object
- #init ⇒ Object
- #point_recognizer(item, dt, options = {}) ⇒ Object
-
#points_range(dt_from, dt_to, options = {}) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range.
-
#points_since(dt_from) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range ending now.
-
#set_permissions(agent, read = false, write = false, admin = false) ⇒ Object
Sets permissions flag for this address, for a specific agent.
-
#set_public_read(value) ⇒ Object
Sets public read flag for this address.
-
#set_public_write(value) ⇒ Object
Sets public write flag for this address.
- #value_recognizer(item, dt, options = {}) ⇒ Object
-
#values_range(dt_from, dt_to, options = {}) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range.
-
#values_since(dt_from) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range ending now.
Instance Attribute Details
#access ⇒ Object
Returns the value of attribute access.
37 38 39 |
# File 'lib/node.rb', line 37 def access @access end |
#agent ⇒ Object
Returns the value of attribute agent.
28 29 30 |
# File 'lib/node.rb', line 28 def agent @agent end |
Instance Method Details
#_add(args, type, options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/node.rb', line 89 def _add(args, type, ={}) check_write_access ni = NodeInvalidator.new(self) [:observed_at] ? dt = [:observed_at] : dt = Time.now _unravel(args) do |arg| case type when :point ni << point_recognizer(arg, dt, ) when :value ni << value_recognizer(arg, dt, ) end end ni.invalidate! end |
#_insert_point(val, ts, options = {}) ⇒ Object
118 119 120 121 |
# File 'lib/node.rb', line 118 def _insert_point(val,ts,={}) self.points.create(_prepare_insert(val, ts, )) ts end |
#_insert_point_array(item, options) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/node.rb', line 135 def _insert_point_array(item,) if item.length == 2 && item[0].is_a?(Integer) _insert_point item[0],item[1], else raise ArgumentError, "Array Items must be in [value,timestamp] format. #{item.inspect}" end end |
#_insert_point_hash(item, ts, options) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/node.rb', line 123 def _insert_point_hash(item,ts,) if item['v'] && item['v'].is_a?(Integer) if item['t'] _insert_point item['v'],item['t'], else _insert_point item['v'],ts, end else raise ArgumentError, "Hash must have 'v' key set to proper type.. #{item.inspect}" end end |
#_insert_point_string(item, ts, options) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/node.rb', line 143 def _insert_point_string(item,ts,) if item.length > 0 && item =~ /\A[-+]?[0-9]+/ _insert_point item.to_i,ts, else raise ArgumentError, "String Items must represent Integer. #{item.inspect}" end end |
#_insert_value(val, ts, options = {}) ⇒ Object
65 66 67 68 |
# File 'lib/node.rb', line 65 def _insert_value(val,ts,={}) self.values.create(_prepare_insert(val, ts, )) ts end |
#_prepare_insert(val, ts, options) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/node.rb', line 53 def _prepare_insert(val, ts, ) values = {value:val,observed_at:ts.to_f} if [:meta] if [:meta].is_a?(Hash) values[:meta] = [:meta].to_json else raise ArgumentError, "Meta must be a JSON-representable Hash. #{options[:meta].inspect}" end end values end |
#_range(dt_from, dt_to, coll, options = {}) ⇒ Object
203 204 205 206 |
# File 'lib/node.rb', line 203 def _range(dt_from, dt_to, coll, ={}) check_read_access ret = coll.where("observed_at >= :dt_from AND observed_at <= :dt_to",{dt_from:dt_from.to_f,dt_to:dt_to.to_f}, limit:1000,order:"observed_at asc, id asc") end |
#_unravel(items) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/node.rb', line 79 def _unravel(items) if items.is_a?(Array) items.each do |item| yield item end else yield items end end |
#add_points(args, options = {}) ⇒ Object
Add an item or an array of items (one at a time) to the datastore.
114 115 116 |
# File 'lib/node.rb', line 114 def add_points(args,={}) _add(args,:point,) end |
#add_values(args, options = {}) ⇒ Object
Add an item or an array of items (one at a time) to the datastore.
107 108 109 |
# File 'lib/node.rb', line 107 def add_values(args,={}) _add(args,:value, ) end |
#check_admin_access ⇒ Object
178 179 180 181 182 |
# File 'lib/node.rb', line 178 def check_admin_access unless [:full,:admin,:read].include? @access raise SecurityError,"You do not have admin access to this node." end end |
#check_read_access ⇒ Object
172 173 174 175 176 |
# File 'lib/node.rb', line 172 def check_read_access unless [:full,:admin,:read].include? @access raise SecurityError,"You do not have read access to this node." end end |
#check_write_access ⇒ Object
166 167 168 169 170 |
# File 'lib/node.rb', line 166 def check_write_access unless [:full,:admin,:write].include? @access raise SecurityError,"You do not have write access to this node." end end |
#clear_points! ⇒ Object
48 49 50 51 |
# File 'lib/node.rb', line 48 def clear_points! check_admin_access points.destroy_all end |
#clear_values! ⇒ Object
43 44 45 46 |
# File 'lib/node.rb', line 43 def clear_values! check_admin_access values.destroy_all end |
#decrement(value = 1, options = {}) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/node.rb', line 194 def decrement(value=1, ={}) check_write_access if value.is_a?(Integer) increment (-1) * value, else raise ArgumentError, "Value must be an Integer" end end |
#increment(value = 1, options = {}) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/node.rb', line 184 def increment(value=1, ={}) check_write_access if value.is_a?(Integer) last = self.points.last add_points last.value + value, else raise ArgumentError, "Value must be an Integer" end end |
#init ⇒ Object
39 40 41 |
# File 'lib/node.rb', line 39 def init @agent = nil end |
#point_recognizer(item, dt, options = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/node.rb', line 151 def point_recognizer(item, dt, ={}) case when item.is_a?(Integer) _insert_point item,dt, when item.is_a?(Array) _insert_point_array(item, ) when item.is_a?(Hash) _insert_point_hash(item,dt,) when item.is_a?(String) _insert_point_string(item,dt,) else raise ArgumentError, "Can't recognize format of data item. #{item.inspect}" end end |
#points_range(dt_from, dt_to, options = {}) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range.
227 228 229 |
# File 'lib/node.rb', line 227 def points_range(dt_from, dt_to,={}) _range(dt_from, dt_to, points, ) end |
#points_since(dt_from) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range ending now.
234 235 236 |
# File 'lib/node.rb', line 234 def points_since(dt_from) self.points_range(dt_from,Time.now) end |
#set_permissions(agent, read = false, write = false, admin = false) ⇒ Object
Sets permissions flag for this address, for a specific agent. The existing Chawk::Relationship will be destroyed and a new one created as specified. Write access is not yet checked.
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/node.rb', line 260 def (agent,read=false,write=false,admin=false) rels = relations.where(:agent_id => agent.id) rels.delete_all() rels = relations.where(:agent_id => agent.id) if read || write || admin vals = {agent:agent,read:(read ? true : false),write:(write ? true : false),admin:(admin ? true : false)} relations.create(vals) end nil end |
#set_public_read(value) ⇒ Object
Sets public read flag for this address
240 241 242 243 244 |
# File 'lib/node.rb', line 240 def set_public_read(value) value = value ? true : false self.update_attributes :public_read => value #save end |
#set_public_write(value) ⇒ Object
Sets public write flag for this address
248 249 250 251 252 |
# File 'lib/node.rb', line 248 def set_public_write(value) value = value ? true : false self.update_attributes :public_write => value #save end |
#value_recognizer(item, dt, options = {}) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/node.rb', line 70 def value_recognizer(item, dt, ={}) case when item.is_a?(String) _insert_value item,dt, else raise ArgumentError, "Can't recognize format of data item. #{item.inspect}" end end |
#values_range(dt_from, dt_to, options = {}) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range.
212 213 214 |
# File 'lib/node.rb', line 212 def values_range(dt_from, dt_to,={}) _range(dt_from, dt_to, values, ) end |
#values_since(dt_from) ⇒ Array of Objects
Returns items whose observed_at times fit within from a range ending now.
219 220 221 |
# File 'lib/node.rb', line 219 def values_since(dt_from) self.values_range(dt_from,Time.now) end |