Class: Chawk::Models::Node

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/node.rb

Overview

The Node, where most Chawk:Node information is persisted..

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accessObject

Returns the value of attribute access.



78
79
80
# File 'lib/node.rb', line 78

def access
  @access
end

#agentObject

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, options={})
  check_write_access
  ni = NodeInvalidator.new(self)
  options[:observed_at] ? dt = options[:observed_at] : dt = Time.now
  _unravel(args) do |arg|
    case type
    when :point
      ni << point_recognizer(arg, dt, options)
    when :value
      ni << value_recognizer(arg, dt, options)
    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,options={})        
  self.points.create(_prepare_insert(val, ts, options))
  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,options)
  if item.length == 2 && item[0].is_a?(Integer)
    _insert_point item[0],item[1], options
  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,options)
  if item['v'] && item['v'].is_a?(Integer)
    if item['t']
      _insert_point item['v'],item['t'], options
    else
      _insert_point item['v'],ts, options
    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,options)
  if item.length > 0 && item =~ /\A[-+]?[0-9]+/
    _insert_point item.to_i,ts, options
  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,options={})
  self.values.create(_prepare_insert(val, ts, options))
  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, options)
  values = {value:val,observed_at:ts.to_f}
  if options[:meta]
    if options[:meta].is_a?(Hash)
      values[:meta] = options[: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, options={})
  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.

Parameters:

  • args (Object, Array of Objects)
  • options (Hash) (defaults to: {})

    You can also pass in :meta and :timestamp



155
156
157
# File 'lib/node.rb', line 155

def add_points(args,options={})
  _add(args,:point,options)
end

#add_values(args, options = {}) ⇒ Object

Add an item or an array of items (one at a time) to the datastore.

Parameters:

  • args (Object, Array of Objects)
  • options (Hash) (defaults to: {})

    You can also pass in :meta and :timestamp



148
149
150
# File 'lib/node.rb', line 148

def add_values(args,options={})
  _add(args,:value, options)
end

#check_admin_accessObject



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_accessObject



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_accessObject



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, options={})
  check_write_access
  if value.is_a?(Integer)
    increment (-1) * value, options
  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, options={})
  check_write_access
  if value.is_a?(Integer)
    last = self.points.last
    add_points last.value + value,options 
  else 
    raise ArgumentError, "Value must be an Integer"
  end
end

#initObject



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, options={})
  case 
  when item.is_a?(Integer)
    _insert_point item,dt, options
  when item.is_a?(Array)
    _insert_point_array(item, options)
  when item.is_a?(Hash)
    _insert_point_hash(item,dt,options)
  when item.is_a?(String)
    _insert_point_string(item,dt,options)
  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.

Parameters:

  • dt_from (Time::Time)

    The start time.

  • dt_to (Time::Time)

    The end time.

Returns:

  • (Array of Objects)


268
269
270
# File 'lib/node.rb', line 268

def points_range(dt_from, dt_to,options={})
  _range(dt_from, dt_to, points, options)
end

#points_since(dt_from) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range ending now.

Parameters:

  • dt_from (Time::Time)

    The start time.

Returns:

  • (Array of Objects)


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.

Parameters:

  • agent (Chawk::Agent)

    the agent to give permission.

  • read (Boolean) (defaults to: false)

    true/false can the agent read this address.

  • write (Boolean) (defaults to: false)

    true/false can the agent write this address. (Read acces is required to write.)

  • admin (Boolean) (defaults to: false)

    does the agent have ownership/adnim rights for this address. (Read and write are granted if admin is as well.)



301
302
303
304
305
306
307
308
309
310
# File 'lib/node.rb', line 301

def set_permissions(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

Parameters:

  • value (Boolean)

    true if public reading is allowed, false if it is not.



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

Parameters:

  • value (Boolean)

    true if public writing is allowed, false if it is not.



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, options={})
  case 
  when item.is_a?(String)
    _insert_value item,dt, options
  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.

Parameters:

  • dt_from (Time::Time)

    The start time.

  • dt_to (Time::Time)

    The end time.

Returns:

  • (Array of Objects)


253
254
255
# File 'lib/node.rb', line 253

def values_range(dt_from, dt_to,options={})
  _range(dt_from, dt_to, values, options)
end

#values_since(dt_from) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range ending now.

Parameters:

  • dt_from (Time::Time)

    The start time.

Returns:

  • (Array of Objects)


260
261
262
# File 'lib/node.rb', line 260

def values_since(dt_from)
  self.values_range(dt_from,Time.now)
end