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.
78 79 80 |
# File 'lib/node.rb', line 78 def access @access end |
#agent ⇒ Object
Returns the value of attribute agent.
69 70 71 |
# File 'lib/node.rb', line 69 def agent @agent end |
Instance Method Details
#_add(args, type, options = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/node.rb', line 130 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
159 160 161 162 |
# File 'lib/node.rb', line 159 def _insert_point(val,ts,={}) self.points.create(_prepare_insert(val, ts, )) ts end |
#_insert_point_array(item, options) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/node.rb', line 176 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
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/node.rb', line 164 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
184 185 186 187 188 189 190 |
# File 'lib/node.rb', line 184 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
106 107 108 109 |
# File 'lib/node.rb', line 106 def _insert_value(val,ts,={}) self.values.create(_prepare_insert(val, ts, )) ts end |
#_prepare_insert(val, ts, options) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/node.rb', line 94 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
244 245 246 247 |
# File 'lib/node.rb', line 244 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/node.rb', line 120 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.
155 156 157 |
# File 'lib/node.rb', line 155 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.
148 149 150 |
# File 'lib/node.rb', line 148 def add_values(args,={}) _add(args,:value, ) end |
#check_admin_access ⇒ Object
219 220 221 222 223 |
# File 'lib/node.rb', line 219 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
213 214 215 216 217 |
# File 'lib/node.rb', line 213 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
207 208 209 210 211 |
# File 'lib/node.rb', line 207 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
89 90 91 92 |
# File 'lib/node.rb', line 89 def clear_points! check_admin_access points.destroy_all end |
#clear_values! ⇒ Object
84 85 86 87 |
# File 'lib/node.rb', line 84 def clear_values! check_admin_access values.destroy_all end |
#decrement(value = 1, options = {}) ⇒ Object
235 236 237 238 239 240 241 242 |
# File 'lib/node.rb', line 235 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
225 226 227 228 229 230 231 232 233 |
# File 'lib/node.rb', line 225 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
80 81 82 |
# File 'lib/node.rb', line 80 def init @agent = nil end |
#point_recognizer(item, dt, options = {}) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/node.rb', line 192 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.
268 269 270 |
# File 'lib/node.rb', line 268 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.
275 276 277 |
# File 'lib/node.rb', line 275 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.
301 302 303 304 305 306 307 308 309 310 |
# File 'lib/node.rb', line 301 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
281 282 283 284 285 |
# File 'lib/node.rb', line 281 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
289 290 291 292 293 |
# File 'lib/node.rb', line 289 def set_public_write(value) value = value ? true : false self.update_attributes :public_write => value #save end |
#value_recognizer(item, dt, options = {}) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/node.rb', line 111 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.
253 254 255 |
# File 'lib/node.rb', line 253 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.
260 261 262 |
# File 'lib/node.rb', line 260 def values_since(dt_from) self.values_range(dt_from,Time.now) end |